「Build on Darwinia 2–3」Deploying Uniswap V2 to Crab/Pangolin Network — I

Published in
5 min readJan 25, 2022


Crab Smart Chain is a smart contract platform in the Polkadot ecosystem that provides a seamless Ethereum-like environment. This compatibility is designed to make it easy to re-deploy existing Ethereum applications to Crab (and thus, Polkadot) without significantly altering the codebase.

As an example of what an integration looks like, we have created a walkthrough guide on how to deploy applications to Crab/Pangolin. This tutorial aims to hackathon participants who are getting set up to build something from scratch quickly. In this guide, we will demonstrate how to port UNISWAP, the largest DEX on Ethereum, to Crab/Pangolin. The guide will be devided into two articles. In this article, we will guide you through the deployment of the smart contracts of Uniswap V2 Core to the Pangolin TestNet. We will also explain the changes needed to have a fully-functional Uniswap V2 DEX with basic functionality on Crab/Pangolin. In the next article, we will explain the integration of the interface and the SDK. As for how to get test tokens(PRING), refer to this tutorial.

This article covers the process of deploying all the necessary Uniswap V2 contracts to Pangolin TestNet. The steps are similar for the deployment on Crab Smart Chain.

We have prepared a github repo for the demo. The uniswap/smart-contracts folder includes Hardhat setup, which contains all the necessary contract files and a deployment script for the following contracts (essentials to run Uniswap):

  • WETH: ether wrapped in an ERC20 token interface (in the interface it will be called wrapped DEV or WDEV)
  • UniswapV2Factory: only input required is an address that can activate a protocol-level fee (input needed but not used in this example)
  • UniswapV2Router02: requires the address of both the WETH and UniswapV2Factory contracts
  • Multicall: aggregates results from multiple contract constant function calls, reducing the number of separate JSON RPC requests that need to be sent. This is required by the Uniswap interface

Getting started is just as easy as cloning the repo, installing the dependencies, and deploying the contracts with Hardhat. So first, let’s start cloning the repo and installing the dependencies:

git clone <https://github.com/darwinia-network/dvm-workshop>
cd dvm-workshop/uniswap/smart-contracts

In the folder uniswap/smart-contracts, you will find a “hardhat-config.js” file that contains two pre-configured networks: the Crab Smart Chain and the Pangolin TestNet. Here, you need to replace privateKey with your own to deploy the contracts in Pangolin. It is of utter importance that you keep your private key safe and never commit it the repository, so a better practice to set your private key is by setting an environmental variable.

export PRIVATE_KEY=.....

Now you can compile the smart contracts using the following command.

yarn build

Now that there is no error, we can continue to deploy the contracts to the Pangolin TestNet. The command is as follows

yarn deploy:pangolin

You can check the result on the blockchain explorer Subscan.

Using only a simple Hardhat + Ethers.js script, we’ve deployed all the necessary Uniswap V2 contracts to Pangolin TestNet without modifying the code.

However, a new approach is taken to calculate the address of a pool (created by the opcode) by providing only the addresses of the two ERC20 tokens. The address can be calculated with the following formula:

Where the salt is the same as the one shown in the UniswapV2Factory code (related to both token addresses). The init_code hash is the last bit of the equation (keccak256(UniswapV2Pair.bytecode)), that is the keccak256 hash of the bytecode of the UniswapV2Pair contract. If the wrong init_code is provided, the smart contracts (and interface) won’t be able to calculate the correct address of the pool. The advantage of this approach is that the address of a pool can be calculated in advance without any external calls.

The init_code can be obtained in different ways. Here we provide one. After successful compiling, you can find a file artifacts/contracts/core/UniswapV2Pair.sol/UniswapV2Pair.json in which you can find the byte code.

Double click to copy and paste it into an online hash tool, remove the prefix 0x, set Input type to Hex, and then you get the hash.

Then paste the hash after hex in the file contracts/periphery/libraries/UniswapV2Library.sol.

// calculates the CREATE2 address for a pair without making any external calls
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
keccak256(abi.encodePacked(token0, token1)),
hex'2fe714b2a8ae1bc2f8a4181f48e8d9cb072c8ec7193ad627a77f095d2d406c02' // init code hash

Now you will be able to run the test in which we will perform some swap operations among two test tokens.

How to Get Tokens

To deploy on Pangolin TestNet or Crab Smart Chain you need to have some tokens in the account that performs the deployment. You can get some tokens for Pangolin(PRING) from a faucet, a service that distributes test token for free. These tokens have no real value and can only be used for development and test. While CRAB, the native token on Crab Smart Chain, has real value, which users need to purchase from an exchange. As we are holding a hackathon to attract developers to Crab Smart Chain, we lauched an airdrop program in which users can apply for some CRAB for fee. For how to apply, refer here.

Series Articles:

「Build on Darwinia 2–1」Address Formats in Darwinia

「Build on Darwinia 2–2」EVM-Compatible Crab Smart Chain


Developer Documentation: https://docs.crab.network/builders/get-started/darwinia-dev

Developer Telegraph Group: https://t.me/DarwiniaDev

About Crab Network

Crab is a canary network with real economic value for Darwinia, and its positioning is similar to Polkadot’s Kusama Network. You can check Crab status through Polkadot{.js} , Subscan or Subview.

Crab provides smart contract solutions based on DVM (Darwinia Virtual Machine), which is compatible with the EVM (Ethereum Virtual Machine) paradigm at a low level. Therefore, it is easy for projects in the Ethereum ecosystem to migrate to the Crab Smart Chain. (Tips: DVM is built on Frontier with a fully EVM-compatible instruction set and an Ethereum RPC-like external interface.)




As an open cross-chain bridge protocol based on Substrate, Darwinia focuses on the construction of future Internet of Tokens. TG: http://t.me/DarwiniaNetwork