Skip to content

Setting Up a Trust Line (Bob's Edition) | XRPL Development in JavaScript Level 1

Next, let’s set up a trust line so that Bob can also receive DOJ tokens.

Prerequisites

This guide proceeds with the following characters:

  • Charlie (Token Issuer)
  • Bob (Token Recipient)

Create the Script

The following is a script to set a trust line for Charlie’s tokens from Bob.

  1. Modify the existing trust_set.js file in your project directory.

  2. Add the function call of trust_set.js as follows:

    const xrpl = require('xrpl');
    // Function to send a TrustSet transaction
    async function trustSet(recipientSecret) {
    // Connect to the Testnet server
    const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
    await client.connect();
    try {
    // Wallet address of the token issuer (Charlie)
    const issuerWalletAddress = 'charlie_wallet_address_here';
    // Set the recipient's wallet
    const recipientWallet = xrpl.Wallet.fromSecret(recipientSecret);
    // Fixed information about the token
    const currencyCode = 'DOJ'; // Currency code
    const amount = '10000'; // Maximum receivable amount of tokens
    // Send the TrustSet transaction
    const response = await client.submitAndWait(
    {
    TransactionType: 'TrustSet', // Set the trust line
    Account: recipientWallet.address, // Bob's address
    LimitAmount: {
    issuer: issuerWalletAddress, // Issuer's address (Charlie)
    currency: currencyCode, // DOJ
    value: amount, // 10000
    },
    },
    { wallet: recipientWallet } // Sign with Bob's wallet
    );
    // Output the result to the console
    console.log('Transaction result:', response);
    } catch (error) {
    // Error handling
    console.error('An error occurred:', error);
    }
    // Disconnect from the server
    client.disconnect();
    }
    // Execute the function
    // const recipientSecret = 'alice_wallet_secret_here'; // Comment out
    const recipientSecret = 'bob_wallet_secret_here'; // Set Bob's wallet
    trustSet(recipientSecret);
    1. Enter Charlie’s testnet address in charlie_wallet_address_here.
    2. Enter Charlie’s testnet address in bob_wallet_secret_here.

Run the Script

  1. Run the following command in the command line to execute the script.

    Terminal window
    node trust_set.js

    If successful, the console will display the following.

    Terminal window
    Transaction result: {
    id: 12,
    result: {
    Account: 'rDNVaJJSp9iQmHC8YAAwHytnLP9aXEPhDw', // Bob's address
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 475618,
    LimitAmount: {
    currency: 'DOJ', // Currency code
    issuer: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU', // Issuer's address (Charlie)
    value: '10000' // Maximum receivable amount of tokens
    },
    Sequence: 473782,
    SigningPubKey: 'EDEE50D2E97DDB649EC43B36A0EF05AF0E9D33805CC11E7693A04D42E20B5E4C4C',
    TransactionType: 'TrustSet', // Transaction type
    TxnSignature: 'EC29F6806FE4A422AD5BF20610179F0791A908D4262D86C355966831217F0613956BF8D222E40018BC5C47BECDF0B77EB31426C5217276A4FE0F1884A53A890C',
    ctid: 'C00741D000010001',
    date: 768404650,
    hash: 'EADE8622DA5CC922CDBE52EDF7136D09C7F3C08CAC4C5BE4F07E946FE360893C', // Hash value
    inLedger: 475600,
    ledger_index: 475600, // Ledger index number
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 1,
    TransactionResult: 'tesSUCCESS' // Success
    },
    validated: true // Validated transaction
    },
    type: 'response'
    }

    Now Bob can also receive the DOJ tokens issued by Charlie.

    Using the payment_token.js created in the previous chapter, try sending tokens to Bob as well.