Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Add swap test
Browse files Browse the repository at this point in the history
  • Loading branch information
raykyri committed Jun 3, 2021
1 parent a71985f commit c1aa991
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 21 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"license": "ISC",
"dependencies": {
"@edgeware/node-types": "^3.1.0-beta.3",
"@ethersproject/contracts": "^5.3.0",
"@ethersproject/providers": "^5.3.0",
"@ethersproject/solidity": "^5.3.0",
"@openzeppelin/contracts": "^2.5.0",
"@polkadot/api": "2.10.2-11",
"@polkadot/keyring": "^4.2.2-11",
Expand All @@ -24,6 +27,7 @@
"@truffle/hdwallet-provider": "^1.2.0",
"@types/chai": "^4.2.12",
"@types/mocha": "^8.0.3",
"@uniswap/sdk": "^3.0.3",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"bn.js": "^5.1.3",
Expand Down
1 change: 1 addition & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const account = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
const privKey = '1111111111111111111111111111111111111111111111111111111111111111';

const initWeb3 = (pkey = privKey) => {
console.log('Initializing web3 provider at http://localhost:9933/');
// const provider = new Web3.providers.HttpProvider('http://localhost:9933/');
const provider = new HDWalletProvider({
privateKeys: [ pkey ],
Expand Down
52 changes: 35 additions & 17 deletions web3tests/addLiquidity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

const contract = require("@truffle/contract");
const { assert } = require("chai");
const { account, initWeb3 } = require('../utils');
const { deploy } = require('../deploy');

const TokenA = require('../build/contracts/TokenA.json');
const TokenB = require('../build/contracts/TokenB.json');

const UniswapV2Router02 = require('../node_modules/@uniswap/v2-periphery/build/UniswapV2Router02.json');
const UniswapV2Factory = require('../node_modules/@uniswap/v2-core/build/UniswapV2Factory.json');
const UniswapV2Pair = require('../node_modules/@uniswap/v2-core/build/UniswapV2Pair.json');
Expand All @@ -18,12 +20,12 @@ describe('Add Liquidity Test', () => {
[FACTORY_ADDRESS, ROUTER_ADDRESS] = d;
});

it('should create uniswap pair', async () => {
it('should create uniswap pair', async () => {
// deploy two tokens
const web3 = initWeb3();
const amount0 = web3.utils.toWei('10');
const amount1 = web3.utils.toWei('10');

console.log('Deploying first token...');
const TokenAContract = contract({
abi: TokenA.abi,
Expand All @@ -50,7 +52,7 @@ describe('Add Liquidity Test', () => {
const receipt1 = await token1.approve(ROUTER_ADDRESS, amount1, {
from: account
});

// create the pair
const RouterContract = contract({
abi: UniswapV2Router02.abi,
Expand All @@ -64,11 +66,12 @@ describe('Add Liquidity Test', () => {
"0", "0",
account,
Math.ceil(Date.now() / 1000) + (60 * 20), // 1 day
{ from: account, gas: web3.utils.toWei('100') }, // { from: account, gasLimit: 10000000, gasPrice: 1500000000 },
// { from: account, gas: web3.utils.toWei('100') },
{ from: account, gasLimit: 10000000, gasPrice: 1500000000 },
];
console.log('Adding liquidity with args: ', args);
const liquidityReceipt = await router.addLiquidity(...args);

// query the pair
const FactoryContract = contract({
abi: UniswapV2Factory.abi,
Expand All @@ -80,28 +83,43 @@ describe('Add Liquidity Test', () => {
const pairAddress = await factory.getPair.call(address0, address1, {
from: account,
});
console.log(pairAddress);
const nPairs = await factory.allPairsLength.call({ from: account });

// query the pair's reserves
console.log(`Got pair: ${pairAddress} (${nPairs} total pairs).`);
assert.notEqual(+nPairs, 0);
assert.notEqual(+(pairAddress.slice(2)), 0);
console.log(`Got pair: ${pairAddress} (${nPairs} total pairs).`);
const PairContract = contract({
abi: UniswapV2Pair.abi,
unlinked_binary: UniswapV2Pair.bytecode,
});
PairContract.setProvider(web3.currentProvider);
const pair = await PairContract.at(pairAddress);
const result = await pair.getReserves.call({ from: account });
console.log(result);
console.log('result[0].toString()');
console.log(result[0].toString());
console.log(amount0);
console.log('result[1].toString()');
console.log(result[1].toString());
console.log(amount1);
assert.equal(result[0].toString(), amount0);
assert.equal(result[1].toString(), amount1);
assert(result[0].toString() === amount0);
assert(result[1].toString() === amount1);
console.log('Deposited token0 in LP pool:', web3.utils.fromWei(amount0));
console.log('Deposited token1 in LP pool:', web3.utils.fromWei(amount1));

// approve swap
const amountIn = web3.utils.toWei('5');
const amountOutMin = web3.utils.toWei('1');
console.log('Approving token for swap...');
const swapApproveReceipt = await token0.approve(ROUTER_ADDRESS, amountIn, {
from: account
});

// swap
console.log('Swapping', amountIn, 'token0 for at least', amountOutMin, 'token1');
const deadline = (await web3.eth.getBlock('latest')).timestamp + 1000;
const swapReceipt = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[ token0.address, token1.address ],
account,
deadline,
{ from: account, gasLimit: 10000000, gasPrice: 1500000000 },
);
console.log('Done!', swapReceipt);
});
});
Loading

0 comments on commit c1aa991

Please sign in to comment.