Section 7 of 16

Build
+15 Lynx

Pair: The Swap

Key takeaway: Uniswap V2's swap() uses an optimistic-transfer pattern: tokens leave the pair before any input is verified, the optional IUniswapV2Callee callback fires (enabling flash swaps with no flash-loan-specific code), and only at the end does the contract enforce the constant-product invariant K' >= K against the actual final balances. A reentrancy lock modifier 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

Solution.sol
Solidity
Loading editor...

Requirements

Write your implementation, then click Run Tests. Tests execute on the server.

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 continue

Already have an account? Log in