Core Dev Bootcamp
Docs

05Core Dev Bootcamp

AMM: Automated Market Maker

Module Overview

This module explores the Automated Market Maker (AMM) implementation in rippled, one of the most sophisticated DeFi features on the XRP Ledger. You will learn how AMM pools provide decentralized liquidity, how the constant product formula maintains price equilibrium, and how the pathfinding engine integrates AMM liquidity alongside the traditional order book (CLOB).

Building on your understanding of transactors (Module 02), ledger entries (Module 03), and the consensus mechanism (Module 08-09), you will dive into the complete AMM architecture: from pool creation to liquidity provision, trading fee governance, and auction slot mechanics.

By the end of this module, you will understand:

  • How AMM pools are created and managed as pseudo-accounts
  • The mathematical formulas behind swaps, deposits, and withdrawals
  • How LP tokens represent ownership of pool liquidity
  • The governance mechanisms for trading fees and auction slots
  • How pathfinding integrates AMM offers with CLOB offers

Prerequisites

Before starting this module, ensure you have completed:

Understanding these concepts is essential as AMM builds upon the transactor framework and introduces new ledger entry types.


Explore the Topics

This module covers the complete AMM implementation through focused topics:


Architecture

AMM Data Structures and Pool Management

Understand how AMM pools are represented in the ledger, the pseudo-account architecture, LP token design, and the relationships between AMM ledger entries and trustlines.

Key Topics: ltAMM ledger entry, pseudo-account, LP token currency code, VoteSlots, AuctionSlot

Codebase: src/xrpld/app/misc/AMMHelpers.h, include/xrpl/protocol/detail/ledger_entries.macro

Explore AMM Architecture


Transactions

AMM Transaction Types and Lifecycle

Master all seven AMM transaction types: Create, Deposit, Withdraw, Vote, Bid, Delete, and Clawback. Learn the validation phases (preflight, preclaim, doApply) for each transactor.

Key Topics: AMMCreate, AMMDeposit, AMMWithdraw, AMMVote, AMMBid, AMMDelete, AMMClawback

Codebase: src/xrpld/app/tx/detail/AMM*.cpp

Explore AMM Transactions


Logic

Constant Product Formula and LP Token Calculations

Deep dive into the AMM logic: the constant product invariant (x * y = k), swap formulas, LP token minting/burning calculations, and precision handling.

Key Topics: Constant product formula, swapAssetIn, swapAssetOut, lpTokensOut, ammLPTokens, fee multipliers

Codebase: src/xrpld/app/misc/AMMHelpers.cpp

Explore AMM Logic


Pathfinding Integration

AMM Liquidity in Payment Paths

Discover how the pathfinding engine generates synthetic AMM offers, competes with CLOB liquidity, and routes payments through optimal paths combining both liquidity sources.

Key Topics: AMMLiquidity, AMMOffer, AMMContext, Fibonacci sequence offers, quality matching

Codebase: src/xrpld/app/paths/detail/AMMLiquidity.cpp, src/xrpld/app/paths/detail/AMMOffer.cpp

Explore AMM Pathfinding


What You Will Learn

By completing this module, you will be able to:

  • Explain how AMM pools maintain price equilibrium through the constant product formula
  • Trace AMM transactions through the complete transactor lifecycle
  • Calculate LP token amounts for deposits and withdrawals
  • Understand the trading fee governance mechanism and auction slot bidding
  • Debug AMM-related issues using pool balances and invariant checks
  • Implement test scenarios for AMM functionality
  • Navigate the AMM codebase efficiently

Key Files Reference

ComponentLocationPurpose
AMMCreatesrc/xrpld/app/tx/detail/AMMCreate.cppPool creation transactor
AMMDepositsrc/xrpld/app/tx/detail/AMMDeposit.cppLiquidity deposit transactor
AMMWithdrawsrc/xrpld/app/tx/detail/AMMWithdraw.cppLiquidity withdrawal transactor
AMMVotesrc/xrpld/app/tx/detail/AMMVote.cppFee voting transactor
AMMBidsrc/xrpld/app/tx/detail/AMMBid.cppAuction slot bidding transactor
AMMDeletesrc/xrpld/app/tx/detail/AMMDelete.cppPool deletion transactor
AMMHelperssrc/xrpld/app/misc/detail/AMMHelpers.cppMathematical formulas
AMMUtilssrc/xrpld/app/misc/detail/AMMUtils.cppUtility functions
AMMLiquiditysrc/xrpld/app/paths/detail/AMMLiquidity.cppPathfinding integration
AMMOffersrc/xrpld/app/paths/detail/AMMOffer.cppSynthetic offer generation
ltAMM Entryinclude/xrpl/protocol/detail/ledger_entries.macroLedger entry definition
AMM Featureinclude/xrpl/protocol/detail/features.macroAmendment registration

Homework

Implement and test AMM functionality, exploring pool creation, liquidity management, and pathfinding integration.

Homework 1: AMM Pool Lifecycle

Create an AMM pool, perform deposits and withdrawals, and verify the constant product invariant is maintained throughout.

Homework 2: AMM Pathfinding Analysis

Analyze how payments route through AMM pools versus CLOB offers, comparing execution quality and slippage.


Helpful Resources

  • Rippled Repository: github.com/XRPLF/rippled
  • AMM Transactors: src/xrpld/app/tx/detail/AMM*.cpp
  • AMM Helpers: src/xrpld/app/misc/detail/AMMHelpers.cpp
  • AMM Tests: src/test/app/AMM_test.cpp
  • XLS-30d Specification: XRPL AMM Amendment

Tips for Success

  1. Start with AMMCreate: Understanding pool creation helps contextualize all other operations
  2. Trace the Math: Use a calculator to verify swap formulas with example values
  3. Check Invariants: The constant product (A * B) should never decrease
  4. Use Test Harness: src/test/app/AMM.h provides utilities for testing

Questions

If you have any questions about the homework or would like us to review your work, feel free to contact us.

Submit Feedback


Next Module: Building an amendment I - Lifecycle and Core Protocol Impact

In this section

  1. 01AMM ArchitectureAMM Architecture for AMM: Automated Market Maker, in the Core Dev Bootcamp: XRPL Commons docs.
  2. 02AMM TransactionsAMM Transactions for AMM: Automated Market Maker, in the Core Dev Bootcamp: XRPL Commons docs.
  3. 03AMM LogicAMM Logic for AMM: Automated Market Maker, in the Core Dev Bootcamp: XRPL Commons docs.
  4. 04Pathfinding IntegrationPathfinding Integration for AMM: Automated Market Maker, in the Core Dev Bootcamp: XRPL Commons docs.