-
Notifications
You must be signed in to change notification settings - Fork 4
/
run-arbitrage.js
220 lines (196 loc) · 6.67 KB
/
run-arbitrage.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
require("dotenv").config();
const log = require("log-to-file");
const Web3 = require("web3");
const abis = require("./abis");
const { mainnet: addresses } = require("./addresses");
const web3 = new Web3(
new Web3.providers.WebsocketProvider(process.env.WSS_URL)
);
const wbnbamount = "1";
const flashloanToken1 = "10";
const flashloanToken2 = "10";
const amountInToken1 = web3.utils.toBN(web3.utils.toWei(flashloanToken1));
const amountInToken2 = web3.utils.toBN(web3.utils.toWei(flashloanToken2));
const Exchange1 = new web3.eth.Contract(
abis.exchange1.router,
addresses.exchange1.router
);
const Exchange2 = new web3.eth.Contract(
abis.exchange2.router,
addresses.exchange2.router
);
const init = async () => {
const networkId = await web3.eth.net.getId();
web3.eth
.subscribe("newBlockHeaders")
.on("data", async (block) => {
console.log(`New block received. Block # ${block.number}`);
const getbnbprice = await Exchange1.methods
.getAmountsOut(wbnbamount, [
addresses.tokens.Wbnb,
addresses.tokens.busd,
])
.call();
const amountsOut1 = await Exchange1.methods
.getAmountsIn(amountInToken1, [
addresses.tokens.Token2,
addresses.tokens.Token1,
])
.call();
const amountsOut2 = await Exchange1.methods
.getAmountsOut(amountInToken1, [
addresses.tokens.Token1,
addresses.tokens.Token2,
])
.call();
const amountsOut3 = await Exchange2.methods
.getAmountsIn(amountInToken1, [
addresses.tokens.Token2,
addresses.tokens.Token1,
])
.call();
const amountsOut4 = await Exchange2.methods
.getAmountsOut(amountInToken1, [
addresses.tokens.Token1,
addresses.tokens.Token2,
])
.call();
const amountsOut5 = await Exchange1.methods
.getAmountsIn(amountInToken2, [
addresses.tokens.Token1,
addresses.tokens.Token2,
])
.call();
const amountsOut6 = await Exchange1.methods
.getAmountsOut(amountInToken2, [
addresses.tokens.Token2,
addresses.tokens.Token1,
])
.call();
const amountsOut7 = await Exchange2.methods
.getAmountsIn(amountInToken2, [
addresses.tokens.Token1,
addresses.tokens.Token2,
])
.call();
const amountsOut8 = await Exchange2.methods
.getAmountsOut(amountInToken2, [
addresses.tokens.Token2,
addresses.tokens.Token1,
])
.call();
const Exchange1results = {
buy: amountsOut1[0] / 10 ** 18,
sell: amountsOut2[1] / 10 ** 18,
};
const Exchange1results2 = {
buy: amountsOut5[0] / 10 ** 18,
sell: amountsOut6[1] / 10 ** 18,
};
const Exchange2results = {
buy: amountsOut3[0] / 10 ** 18,
sell: amountsOut4[1] / 10 ** 18,
};
const Exchange2results2 = {
buy: amountsOut7[0] / 10 ** 18,
sell: amountsOut8[1] / 10 ** 18,
};
console.log(`exchange1 ${flashloanToken1} Token1/Token2 `);
console.log(Exchange1results);
console.log(`exchange2 ${flashloanToken1} Token1/Token2`);
console.log(Exchange2results);
console.log(`exchange1 ${flashloanToken2} Token2/Token1`);
console.log(Exchange1results2);
console.log(`exchange2 ${flashloanToken2} Token2/Token1 `);
console.log(Exchange2results2);
let Exchange2PaybackCalcToken1 =
(Exchange2results.buy / 0.997) * 10 ** 18;
let Exchange1PaybackCalcToken1 =
(Exchange1results.buy / 0.997) * 10 ** 18;
let Exchange1PaybackCalcToken2 =
(Exchange1results2.buy / 0.997) * 10 ** 18;
let Exchange2PaybackCalcToken2 =
(Exchange2results2.buy / 0.997) * 10 ** 18;
let repayToken1Exchange2Fee =
Exchange2PaybackCalcToken1 / 10 ** 18 - Exchange2results.buy;
let repayToken1Exchange1Fee =
Exchange1PaybackCalcToken1 / 10 ** 18 - Exchange1results.buy;
let repayToken2Exchange2Fee =
Exchange2PaybackCalcToken2 / 10 ** 18 - Exchange2results2.buy;
let repayToken2Exchange1Fee =
Exchange1PaybackCalcToken2 / 10 ** 18 - Exchange1results2.buy;
const gasPrice = await web3.eth.getGasPrice();
const bnbprice = getbnbprice[1].toString() / 10 ** 8;
const txCost =
(330000 * parseInt(gasPrice) * getbnbprice[1].toString()) / 10 ** 18;
txCost - repayToken1Exchange1Fee;
const profit1 =
Exchange1results.sell -
Exchange2results.buy -
txCost -
repayToken1Exchange2Fee;
const profit2 =
Exchange2results.sell -
Exchange1results.buy -
txCost -
repayToken1Exchange2Fee;
const profit3 =
Exchange2results2.sell -
Exchange1results2.buy -
txCost -
repayToken2Exchange2Fee;
const profit4 =
Exchange1results2.sell -
Exchange2results2.buy -
txCost -
repayToken2Exchange1Fee;
if (profit1 > 0 && profit1 > profit2) {
log(
`Arb opportunity found!
Flashloan Token2 on exchange1 at ${Exchange1results.buy}
Sell Token2 on exchange2 at ${Exchange2results.sell}
Expected cost of flashswap: ${repayToken1Exchange2Fee}
Expected Gas cost: ${txCost}
Expected profit: ${profit1} Token1`,
`arb.log`
);
}
if (profit2 > 0 && profit2 > profit1) {
log(
`Arb opportunity found!
Buy Token2 from exchange2 at ${Exchange2results.buy}
Sell Token2 from exchange1 at ${Exchange1results.sell}
Expected cost of flashswap: ${repayToken1Exchange1Fee}
Expected Gas cost: ${txCost}
Expected profit: ${profit2} Token1`,
`arb.log`
);
}
if (profit3 > 0 && profit3 > profit4) {
log(
`Arb opportunity found!
Flashloan Token1 on exchange1 at ${Exchange1results2.buy}
Sell Token1 on exchange2 at ${Exchange2results2.sell}
Expected cost of flashswap: ${repayToken2Exchange2Fee}
Expected Gas cost: ${txCost}
Expected profit: ${profit3} Token2`,
`arb.log`
);
}
if (profit4 > 0 && profit4 > profit3) {
log(
`Arb opportunity found!
Flashloan Token1 on exchange2 at ${Exchange2results2.buy}
Sell Token1 on at exchange1 ${Exchange1results2.sell}
Expected cost of flashswap: ${repayToken2Exchange1Fee}
Expected Gas cost: ${txCost}
Expected profit: ${profit4} Token2`,
`arb.log`
);
}
})
.on("error", (error) => {
console.log(error);
});
};
init();