5/5
## Uniswap v2 Factory - Create Pair In this lesson, we'll learn how to create a Uniswap v2 pair contract using the Uniswap v2 Factory. We'll create a new test file called `uniswapV2Factory.test.sol`. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; interface IUniswapV2Factory { event PairCreated(address indexed token0, address indexed token1, address pair, uint256); function feeTo() external view returns (address); function feeToSetter() external view returns (address); function getPair(address tokenA, address tokenB) external view returns (address); function allPairs(uint256) external view returns (address); function allPairsLength() external view returns (uint256); function createPair(address tokenA, address tokenB) external returns (address pair); function setFeeTo(address) external; function setFeeToSetter(address) external; } contract UniswapV2FactoryTest is Test { IUniswapV2Factory private constant WETH_FACTORY = IUniswapV2Factory(UNISWAP_V2_FACTORY); address private constant WETH = address(0); function testCreatePair() public { // Exercise - deploy `token` - WETH pair contract // Write your code here // Don't change any other code // address pair; // YOUR CODE HERE } } ``` The first thing we'll do is assign the address of the pair contract to the `address pair` variable. We'll use the `createPair()` function from our factory, and we'll pass in the address of our token and the address of WETH as arguments. The `createPair()` function returns the address of the pair contract. We'll write: ```solidity address pair = factory.createPair(address(token), WETH); ``` Next, we'll need to access the `token0` and `token1` addresses, which are the addresses of the two tokens in the pair. We'll do this by using the `IUniswapV2Pair` interface. We'll cast the pair address to the `IUniswapV2Pair` interface and then call the `token0()` and `token1()` functions, and then we'll assign these addresses to the `address token0` and `address token1` variables. We'll write: ```solidity address token0 = IUniswapV2Pair(pair).token0(); address token1 = IUniswapV2Pair(pair).token1(); ``` The order of the tokens that we pass to the `createPair()` function doesn't matter. If the address of the token is less than the address of WETH, then `token0` is our token and `token1` is WETH. Otherwise, `token0` is WETH and `token1` is our token. We'll check this by using an if/else statement and using the `assertEq()` function. We'll write: ```solidity if (address(token) < WETH) { assertEq(token0, address(token), "token 0"); assertEq(token1, WETH, "token 1"); } else { assertEq(token0, WETH, "token 0"); assertEq(token1, address(token), "token 1"); } ``` We'll run our test using the fork URL from our `.env` file. We'll copy the fork URL from our `.env` file and paste it into our terminal. Then we'll execute the test with the following command. ```bash forge test --fork-url $FORK_URL --mp test/uniswap-v2/exercises/UniswapV2Factory.test.sol -vvvv ``` We can see that the test passes.
In this lesson, we'll learn how to create a Uniswap v2 pair contract using the Uniswap v2 Factory.
We'll create a new test file called uniswapV2Factory.test.sol.
The first thing we'll do is assign the address of the pair contract to the address pair variable. We'll use the createPair() function from our factory, and we'll pass in the address of our token and the address of WETH as arguments. The createPair() function returns the address of the pair contract.
We'll write:
Next, we'll need to access the token0 and token1 addresses, which are the addresses of the two tokens in the pair. We'll do this by using the IUniswapV2Pair interface. We'll cast the pair address to the IUniswapV2Pair interface and then call the token0() and token1() functions, and then we'll assign these addresses to the address token0 and address token1 variables.
We'll write:
The order of the tokens that we pass to the createPair() function doesn't matter. If the address of the token is less than the address of WETH, then token0 is our token and token1 is WETH. Otherwise, token0 is WETH and token1 is our token. We'll check this by using an if/else statement and using the assertEq() function.
We'll write:
We'll run our test using the fork URL from our .env file. We'll copy the fork URL from our .env file and paste it into our terminal. Then we'll execute the test with the following command.
We can see that the test passes.
A practical demonstration of how to deploy a new Uniswap V2 pair contract. The lesson shows the code for creating the contract, and then goes through running tests against the actual contract on mainnet, using a forked network.
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