You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ripple-lib与rippled服务器发送XRP失败,报DisconnectedError错误求助

解决ripple-lib提交支付时的DisconnectedError: not opened错误

这个DisconnectedError: not opened错误我之前帮不少开发者排查过,本质上就是你的ripple-lib API实例没和rippled服务器建立有效连接,或者连接在提交交易前就断了。下面给你一步步排查和解决的方案:

1. 确保先完成连接再执行交易操作

ripple-lib的connect()方法是异步的,必须等它完成后再去构建、签名和提交交易——很多人踩坑就是因为没等连接就绪就跑后续代码。

异步函数写法(推荐)

用async/await清晰控制流程:

const RippleAPI = require('ripple-lib').RippleAPI;
// 测试网地址,主网替换成wss://s1.ripple.com:443
const api = new RippleAPI({ server: 'wss://s.altnet.rippletest.net:51233' });

async function sendXRP() {
  try {
    // 先等待连接成功
    await api.connect();
    console.log('已成功连接到rippled服务器');

    // 你的交易构建逻辑
    const fee = await api.getFee(); // 推荐用API获取当前网络手续费,避免手动设置出错
    const instructions = { maxLedgerVersionOffset: 5, fee: fee }; 
    const payment = { 
      'source': { 
        'address': from, 
        'maxAmount': { 
          'value': send_amount, 
          'currency': 'XRP' 
        } 
      }, 
      'destination': { 
        'address': to_address, 
        'tag': to_tag, 
        'amount': { 
          'value': send_amount, 
          'currency': 'XRP' 
        } 
      } 
    };

    // 准备、签名、提交交易
    const preparedTx = await api.preparePayment(from, payment, instructions);
    const signedTx = api.sign(preparedTx.txJSON, your_secret_key); // 替换成你的账户秘钥
    const submitResult = await api.submit(signedTx.signedTransaction);
    
    console.log('交易提交结果:', submitResult);
  } catch (error) {
    console.error('交易过程出错:', error);
  } finally {
    // 不管成功失败,最后断开连接
    await api.disconnect();
    console.log('已断开与服务器的连接');
  }
}

// 执行交易函数
sendXRP();

链式调用写法(兼容老版本JS)

如果不能用async/await,用.then()确保顺序:

api.connect()
  .then(() => {
    console.log('连接成功');
    // 这里放交易构建、签名、提交的逻辑
    return api.getFee();
  })
  .then(fee => {
    // 构建payment和instructions,然后准备交易
    return api.preparePayment(from, payment, { maxLedgerVersionOffset:5, fee });
  })
  .then(preparedTx => {
    return api.sign(preparedTx.txJSON, your_secret_key);
  })
  .then(signedTx => {
    return api.submit(signedTx.signedTransaction);
  })
  .then(result => console.log('交易成功:', result))
  .catch(error => console.error('出错:', error))
  .finally(() => api.disconnect());

2. 检查服务器地址有效性

  • 测试网必须用:wss://s.altnet.rippletest.net:51233
  • 主网必须用:wss://s1.ripple.com:443
  • 如果是自建rippled服务器,要确保WebSocket端口(默认6006)对外开放,且服务器处于运行状态,没有防火墙拦截。

3. 主动检查连接状态

在执行交易前可以主动确认连接是否正常,避免无效操作:

if (!api.isConnected()) {
  console.log('连接已断开,重新连接中...');
  await api.connect();
}

4. 监听连接断开事件(可选)

如果你的应用需要长期保持连接,可以监听断开事件并自动重连:

api.on('disconnected', (code, reason) => {
  console.log(`连接断开:代码${code},原因:${reason}`);
  // 这里可以添加重连逻辑,比如延迟几秒后重新调用api.connect()
});

额外注意事项

  • 不要手动硬编码手续费,用api.getFee()获取当前网络推荐值,避免手续费过低导致交易被拒绝。
  • 确保签名用的秘钥和from地址完全对应,秘钥错误会导致签名无效,也可能间接引发连接相关的报错。

内容的提问来源于stack exchange,提问作者Jayson Reyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:07:17