Section 3 of 16
Pair: State and Initialization
Key takeaway: Uniswap V2's pair contract packs
uint112 reserve0,uint112 reserve1, anduint32 blockTimestampLastinto a single 256-bit storage slot, saving 2,100 gas on every swap, mint, and burn that reads reserves. The uint112 ceiling fits roughly 5.19e33 tokens, more headroom than any practical token needs, and the uint32 timestamp wraps in January 2106 by design. The TWAP oracle only ever subtracts cumulative timestamps, and modular subtraction handles the wrap correctly without intervention.
What You Are Building
UniswapV2Pair is the core AMM contract. It inherits UniswapV2ERC20 (the LP token you just built) and adds everything that makes the constant product market maker work: reserves, the oracle, minting, burning, and swapping.
In this section you are setting up the foundation. The state variables, the initialization function, a safe ERC20 transfer helper, the reentrancy guard, and the reserve getter. Nothing here moves tokens or enforces invariants yet. That comes in the next sections. But every function in the pair depends on what you write here. Get this wrong and every subsequent section breaks.
Your Code
Requirements
Sign up free — keep reading + earn 15 Lynx
Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.
Sign up free to continueAlready have an account? Log in