0

I'm building an API to transfer assets from stellar based wallets, I am currently using the JS SDK. This is my code below:

var StellarSdk = require('stellar-sdk');
var express = require('express');
var router = express.Router();
StellarSdk.Network.usePublicNetwork();
var server = new StellarSdk.Server('https://horizon.stellar.org');

router.post('/sendToken', function (req, res, next) {
  var sourceKey = req.body.source_keys;
  var issuingKeys = StellarSdk.Keypair.fromSecret(req.body.issuing_keys);
  var receivingKeys = req.body.receiving_keys;

  var l2l = new StellarSdk.Asset('L2L', sourceKey);

  server.loadAccount(receivingKeys)
      .then(function (account) {
          var trusted = account.balances.some(function (balance) {
              console.log(balance);
              return balance.asset_code === 'XXX' && balance.asset_issuer === sourceKey;
          });
          if (trusted === true) {
              console.log('trusted')
              server.loadAccount(issuingKeys.publicKey())
                  .then(function (issuer) {
                    var transaction = new StellarSdk.TransactionBuilder(issuer, {
    networkPassphrase: StellarSdk.Networks.PUBLIC,
})
                          .addOperation(StellarSdk.Operation.payment({
                              destination: receivingKeys,
                              asset: l2l,
                              amount: req.body.amount
                          }))
                          .build();
                      transaction.sign(issuingKeys);
                      return server.submitTransaction(transaction);
                  })
                  .then(function (result) {
                      console.log('Success! Results:', result);
                      res.send(JSON.stringify(result));
                  })
                  .catch(function (error) {
                      console.error('Error!', error);
                  });
          } else {
              console.log('not trusted')
          }
      });
});

module.exports = router;

I have no idea where I'm going wrong, could anyone guide me?

4
  • Please add full error stacktrace to the question description.There is no way to tell where it errorred just from the code itself. Also check the extras.result_codes from the error object.
    – Orbit Lens
    Commented Sep 19, 2019 at 2:18
  • I didn't get the extra.results_code just got a generic code of 400, here it is (node:16153) UnhandledPromiseRejectionWarning: Error: Bad Request at new NetworkError (/home/ubuntu/Stellar Material/Rest-Api/node_modules/stellar-sdk/lib/errors.js:10:24) Commented Sep 19, 2019 at 6:30
  • at /home/ubuntu/Stellar Material/Rest-Api/node_modules/stellar-sdk/lib/call_builder.js:241:55 at step (/home/ubuntu/Stellar Material/Rest-Api/node_modules/tslib/tslib.js:136:27) at Object.next (/home/ubuntu/Stellar Material/Rest-Api/node_modules/tslib/tslib.js:117:57) at /home/ubuntu/Stellar Material/Rest-Api/node_modules/tslib/tslib.js:110:75 at new Promise (<anonymous>) at Object.__awaiter (/home/ubuntu/Stellar Material/Rest-Api/node_modules/tslib/tslib.js:106:16) Commented Sep 19, 2019 at 6:32
  • However, I always get this before starting the instance Global class Network is deprecated. Please pass explicit argument instead, e.g. new Transaction(envelope, Networks.PUBLIC) (see git.io/fj9fG for more info). Commented Sep 19, 2019 at 6:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.