-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.js
216 lines (197 loc) · 8.12 KB
/
test.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require('dotenv').config();
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.WebsocketProvider(process.env.INFURA_URL)
);
const interfaces = require('./abis');
const { mainnet: addresses } = require('./addresses');
const { address: admin } = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
const BigNumber = require('bignumber.js');
// use @sushiswap/sdk@3.0.0
const {
ChainId: sChainId,
Token: sToken,
WETH: sWETH,
Fetcher: sFetcher,
Trade: sTrade,
Route: sRoute,
TokenAmount: sTokenAmount,
TradeType: sTradeType } = require('@sushiswap/sdk');
// use @uniswap/sdk@3.0.0
const {
ChainId: uChainId,
Token: uToken,
WETH: uWETH,
Fetcher: uFetcher,
Trade: uTrade,
Route: uRoute,
TokenAmount: uTokenAmount,
TradeType: uTradeType } = require('@uniswap/sdk');
/* use sdk instead of calling of each contract, just preserved
const sushiswapRouter = new web3.eth.Contract(
interfaces.sushiswap.sushiswapRouter,
addresses.sushiswap.sushiswapRouter
);
const sushiswapFactory = new web3.eth.Contract(
interfaces.sushiswap.sushiswapFactory,
addresses.sushiswap.sushiswapFactory
);
const uniswapRouter = new web3.eth.Contract(
interfaces.uniswap.uniswapRouter,
addresses.uniswap.uniswapRouter
);
const uniswapFactory = new web3.eth.Contract(
interfaces.uniswap.uniswapFactory,
addresses.uniswap.uniswapFactory
); */
// to provide initial token amount
const amount = process.env.TRADE_AMOUNT;
const init = async () => {
const networkId = await web3.eth.net.getId();
// leverage Fetcher by the SDK
// https://uniswap.org/docs/v2/javascript-SDK/fetching-data/
const [sDai, sWeth, sUsdt, sUsdc, sWbtc, sComp, sLink, sRari, sSnx, sYfi] = await Promise.all(
[
addresses.tokens.dai,
addresses.tokens.weth,
addresses.tokens.usdt,
addresses.tokens.usdc,
addresses.tokens.wbtc,
addresses.tokens.comp,
addresses.tokens.link,
addresses.tokens.rari,
addresses.tokens.snx,
addresses.tokens.yfi
]
.map(tokenAddress => (
sFetcher.fetchTokenData(
sChainId.MAINNET,
tokenAddress,
)
))
);
// same Fetcher by the SDK
// https://uniswap.org/docs/v2/javascript-SDK/fetching-data/
const [uDai, uWeth, uUsdt, uUsdc, uWbtc, uComp, uLink, uRari, uSnx, uYfi] = await Promise.all(
[
addresses.tokens.dai,
addresses.tokens.weth,
addresses.tokens.usdt,
addresses.tokens.usdc,
addresses.tokens.wbtc,
addresses.tokens.comp,
addresses.tokens.link,
addresses.tokens.rari,
addresses.tokens.snx,
addresses.tokens.yfi
]
.map(tokenAddress => (
uFetcher.fetchTokenData(
uChainId.MAINNET,
tokenAddress,
)
))
);
const tokenPairs = [
['WETH', 'DAI', 'LINK']
];
const tokenFetchers = [
[uWeth, uDai, sDai, sWeth, sLink, uLink, uWeth]
];
let subscription = web3.eth.subscribe('newBlockHeaders', (error, result) => {
if (!error) {
// console.log(result);
return;
}
console.error(error);
})
.on("connected", subscriptionId => {
console.log(`You are connected on ${subscriptionId}`);
})
.on('data', async block => {
console.log('-------------------------------------------------------------');
console.log(`New block received. Block # ${block.number}`);
console.log(`GasLimit: ${block.gasLimit} and Timestamp: ${block.timestamp}`);
for (let i = 0; i < tokenPairs.length; i++) {
console.log(`Investigation path: ${tokenPairs[i][0]} -> ${tokenPairs[i][1]} -> ${tokenPairs[i][2]}`);
// 0. Prepare a trading amout from environmental variable
const unit0 = await new BigNumber(amount).toString(); // BigNumber
const amount0 = await BigInt(new BigNumber(unit0).shiftedBy(tokenFetchers[i][0].decimals).toString());
// 1. Sell A for B at Uniswap
const uAtoB = await uFetcher.fetchPairData(
tokenFetchers[i][1],
tokenFetchers[i][0]
);
const route1 = await new uRoute([uAtoB], tokenFetchers[i][0]);
const trade1 = await new uTrade(
route1,
new uTokenAmount(tokenFetchers[i][0], amount0),
uTradeType.EXACT_INPUT
);
const rate1 = await new BigNumber(trade1.executionPrice.toSignificant(6)).toString();
console.log(`Putting ${unit0} ${tokenPairs[i][0]} into Uniswap pool`);
console.log(`Rate ${tokenPairs[i][0]}/${tokenPairs[i][1]}: ${rate1}`);
console.log(`Sell ${tokenPairs[i][0]} for ${tokenPairs[i][1]} at Uniswap: ${rate1 * unit0}`);
// 2. Sell B for C at Sushiswap
const unit1 = await new BigNumber(rate1).times(unit0).toString(); // BigNumber
const amount1 = await BigInt(new BigNumber(rate1).times(unit1).shiftedBy(tokenFetchers[i][2].decimals).toFixed()); // BigInt
// indirect path from B to C, refer to the pricing chapter
// https://uniswap.org/docs/v2/javascript-SDK/pricing/
const sBtoWeth = await sFetcher.fetchPairData(
tokenFetchers[i][3],
tokenFetchers[i][2]
);
const sWethtoC = await sFetcher.fetchPairData(
tokenFetchers[i][4],
tokenFetchers[i][3]
);
const route2 = await new sRoute([sBtoWeth, sWethtoC], tokenFetchers[i][2]);
const trade2 = await new sTrade(
route2,
new sTokenAmount(tokenFetchers[i][2], amount1),
sTradeType.EXACT_INPUT
);
const rate2 = await new BigNumber(trade2.executionPrice.toSignificant(6)).toString();
console.log(`Putting ${unit1} ${tokenPairs[i][1]} into Sushiswap pool`);
console.log(`Rate ${tokenPairs[i][1]}/${tokenPairs[i][2]}: ${rate2}`);
console.log(`Sell ${tokenPairs[i][1]} for ${tokenPairs[i][2]} at Sushiswap: ${rate2 * unit1}`);
// 3. Sell C for A at Uniswap
const unit2 = await new BigNumber(rate2).times(unit1).toString(); // BigNumber
const amount2 = await BigInt(new BigNumber(rate2).times(unit1).shiftedBy(tokenFetchers[i][4].decimals).toFixed()); // BigInt
const uCtoA = await uFetcher.fetchPairData(
tokenFetchers[i][6],
tokenFetchers[i][5]
);
const route3 = await new uRoute([uCtoA], tokenFetchers[i][5]);
const trade3 = await new uTrade(
route3,
new uTokenAmount(tokenFetchers[i][5], amount2),
uTradeType.EXACT_INPUT
);
const rate3 = await new BigNumber(trade3.executionPrice.toSignificant(6)).toString();
console.log(`Putting ${unit2} ${tokenPairs[i][2]} into Uniswap pool`);
console.log(`Rate ${tokenPairs[i][2]} for ${tokenPairs[i][0]}: ${rate3}`);
console.log(`Sell ${tokenPairs[i][2]} for ${tokenPairs[i][0]} at Uniswap: ${rate3 * unit2}`);
const unit4 = await new BigNumber(rate3).times(unit2).toString(); // BigNumber
let profit = await new BigNumber(unit4).minus(unit0).toString();
console.log(`Initial supply from a flashloan: ${unit0}`);
console.log(`Obtained amount after an arbitrage: ${unit4}`);
console.log(`Profit: ${profit}`);
if (profit > 0) {
console.log(`
Block # ${block.number}: Arbitrage opportunity found!
Expected profit: ${profit}
`);
} else {
console.log(`
Block # ${block.number}: Arbitrage opportunity not found.
Expected profit: ${profit}
`);
}
}
})
.on('error', error => {
console.log(error);
});
}
init();