Checking Payment Paths with path_find | XRPL Development in JavaScript - Level 3
This guide explains cross-currency payments by dealing with A.USD
issued by Issuer A and B.EUR
issued by Issuer B.
In the previous section, Alice provided liquidity by placing an offer to sell A.USD
issued by Issuer A and buy B.EUR
issued by Issuer B.
In this section, before executing a cross-currency payment, we will introduce the path_find command to search for payment paths.
Prerequisites
Alice has already provided liquidity by placing an offer to sell A.USD
issued by Issuer A and buy B.EUR
issued by Issuer B.
Daniel needs to make a payment to Charlie, who wants to receive A.USD
. However, Daniel currently does not hold any A.USD
.
Daniel only holds XRP or B.EUR
issued by Issuer B and is considering whether he can exchange his B.EUR
. If someone is willing to exchange B.EUR
for the desired amount of A.USD
, Daniel should be able to send his B.EUR
as A.USD
to Charlie.
Daniel wants to search if it is possible to send his B.EUR
to Charlie as A.USD
.
Creating the Script
-
Create a new file named
pathFinding.js
in your project directory. -
Paste the following code into
pathFinding.js
.import { Client, xrpToDrops } from 'xrpl';import { wallets } from './wallets.js';const client = new Client('wss://s.altnet.rippletest.net:51233');const main = async () => {await client.connect();const { issuerA, issuerB, alice, bob, charlie, daniel } = wallets;client.on('path_find', (stream) => {console.log(JSON.stringify(stream.alternatives, null, ' '));});client.request({command: 'path_find',subcommand: 'create',source_account: daniel.address, // Sender: Danieldestination_account: charlie.address, // Recipient: Charliedestination_amount: {currency: 'USD', // Currency codeissuer: issuerA.address, // Issuer A's addressvalue: '100', // Amount of A.USD Daniel wants to send to Charlie},});};main();
Running the Script
-
Run the following command in the command line to execute the script.
Terminal window node pathFinding.js -
If you see logs like the following, the operation was successful.
Terminal window [{"paths_computed": [[{"account": "rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU", // Issuer B's address"type": 1},{"currency": "USD","issuer": "rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp", // Issuer A's address"type": 48}]],"source_amount": {"currency": "EUR","issuer": "rf6R7pFgytT9JKjMVjKqeTv6MeDTrbNw3", // Daniel's address"value": "100"}}]