Section 2 of 4

Build
+15 Shields

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.

Your Code

Solution.sol
Solidity
Loading editor...

Requirements

reserve0 declared as uint112
reserve1 declared as uint112
blockTimestampLast declared as uint32
getReserves returns three values
getSpotPrice checks for zero reserves
_update reads token balances