4

If I want to create a trustline between my issuing account and new users accounts, how do I do this. I tried with this code:

const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');

// an arbitary list of trusted assets
const ASSETS = {
    'TestToken': new StellarSdk.Asset('TestToken', 'G... *issuer account*'), //name, ISSUER ACC
    'USDT': new StellarSdk.Asset('USDT', 'GCQTGZQQ5G4PTM2GL7CDIFKUBIPEC52BROAQIAPW53XBRJVN6ZJVTG6V'),
    'MOBI' : new StellarSdk.Asset('MOBI', 'GA6HCMBLTZS5VYYBCATRBRZ3BZJMAFUDKYYF6AH6MVCMGWMRDNSWJPIH'),
    'XLM': StellarSdk.Asset.native()
}

// helper function to get desired asset object
const getAsset = (asset) => {
    if (ASSETS[asset]) return ASSETS[asset]
    return StellarSdk.Asset.native()
}

//create the distributor account keypair
const distributorKeypair = StellarSdk.Keypair.fromSecret('S... *Distrubutor Account Secret*') //DISTRIBUTOR ACCOUNT
//create the new account keypairs to use
var newAccountKeypair = StellarSdk.Keypair.random();
var newAccount = newAccountKeypair.publicKey(); // newAccount account address, taken from random keypair's public key
var newAccountSecret = newAccountKeypair.secret();


//load the new account to create trustline
server.loadAccount(newAccountKeypair.publicKey())
.then(account => {

    // new transaction builder (convenience constructor grabs tx sequence number from account object)
    let builder = new StellarSdk.TransactionBuilder(account);

    //OPERATION 2: Change Trustline to trust the asset to be used on the platform.
    builder.addOperation(
        StellarSdk.Operation.changeTrust({
            asset: asset: getAsset('TestToken'), //asset
            source: "G... *issuer account*" //issuer account
        })
    )

    // create the transaction XDR
    let transaction = builder.build();

    // sign the XDR
    transaction.sign(newAccountKeypair);

    // submit to the network. this returns a promise (resolves or rejects depending on the outcome of the transaction)
    server.submitTransaction(transaction);
})

but got errors:

(node:17428) UnhandledPromiseRejectionWarning: Error: Request failed with status code 400
warning.js:18
    at createError (d:Stellar\node_modules\axios\lib\core\createError.js:16:15)
    at settle (d:\Stellar\node_modules\axios\lib\core\settle.js:18:12)
    at IncomingMessage.handleStreamEnd (d:\Stellar\node_modules\axios\lib\adapters\http.js:201:11)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:17428) 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: 3)
warning.js:18
(node:17428) [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.

Can Anyone help me figure out what I am doing wrong. I got pretty much the same method of action to work for creating and funding the new account but this gives errors that I don't understand.

Any help is appreciated. Thank you.

1
  • You have asset: asset: Commented Aug 3, 2022 at 19:37

3 Answers 3

0

server.loadAccount(newAccountKeypair.publicKey()) can't find your account on the ledger, because it's not on the ledger.

You need to create the account using createAccount before you can run any other operation on it.

0

In your previous question you posted the link to the SDK API documentation, pointing exactly at the Operation.changeTrust static method. So just follow the described method signature. It says that method accepts single argument - object with the following fields: asset of type Asset, optional limit as string, and optional source as string. So you should do the following:

const builder = new StellarSdk.TransactionBuilder(account);

builder.addOperation(
    StellarSdk.Operation.changeTrust({
        asset: new StellarSdk.Asset("TestToken", "G...*issuer account*")
    })
)

const transaction = builder.build();
0

Looks like you use issuer as source in the changeTrust operation. This is incorrect. The source here is the account that trusts the issuer, whereas the issuer is in the asset the source trusts.

Your Answer

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

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