Pathfinding Integration
Back to AMM: Automated Market Maker
Introduction
One of XRPL's unique features is the deep integration between AMM pools and the pathfinding engine. Unlike other blockchains where AMM and order book liquidity are separate, XRPL's pathfinding engine can route payments through both CLOB offers and AMM pools, automatically selecting the best execution path.
This chapter explores how AMM offers are generated, how they compete with CLOB offers, and the algorithms that ensure optimal payment routing.
Architecture Overview
Dual Liquidity Model
┌─────────────────────────────────────────────────────────────┐
│ Payment Request │
│ (Source -> Destination) │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Pathfinding Engine │
│ │
│ ┌───────────────────┐ ┌───────────────────┐ │
│ │ Order Book │ │ AMM Pool │ │
│ │ (CLOB) │ │ (Synthetic) │ │
│ │ │ │ │ │
│ │ Offer 1: 2.00 │ │ Quality: 2.05 │ │
│ │ Offer 2: 2.01 │ │ (calculated) │ │
│ │ Offer 3: 2.02 │ │ │ │
│ └─────────┬─────────┘ └─────────┬─────────┘ │
│ │ │ │
│ └───────────┬───────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ Best Execution │ │
│ │ Path Selection │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘Key Components
AMMLiquidity
src/xrpld/app/paths/AMMLiquidity.cpp
Generate synthetic offers
AMMOffer
src/xrpld/app/paths/AMMOffer.cpp
Represent AMM offers
AMMContext
src/xrpld/app/paths/AMMContext.h
Track AMM state during payment
BookStep
src/xrpld/app/paths/impl/BookStep.cpp
Integrate AMM with order book
AMMContext: State Management
The AMMContext class tracks AMM usage during payment execution.
Location: src/xrpld/app/paths/AMMContext.h
Iteration Limit
The 30-iteration limit prevents:
Infinite loops in complex path finding
Excessive computation for large payments
Potential DoS through pathfinding abuse
AMMLiquidity: Offer Generation
The AMMLiquidity class generates synthetic offers from AMM pools.
Location: src/xrpld/app/paths/AMMLiquidity.cpp
Offer Generation Strategies
Single Path Mode
When processing a single payment path, AMM offers are sized to match CLOB quality:
Multi-Path Mode
For payments using multiple paths, Fibonacci sequence sizing prevents over-concentration:
AMMOffer: Synthetic Offer Representation
The AMMOffer class represents an AMM-backed offer in the pathfinding system. Unlike CLOB offers which are stored on the ledger, AMMOffers are created dynamically during payment execution.
Location: src/xrpld/app/paths/AMMOffer.cpp
When Are AMMOffers Created?
AMMOffers are not ledger objects - they are synthetic, ephemeral offers generated on-the-fly:
Key Insight: AMMOffers Are Ephemeral
Stored on ledger
Created in memory
Persists until filled/cancelled
Exists only during payment execution
Has explicit size
Size calculated dynamically
Fixed quality
Quality adapts to pool state
Created by OfferCreate tx
Created by AMMLiquidity::getOffer()
How AMMOffers Are Sized
The size of an AMMOffer depends on the context:
Single-Path Mode:
Multi-Path Mode:
Structure
Quality Calculation
Limiting Offers
When the payment doesn't need the full offer:
Consuming Offers
Integration with BookStep
The BookStep class handles order book traversal, including AMM integration.
Location: src/xrpld/app/paths/impl/BookStep.cpp
Processing Order
Quality Competition
AMM and CLOB offers compete purely on quality (exchange rate):
This ensures best execution regardless of liquidity source.
Multi-Path Payment Example
Scenario
Payment: 10,000 XRP -> USD Available liquidity:
CLOB: 5,000 XRP @ 2.00 USD/XRP
CLOB: 3,000 XRP @ 1.98 USD/XRP
AMM Pool: 100,000 XRP / 200,000 USD (spot: 2.00)
Execution
Flow Diagram
Performance Considerations
Iteration Limits
Why 30?
Balances payment quality with computation cost
Prevents pathfinding abuse
Sufficient for most payment sizes
Quality Caching
Balance Snapshots
Edge Cases
Empty AMM Pool
Iteration Limit Reached
Quality Worse Than CLOB
Debugging Tips
Tracing AMM Offers
Enable detailed logging:
Verifying Integration
Check that AMM is being considered:
Testing Quality Competition
Create test scenarios with known CLOB and AMM qualities to verify correct selection.
Summary
The AMM pathfinding integration enables:
Best Execution: Automatic selection between AMM and CLOB
Seamless Liquidity: Users don't need to know liquidity source
Fair Competition: Quality-based selection ensures best rates
Bounded Computation: Iteration limits prevent abuse
Multi-Path Support: Fibonacci sizing for complex payments
This integration is unique to XRPL and provides significant advantages over systems where AMM and order book liquidity are separate.
References to Source Code
src/xrpld/app/paths/AMMLiquidity.cpp- Offer generationsrc/xrpld/app/paths/AMMOffer.cpp- Offer representationsrc/xrpld/app/paths/AMMContext.h- State managementsrc/xrpld/app/paths/impl/BookStep.cpp- Integration pointsrc/xrpld/app/misc/AMMHelpers.h- Quality calculations
Cross-References
AMM Architecture - Pool structure
AMM Logic - Quality and swap formulas
Module 02: Transaction Lifecycle - Payment flow
Last updated

