Sending Tokens from the Issuer | XRPL Development in JavaScript Level 1
With XRPL, once a trust line is set up between accounts and users, tokens can be transferred instantly.
In the previous chapter, Alice obtained the right to receive about 10,000 DOJ
tokens issued by Charlie. Now, let’s send 1,000 DOJ
from Charlie to Alice.
Prerequisites
This guide proceeds with the following characters:
- Charlie (Token Issuer)
- Alice (Token Recipient)
Create the Script
The following is a script to send tokens from Charlie to Alice.
-
Create a new file named
payment_token.js
in your project directory. -
Paste the following code into
payment_token.js
.const xrpl = require('xrpl');async function paymentToken() {// Connect to the Testnet serverconst client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');await client.connect();// Set the wallet secret key of the token issuer (Charlie)const issuerWallet = xrpl.Wallet.fromSecret('charlie_wallet_secret_here');// Set the wallet address of the token recipient (Alice)const recipientWalletAddress = 'alice_wallet_address_here';const currencyCode = 'DOJ'; // Token currency codeconst amount = '1000'; // Amount of tokens to sendtry {const response = await client.submitAndWait({TransactionType: 'Payment', // PaymentAccount: issuerWallet.address, // Charlie's address (token issuer)Destination: recipientWalletAddress, // Alice's addressAmount: {issuer: issuerWallet.address, // Charlie's address (token issuer)currency: currencyCode, // DOJvalue: amount, // 1000},},{ wallet: issuerWallet } // Authenticate with Charlie's (token issuer's) wallet);// Output the result to the consoleconsole.log('Transaction result:', response);} catch (error) {// Error handlingconsole.error('An error occurred:', error);}// Call the client.disconnect() method to disconnect from the serverclient.disconnect();}paymentToken().catch(console.error);- Enter Charlie’s testnet secret key in
charlie_wallet_secret_here
. - Enter Alice’s testnet address in
alice_wallet_address_here
.
- Enter Charlie’s testnet secret key in
Run the Script
-
Run the following command in the command line to execute the script.
Terminal window node payment_token.js -
If successful, the console will display the following.
Terminal window {id: 12,result: {Account: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU', // Sender's address (token issuer: Charlie)Amount: {currency: 'DOJ',issuer: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU', // Issuer's address (Charlie)value: '1000'},DeliverMax: {currency: 'DOJ',issuer: 'rh8xETfGs1Ktizj9EXZb5QpW4aqk8bWUSU',value: '1000'},Destination: 'rQr8KfkCQcpqvFwmRspDoaL36reZhcdaeA', // Alice's addressFee: '12', // Transaction fee: 12 dropsFlags: 0,LastLedgerSequence: 475482,Sequence: 473776,SigningPubKey: 'EDF36017DE68C9A5E24ECB129161EACE7AE303E3FF151FF71E1160B0EEA6B8074F',TransactionType: 'Payment',TxnSignature: '8BFCFDD9E88C09277376C295C494231883C15BF91AEC86CFC288CF58731526CD8BDE4B13CC4EAEE1E6E51254F0BB0A8B9E7ACC1C0ABFEB9EB5C4ECBA891A330B',ctid: 'C007414800000001',date: 768404212,hash: '89283A3D48F7776E9FCE061713A787415A8E79428C318709C133A7A5F085EDC5', // Hash valueinLedger: 475464,ledger_index: 475464, // Ledger index numbermeta: {AffectedNodes: [Array],TransactionIndex: 0,TransactionResult: 'tesSUCCESS', // Successdelivered_amount: [Object]},validated: true // Validated transaction},type: 'response'}Now, using Alice’s account address information, check the results in Explorer. If successful, you will see that Alice has received
1,000 DOJ
.In this way, XRPL allows tokens to be issued in a very smart and secure way using the trust line functionality.