-
Notifications
You must be signed in to change notification settings - Fork 191
/
index.js
278 lines (235 loc) · 10.7 KB
/
index.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
require('dotenv').config();
const Web3 = require('web3');
const BigNumber = require('bignumber.js');
const { performance } = require('perf_hooks');
const Flashswap = require('./build/contracts/Flashswap.json');
const BlockSubscriber = require('./src/block_subscriber');
const TransactionSender = require('./src/transaction_send');
const fs = require('fs');
const util = require('util');
const request = require('async-request');
var log_file = fs.createWriteStream(__dirname + '/log_arbitrage.txt', { flags: 'w' });
var log_stdout = process.stdout;
console.log = function (d) {
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
const web3 = new Web3(
new Web3.providers.WebsocketProvider(process.env.WSS_BLOCKS, {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 15,
onTimeout: false
}
})
);
const { mainnet: addresses } = require('./addresses');
const { address: admin } = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
const prices = {};
const flashswap = new web3.eth.Contract(
Flashswap.abi,
Flashswap.networks[process.env.NETWORKID].address
);
const BNB_MAINNET = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const BUSD_MAINNET = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56';
const getPrices = async() => {
const response = await request('https://api.coingecko.com/api/v3/simple/price?ids=binancecoin,ethereum,bitcoin,tether,usd-coin,busd&vs_currencies=usd');
const prices = {};
try {
const json = JSON.parse(response.body);
prices['0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'.toLowerCase()] = json.binancecoin.usd;
prices['0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'.toLowerCase()] = json.busd.usd;
prices['0x2170Ed0880ac9A755fd29B2688956BD959F933F8'.toLowerCase()] = json.ethereum.usd;
prices['0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c'.toLowerCase()] = json.bitcoin.usd;
prices['0x55d398326f99059ff775485246999027b3197955'.toLowerCase()] = json.tether.usd;
// prices['??'.toLowerCase()] = json['usd-coin'].usd;
} catch (e) {
console.error(e)
return {};
}
return prices;
}
const pairs = [
{
name: 'BNB to BUSD, pancake>panther',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.pancake.router,
targetRouter: addresses.panther.router,
sourceFactory: addresses.pancake.factory,
},
{
name: 'BNB to BUSD, panther>pancake',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.panther.router,
targetRouter: addresses.pancake.router,
sourceFactory: addresses.panther.factory,
},
{
name: 'BNB to BUSD, pancake>ape',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.pancake.router,
targetRouter: addresses.ape.router,
sourceFactory: addresses.pancake.factory,
},
{
name: 'BNB to BUSD, ape>pancake',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.ape.router,
targetRouter: addresses.pancake.router,
sourceFactory: addresses.ape.factory,
},
{
name: 'BNB to BUSD, pancake>bakery',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.pancake.router,
targetRouter: addresses.bakery.router,
sourceFactory: addresses.pancake.factory,
},
{
name: 'BNB to BUSD, bakery>pancake',
amountTokenPay: process.env.BNB_AMOUNT,
tokenPay: BNB_MAINNET,
tokenSwap: BUSD_MAINNET,
sourceRouter: addresses.bakery.router,
targetRouter: addresses.pancake.router,
sourceFactory: addresses.bakery.factory,
}
]
const init = async () => {
console.log('starting: ', JSON.stringify(pairs.map(p => p.name)));
const transactionSender = TransactionSender.factory(process.env.WSS_BLOCKS.split(','));
let nonce = await web3.eth.getTransactionCount(admin);
let gasPrice = await web3.eth.getGasPrice();
setInterval(async () => {
nonce = await web3.eth.getTransactionCount(admin);
}, 1000 * 19);
setInterval(async () => {
gasPrice = await web3.eth.getGasPrice()
}, 1000 * 60 * 3);
const owner = await flashswap.methods.owner().call();
console.log(`started: wallet ${admin} - gasPrice ${gasPrice} - contract owner: ${owner}`);
let handler = async () => {
const myPrices = await getPrices();
if (Object.keys(myPrices).length > 0) {
for (const [key, value] of Object.entries(myPrices)) {
prices[key.toLowerCase()] = value;
}
}
};
await handler(); // get prices from CoinGecko
setInterval(handler, 1000 * 60 * 5);
const onBlock = async (block, web3, provider) => {
const start = performance.now();
const calls = [];
// const flashswap = new web3.eth.Contract(
// Flashswap.abi,
// Flashswap.networks[process.env.NETWORKID].address
// );
pairs.forEach((pair) => {
calls.push(async () => {
// 1e18 is a decimal for amoutTokenPay, you will need to change it based on currency
const check = await flashswap.methods.check(pair.tokenPay, pair.tokenSwap, new BigNumber(pair.amountTokenPay * 1e18), pair.sourceRouter, pair.targetRouter).call();
const profit = check[0];
let s = pair.tokenPay.toLowerCase();
const price = prices[s];
if (!price) {
console.log('invalid price', pair.tokenPay);
return;
}
const profitUsd = profit / 1e18 * price;
const percentage = (100 * (profit / 1e18)) / pair.amountTokenPay;
console.log(`[${block.number}] [${new Date().toLocaleString()}]: [${provider}] [${pair.name}] Arbitrage checked! Expected profit: ${(profit / 1e18).toFixed(3)} $${profitUsd.toFixed(2)} - ${percentage.toFixed(2)}%`);
if (profit > 0) {
console.log(`[${block.number}] [${new Date().toLocaleString()}]: [${provider}] [${pair.name}] Arbitrage opportunity found! Expected profit: ${(profit / 1e18).toFixed(3)} $${profitUsd.toFixed(2)} - ${percentage.toFixed(2)}%`);
const tx = flashswap.methods.startArbitrage(
block.number + process.env.BLOCKNUMBER,
pair.tokenPay,
pair.tokenSwap,
new BigNumber(pair.amountTokenPay * 1e18),
pair.sourceRouter,
pair.targetRouter,
pair.sourceFactory,
);
let estimateGas
try {
estimateGas = await tx.estimateGas({from: admin});
} catch (e) {
console.log(`[${block.number}] [${new Date().toLocaleString()}]: [${pair.name}]`, 'gasCost error', e.message);
return;
}
const myGasPrice = new BigNumber(gasPrice).plus(gasPrice * 0.2212).toString();
const txCostBNB = Web3.utils.toBN(estimateGas) * Web3.utils.toBN(myGasPrice);
// calculate the estimated gas cost in USD
let gasCostUsd = (txCostBNB / 1e18) * prices[BNB_MAINNET.toLowerCase()];
const profitMinusFeeInUsd = profitUsd - gasCostUsd;
if (profitMinusFeeInUsd < 0.6) {
console.log(`[${block.number}] [${new Date().toLocaleString()}] [${provider}]: [${pair.name}] stopped: `, JSON.stringify({
profit: "$" + profitMinusFeeInUsd.toFixed(2),
profitWithoutGasCost: "$" + profitUsd.toFixed(2),
gasCost: "$" + gasCostUsd.toFixed(2),
duration: `${(performance.now() - start).toFixed(2)} ms`,
provider: provider,
myGasPrice: myGasPrice.toString(),
txCostBNB: txCostBNB / 1e18,
estimateGas: estimateGas,
}));
}
if (profitMinusFeeInUsd > 0.6) {
console.log(`[${block.number}] [${new Date().toLocaleString()}] [${provider}]: [${pair.name}] and go: `, JSON.stringify({
profit: "$" + profitMinusFeeInUsd.toFixed(2),
profitWithoutGasCost: "$" + profitUsd.toFixed(2),
gasCost: "$" + gasCostUsd.toFixed(2),
duration: `${(performance.now() - start).toFixed(2)} ms`,
provider: provider,
}));
const data = tx.encodeABI();
const txData = {
from: admin,
to: flashswap.options.address,
data: data,
gas: estimateGas,
gasPrice: new BigNumber(myGasPrice),
nonce: nonce
};
let number = performance.now() - start;
if (number > 1500) {
console.error('out of time window: ', number);
return;
}
console.log(`[${block.number}] [${new Date().toLocaleString()}] [${provider}]: sending transactions...`, JSON.stringify(txData))
try {
await transactionSender.sendTransaction(txData);
} catch (e) {
console.error('transaction error', e);
}
}
}
})
})
try {
await Promise.all(calls.map(fn => fn()));
} catch (e) {
console.log('error', e)
}
let number = performance.now() - start;
if (number > 1500) {
console.error('warning to slow', number);
}
if (block.number % 40 === 0) {
console.log(`[${block.number}] [${new Date().toLocaleString()}]: alive (${provider}) - took ${number.toFixed(2)} ms`);
}
};
BlockSubscriber.subscribe(process.env.WSS_BLOCKS.split(','), onBlock);
}
init();