Deploy Uniswap V2¶
Introduction¶
Decentralized exchanges (DEXs) are a cornerstone of the DeFi ecosystem, allowing for permissionless token swaps without intermediaries. Uniswap V2, with its Automated Market Maker (AMM) model, revolutionized DEXs by enabling liquidity provision for any ERC-20 token pair.
This tutorial will guide you through how Uniswap V2 works so you can take advantage of it in your projects deployed to Polkadot Hub. By understanding these contracts, you'll gain hands-on experience with one of the most influential DeFi protocols and understand how it functions across blockchain ecosystems.
Prerequisites¶
Before starting, make sure you have:
- Node.js (v16.0.0 or later) and npm installed
- Basic understanding of Solidity and JavaScript
- Familiarity with
hardhat-polkadot
development environment - Some WND test tokens to cover transaction fees (obtained from the Polkadot faucet)
- Basic understanding of how AMMs and liquidity pools work
Set Up the Project¶
Let's start by cloning the Uniswap V2 project:
-
Clone the Uniswap V2 repository:
-
Install the required dependencies:
-
Update the
hardhat.config.js
file so the paths for the Substrate node and the ETH-RPC adapter match with the paths on your machine. For more info, check the Testing your Contract section in the Hardhat guide -
Create a
.env
file in your project root to store your private keys (you can use as an example theenv.example
file):Ensure to replace
"INSERT_LOCAL_PRIVATE_KEY"
with a private key available in the local environment (you can get them from this file). And"INSERT_AH_PRIVATE_KEY"
with the account's private key you want to use to deploy the contracts. You can get this by exporting the private key from your wallet (e.g., MetaMask).Warning
Keep your private key safe, and never share it with anyone. If it is compromised, your funds can be stolen.
-
Compile the contracts:
If the compilation is successful, you should see the following output:
After running the above command, you should see the compiled contracts in the artifacts-pvm
directory. This directory contains the ABI and bytecode of your contracts.
Understanding Uniswap V2 Architecture¶
Before interacting with the contracts, it's essential to understand the core architecture that powers Uniswap V2. This model forms the basis of nearly every modern DEX implementation and operates under automated market making, token pair liquidity pools, and deterministic pricing principles.
At the heart of Uniswap V2 lies a simple but powerful system composed of two major smart contracts:
- Factory Contract - the factory acts as a registry and creator of new trading pairs. When two ERC-20 tokens are to be traded, the Factory contract is responsible for generating a new Pair contract that will manage that specific token pair’s liquidity pool. It keeps track of all deployed pairs and ensures uniqueness—no duplicate pools can exist for the same token combination
- Pair Contract - each pair contract is a decentralized liquidity pool that holds reserves of two ERC-20 tokens. These contracts implement the core logic of the AMM, maintaining a constant product invariant (x * y = k) to facilitate swaps and price determination. Users can contribute tokens to these pools in return for LP (liquidity provider) tokens, which represent their proportional share of the reserves
This minimal architecture enables Uniswap to be highly modular, trustless, and extensible. By distributing responsibilities across these components, developers, and users can engage with the protocol in a composable and predictable manner, making it an ideal foundation for DEX functionality across ecosystems, including Polkadot Hub.
The project scaffolding is as follows:
uniswap-V2-polkadot
├── bin/
├── contracts/
│ ├── interfaces/
│ │ ├── IERC20.sol
│ │ ├── IUniswapV2Callee.sol
│ │ ├── IUniswapV2ERC20.sol
│ │ ├── IUniswapV2Factory.sol
│ │ └── IUniswapV2Pair.sol
│ ├── libraries/
│ │ ├── Math.sol
│ │ ├── SafeMath.sol
│ │ └── UQ112x112.sol
│ ├── test/
│ │ └── ERC20.sol
│ ├── UniswapV2ERC20.sol
│ ├── UniswapV2Factory.sol
│ └── UniswapV2Pair.sol
├── ignition/
├── scripts/
│ └── deploy.js
├── node_modules/
├── test/
│ ├── shared/
│ │ ├── fixtures.js
│ │ └── utilities.js
│ ├── UniswapV2ERC20.js
│ ├── UniswapV2Factory.js
│ └── UniswapV2Pair.js
├── .env.example
├── .gitignore
├── hardhat.config.js
├── package.json
└── README.md
Test the Contracts¶
You can run the provided test suite to ensure the contracts are working as expected. The tests cover various scenarios, including creating pairs, adding liquidity, and executing swaps.
To test it locally, you can run the following commands:
-
Spawn a local node for testing:
This command will spawn a local Substrate node along with the ETH-RPC adapter. The node will be available at
ws://127.0.0.1:8000
and the ETH-RPC adapter athttp://localhost:8545
. -
In a new terminal, run the tests:
The result should look like this:
Deploy the Contracts¶
After successfully testing the contracts, you can deploy them to the local node or Polkadot Hub. The deployment script is located in the scripts
directory and is named deploy.js
. This script deploys the Factory
and Pair
contracts to the network.
To deploy the contracts, run the following command:
This command deploys the contracts to your local blockchain for development and testing. If you want to deploy to Polkadot Hub, you can use the following command:
The command above deploys to the actual Polkadot TestNet. It requires WND test tokens, persists on the network, and operates under real network conditions.
The deployment script will output the addresses of the deployed contracts. Save these addresses, as you will need them to interact with the contracts. For example, the output should look like this:
Conclusion¶
This tutorial guided you through deploying Uniswap V2 contracts to Polkadot Hub. This implementation brings the powerful AMM architecture to the Polkadot ecosystem, laying the foundation for the decentralized trading of ERC-20 token pairs.
By following this guide, you've gained practical experience with:
- Setting up a Hardhat project for deploying to Polkadot Hub
- Understanding the Uniswap V2 architecture
- Testing Uniswap V2 contracts in a local environment
- Deploying contracts to both local and testnet environments
To build on this foundation, you could extend this project by implementing functionality to create liquidity pools, execute token swaps, and build a user interface for interacting with your deployment.
This knowledge can be leveraged to build more complex DeFi applications or to integrate Uniswap V2 functionality into your existing projects on Polkadot.
| Created: May 29, 2025