基于Uniswap部署自己的去中心化交易所

  • 所需代码 合约: ​1.工厂合约 2.路由合约 3.weth合约 4.multical合约 知识点: 快速找到uniswap合约 weth功能,其他链对应的合约 multicall功能

前端: 1.uniswap前端 2.uniswap-sdk 知识点:从github找到历史提交

  • 操作流程

    • 1.部署工厂合约

交易对的合约由工厂合约动态部署出来

  • 2.部署路由合约 (需要用到工厂合约地址)

  • 3.前端工程中配置路由合约地址,部署前端

  • 准备部署合约的账户地址 部署合约的账户地址需要没交易过,即nonce值为0。目的:多个测试、主网络部署的合约地址一致,web切换以太坊网络路由地址不会变。合约地址由部署地址和nonce值计算得出

  • 部署合约
    从Etherscan复制uniswap线上源代码用自己的账户地址部署

    • uniswap v2 factory contract:

0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f

编译选项: ENVIROMENT选web3 EVM VERSION:istanbbul Enable optimization Auto compile

Deploy按钮旁边 Deploy address _feeToSetter填上面ACCUNT地址,我们暂时不会设置gas fee

deploy的账户地址nonce应为0(未发出过交易),向该账户转入eth做gas deploy deploy sucess后左下角就是部署成功的合约地址:MyUniswap2: 0x614feF16799C4aCd442166f13Fa087b96B54c5fE ​

  • uniswapV2Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D 将部署产生的工厂合约地址填到路由合约:DEPOLOY
    _FACTORY: _WETH: //此处填WETH Contract address

gas limit调大点,该合约比较大 transact 部署 sucess后左下角copy路由合约地址: MyUniswap2:0xC2606927D270554AAb19F79De55348e629a82a8A 该地址在前端工程index.ts中用到

* WETH Contract: 

路由合约构造函数参数需要. WETH合约的地址在主网和测试网的地址都不相同,需要找到每个网络中WETH Contract地址 WETH合约用于将eth交换为erc20的eth,由于eth不是erc20 token,所以我们必须使用WETH作为交换媒介

  * mainnet: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2


  * reposten: 0xb603cEa165119701B58D56d10D2060fBFB3efad8(I find)


  * ropsten:0xc778417E063141139Fce010982780140Aa0cD5Ab


  * rinkeby:0xc778417E063141139Fce010982780140Aa0cD5Ab


  * goerl1:0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6


  * kovan:0xd0A1E359811322d97991E03f863a0C30C2cF029C


  * 


  * WETH9: 


    * rinkeby: 0xDf032Bc4B9dC2782Bb09352007D4C57B75160B15
  • 将前端与自己部署的合约链接

    • 运行前端网页:

yarn ​yarn start

  • 修改路由地址 修改路由地址让前端链接到我自己的路由合约中 修改文件:uniswap-interface/src/constants/index.ts 第6行ROUTER_ADDRESS

  • Deploy前端界面到公网

    • 将前端代码添加到github仓库

cd uniswap-interface rm -rf .git git init git remote add origin https://github.com/用户名/项目名.git (github上自己创建的项目地址)

  • 安装并并部署gh-pages yarn add gh-pages //用gh-pages将前端代码部署 到github yarn build

修改./package.json
1.“homepage”地址为github项目地址: https://github.com/bagguo/MySwap https://github.com/bagguo/MyUniswap2 2.script模块添加 "scripts": { "deploy": "gh-pages -d build" } yarn build//编译 yarn deploy //部署 gh-pages分支到公网 https://bagguo.github.io/MySwap/index.html 部署完成要过段时间才能访问

  • 部署token BaGGuo Token: creator: 0xB1aCccFe72279bC7b289A55eac84B4C339b62BEb (MetaMask钱包Account1账户) Address: Rinkeby: 0x9286D1c9DEf2F5241e0D32451C35A6355604d150

    • erc20 part1

编译后,部署时填代币名称、简写、初始供应量 ​pragma solidity ^0.4.16; contract TokenERC20 { string public name; string public symbol; uint8 public decimals = 18; // 18 是建议的默认值 uint256 public totalSupply; mapping (address => uint256) public balanceOf; // mapping (address => mapping (address => uint256)) public allowance; event Transfer(address indexed from, address indexed to, uint256 value); event Burn(address indexed from, uint256 value); function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public { totalSupply = initialSupply * 10 ** uint256(decimals); balanceOf[msg.sender] = totalSupply; name = tokenName; symbol = tokenSymbol; } function _transfer(address _from, address _to, uint _value) internal { require(_to != 0x0); require(balanceOf[_from] >= _value); require(balanceOf[_to] + _value > balanceOf[_to]); uint previousBalances = balanceOf[_from] + balanceOf[_to]; balanceOf[_from] -= _value; balanceOf[_to] += _value; Transfer(_from, _to, _value); assert(balanceOf[_from] + balanceOf[_to] == previousBalances); } function transfer(address _to, uint256 _value) public returns (bool) { _transfer(msg.sender, _to, _value); return true; } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(_value <= allowance[_from][msg.sender]); // Check allowance allowance[_from][msg.sender] -= _value; _transfer(_from, _to, _value); return true; } function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; return true; } -----其余见erc20 part2

  • erc20 part2 function burn(uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -= _value; totalSupply -= _value; Burn(msg.sender, _value); return true; } function burnFrom(address _from, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value); require(_value <= allowance[_from][msg.sender]); balanceOf[_from] -= _value; allowance[_from][msg.sender] -= _value; totalSupply -= _value; Burn(_from, _value); return true; } }