Section 14 of 16
Router: Swapping
Key takeaway: Uniswap V2's Router exposes six swap variants (exact-in vs exact-out, token vs ETH on either side), all built on a single internal
_swap()that loops through a path of pairs. The key gas optimization: intermediate hops send their output token directly to the next pair in the path, so the Router never touches intermediate tokens. This saves roughly 10-15k gas per hop versus routing through the Router twice.amountOutMin(oramountInMaxfor exact-out) provides slippage protection against MEV and stale-mempool execution.
What You Are Building
This is the heart of the Router. Every token trade on Uniswap V2 flows through these functions. There are six external swap variants covering every combination of exact input vs. exact output, and token vs. ETH on either side. All of them rely on a single internal _swap() function that handles the core routing logic. Understanding _swap() deeply is the key to understanding every swap variant.
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