Constructing uniswap environment with truss

Pre environment preparation

  1. Install nodejs, Download the installation package directly for installation
  2. Install trufflenpm install truffle -g
  3. Install yarn, a package management tool released by facebook to replace npm, npm install -g yarn
  4. Install MetaMask plug-in in Google browser


  1. uniswp documentation
  2. uniswap github
  3. truffle official website trufle Chinese document
  4. infura

Operation steps

1. Download source code

Uniswap open source all contract codes on Github, including core contract and peripheral contract. Uniswap also open source front-end code, which is developed by React

Because different versions are difficult to compile, I choose to integrate one version of all contracts and have core comments, which is very nice, Click here to arrive

After downloading, execute truss init in the current directory to generate a truss scaffold for later development and deployment

directory structure

    └───Migrations.sol truffle	Scaffolding contract
    └───online.sol 				Routing contract
    └───UniswapV2Factory.sol 	Factory contract
    └───UniswapV2Pair.sol 		Pairing contract
    └───WETH9.sol				weth contract

2. Modify the truss configuration file

The prerequisite is to install the toll hdwallet provider NPM install toll hdwallet provider

2.1 truffle-config.js

Add ropsten node

var HDWalletProvider = require("truffle-hdwallet-provider");
// Mnemonic words
var mnemonic = "***";

module.exports = {

  networks: {
    // Local development environment
    development: {
     host: "",     // Localhost (default: none)
     port: 7545,            // Standard Ethereum port (default: none)
     network_id: "1",       // Any network (default: none)
    // ropsten node environment,
    ropsten: {
      provider: function() {
        // Define Ethereum nodes
         return new HDWalletProvider(mnemonic, "");
      network_id: 3,       // Any network (default: none)

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.5.16",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }

2.2 add js files in the migrations directory

In the migrations directory, there are JS files starting with numbers. When deploying the truss, the number is executed from small to large. The original one starting with 1 cannot be deleted. Add 2_deploy.js

3. Deploy WETH contract

Because Ethereum's decentralized platform needs to use smart contracts to complete direct transactions between platform users, it is necessary to have a unified system format for the tokens to be traded. Only in this way can we avoid the loss of platform tokens in the process of users' free transactions. WETH is an Ethereum token conforming to ERC-20 standard, It can be exchanged equivalently with the native token ether (ETH) of Ethereum network,

Edit 2_deploy.js

const WETH9 = artifacts.require("WETH9");

module.exports = (deployer, network, accounts) => {

Compile first

truffle compile

truffle migrate -f 2 --network ropsten

Return to contract address

   Deploying 'WETH9'
   > transaction hash:    0x5c539dbd7d4a2d377fb5d644a2cc55302701559ae50fb511cb1e3a7eea8c37d2
   > Blocks: 3            Seconds: 57
   > contract address:    0x5379A5C10bC4df2B4A2f78379b1C05e97d490f00
   > block number:        10948908
   > block timestamp:     1630507517
   > account:             0xf1D1De877d9fDB4137a3F09Be569798408d86Ed8
   > balance:             4.992961117491073413
   > gas used:            826802 (0xc9db2)
   > gas price:           4.999999999 gwei
   > value sent:          0 ETH
   > total cost:          0.004134009999173198 ETH

4. Deployment plant contract

const UniswapV2Factory = artifacts.require("UniswapV2Factory");

const feeToSetter = 'Set the administrator address of the handling fee account';

const WETH = 'WETH Contract address'

module.exports = (deployer, network, accounts) => {

    //Use solc vresion 0.5.16
    deployer.deploy(UniswapV2Factory, feeToSetter);


5. Deploy routing contract

const UniswapV2Router01 = artifacts.require("UniswapV2Router01");

const feeToSetter = 'Set the administrator address of the handling fee account';

const WETH = 'WETH Contract address'

module.exports = (deployer, network, accounts) => {

    deployer.deploy(UniswapV2Router01,'Factory contract address',WETH);


Tags: Blockchain

Posted on Mon, 06 Sep 2021 14:22:53 -0400 by b2k