Automated Market Maker (AMM)
Understanding and working with the AMM in the PumpFundler SDK
Automated Market Maker (AMM)
The Automated Market Maker (AMM) is a core component of the PumpFundler protocol, responsible for managing token liquidity and determining prices for buy and sell operations. It implements the bonding curve mechanism that governs token price dynamics.
Structure
The AMM
class has the following structure:
Let’s break down each property:
virtualSolReserves
: The virtual SOL balance used for price calculations.virtualTokenReserves
: The virtual token balance used for price calculations.realSolReserves
: The actual SOL balance in the AMM.realTokenReserves
: The actual token balance in the AMM.initialVirtualTokenReserves
: The initial virtual token reserves, used for scaling calculations.
Key Methods
Static Factory Methods
These methods create an AMM instance from either a GlobalAccount
or a BondingCurveAccount
, initializing it with the appropriate reserve values.
getBuyPrice
This method calculates the price in SOL for buying a specific amount of tokens. It:
- Calculates the product of virtual reserves.
- Determines the new virtual token reserves after the purchase.
- Computes the new virtual SOL reserves.
- Returns the amount of SOL needed for the purchase.
applyBuy
This method executes a buy operation, updating the AMM’s state. It:
- Determines the final token amount (limited by available reserves).
- Calculates the SOL amount required.
- Updates virtual and real reserves.
- Returns a
BuyResult
with the token and SOL amounts involved.
getSellPrice
This method calculates the amount of SOL to be received for selling a specific amount of tokens. It:
- Calculates the scaling factor.
- Determines the proportion of tokens being sold.
- Computes the SOL to be received.
- Returns the minimum of the calculated SOL and the real SOL reserves.
applySell
This method executes a sell operation, updating the AMM’s state. It:
- Updates virtual and real token reserves.
- Calculates the sell price.
- Updates virtual and real SOL reserves.
- Returns a
SellResult
with the token and SOL amounts involved.
Usage in the SDK
The AMM
class is primarily used internally by the PumpFundler SDK to:
- Calculate buy and sell prices for tokens.
- Execute buy and sell operations.
- Manage the state of the bonding curve for each token.
Here’s an example of how you might use the AMM
class to simulate trading operations:
AMM Dynamics
Understanding the AMM dynamics is crucial for participants in the PumpFundler ecosystem:
-
Constant Product Formula: The AMM uses a constant product formula (
virtualSolReserves * virtualTokenReserves = constant
) to determine prices, ensuring that the product of reserves remains constant after trades. -
Price Slippage: Larger trade sizes will experience more slippage, as they have a more significant impact on the reserve ratios.
-
Virtual vs. Real Reserves: The use of virtual reserves allows for more flexible pricing models, while real reserves represent the actual assets in the pool.
-
Liquidity Depth: The difference between virtual and real reserves influences the liquidity depth and price stability of the token.