-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
361 lines (312 loc) · 10.5 KB
/
bot.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const { WETH } = require("@sushiswap/sdk");
const { ethers } = require("hardhat");
require("dotenv").config();
// const etherss = require("ethers");
const Scalar = require("ffjavascript").Scalar;
const uniswapLib = require("./uniswap-lib");
const FLASHLOAN_CONTRACT = "0xdbBEF6D9e14904b59dc9D652334B31F0aB84Ef6e"; // Deployed Flashloan Contract address on Mainnet
const DAI_ADDRESS = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // Dai address
const FLASHLOAN_ABI = [
{
"inputs": [
{
"internalType": "contract IPoolAddressesProvider",
"name": "_addressProvider",
"type": "address"
},
{
"internalType": "address",
"name": "_uniswapRouter",
"type": "address"
},
{
"internalType": "address",
"name": "_sushiswapRouter",
"type": "address"
},
{
"internalType": "address",
"name": "_weth",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "ADDRESSES_PROVIDER",
"outputs": [
{
"internalType": "contract IPoolAddressesProvider",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "POOL",
"outputs": [
{
"internalType": "contract IPool",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
}
],
"name": "executeFlashloan",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "assets",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "premiums",
"type": "uint256[]"
},
{
"internalType": "address",
"name": "initiator",
"type": "address"
},
{
"internalType": "bytes",
"name": "params",
"type": "bytes"
}
],
"name": "executeOperation",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "sushiswapRouter",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "uniswapRouter",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "weth",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenAddr",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "address",
"name": "receiver",
"type": "address"
}
],
"name": "withdrawERC20",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]; // ABI of the Flashloan Contract
// Initialize provider and signer
const provider = new ethers.providers.JsonRpcProvider(process.env.MAINNET_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
// Initialize Flashloan contract instance
const flashloanContract = new ethers.Contract(FLASHLOAN_CONTRACT, FLASHLOAN_ABI, wallet);
let loanAmount = ethers.utils.parseEther("10000000"); // Initial loan amount
const loanAmountIn = 10000000; //Initial loan amount
const loanFee = 0.0005
// Function to monitor arbitrage opportunities
async function monitorArbitrage() {
while (true) {
const isArbitrageAvailable = await checkArbitrageOpportunities();
if (isArbitrageAvailable) {
console.log("Arbitrage opportunity detected!");
const tx = await flashloanContract.executeFlashloan(loanAmount, DAI_ADDRESS);
await tx.wait();
console.log("Arbitrage trade executed successfully!");
}
await new Promise(resolve => setTimeout(resolve, 30000)); // Check every 30 seconds
}
}
// Placeholder function to check for arbitrage opportunities
async function checkArbitrageOpportunities() {
const priceOfUniswap = await getPriceFromUniswap();
const priceOfSushiswap = await getPriceFromSushiswap();
const {
gasPrice, maxFeePerGas, maxPriorityFeePerGas
} = await provider.getFeeData();
// { BigNumber: "23238878592" }
console.log("WETH : ", priceOfUniswap, "Dai From Uniswap");
console.log("WETH : ", priceOfSushiswap, "Dai From Sushiswap");
// ...often this gas price is easier to understand or
// display to the user in gwei
console.log("gas price: ", ethers.utils.formatEther(maxFeePerGas),"Loan Amount: ", loanAmountIn, "FlashLoan Fee: ", loanAmountIn*loanFee, "Dai");
const profit = ((priceOfSushiswap-priceOfUniswap)*loanAmountIn/priceOfUniswap)-20-loanAmountIn*loanFee-60000-1000;
console.log("Profit: ", profit, "Dai");
console.log("-----------------------------------------------------------------");
if(profit>0){
return true;
}else {
return false;
}
}
// get price from uniswap
async function getPriceFromUniswap(){
// load abi uniswap pair & pair addresses & erc20 abi
const pairInfo = require("./config-uniswap/config-uniswap-pairs.json");
const abiUniswapPair = require("./config-uniswap/abi-uniswap-pair.json").abi;
const abiERC20 = require("./config-uniswap/abi-erc20.json").abi;
// load params
const provider = new ethers.providers.JsonRpcProvider(process.env.MAINNET_URL);
// load contract pair uniswap
const uniswapPair = new ethers.Contract(pairInfo.pairAddress, abiUniswapPair, provider);
// get pair tokens and its info associated
const addressToken0 = await uniswapPair.token0();
const addressToken1 = await uniswapPair.token1();
const reservesInfo = await uniswapPair.getReserves();
const reserve0 = Scalar.e(reservesInfo.reserve0);
const reserve1 = Scalar.e(reservesInfo.reserve1);
// get info tokens
const token0 = new ethers.Contract(addressToken0, abiERC20, provider);
const token1 = new ethers.Contract(addressToken1, abiERC20, provider);
const token0Decimals = await token0.decimals();
const token1Decimals = await token1.decimals();
const token0Symbol = await token0.symbol();
const token1Symbol = await token1.symbol();
// sort tokens & compute price
if (pairInfo.mainReserveSymbol !== token0Symbol && pairInfo.mainReserveSymbol !== token1Symbol)
throw Error(`${pairInfo.mainReserveSymbol} has not been found in uniswap pair ${pairInfo.pairAddress}`);
const amountIn = Scalar.pow(10, token0Decimals);
const amountOut = uniswapLib.getAmountOut(amountIn, reserve0, reserve1);
// compute price
const finalAmountIn = Number(amountIn) / 10**token0Decimals;
const finalAmountOut = Number(amountOut) / 10**token1Decimals;
const price = finalAmountIn / finalAmountOut;
return price;
}
// get price from sushiswap
async function getPriceFromSushiswap() {
const abiERC20 = require("./config-uniswap/abi-erc20.json").abi;
// load abi sushiswap pair & pair addresses
const sushiswap_pairInfo = require("./config-uniswap/sushiswap_config-uniswap-pairs.json");
const sushiswap_abiUniswapPair = require("./config-uniswap/sushiswap_abi-uniswap-pair.json").abi;
// load params
const provider = new ethers.providers.JsonRpcProvider(process.env.MAINNET_URL);
// load contract pair sushiswap
const sushiswapPair = new ethers.Contract(sushiswap_pairInfo.pairAddress, sushiswap_abiUniswapPair, provider);
// get pair tokens and its info associated from sushiswap
const sushiswap_addressToken0 = await sushiswapPair.token0();
const sushiswap_addressToken1 = await sushiswapPair.token1();
const sushiswap_reservesInfo = await sushiswapPair.getReserves();
const sushiswap_reserve0 = Scalar.e(sushiswap_reservesInfo._reserve0);
const sushiswap_reserve1 = Scalar.e(sushiswap_reservesInfo._reserve1);
// get info tokens
const sushiswap_token0 = new ethers.Contract(sushiswap_addressToken0, abiERC20, provider);
const sushiswap_token1 = new ethers.Contract(sushiswap_addressToken1, abiERC20, provider);
const sushiswap_token0Decimals = await sushiswap_token0.decimals();
const sushiswap_token1Decimals = await sushiswap_token1.decimals();
const sushiswap_token0Symbol = await sushiswap_token0.symbol();
const sushiswap_token1Symbol = await sushiswap_token1.symbol();
// sort tokens & compute price from sushiswap
if (sushiswap_pairInfo.mainReserveSymbol !== sushiswap_token0Symbol && sushiswap_pairInfo.mainReserveSymbol !== sushiswap_token1Symbol)
throw Error(`${sushiswap_pairInfo.mainReserveSymbol} has not been found in uniswap pair ${sushiswap_pairInfo.pairAddress}`);
const sushiswap_amountIn = Scalar.pow(10, sushiswap_token0Decimals);
const sushiswap_amountOut = uniswapLib.getAmountOut(sushiswap_amountIn, sushiswap_reserve0, sushiswap_reserve1);
// compute price from sushiswap
const sushiswap_finalAmountIn = Number(sushiswap_amountIn) / 10**sushiswap_token0Decimals;
const sushiswap_finalAmountOut = Number(sushiswap_amountOut) / 10**sushiswap_token1Decimals;
//sushiswap
const sushiswap_price = sushiswap_finalAmountIn / sushiswap_finalAmountOut;
return sushiswap_price;
}
// Placeholder function to calculate profit
async function calculateProfit() {
// Implement logic to calculate the profit from the last trade
return ethers.utils.parseEther("0.2"); // Example profit
}
monitorArbitrage().catch(console.error);