2

I am having some issues with an application I'm trying to build. I am new to JS.

What I am trying to accomplish:

Sending 2 variables (wallet, amount) to the function transferLumens. These 2 variables come from another function where amount and wallet are stored in a list. This list is looped through to get wallet and amount thrown to transferLumens function.

I checked the passing variables and they are valid.

The payment's go through sometimes but it fails more than anything. The error code is:
Error: Request failed with status code 400 ... result_codes: { transaction: 'tx_bad_seq' }, result_xdr: 'AAAAAAAAAGT////7AAAAAA==' }

The guide for Stellar Developers, sequenceNumbers was some what helpful. How do I get the currenct sequence number before a transaction submit?

Any guidance is much appreciated.

function transferLumens(wallet, amount) { var StellarSdk = require('stellar-sdk'); var server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); StellarSdk.Network.useTestNetwork(); var sourceKeys = StellarSdk.Keypair .fromSecret('SECRETKEY'); //var testamount = "2.4"; server.loadAccount(wallet) .catch(StellarSdk.NotFoundError, function (error) { throw new Error('The destination account does not exist!'); }) .then(function () { return server.loadAccount(sourceKeys.publicKey()); }) .then(function (sourceAccount) { transaction = new StellarSdk.TransactionBuilder(sourceAccount) .addOperation(StellarSdk.Operation.payment({ destination: wallet, asset: StellarSdk.Asset.native(), amount: amount })) .addMemo(StellarSdk.Memo.text("TEST PAYMENT")) .build(); transaction.sign(sourceKeys); // console.log(transaction.toEnvelope().toXDR('base64')); return server.submitTransaction(transaction); }) .then(function (result) { console.log('Success! Results:', result); }) .catch(function (error) { console.error('Something went wrong!', error); }); }

  • error.response.data.extras will give you more information about the error. You can check the errors here stellar.org/developers/guides/concepts/list-of-operations.html – Bartek Nowotarski Jul 18 '18 at 19:27
  • Thank you @BartekNowotarski I've updated my post with the error code. It is related to sequence number. How in JS can I get an array of transactions before building, incrementing sequence number automatically? I assume this might solve the issue. – Chris Jul 18 '18 at 20:27
  • Can you post the envelope XDR (the commented line)? – Bartek Nowotarski Jul 18 '18 at 22:01
1

The guide for Stellar Developers, sequenceNumbers was some what helpful. How do I get the currenct sequence number before a transaction submit?

Look at the following line in the ample code you provided:

return server.loadAccount(sourceKeys.publicKey());

It's intended to get the recent account information, including current account sequence, from Horizon server. The account is used as a source account in a transaction builder constructor:

transaction = new StellarSdk.TransactionBuilder(sourceAccount)

In most cases, you should call server.loadAccount() before building any transaction. If you cache the value somewhere in your code, it won't work, because account sequence is incremented every time a transaction is applied on the source account, and you'll get tx_bad_seq error.

For example, you are building two transactions (let's say payments). You are calling server.loadAccount() only once, and use the returned value to build two transactions. So far everything works. However, when you'll try to submit both transactions to the network, one of them will fail because the first applied transaction will increment the source account sequence.

In this particular case the correct way will look like:

  1. server.loadAccount()
  2. Build transaction 1.
  3. Submit transaction 1 to the network.
  4. server.loadAccount() to retrieve recent account information
  5. Build transaction 2.
  6. Submit transaction 2 to the network.

Of course, it's better to combine all operations into a single transaction. A transaction can contain up to 100 operations. BUMP_SEQUENCE operation is another, more advanced way, to deal with complex cases of account sequence collisions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.