JavaScript'then'在Ripple-lib调用中无法按预期运行

时间:2018-10-18 15:08:50

标签: javascript promise blockchain rippled

我正在尝试使用Ripple-lib创建一个通过XRPL付款的简单示例。想法是将几笔付款发送到存储在数组中的不同帐户。我已经按照预期的方式进行了某种工作,但是使用'then'方法(如文档所建议)时根本无法工作。

我是Java的新手,所以我对语言没有很好的了解,也没有异步的编码和承诺。当使用“ then”范例时,代码停止工作,并且在控制台中看不到任何输出。这是我当前正在使用的代码。在“ SendXRP”功能内的注释中,我解释了该问题。如何重新安排呢?在这两种方式之间,什么是合适的编码方式?

'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;

const sender = 'r*********************************';
const secret = 's****************************';
const destinations = ['r*********************************',
                      'r*********************************',
                      'r*********************************'];
const amount = 5;

// Instantiate Ripple API
const api = new RippleAPI({
  server: "wss://s.altnet.rippletest.net:51233"
});

run();


async function sendXRP(amount, fee, destination, memo) {

  // Update amount
  amount = (amount - fee).toString();

  // Build payment
  const payment = {
    source: {
      address: sender,
      maxAmount: {
        value: amount,
        currency: 'XRP'
      }
    },
    destination: {
      address: destination,
      amount: {
        value: amount,
        currency: 'XRP'
      }
    },
    memos: [
      {
          data: memo
      }
    ]
  };

  // Build instuctions
  const instructions = {
    maxLedgerVersionOffset: 5
  };

  console.log('Sending ' + amount + ' to ' + destination);

  // THIS KIND OF WORKS FOR NOW
  // Prepare the payment
  const preparedTX = await api.preparePayment(sender, payment, instructions);

  // Sign the payment
  const signedTX = api.sign(preparedTX.txJSON, secret);

  // Submit the payment
  const result = await api.submit(signedTX['signedTransaction']);

  // Return TX hash on successful TX
  if ('resultCode' in result && result['resultCode'] == 'tesSUCCESS') {
      return signedTX.id;
  } else {
      return null;
  }

  // THIS IS MORE SIMILAR TO HOW IT IS DONE IN THE DOCS! NOT WORKING!
  // ALSO, HOW DO I RETURN THE RESULT OF API.SIGN TO THE MAIN FUNCTION?
  // Prepare the payment
  // api.preparePayment(sender, payment, instructions).then(preparedTX => {
  //     // Sign the payment
  //     api.sign(preparedTX.txJSON, secret).then(signedTX => {
  //     // Submit the payment
  //     api.submit(signedTX['signedTransaction']);
  //     })
  // }).catch(console.error);
}

function run() {
// Connect to Ripple server
api.connect().then(() => {
  return api.getFee();
}).then(async fee => {

  for (var i in destinations) {
    var hash = await sendXRP(amount, Number(fee), destinations[i], 'memotext');
    console.log(hash);
  }

}).then(() => {
  return api.disconnect();
}).catch(console.error);
}

1 个答案:

答案 0 :(得分:1)

可能是某些交易发送失败吗?如果失败,则sendXRP的结果变量应具有txresult,但是如果结果代码不是tesSUCCESS,则由于返回null,因此不会返回结果信息。

const result = await api.submit(signedTX['signedTransaction']);

if ('resultCode' in result && result['resultCode'] == 'tesSUCCESS') {
  return signedTX.id;
} else {
  return null;
}

之前,当我尝试连续提交事务时,它将失败并返回错误代码tefPAST_SEQ

“交易的序号低于发送交易的帐户的当前序号。”来自https://developers.ripple.com/tef-codes.html

我建议删除if('resultCode'in result ...)块并检查交易结果。如果交易失败并出现tefPAST_SEQ错误,我的解决方案是手动设置说明中的帐户顺序,或在每次提交后添加setTimeOut。