Coding lab: XRPL order-book client
Read both sides of a market, work out the best price and how much can really trade, then place and cancel your own offer.
What you'll learn
- Normalize XRP and issued amounts with integer-safe arithmetic.
- Calculate best prices, spread, and funded depth.
- Create, monitor, and cancel an offer.
- Reconcile the final offer state and balances.
How the provided code works
The read-only helper gives you a small, fixed sample of an order book plus a few number helpers, so you can get the maths right before touching the live network. When you press Run, it:
- Provides a sample list of offers shaped exactly like a real
book_offersresponse, including the*_fundedfields that show what can actually trade. - Provides integer-safe helpers (
decimalToScaled,dropsToScaled,ratio) so prices do not pick up floating-point error. - For the live checkpoint, provides functions that read a real book and place/cancel an offer.
Your job is to summarise the book: best price and funded depth. Start on the sample data, then switch to the live book when you are ready.
Build the read side first
First you will read a market, work out the best price and how much can actually trade, then place and cancel a small order of your own. Begin with fixed sample data where the numbers are predictable, and move to the live network once that works.
Finish the deterministic fixture before you touch the live book. Prices and token values arrive as decimal strings and XRP as drops, so the starter scales both to integers and your maths never picks up binary floating-point error. Read the funded values whenever the server gives them.
On the live checkpoint, request book_offers in both directions and label each view with what the taker pays and receives. Show best bid, best ask, spread, and funded depth for the size you want.
Add the write side
Place a small limit offer in the disposable market and keep both its transaction hash and creating sequence. Query account_offers to see whether it is still open, partly consumed, or gone, then cancel any remainder with OfferCancel and query balances and offers again. A cancel reporting success does not prove the offer ever existed, so let the reconciliation be the final word rather than the response label.
Resources
XRPL decentralized exchange
reading· xrpl.org
Offers
reading· xrpl.org
book_offers API
reference· xrpl.org
OfferCreate transaction
reference· xrpl.org
OfferCancel transaction
reference· xrpl.org
Get started with xrpl.js
tutorial· xrpl.org
xrpl.js API reference
reference· js.xrpl.org
XRP Testnet faucet
tool· xrpl.org
Testnet Explorer
tool· testnet.xrpl.org
Public XRPL servers
reference· xrpl.org
Assignments
0 of 1 complete