advanced 55 min

Coding lab: manage AMM liquidity safely

Look at a liquidity pool, add and remove liquidity, and prove exactly what changed by comparing the pool before and after.

Prerequisites

Complete these before starting this module:

What you'll learn

  • Normalize amm_info state for display.
  • Estimate and execute controlled liquidity changes.
  • Compare balances and LP supply before and after validation.
  • Continue the analysis from prepared snapshots during Testnet disruption.
Complete this module by self-assessment and mentor review. Jump to assessment

How the provided code works

The read-only helper gives you two snapshots of a liquidity pool — one before a deposit and one after — plus (for the live checkpoint) functions that create a training pool and deposit/withdraw real liquidity. When you press Run in the default mode, it works entirely from those snapshots, so you can get the maths right with no network.

Your job is two small functions: pull the useful numbers out of one snapshot, and compare two snapshots to report exactly what changed (XRP, TST, and LP tokens). The console prints the before state and the change as JSON.

The before/after discipline

You will add liquidity to a shared pool and take some back out, checking the pool before and after each step so you can show exactly what moved. Start on fixed sample data, then repeat it for real on Testnet.

Before every operation, record the participant balances, the pool's reserves, its trading fee, and the LP-token supply. Estimate the direction and rough size of the change, submit with bounded amounts, wait for validation, then pull fresh state. The metadata explains the ledger changes, and a second amm_info confirms where the pool ended up.

Begin with the offline fixture below. It runs the same functions and prints the same shape as the live checkpoint, so moving to the network only swaps the data source. Once the fixture reconciles, query the instructor-supplied Testnet pool, run the controlled AMMDeposit, and withdraw only the LP tokens issued to your disposable participant.

Leave the shared training pool with liquidity in it — never drain it. If Testnet or the faucet wobbles, finish the fixture analysis and attach the instructor's prepared hash at review.

Assignments

0 of 2 complete

Unlocks

Finishing this module opens up: