5/5
## Deploying a Uniswap V2 Pair Contract We'll learn about the `createPair` function inside the Uniswap V2 Factory contract. This function deploys a Uniswap V2 Pair contract, which is responsible for managing the swapping of tokens and adding and removing liquidity. The `createPair` function takes two addresses as input: `tokenA` and `tokenB`. It returns the address of the newly deployed Pair contract. Here is how the `createPair` function works: 1. **Check for Identical Tokens:** The function first checks that `tokenA` and `tokenB` are not the same address. 2. **Sort Tokens:** It then sorts the two addresses alphabetically, ensuring that the smaller address is assigned as `token0` and the larger as `token1` in the Pair contract. This is important for consistent order in the Pair contract's logic. 3. **Check for Existing Pair:** The function checks if a Pair contract has already been deployed for this specific pair of tokens (`token0` and `token1`) using the `getPair` mapping. 4. **Calculate Contract Address:** If the pair hasn't been deployed, the function uses the `create2` function to calculate the address of the new Pair contract. This function is a deterministic way to calculate the address of a contract before it's actually deployed. 5. **Deploy the Pair Contract:** The function uses the calculated address and the bytecode of the Pair contract to deploy the Pair contract. 6. **Initialize the Pair Contract:** After deployment, the function calls the `initialize` function on the newly deployed Pair contract, passing in the addresses of `token0` and `token1` as arguments. This initializes the Pair contract with the correct token information. 7. **Populate Mapping:** Finally, the function updates the `getPair` mapping with the deployed Pair contract address and the associated `token0` and `token1` addresses. This ensures that future interactions with this pair can find the correct Pair contract. The `create2` function is a clever trick that Uniswap V2 uses to make deploying Pair contracts more efficient. It allows the address of the Pair contract to be calculated directly from the addresses of the tokens that will be traded. This eliminates the need for a separate deployment transaction for each new Pair contract. --- This is the implementation process of the `createPair` function. ```solidity function createPair(address tokenA, address tokenB) external returns (address pair) { require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES'); // NOTE: sort tokens by address // address <-> 20 bytes hexadecimal <-> 160 bit number // 0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97 <-> 412311598482915581890913355723629879470649597847 // address(1), address(2) (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS'); require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient // NOTE: creation code = runtime code + constructor args bytes memory bytecode = type(UniswapV2Pair).creationCode; // NOTE: deploy with create2 - UniswapV2Library.pairFor // NOTE: create2 addr <- keccak256(creation bytecode) <- constructor args // create2 addr = keccak256(0xff, deployer, salt, keccak256(creation bytecode)) bytes32 salt = keccak256(abi.encodePacked(token0, token1)); assembly { // NOTE: pair = address(new UniswapV2Pair{salt: salt}()); pair := create2(0, add(bytecode, 32), mload(bytecode), salt) } // NOTE: call initialize to initialize contract without constructor args IUniswapV2Pair(pair).initialize(token0, token1); getPair[token0][token1] = pair; getPair[token1][token0] = pair; // populate mapping in the reverse direction allPairs.push(pair); emit PairCreated(token0, token1, pair, allPairs.length); } ```
We'll learn about the createPair function inside the Uniswap V2 Factory contract. This function deploys a Uniswap V2 Pair contract, which is responsible for managing the swapping of tokens and adding and removing liquidity.
The createPair function takes two addresses as input: tokenA and tokenB. It returns the address of the newly deployed Pair contract.
Here is how the createPair function works:
Check for Identical Tokens: The function first checks that tokenA and tokenB are not the same address.
Sort Tokens: It then sorts the two addresses alphabetically, ensuring that the smaller address is assigned as token0 and the larger as token1 in the Pair contract. This is important for consistent order in the Pair contract's logic.
Check for Existing Pair: The function checks if a Pair contract has already been deployed for this specific pair of tokens (token0 and token1) using the getPair mapping.
Calculate Contract Address: If the pair hasn't been deployed, the function uses the create2 function to calculate the address of the new Pair contract. This function is a deterministic way to calculate the address of a contract before it's actually deployed.
Deploy the Pair Contract: The function uses the calculated address and the bytecode of the Pair contract to deploy the Pair contract.
Initialize the Pair Contract: After deployment, the function calls the initialize function on the newly deployed Pair contract, passing in the addresses of token0 and token1 as arguments. This initializes the Pair contract with the correct token information.
Populate Mapping: Finally, the function updates the getPair mapping with the deployed Pair contract address and the associated token0 and token1 addresses. This ensures that future interactions with this pair can find the correct Pair contract.
The create2 function is a clever trick that Uniswap V2 uses to make deploying Pair contracts more efficient. It allows the address of the Pair contract to be calculated directly from the addresses of the tokens that will be traded. This eliminates the need for a separate deployment transaction for each new Pair contract.
This is the implementation process of the createPair function.
A comprehensive explanation of the createPair function in the Uniswap V2 factory contract. The lesson covers how the createPair function works, including how addresses are sorted and converted into numerical values, as well as how the factory contract uses create2 to deploy the Uniswap V2 pair contract.
Previous lesson
Previous
Next lesson
Next
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on November 13, 2025
Duration: 14min
Duration: 1h 20min
Duration: 10min
Duration: 54min
Duration: 25min
Duration: 26min
Duration: 1h 03min
Duration: 59min
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on November 13, 2025