How to Set Up a Trust Line | XRPL Development in JavaScript Level 1
What is a Trust Line?
The trust line feature of XRPL (XRP Ledger) allows users to show their intention to accept specific currencies. This is used to handle assets other than XRP on XRPL. For example, if a user wants to accept another currency or a token from another issuer, they need to set a trust line for that currency or token.
The trust line feature allows users to hold and trade assets issued by other users or issuers in their accounts. For example, Alice can receive tokens issued by Charlie by setting a trust line for Charlie’s tokens.
One benefit is that it prevents unauthorized tokens from being sent to a user’s address, maintaining order among users.
Prerequisites
This guide proceeds with the following characters:
- Charlie (Token Issuer)
- Alice (Token Recipient)
Create the Script
The following is a script to set a trust line for Charlie’s tokens from Alice.
-
Create a new file named
trust_set.js
in your project directory. -
Paste the following code into
trust_set.js
.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, // Alice's addressLimitAmount: {issuer: issuerWalletAddress, // Issuer's address (Charlie)currency: currencyCode, // DOJvalue: amount, // 10000},},{ wallet: recipientWallet } // Sign with Alice'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 functionconst recipientSecret = 'alice_wallet_secret_here'; // Set Alice's secret keytrustSet(recipientSecret);- Enter Charlie’s testnet address in
charlie_wallet_address_here
. - Enter Alice’s testnet secret key in
alice_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: 14,result: {Account: 'rQr8KfkCQcpqvFwmRspDoaL36reZhcdaeA', // Alice's addressFee: '12',Flags: 0,LastLedgerSequence: 474997,LimitAmount: {currency: 'DOJ', // Currency codeissuer: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU', // Token issuer's address (Charlie)value: '10000' // Maximum amount of DOJ tokens Alice can receive},Sequence: 474064,SigningPubKey: 'ED2924DBD08E093DD7FC9C35B8DCEBD74CA4C8349D68D7BF566AD6C2533B8BB865',TransactionType: 'TrustSet',TxnSignature: '8245C9412BA9E247D962B2D9B9B6B74A90D3757FDA66422527611971004F94D803607A3CC5357BA83B0FA0B7D010B10F3D0F5188EE8D4F1D72181BFA4D052B0B',ctid: 'C0073F6300000001',date: 768402681,hash: 'A20A377A4E9D8CAAA62E5AEE7CDFB9FBC690171B1D13485AC40A6313996A98C8', // Hash valueinLedger: 474979,ledger_index: 474979, // Ledger index numbermeta: {AffectedNodes: [Array],TransactionIndex: 0,TransactionResult: 'tesSUCCESS' // Success},validated: true // Validated transaction},type: 'response'}Now Alice can receive the DOJ tokens issued by Charlie.