More Decentralized, More Valueable
作為一個區(qū)塊鏈工程師,不熟悉uniswap機制而且不懂如何接入uniswap明顯是不及格的醇滥。所以漏设,就總結(jié)一個一下怎么接入uniswap职祷。
環(huán)境
nvm install v12.22.1 (nvm ls-remote)
npm i -g truffle@5.3.4
nodejs和npm的安裝自行g(shù)oogle了曼尊。
創(chuàng)建工程
因為uniswap官方封裝了一個uniswap的truffle box使用模版,我強烈建議使用truffle box進行快速創(chuàng)建項目曹质。
mkdir uniswapDemo
cd uniswapDemo
npx truffle init
Set up npm
接下來根據(jù)官方的默認(rèn)配置婴噩,初始化npm的環(huán)境。
npm init
添加uniswap 依賴
初始化了npm環(huán)境之后羽德,我們就可以添加@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
實現(xiàn)合約
我們先實現(xiàn)一個簡單的功能,獲取uniswap上的流動池宅静。在目錄下創(chuàng)建interfaces的接口和實現(xiàn)文件章蚣,如下圖:
從構(gòu)造函數(shù)開始實現(xiàn),首先我們需要知道UniswapV2Factory這個合約的地址(mainnet和testnets)姨夹,這個地址是不變的(除非uniswap出事情了)纤垂。為了方便起見,最好是建立一個常量存儲起來磷账。合約實現(xiàn):
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就可以完成了吼鱼。
測試網(wǎng)測試
從官網(wǎng)上的提示,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進行文件的編譯后菇肃,進行系統(tǒng)的啟動/部署。
本地測試:
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
部署測試網(wǎng)測試取募,這里需要改truffle-config.js琐谤,取消注釋ropsten的內(nèi)容之后:
需要做以下幾個事情:
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
部署完成(效果截圖,未完待續(xù))