/AvalancheBuildAMMTutorial

build amm like uniswap on avalanche

Primary LanguageTypeScript

avalanche-build-AMM-tutorial

*** build amm like uniswap on avalanche network ***

Demo

Introduction

What is Avalanche?

Avalanche is the fastest smart contract platform in the blockchain, and the fastest transaction completion on the avalanche protocol chain, the largest number of validators, and the security of all PoS protocol activities. This tutorial choose Avalanche's C-Chain test network to deploy AMM smart contract.

What is AMM ?

AMM(Automated Market Maker), market maker play both buyer and seller, they get fee from other people transactions by adding liquidity, compare to centralized exchange, AMM doesn't need toekn buyer,so it can achieve the greatest liquidity and the highest average daily trading volume.

What is Uniswap ?

Uniswap is the top Decentralized exchange deploy on ethereum, the famous project use AMM protocol to achieve commercial success.

Why choose Uniswap V2 AMM protocol deploy on Avalanche ?

Currently most of the Decentralized exchange fork the AMM smart contract of Uniswap V2, for example sushiswap/pancakeswap/quickswap, they have achieved success in commercial applications.
Create AMM is a complicated process, and have very high requirements for solodity code writing, Considering the development cost and practicality, we choose to fork the smart contract of Uniswap V2 to build the decentralized exchange.

What is Remix ?

Remix solidity IDE, we can write and deploy smart contract.


The Components of AMM

  • Router smart contract(the function of swap token and add/remove liqidity)
  • Factory smart contract(the function of create liquidity pair and manage which address to get transaction fees)
  • WETH Token smart contract(mainnet token is used to pay gas rather than swap token, we often need wrap mainnet token fist, beacause of deploying on Avalanche network, we should use WAVAX replace of the WETH)

Deploy AMM smart contract

  1. Go to Chainlist and add Avalanche Testnet to Metamask, Testnet ChainID is 43113.

2.Before we deploy smart contract on Avalanche testnet,we should go to Avalanche faucets get some token for test.

3.Deploy Factory smart contract

  • Open Remix, create Factory.sol, use this code Factory Code

  • Add const 'INIT_CODE_PAIR_HASH' in UniswapV2Factory

contract UniswapV2Factory is IUniswapV2Factory {
    # etc
    
    // Deploy Router.sol need INIT_CODE_PAIR_HASH
    bytes32 public constant INIT_CODE_PAIR_HASH = keccak256(abi.encodePacked(type(UniswapV2Pair).creationCode));
    
    # etc}
  • COMPILER choose solidity version 0.5.16, EVM version choose istanbul, optimization times fill in 999999

  • Metamask choose Avalanche Fuji Testnet, Remix ENVIRONMENT choose Injected Web3, CONTRACT choose UniswapV2Factory deploy

  • DEPLOY _FEETOSETTER fill your address(manage address which get transaction fees)

  • click transact, and confirm in Metamask

  • After deploy success, now we can see the Factory contract in Deployed Contracts

  • Click right and copy depolyed contract address, and then click get 'INIT_CODE_PAIR_HASH', we will use it when deploying router contract.

4.Deploy Router smart contract

  • create Router.sol, use this Router contract code

  • search 'hex' in Router.sol, and change 'init code hash' to Factory contract INIT_CODE_PAIR_HASH(Not included 0x)

  • COMPILER choose Solidity Version 0.6.6, EVM Version choose istanbul, optimization times fill in 999999

  • CONTRACT choose UniswapV2Router02 deploy

  • DEPLOY _FACTORY fill in deployed factory contract address,_WETH fill in Avalanche testnet WAVAX address(you can use Avalanche C-chain testnet Explorer get WAVAX address: 0xd00ae08403B9bbb9124bB305C09058E32C39A48c

  • click deploy and confirm in MetaMask

  • after deploy success. copy your deployed router contract address(it will be used in next step)

5.Deploy multicall contract

  • Remix create multicall.sol, use multicall code

  • COMPILER choose solidity version 0.6.6,EVM Version choose istanbul, optimization times fill in 999999

  • Deploy multicall contract

  • after deploy success. copy your deployed multicall contract address(it will be used in next step)

Now we have built mainly AMM function on avalanche testnet.

Modify avalanche-sdk code

In the next, we will use modified uniswap-sdk and uniswap-interface code,i have modified many place and then you just need change a little

cd avalanche-sdk
npm install
  • [avalanche-sdk\src] choose 'constants.ts' file, fill in your deployed factory contract address and INIT_CODE_PAIR_HASH to 'FACTORY_ADDRESS' and 'INIT_CODE_HASH'

  • avalanche-sdk [avalanche-sdk\src\entities\currency.ts] use 'tAVAX' take replace of the origin token name

  • open package.json change "name" and version, then push as your own npm package
npm login // login your npm account 
npm init // init file
npm publish --access public // push your npm package

ok, push success.

Modify avalanche-interface code

cd avalanche-interface
  • find package.json, search the position like below picture, change to your package name and version

  • use your package name replace of origin avalanche-sdk package name

  • [avalanche-interface\src\constants\index.ts] change router address

  • [avalanche-inteface\src\constants\multicall\index.ts] set your multicall contract address like below picture

npm install
npm start

Now we can look the website like below picture, we deploy the AMM and build the DEX success! you can modifiy the code by your requirements.

Create your ERC20 token

  1. Open Remix, create EIP20.sol and EIP20Interface.sol solidity files.
  • Follow the parameter description below, create your ERC-20 token
Param Description
_INITIALAMOUNT total amount of the token
_TOKENNAME name of the token
_DECIMALUNITS decimals of the token
_TOKENSYMBOL symbol of the token

  1. Deploy EIP20.sol use different TOKENNAME twice and copy their contract address, they will be used to test DEX function.

The video demo of add liquidity and swap token

*** this demo video show the processing of using our create token to add liquidity and swap.*** Demo URL:https://www.bilibili.com/video/BV1cg411j7Hw/