forked from paco0x/amm-arbitrageur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapTest.ts
77 lines (62 loc) · 3.01 KB
/
SwapTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Contract } from '@ethersproject/contracts';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { ethers, waffle } from 'hardhat';
import { FlashBot } from '../typechain/FlashBot';
import { IWETH } from '../typechain/IWETH';
describe('Flashswap', () => {
let weth: IWETH;
let flashBot: FlashBot;
const WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const USDT = '0x55d398326f99059ff775485246999027b3197955';
beforeEach(async () => {
const wethFactory = (await ethers.getContractAt('IWETH', WBNB)) as IWETH;
weth = wethFactory.attach(WBNB) as IWETH;
const fbFactory = await ethers.getContractFactory('FlashBot');
flashBot = (await fbFactory.deploy(WBNB)) as FlashBot;
});
describe('flash swap arbitrage', () => {
let signer: SignerWithAddress;
const uniFactoryAbi = ['function getPair(address, address) view returns (address pair)'];
const uniPairAbi = ['function sync()'];
const mdexFactoryAddr = '0x3CD1C46068dAEa5Ebb0d3f55F6915B10648062B8';
const mdexFactory = new ethers.Contract(mdexFactoryAddr, uniFactoryAbi, waffle.provider);
let mdexPairAddr: any;
let mdexPair: Contract;
const pancakeFactoryAddr = '0xBCfCcbde45cE874adCB698cC183deBcF17952812';
const pancakeFactory = new ethers.Contract(pancakeFactoryAddr, uniFactoryAbi, waffle.provider);
let pancakePairAddr: any;
before(async () => {
[signer] = await ethers.getSigners();
mdexPairAddr = await mdexFactory.getPair(WBNB, USDT);
mdexPair = new ethers.Contract(mdexPairAddr, uniPairAbi, waffle.provider);
pancakePairAddr = await pancakeFactory.getPair(WBNB, USDT);
});
it('do flash swap between Pancake and MDEX', async () => {
// transfer 100000 to mdex pair
const amountEth = ethers.utils.parseEther('100000');
await weth.deposit({ value: amountEth });
await weth.transfer(mdexPairAddr, amountEth);
await mdexPair.connect(signer).sync();
const balanceBefore = await ethers.provider.getBalance(flashBot.address);
await flashBot.flashArbitrage(mdexPairAddr, pancakePairAddr);
const balanceAfter = await ethers.provider.getBalance(flashBot.address);
expect(balanceAfter).to.be.gt(balanceBefore);
});
it('calculate how much profit we get', async () => {
// transfer 100000 to mdex pair
const amountEth = ethers.utils.parseEther('100000');
await weth.deposit({ value: amountEth });
await weth.transfer(mdexPairAddr, amountEth);
await mdexPair.connect(signer).sync();
const res = await flashBot.getProfit(mdexPairAddr, pancakePairAddr);
expect(res.profit).to.be.gt(ethers.utils.parseEther('500'));
expect(res.baseToken).to.be.eq(WBNB);
});
it('revert if callback is called from address without permission', async () => {
await expect(
flashBot.uniswapV2Call(flashBot.address, ethers.utils.parseEther('1000'), 0, '0xabcd')
).to.be.revertedWith('Non permissioned address call');
});
});
});