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.
-
Modify the existing
trust_set.js
file in your project directory. -
Add the function call of
trust_set.js
as follows:const xrpl = require('xrpl');// Function to send a TrustSet transactionasync function trustSet(recipientSecret) {// Connect to the Testnet serverconst 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 walletconst recipientWallet = xrpl.Wallet.fromSecret(recipientSecret);// Fixed information about the tokenconst currencyCode = 'DOJ'; // Currency codeconst amount = '10000'; // Maximum receivable amount of tokens// Send the TrustSet transactionconst response = await client.submitAndWait({TransactionType: 'TrustSet', // Set the trust lineAccount: recipientWallet.address, // Bob's addressLimitAmount: {issuer: issuerWalletAddress, // Issuer's address (Charlie)currency: currencyCode, // DOJvalue: amount, // 10000},},{ wallet: recipientWallet } // Sign with Bob's wallet);// Output the result to the consoleconsole.log('Transaction result:', response);} catch (error) {// Error handlingconsole.error('An error occurred:', error);}// Disconnect from the serverclient.disconnect();}// Execute the function// const recipientSecret = 'alice_wallet_secret_here'; // Comment outconst recipientSecret = 'bob_wallet_secret_here'; // Set Bob's wallettrustSet(recipientSecret);- Enter Charlie’s testnet address in
charlie_wallet_address_here
. - Enter Charlie’s testnet address in
bob_wallet_secret_here
.
- Enter Charlie’s testnet address in
Run the Script
-
Run the following command in the command line to execute the script.
Terminal window node trust_set.jsIf successful, the console will display the following.
Terminal window Transaction result: {id: 12,result: {Account: 'rDNVaJJSp9iQmHC8YAAwHytnLP9aXEPhDw', // Bob's addressFee: '12',Flags: 0,LastLedgerSequence: 475618,LimitAmount: {currency: 'DOJ', // Currency codeissuer: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU', // Issuer's address (Charlie)value: '10000' // Maximum receivable amount of tokens},Sequence: 473782,SigningPubKey: 'EDEE50D2E97DDB649EC43B36A0EF05AF0E9D33805CC11E7693A04D42E20B5E4C4C',TransactionType: 'TrustSet', // Transaction typeTxnSignature: 'EC29F6806FE4A422AD5BF20610179F0791A908D4262D86C355966831217F0613956BF8D222E40018BC5C47BECDF0B77EB31426C5217276A4FE0F1884A53A890C',ctid: 'C00741D000010001',date: 768404650,hash: 'EADE8622DA5CC922CDBE52EDF7136D09C7F3C08CAC4C5BE4F07E946FE360893C', // Hash valueinLedger: 475600,ledger_index: 475600, // Ledger index numbermeta: {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.