Checking Account Information | XRPL Development in JavaScript Level 1
In the previous chapter, Alice set a trust line for the token issued by Charlie.
In this chapter, we aim to confirm if the trust line has been set correctly, and at the same time, learn about the public methods
of rippled
!
To retrieve account information, use the account methods
, which are public methods
of rippled
.
Prerequisites
This guide proceeds with the following characters:
- Charlie (Token Issuer)
- Alice (Token Recipient)
Create the Script
To check the list of currencies an account can send or receive, use the account_currencies command.
-
Create a new file named
check_account_currencies.js
in your project directory. -
Paste the following code into
check_account_currencies.js
.This example creates a reusable method. The
checkAccountCurrencies()
method retrieves information based on theaccountAddress
passed in as an argument.const xrpl = require('xrpl');async function checkAccountCurrencies(accountAddress) {// Connect to the Testnet serverconst client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');// Call the client.connect() method to connect to the server// Use await to wait until the connection is completeawait client.connect();try {// Retrieve the token list using the account_currencies commandconst response = await client.request({command: 'account_currencies',account: accountAddress,ledger_index: 'validated' // Specify 'validated' to retrieve from a validated ledger});// Output the result to the consoleconsole.log('Account Lines:', response);} catch (error) {console.error('Error retrieving account lines:', error);}// Call the client.disconnect() method to disconnect from the serverawait client.disconnect();}const charlieAddress = 'charlie_wallet_address_here'; // Charlie's addresscheckAccountCurrencies(charlieAddress); // Pass in the account address as an argument to the functionconst aliceAddress = 'alice_wallet_address_here'; // Alice's addresscheckAccountCurrencies(aliceAddress);- Enter Charlie’s testnet address in
charlie_wallet_address_here
. - Enter Alice’s testnet address in
alice_wallet_address_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 check_account_currencies.js -
If successful, the console will display the following.
Terminal window Account Lines: { // Charlieid: 1,result: {ledger_hash: 'D05315702B8617B192FB1666BF05F588A457C30D0F493C4FA802BAABDE34209E',ledger_index: 441812, // Ledger index number (source of this data)receive_currencies: [], // Array of tokens that can be receivedsend_currencies: [ 'DOJ' ], // Array of tokens that can be sentvalidated: true // Whether the data is from the latest validated ledger},type: 'response'}Account Lines: { // Aliceid: 1,result: {ledger_hash: 'D05315702B8617B192FB1666BF05F588A457C30D0F493C4FA802BAABDE34209E',ledger_index: 441812,receive_currencies: [ 'DOJ' ], // Array of tokens that Alice can receivesend_currencies: [],validated: true},type: 'response'}From Charlie’s account information, which is the issuer of the DOJ token,
DOJ
is included in send_currencies, and from Alice’s account information,DOJ
is included in the receive_currencies array, confirming that Alice can receive theDOJ
token.This shows that XRPL maintains account information (state) for each wallet and transactions are strictly verified based on this information.
What are ledger_index and ledger_hash?
The ledger_hash displayed in the console log is a unique hash value for the ledger. In this sample, data was retrieved from the 441812th validated ledger.
Let’s check the ledger validated at the 441812th position on the testnet.
The ledger hash value for this ledger (441812
) is D05315702B8617B192FB1666BF05F588A457C30D0F493C4FA802BAABDE34209E
, which matches the console log above.
In order to understand the overall big picture of XRPL, it is very important to confirm using your eyes and understand each of these meanings along the way.
Additionally, XRPL Explorer offers a testnet version as well, which is one of the necessary tools for development, so make sure you bookmark it.