Search code examples
javascriptnode.jswebsocketripplecryptocurrency

Ripple-lib submit error 'UnhandledPromiseRejectionWarning: DisconnectedError: not opened'


I can't send Ripple XRP using ripple-lib and rippled server.
If I submit with signed payment object, then UnhandledPromiseRejectionWarning: DisconnectedError: not opened error shows up.

This is my code:

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'
     }
   }
};
ripple_api.connect().then(() => {
   ripple_api.preparePayment(from, payment, instructions).then(prepared => {
     const {signedTransaction, id} = ripple_api.sign(prepared.txJSON, privateKey);
     ripple_api.submit(signedTransaction).then(result => {
       if(result.resultCode == 'tesSUCCESS'){
         res.send(JSON.stringify({status: 'success', txid : id}));
       }else{
         res.send(JSON.stringify({status: 'error', msg : result.resultMessage}));
       }
     }).catch(console.error);
   }).catch(console.error);
}).then(() => {
   return ripple_api.disconnect();
}).catch(console.error);

And after this code, below error show:

(node:46089) UnhandledPromiseRejectionWarning: DisconnectedError: not opened
    at _ws.send.error (/var/www/web3app/node_modules/ripple-lib/dist/npm/common/connection.js:355:28)
    at WebSocket.send (/var/www/web3app/node_modules/ws/lib/WebSocket.js:359:15)
    at Promise (/var/www/web3app/node_modules/ripple-lib/dist/npm/common/connection.js:353:22)
    at new Promise (<anonymous>)
    at RestrictedConnection._send (/var/www/web3app/node_modules/ripple-lib/dist/npm/common/connection.js:352:16)
    at Promise (/var/www/web3app/node_modules/ripple-lib/dist/npm/common/connection.js:407:34)
    at new Promise (<anonymous>)
    at RestrictedConnection.request (/var/www/web3app/node_modules/ripple-lib/dist/npm/common/connection.js:364:16)
    at RestrictedConnection.request (/var/www/web3app/node_modules/ripple-lib/dist/npm/api.js:56:22)
    at RippleAPI.submit (/var/www/web3app/node_modules/ripple-lib/dist/npm/transaction/submit.js:31:28)
    at ripple_api.preparePayment.then.prepared (/var/www/web3app/routes/index.js:316:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)
(node:46089) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:46089) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Solution

  • I think you should return the promise at

    return ripple_api.preparePayment(from, payment, instructions).then(...)
    

    that way you can chain multiple then statements following ripple_api.connect()