truffle搭建uniswap环境

前置环境准备

  1. 安装nodejs,直接下载安装包进行安装
  2. 安装trufflenpm install truffle -g
  3. 安装yarn,是facebook发布的一款取代npm的包管理工具 npm install -g yarn
  4. 谷歌浏览器安装MetaMask插件

传送门

  1. uniswp 文档
  2. uniswap github
  3. truffle官网 & trufle中文文档
  4. infura

操作步骤

1、下载源码

Uniswap在Github上面开源了全部合约代码,其中包括核心合约,周边合约两部分.Uniswap还开源了前端代码,前端代码使用React开发.

因版本不同比较难编译,所以我选择整合所有合约的一个版本,并且拥有核心注释,非常nice,点此到达

下载后在当前目录下执行truffle init,生成truffle脚手架,方便后面开发以及部署

目录结构

uniswap
└───contracts
    └───Migrations.sol truffle	脚手架合约
    └───online.sol 				路由合约
    └───UniswapV2Factory.sol 	工厂合约
    └───UniswapV2Pair.sol 		配对合约
    └───WETH9.sol				weth合约
└───migrations
    └───1_initial_migration.js
└───test
└───truffle-config.js

2、修改truffle配置文件

前提先安装truffle-hdwallet-provider npm install truffle-hdwallet-provider

2.1 truffle-config.js

添加ropsten节点

// 
var HDWalletProvider = require("truffle-hdwallet-provider");
// 助记词
var mnemonic = "***";

module.exports = {

  networks: {
    // 本地开发环境
    development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 7545,            // Standard Ethereum port (default: none)
     network_id: "1",       // Any network (default: none)
    },
    // ropsten节点环境,
    ropsten: {
      provider: function() {
        // 定义以太坊节点 https://ropsten.infura.io/your-api-key
         return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/ca18135430664f1aafde393f9a6aa8b1");
        },
      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 在migrations目录下添加js文件

migrations目录下是以数字开头的js文件,truffle部署的时候按照数字从小到大依次执行,原先1开头的不能删除,添加2_deploy.js

3、部署WETH合约

由于以太坊的去中心化平台需要利用智能合约才能完成平台用户之间的直接交易,所以就需要交易的代币有一个统一的系统格式,只有这样才能避免平台代币在用户自由交易的过程中丢失,而WETH是一种符合ERC-20标准以太坊代币,与以太坊网络的原生代币以太币(ETH)可以进行等价互换,

编辑2_deploy.js

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

module.exports = (deployer, network, accounts) => {
    deployer.deploy(WETH9);
}

首先编译

truffle compile

truffle migrate -f 2 --network ropsten

返回合约地址

   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、部署工厂合约

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

const feeToSetter = '设置手续费账户的管理员地址';

const WETH = 'WETH合约地址'

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

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

}

5、部署路由合约

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

const feeToSetter = '设置手续费账户的管理员地址';

const WETH = 'WETH合约地址'

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

    deployer.deploy(UniswapV2Router01,'工厂合约地址',WETH);

}

6、部署前端

下载前端代码,修改第六行为自己的路由合约

import { AbstractConnector } from '@web3-react/abstract-connector'
import { ChainId, JSBI, Percent, Token, WETH } from '@uniswap/sdk'

import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors'

export const ROUTER_ADDRESS = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D' 

下载依赖包和启动

yarn
yarn start
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值