Section 6 of 9
Liquidity Provision + LP Tokens
Liquidity Provision + LP Tokens
What You Are Building
Anyone can become a liquidity provider by depositing tokens into a pair. In return, they receive LP tokens representing their share of the pool. When they want to exit, they burn LP tokens and receive their proportional share of both tokens.
In this section, you will implement:
- LP token minting logic (ERC-20)
mint(): calculate LP tokens for a depositburn(): calculate token amounts for a withdrawal- The
MINIMUM_LIQUIDITYmechanism
How LP Token Math Works
First deposit (empty pool):
liquidity = sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY
The geometric mean (sqrt(amount0 * amount1)) prevents the first depositor from manipulating the LP token supply. MINIMUM_LIQUIDITY (1000 wei) is burned permanently to address(0) to prevent dust attacks.
Subsequent deposits:
liquidity = min(
amount0 * totalSupply / reserve0,
amount1 * totalSupply / reserve1
)
This ensures new deposits are proportional to existing reserves. If you deposit tokens in a different ratio than the pool, you get fewer LP tokens (the excess goes to existing LPs as a bonus).
The MINIMUM_LIQUIDITY Constant
When the first LP deposits, 1000 wei of LP tokens are burned forever. This prevents an attacker from:
- Being the first LP with 1 wei of each token
- Donating tokens directly to the contract
- Inflating the LP token value to make subsequent deposits round to 0
This is a critical security mechanism. Uniswap V2 uses 10**3 (1000 wei).
Your Task
Extend your SimpleAMM with ERC-20 LP token functionality and implement mint() and burn().