DEV Community

Cover image for Build an XRP Ledger Trading Bot in JavaScript
xrpl.to
xrpl.to

Posted on

Build an XRP Ledger Trading Bot in JavaScript

The XRP Ledger has a built-in decentralized exchange. Every token trade happens on-chain through the protocol's order book and AMM pools. You do not need an exchange account, an API key or a custody agreement.

This guide builds a trading bot that reads live prices, places a limit order, checks if it filled and cancels it. Everything runs against the Testnet first so you do not risk real XRP.

1. Set up

mkdir xrpl-bot && cd xrpl-bot
npm init -y
npm install xrpl
Enter fullscreen mode Exit fullscreen mode

The examples use xrpl.js 5.x and Node.js 22+.

2. Connect to the Testnet

const xrpl = require('xrpl');

async function main() {
  const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
  await client.connect();

  // Fund a test wallet with 100 XRP from the faucet
  const { wallet } = await client.fundWallet();
  console.log('Address:', wallet.address);
  console.log('Seed:', wallet.seed);

  // Check balance
  const balance = await client.getXrpBalance(wallet.address);
  console.log('Balance:', balance, 'XRP');

  await client.disconnect();
}

main();
Enter fullscreen mode Exit fullscreen mode

Request — run this:

node bot.js
Enter fullscreen mode Exit fullscreen mode

Response

Address: rHoLM9Z1FXBQGmFe3u9sSTm9RDzKGrueKi
Seed: sEdVNa4BqxpiKuqH9Z3GZmPpWBSBgPB
Balance: 100 XRP
Enter fullscreen mode Exit fullscreen mode

The Testnet faucet gives you 100 test XRP. These have no real value.

3. Read the order book

Before placing an order, check what is on the book. The book_offers command shows existing orders:

const orderbook = await client.request({
  command: 'book_offers',
  taker_gets: { currency: 'TST', issuer: 'rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd' },
  taker_pays: { currency: 'XRP' },
  limit: 5
});

for (const offer of orderbook.result.offers) {
  const price = Number(offer.TakerPays) / 1e6 / Number(offer.TakerGets.value);
  console.log(`${offer.TakerGets.value} TST @ ${price.toFixed(4)} XRP`);
}
Enter fullscreen mode Exit fullscreen mode

The XRP Ledger stores XRP amounts in drops (1 XRP = 1,000,000 drops). Token amounts are decimal strings.

4. Place a limit order

A limit order on the XRP Ledger is an OfferCreate transaction:

const offer = {
  TransactionType: 'OfferCreate',
  Account: wallet.address,
  TakerPays: {
    currency: 'TST',
    issuer: 'rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd',
    value: '100'           // Buy 100 TST
  },
  TakerGets: xrpl.xrpToDrops('10'),  // Pay 10 XRP
  Flags: 0
};

const prepared = await client.autofill(offer);
const signed = wallet.sign(prepared);
const result = await client.submitAndWait(signed.tx_blob);

console.log('Result:', result.result.meta.TransactionResult);
console.log('Tx hash:', result.result.hash);
Enter fullscreen mode Exit fullscreen mode

This places a buy order: 100 TST for 10 XRP (0.1 XRP per TST). If the order book has a matching sell at or below that price, it fills immediately — partially or fully. Otherwise it sits on the book as a resting order.

5. Check your open orders

const offers = await client.request({
  command: 'account_offers',
  account: wallet.address
});

for (const o of offers.result.offers) {
  console.log(`Offer ${o.seq}: ${JSON.stringify(o.taker_gets)} for ${JSON.stringify(o.taker_pays)}`);
}
Enter fullscreen mode Exit fullscreen mode

6. Cancel an order

const cancel = {
  TransactionType: 'OfferCancel',
  Account: wallet.address,
  OfferSequence: offers.result.offers[0].seq
};

const prepared = await client.autofill(cancel);
const signed = wallet.sign(prepared);
const result = await client.submitAndWait(signed.tx_blob);

console.log('Cancelled:', result.result.meta.TransactionResult);
Enter fullscreen mode Exit fullscreen mode

7. Get market data from the xrpl.to API

For price history, trending tokens and volume data, the xrpl.to API gives you pre-computed analytics without running your own indexer:

# Top tokens by 24h volume
curl https://api.xrpl.to/v1/tokens?sortBy=vol24hXrp&sortType=desc&limit=5

# 1-hour candles for a token
curl "https://api.xrpl.to/v1/ohlc/{md5}?interval=1h&limit=100"

# Live price stream
wscat -c wss://api.xrpl.to/ws/sync/
Enter fullscreen mode Exit fullscreen mode

No API key needed. 2 requests per second on the free tier.

8. Move to Mainnet

When the bot works on the Testnet, switch the connection to a Mainnet node:

// Public nodes (free, no key):
const client = new xrpl.Client('wss://s1.xrpl.stream');  // Full history
// or: wss://s1.ripple.com, wss://xrplcluster.com
Enter fullscreen mode Exit fullscreen mode

On Mainnet you need a funded wallet. The 1 XRP reserve activates the account, plus 0.2 XRP per open order.

The XRP Ledger settles every 3–5 seconds. Transaction fees are 12 drops (0.000012 XRP). There is no gas auction — ordering within a ledger is by canonical hash, not by fee.

What to build next

  • Price alerts: poll the token endpoint every 15 seconds and send a notification when a threshold crosses
  • Grid bot: place buy and sell orders at fixed intervals around the mid price
  • Arbitrage: compare the DEX price against external exchange prices from xrpl.to/v1/tokens
  • Sniper: subscribe to the xrpl.to WebSocket for new token launches and auto-buy

Resources

Data by xrpl.to — the free XRP Ledger data API.

Top comments (0)