Section 7 of 16
Pair: The Swap
Key takeaway: Uniswap V2's
swap()uses an optimistic-transfer pattern: tokens leave the pair before any input is verified, the optionalIUniswapV2Calleecallback fires (enabling flash swaps with no flash-loan-specific code), and only at the end does the contract enforce the constant-product invariantK' >= Kagainst the actual final balances. A reentrancylockmodifier prevents recursive calls._mintFee()reuses the LP-token-dilution mechanism (1/6 of K growth) to realize the protocol's accumulated fee without per-swap accounting overhead.
What You Are Building
The swap() function is the heart of the protocol. Every trade on Uniswap V2 goes through this function. It also enables flash loans without any special flash loan code. You will also implement _mintFee(), which handles protocol fee collection through LP token dilution. Together, these two functions complete the UniswapV2Pair contract. This section is the most important one in the entire module. Take your time with it.
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