2

I am trying to create a trustline between issuer's account and receiver's account to send Custom Assets(token)

I am following this link

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

async function main() {

    let source = StellarSdk.Keypair.fromSecret('distributor's secret key');
    let dest = StellarSdk.Keypair.fromSecret('receiver's secret key');

    // Keypair for the destination account can be generated using StellarSdk.Keypair.random().

    let assetCode = 'COOL';
    let assetIssuerAddress = 'GANQDVASPYIVJF7YJNFZJ6DPXEWKI57NRYVSC7WYM6ZAL5J7FVPXS3NU';

    // 2. Load current source account state from Horizon server

    let sourceAccount = await horizon.loadAccount(source.publicKey());

    // 3. Create a transaction builder

    let builder = new StellarSdk.TransactionBuilder(sourceAccount,opts={fee:200});

    // 4. Add CHANGE_TRUST operation to establish trustline

    builder.addOperation(StellarSdk.Operation.changeTrust({ 
        asset: new StellarSdk.Asset(assetCode, assetIssuerAddress),
        amount: '100',
        source: dest.publicKey()
    }))


    // Note that source parameter contains a public key of our destination account
 because we perform this operation on behalf of the destination account.

    // 5. Add PAYMENT operation to transfer your custom asset

    builder.addOperation(StellarSdk.Operation.payment({ 
        destination: dest.publicKey(),
        asset: new StellarSdk.Asset(assetCode, assetIssuerAddress),
        amount: '10'
    }))

    // 6. Build and sign transaction with both source and destination keypairs

    let tx = builder.setTimeout(180).build()

    tx.sign(source)
    tx.sign(dest)

    // 7. Submit transaction to network

    let txResult = await horizon.submitTransaction(tx)
    console.log(txResult);
}

main();

Above code works perfectly fine but I am wondering why do we need receiver's and distributor's secretkey/privatekey to create trustline b/w them. And what can be the role of allowTrust operation in this transaction.

P.S : Receiver's balance is more than minimum required to be on the ledger

0

Don't confuse the terms. The issuer is the account that controls the asset, distributor is another account that holds the asset when you mint all tokens at once by sending them from issuer to the distributor account. This is a common setup if you want to have a fixed, limited supply or just want to split responsibilities. The issuer could as well distribute the tokens by minting + paying them directly to users without a intermediate distributing account. (Btw: an issuer can not hold it's own assets, tokens sent back to their issuer will be burned.)

The example shows a one time token generation, the recipient is actually the distributor and the transaction is signed by issuer and distributor. The reason it needs both signatures is, that it contains creating a trustline and sending custom tokens - two different operations - in a single transaction. The receiver has to sign the transaction to confirm creating the trustline on their account, the issuer(*) has to sign it to confirm the payment. You could as well split that transaction into two separate transactions with single operations each and that way the receiver would only have to sign the one containing changeTrust and the issuer would only have to sign the payment transaction.

allowTrust

By default anyone can create a trustline for any asset and receive/hold/send that asset. If an issuer wants to keep control about who may use their asset, they can flag their issuing account with authorization required (setOptions operation => setFlags). Now if a user creates a trustline it has to be first approved with allowTrust by the issuer to be active.

  • Thanks @sui for your answer!!! this is really helpful but the above example is not for token generation. I have already created tokens and tokens are held by source i.e distributor and i am trying to send COOL tokens from source to dest(receiver's account) for that trustline is to be created. How can i automate this process of creating a trustline and then send tokens. I want to create a trustline dynamically for every new user who wants to have my tokens. – rohit_sethi Jun 17 at 5:26

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.