Section 2 of 10
Token Pair Setup and Reserve Tracking
Token Pair Setup and Reserve Tracking
What You Are Building
Every AMM starts the same way: a contract that holds two tokens and tracks their reserves. This is the foundation that all three architectures build on.
In this section, you will create:
- A contract that accepts two ERC-20 token addresses
- Internal reserve tracking that syncs with actual balances
- A
_update()function that refreshes reserves - A
getSpotPrice()function that derives price from the reserve ratio
Why Reserves Matter
The reserves are the AMM's state. Every swap, liquidity addition, and removal changes the reserves. The price at any moment is derived from the reserve ratio. If reserves are stale or incorrect, the AMM will misprice trades, and arbitrageurs will drain value.
Uniswap V2 tracks reserves as uint112 state variables (not the actual token balances). This lets the contract detect when tokens were sent directly to it (outside of the expected flow) and handle edge cases like fee-on-transfer tokens.
Your Task
Complete the SimpleAMM contract in the editor. The TODO comments show what to implement.