Section 5 of 16
Pair: Adding Liquidity (mint)
Key takeaway: Uniswap V2's
mint()uses a transfer-first pattern. The user sends tokens to the pair before calling, and the function readsbalanceOf(this) - reserveto determine deposit amounts. The first deposit mintssqrt(amount0 * amount1) - 1000LP tokens to the depositor and locks 1,000 wei (MINIMUM_LIQUIDITY) to address(0) forever, preventing first-depositor share inflation attacks._mintFee()must run before the function readstotalSupplyinto a local variable, otherwise the protocol fee recipient is silently diluted on every liquidity event.
What You Are Building
The mint() function is how liquidity enters the pool. A user sends both tokens to the pair contract, then calls mint(). The function calculates how many LP tokens to issue based on the amounts deposited relative to the existing reserves. These LP tokens represent the user's share of the pool.
This is the first function that actually moves value. Everything you built in the previous sections (reserves, the lock modifier, safe transfers, the oracle) comes together here. Every design choice in mint() exists because of a specific attack vector or accounting requirement. Understanding the why behind each line is what separates someone who can fork Uniswap from someone who can build a DEX from scratch.
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