# 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](https://docs.xrpl-commons.org/core-dev-bootcamp/module03) - SHAMap and NodeStore: Data Persistence and State Management
* [Transactors](https://docs.xrpl-commons.org/core-dev-bootcamp/module03bis) - Understanding the Lifecycle of a Transaction
* [Module Consensus I](https://docs.xrpl-commons.org/core-dev-bootcamp/module08) - Node, Consensus, and Ledger Fundamentals
* [Module Consensus II](https://docs.xrpl-commons.org/core-dev-bootcamp/module09) - 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`

[Explore AMM Architecture](https://docs.xrpl-commons.org/core-dev-bootcamp/module09bis/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](https://docs.xrpl-commons.org/core-dev-bootcamp/module09bis/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](https://docs.xrpl-commons.org/core-dev-bootcamp/module09bis/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](https://docs.xrpl-commons.org/core-dev-bootcamp/module09bis/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

| Component    | Location                                            | Purpose                         |
| ------------ | --------------------------------------------------- | ------------------------------- |
| 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](https://github.com/XRPL-Commons/xrpl-trainings/blob/main/core-dev-bootcamp/module09bis/homeworks/homework1.md)

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

[Homework 2: AMM Pathfinding Analysis](https://github.com/XRPL-Commons/xrpl-trainings/blob/main/core-dev-bootcamp/module09bis/homeworks/homework2.md)

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

***

### Helpful Resources

* Rippled Repository: [github.com/XRPLF/rippled](https://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](https://github.com/XRPLF/XRPL-Standards/tree/master/XLS-30d)

### 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](https://docs.google.com/forms/d/e/1FAIpQLSe7qIbqepUKGVJxmWFHv0rs1zsBAzk8G6y5-bfz0Yd5rqW_5A/viewform)

***

**Next Module**: Building an amendment I - [Lifecycle and Core Protocol Impact](https://docs.xrpl-commons.org/core-dev-bootcamp/module10)
