Architecture & Code
Explore Uniswap's smart contract architecture — click to expand contracts, watch animated transaction flows, understand CREATE2, and walk through flash swaps.
📦 Interactive Contract Explorer
Click on any contract to expand and see its key functions. Uniswap V2 has just 3 core contracts — elegant simplicity.
Registry of all pairs. Creates new pair contracts using CREATE2 for deterministic addresses.
The core contract. Holds reserves, executes swaps, mints/burns LP tokens. One per trading pair.
User-facing contract. Handles token approvals, multi-hop paths, deadline checks, and ETH wrapping.
🔄 Animated Transaction Flow
Follow a swap transaction as it travels from your wallet through the Router, Factory, and Pair contracts.
🔮 CREATE2 Address Derivation
Uniswap uses CREATE2 to compute pair addresses deterministically — no need to query the Factory. Enter two token addresses to see how it works.
// CREATE2 address derivation: // pair = keccak256(0xff, factory, keccak256(tokenA, tokenB), initCodeHash) // // 1. Sort tokens: token0 < token1 (lexicographic) // 2. salt = keccak256(abi.encodePacked(token0, token1)) // 3. address = keccak256(0xff ++ factory ++ salt ++ init_code_hash)[12:] // // This means anyone can compute the pair address offline! // No need to call the Factory contract. Factory: 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f Token A: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 (WETH) Token B: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 (USDC) → Pair: 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc (ETH/USDC)
⚡ Flash Swap Walkthrough
Flash swaps let you receive tokens before paying for them — as long as you pay back (with fee) in the same transaction. Powerful for arbitrage, liquidations, and collateral swaps.
// Example: Flash swap arbitrage
contract FlashArb {
function execute(address pair, uint amount) external {
// Step 1: Request tokens (triggers flash swap)
IUniswapV2Pair(pair).swap(amount, 0, address(this), bytes("flash"));
}
function uniswapV2Call(address sender, uint amount0, uint amount1, bytes data) external {
// Step 4: We have the tokens! Do something profitable...
uint profit = doArbitrage(amount0);
// Step 5: Repay with fee
uint fee = amount0 * 3 / 997 + 1;
IERC20(token).transfer(msg.sender, amount0 + fee);
}
}