More Decentralized, More Valueable
作为一个区块链工程师,不熟悉uniswap机制而且不懂如何接入uniswap明显是不及格的。所以,就总结一个一下怎么接入uniswap。
环境
nvm install v12.22.1 (nvm ls-remote)
npm i -g truffle@5.3.4
nodejs和npm的安装自行google了。
创建工程
因为uniswap官方封装了一个uniswap的truffle box使用模版,我强烈建议使用truffle box进行快速创建项目。
mkdir uniswapDemo
cd uniswapDemo
npx truffle init
Set up npm
接下来根据官方的默认配置,初始化npm的环境。
npm init
添加uniswap 依赖
初始化了npm环境之后,我们就可以添加@uniswap/v2-core和@uniswap/v2-periphery这两个依赖了
npm i --save @uniswap/v2-core
npm i --save @uniswap/v2-periphery
添加了依赖之后,我们可以从@uniswap/contracts上看到contracts可倒入的依赖:
MacBook-Pro-2:contracts csx$ ll
total 80
-rw-r--r-- 1 csx staff 2213 4 30 2020 UniswapV2Migrator.sol
-rw-r--r-- 1 csx staff 12704 6 4 2020 UniswapV2Router01.sol
-rw-r--r-- 1 csx staff 18466 6 6 2020 UniswapV2Router02.sol
drwxr-xr-x 7 csx staff 224 4 30 00:24 examples
drwxr-xr-x 8 csx staff 256 4 30 00:24 interfaces
drwxr-xr-x 5 csx staff 160 4 30 00:24 libraries
drwxr-xr-x 6 csx staff 192 4 30 00:24 test
实现合约
我们先实现一个简单的功能,获取uniswap上的流动池。在目录下创建interfaces的接口和实现文件,如下图:
从构造函数开始实现,首先我们需要知道UniswapV2Factory这个合约的地址(mainnet和testnets),这个地址是不变的(除非uniswap出事情了)。为了方便起见,最好是建立一个常量存储起来。合约实现:
pragma solidity ^0.6.6;
import './interfaces/ILiquidityValueCalculator.sol';
contract LiquidityValueCalculator is ILiquidityValueCalculator {
address public factory;
constructor(address factory_) public {
factory = factory_;
}
}
第二步我们要开始查询流动性,具体分为四步:
1. Look up the pair address // 找到pari address
2. Get the reserves of the pair // 请求获取pair的存量
3. Get the total supply of the pair liquidity // 获得par liquidity的流动性
4. Sort the reserves in the order of tokenA, tokenB
方便的是,UniswapV2Library和IUniswapV2Pair都提供了这些方法:
pragma solidity ^0.6.6;
import './interfaces/ILiquidityValueCalculator.sol';
import '@uniswap/v2-periphery/contracts/libraries/UniswapV2Library.sol';
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
contract LiquidityValueCalculator is ILiquidityValueCalculator {
function pairInfo(address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB, uint totalSupply) {
IUniswapV2Pair pair = IUniswapV2Pair(UniswapV2Library.pairFor(factory, tokenA, tokenB));
totalSupply = pair.totalSupply();
(uint reserves0, uint reserves1,) = pair.getReserves();
(reserveA, reserveB) = tokenA == pair.token0() ? (reserves0, reserves1) : (reserves1, reserves0);
}
}
最后,我们只需要计算sharevalue就可以完成了。
测试网测试
从官网上的提示,UniswapV2Factory
is deployed at 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
on the Ethereum mainnet, and the Ropsten, Rinkeby, Görli, and Kovan testnets. It was built from commit 8160750.
使用truffle compile进行文件的编译,
truffle compile
MacBook-Pro-2:uniswapDemo csx$ truffle compile
Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/LiquidityValueCalculator.sol
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/interfaces/ILiquidityValueCalculator.sol
> Compiling @uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol
> Compiling @uniswap/v2-periphery/contracts/libraries/SafeMath.sol
> Compiling @uniswap/v2-periphery/contracts/libraries/UniswapV2Library.sol
> Compilation warnings encountered:
> Artifacts written to /Users/csx/GitProject/uniswapDemo/build/contracts
> Compiled successfully using:
- solc: 0.6.6+commit.6c089d02.Emscripten.clang
使用truffle deploy进行文件的编译后,进行系统的启动/部署。
本地测试:
truffle develop
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0x334cd17818b0ca116edab67d61d457d66bb0c13b
(1) 0xb5abf602a53c72a335951142d36bb0bde2e451b0
(2) 0x2e38ab86c8521676adee942bf9fa7c1b49301844
(3) 0x48ca8188fe7ab253f8e8c6ccfe5b433350ce965f
(4) 0xa72d6e5bc2af5c75b057f5eece4ba47e6b02428d
(5) 0x3c9e87eaaf4f9bfb0f01d12bca91d9257a14786a
(6) 0xec8c42fe424983febf63298905033fecb471465e
(7) 0xdf733974c757f1c1f982b80e03ec71c2c92f49a3
(8) 0xd1db9c1c0cbfddda33ca8705dc4118e76b1128a7
(9) 0x83aa8cfb08b2e460a374cf00cce7078358200d56
Private Keys:
(0) e2610377b94b15b6341babf024717e5998b3cfe9cd413ab12147ab4ceb1aa382
(1) 5b549ebbaeee277778f7cdee2215695236cbb1293fc1664184e6caa50752864a
(2) c248a078051a1940d87bf43a072978cc7702c92971952997b969d038fdb71a2c
(3) e3a4aa50f7aea2010a669c482ab1e21929078fd8144830fb4d132f5129090e9b
(4) 4e71ce5086cff93ccea21811e9a451e807def810ea61a2bf713d0689206b4662
(5) ec21659312bc6a7475290827fb7002a1bac3741c034f2c9e54eb7cd277d05582
(6) e6d11d1ad4e84d20f51ef15813cbc4157f01eca0de95175daf81f6d327fa9766
(7) 0d6770a995a90c73d5b0ec4fcc66f26cb0d9d2711cd3830a182d7bae02a07fe3
(8) c015e80ace0ef270c6ec2150e09846bb0beabbfb9c61901017bb96dcf7abc3b5
(9) 75d058974721df6dd20e23fd0a7e0a879185818a4d38597e3f64e7c3c0804b8e
Mnemonic: matrix slow what virus mirror desk trade impact fatigue blood thought neutral
⚠️ Important ⚠️ : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds
部署测试网测试,这里需要改truffle-config.js,取消注释ropsten的内容之后:
需要做以下几个事情:
Please check that your Ethereum client:
- is running
- is accepting RPC connections (i.e., "--rpc" option is used in geth)
- is accessible over the network
- is properly configured in your Truffle configuration file (truffle-config.js)
truffle migrate
部署完成(效果截图,未完待续)