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:
SHAMap and NodeStore - SHAMap and NodeStore: Data Persistence and State Management
Transactors - Understanding the Lifecycle of a Transaction
Module Consensus I - Node, Consensus, and Ledger Fundamentals
Module Consensus II - UNLs and Ledger Amendments
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
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
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
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
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
AMMCreate
src/xrpld/app/tx/detail/AMMCreate.cpp
Pool creation transactor
AMMDeposit
src/xrpld/app/tx/detail/AMMDeposit.cpp
Liquidity deposit transactor
AMMWithdraw
src/xrpld/app/tx/detail/AMMWithdraw.cpp
Liquidity withdrawal transactor
AMMVote
src/xrpld/app/tx/detail/AMMVote.cpp
Fee voting transactor
AMMBid
src/xrpld/app/tx/detail/AMMBid.cpp
Auction slot bidding transactor
AMMDelete
src/xrpld/app/tx/detail/AMMDelete.cpp
Pool deletion transactor
AMMHelpers
src/xrpld/app/misc/detail/AMMHelpers.cpp
Mathematical formulas
AMMUtils
src/xrpld/app/misc/detail/AMMUtils.cpp
Utility functions
AMMLiquidity
src/xrpld/app/paths/detail/AMMLiquidity.cpp
Pathfinding integration
AMMOffer
src/xrpld/app/paths/detail/AMMOffer.cpp
Synthetic offer generation
ltAMM Entry
include/xrpl/protocol/detail/ledger_entries.macro
Ledger entry definition
AMM Feature
include/xrpl/protocol/detail/features.macro
Amendment 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*.cppAMM Helpers:
src/xrpld/app/misc/detail/AMMHelpers.cppAMM Tests:
src/test/app/AMM_test.cppXLS-30d Specification: XRPL AMM Amendment
Tips for Success
Start with AMMCreate: Understanding pool creation helps contextualize all other operations
Trace the Math: Use a calculator to verify swap formulas with example values
Check Invariants: The constant product (A * B) should never decrease
Use Test Harness:
src/test/app/AMM.hprovides 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.
Next Module: Building an amendment I - Lifecycle and Core Protocol Impact
Last updated

