-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewFuncs.js
More file actions
353 lines (330 loc) · 9.75 KB
/
newFuncs.js
File metadata and controls
353 lines (330 loc) · 9.75 KB
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
require("dotenv").config();
const fetch = require("node-fetch");
const { ethers } = require("ethers");
const { abiBalancer } = require("./abi/abiBalancer");
const { abiCover } = require("./abi/abiCover");
const { abiArts } = require("./abi/abiArts");
let eth = 2000;
let gasPrice = 200;
let timeout = 0;
const log4js = require("log4js");
log4js.configure({
appenders: { cheese: { type: "file", filename: "cheese2.log" } },
categories: { default: { appenders: ["cheese"], level: "error" } },
});
const logger = log4js.getLogger("cheese");
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8547");
const wallet = new ethers.Wallet(process.env.PK, provider);
const arts = new ethers.Contract(process.env.ARBS_CONTRACT, abiArts, wallet);
const balancerPool = new ethers.Contract(
"0xe093973b45d3ddfc7d789850ad5b5bbd6a59846f",
abiBalancer,
wallet
);
const ALOT = ethers.utils.parseEther("100000");
const calcBuy = async (data, amount) => {
try {
const tx = await arts.calcArbyBuy(
data.protocolAddress,
data.claimPool,
data.noClaimPool,
data.expiration,
ethers.utils.parseEther(amount),
data.poolCollateral,
data.collateral
);
return parseFloat(amount) - parseFloat(ethers.utils.formatEther(tx));
} catch (error) {
console.log(error);
}
};
const calcSell = async (data, amount) => {
const tx = await arts.calcArbySell(
data.protocolAddress,
data.claimPool,
data.noClaimPool,
data.expiration,
ethers.utils.parseEther(amount),
data.poolCollateral,
data.collateral
);
return parseFloat(ethers.utils.formatEther(tx) - parseFloat(amount));
};
const buyToken = async (data) => {
let contract;
try {
if (data.isClaim) {
contract = new ethers.Contract(data.claimPool, abiBalancer, wallet);
} else {
contract = new ethers.Contract(data.noClaimPool, abiBalancer, wallet);
}
const tx = contract.swapExactAmountOut(
"0x6B175474E89094C44Da98b954EedeAC495271d0F",
data.maxAmountIn,
data.covToken,
data.amount,
ALOT,
{ gasPrice: data.gas }
);
return tx;
} catch (error) {
console.log("buyToken", error.message);
logger.error(error.message);
}
};
const redeemCollateral = async (data, amount, gas) => {
const contract = new ethers.Contract(data.coverAddress, abiCover, wallet);
try {
const tx = contract.redeemCollateral(amount, { gasPrice: gas });
return tx;
} catch (error) {
console.log(error.message);
logger.error(error.message);
}
};
const arbitrageBuy = async (data, protocol) => {
let arb = await calcBuy(data, "400");
if (arb < 4) {
return;
}
console.log(`CALCULATING ARB BUY ${data.name}`);
// logger.error(`CALCULATING ARB BUY ${data.name}`);
let bestArb = arb;
let sum = 9900;
let holderSum = 400;
for (let i = 0; i < 19; i++) {
let arb2 = await calcBuy(data, sum.toString());
if (arb2 > bestArb) {
bestArb = arb2;
holderSum = sum;
}
sum -= 500;
}
if (bestArb > 25) {
let add = 0;
timeout++;
console.log(`POSITIVE BUY ${data.name}`, Date());
logger.error(`POSITIVE BUY ${data.name}`);
if (timeout > 60) {
clearInterval(data.interval);
try {
if (bestArb > 500) {
add = 100;
} else if (bestArb > 250) {
add = 75;
} else if (bestArb > 100) {
add = 50;
} else if (bestArb > 50) {
add = 35;
} else {
add = 25;
}
const tx1 = await buyToken({
maxAmountIn: ethers.utils.parseEther((holderSum / 2).toString()),
claimPool: data.claimPool,
covToken: data.covTokenClaim,
amount: ethers.utils.parseEther(holderSum.toString()),
gas: ethers.utils.parseUnits((gasPrice + add).toString(), "gwei"),
isClaim: true,
});
const tx2 = await buyToken({
maxAmountIn: ethers.utils.parseEther((holderSum * 1.2).toString()),
noClaimPool: data.noClaimPool,
covToken: data.covTokenNoClaim,
amount: ethers.utils.parseEther(holderSum.toString()),
gas: ethers.utils.parseUnits((gasPrice + add).toString(), "gwei"),
isClaim: false,
});
await tx1.wait();
console.log("tx1", tx1);
await tx2.wait();
console.log("tx2", tx2);
const redeem = await redeemCollateral(
data,
ethers.utils.parseEther(holderSum.toString()),
ethers.utils.parseUnits(gasPrice.toString(), "gwei")
);
await redeem.wait();
console.log("redeemed", redeem);
data.interval = setInterval(protocol, 1000);
timeout = 0;
} catch (error) {
console.log("arbitrageBuy", error);
logger.error(error.message);
}
}
}
};
// const calculateTxFee = (estimateGas, gasPrice) => {
// return parseFloat(estimateGas) * parseFloat(gasPrice) * eth;
// };
// const estimateGasSell = async (data, amount) => {
// try {
// const tx = await arts.estimateGas.arbitrageSell(
// data.protocolAddress,
// data.claimPool,
// data.noClaimPool,
// data.expiration,
// ethers.utils.parseEther(amount),
// data.collateral
// );
// return tx;
// } catch (error) {}
// };
const execSell = async (data, amount, gas) => {
try {
const tx = await arts.arbitrageSell(
data.protocolAddress,
data.claimPool,
data.noClaimPool,
data.expiration,
ethers.utils.parseEther(amount),
data.collateral,
{ gasPrice: gas, gasLimit: 1000000 }
);
return tx;
} catch (error) {}
};
const arbitrageSell = async (data, protocol) => {
let arb = await calcSell(data, "400");
if (arb < 4) {
return;
}
console.log(`CALCULATING ARB SELL ${data.name}`);
let bestArb = arb;
let sum = 9900;
let holderSum = 1000;
for (let i = 0; i < 19; i++) {
let arb2 = await calcSell(data, sum.toString());
if (arb2 > bestArb) {
bestArb = arb2;
holderSum = sum;
}
sum -= 500;
}
// let estimatedGas = await estimateGasSell(data, holderSum.toString());
// let txFee0 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 50).toString(), "gwei")
// );
// let txFee1 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 100).toString(), "gwei")
// );
// let txFee2 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 200).toString(), "gwei")
// );
if (bestArb > 45) {
console.log(`POSITIVE SELL ${data.name}`, Date());
logger.error(`POSITIVE ARB SELL ${data.name}`);
clearInterval(data.interval);
const tx = await execSell(
data,
holderSum.toString(),
ethers.utils.parseUnits((gasPrice + 30).toString(), "gwei")
);
await tx.wait();
console.log(`ARBITRAGED SELL ${data.name}`);
data.interval = setInterval(protocol, 1000);
}
};
const updateEthPrice = () => {
fetch(
"https://api.etherscan.io/api?module=stats&action=ethprice&apikey=AVVZPVKSDDA2JXD8ZWCMF5IRBVRX3RHER9"
)
.then((res) => res.json())
.then((json) => {
eth = parseFloat(json.result.ethusd);
console.log("ETH PRICE:", eth);
});
};
const updateGasPrice = () => {
fetch(
"https://ethgasstation.info/api/ethgasAPI.json?api-key=efcd202cf4806d632ca8232bde47918159e6ec96b10213dd38e342f05578"
)
.then((res) => res.json())
.then((json) => {
gasPrice = parseFloat(json.fastest / 10);
console.log("GAS PRICE:", gasPrice);
});
};
const calcSellArt = async (data, amount) => {
const tx = await arts.calcArbySell(
data.protocolAddress,
data.claimPool,
data.noClaimPool,
data.expiration,
ethers.utils.parseEther(amount),
data.poolCollateral,
data.collateral
);
return parseFloat(ethers.utils.formatEther(tx) / 1.05) - parseFloat(amount);
};
const execSellArt = async (data, amount, gas) => {
try {
const tx = await arts.arbitrageSellMulti(
data.protocolAddress,
data.claimPool,
data.noClaimPool,
data.expiration,
ethers.utils.parseEther(amount),
data.poolCollateral,
data.collateral,
data.collateral,
{ gasPrice: gas, gasLimit: 1000000 }
);
return tx;
} catch (error) {}
};
const arbitrageSellArt = async (data, protocol) => {
let arb = await calcSellArt(data, "400");
if (arb < 4) {
return;
}
console.log(`CALCULATING ARB SELL ${data.name}`);
let bestArb = arb;
let sum = 7500;
let holderSum = 400;
for (let i = 0; i < 14; i++) {
let arb2 = await calcSellArt(data, sum.toString());
if (arb2 > bestArb) {
bestArb = arb2;
holderSum = sum;
}
sum -= 500;
}
// let estimatedGas = await estimateGasSellArt(data, holderSum.toString());
// let txFee0 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 75).toString(), "gwei")
// );
// let txFee1 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 150).toString(), "gwei")
// );
// let txFee2 = calculateTxFee(
// ethers.utils.formatUnits(estimatedGas),
// ethers.utils.parseUnits((gasPrice + 250).toString(), "gwei")
// );
if (bestArb > 45) {
console.log(`POSITIVE SELL ${data.name}`, Date());
logger.error(`POSITIVE ARB SELL ${data.name}`);
clearInterval(data.interval);
const tx = await execSellArt(
data,
holderSum.toString(),
ethers.utils.parseUnits((gasPrice + 30).toString(), "gwei")
);
await tx.wait();
console.log(`ARBITRAGED SELL ${data.name}`);
data.interval = setInterval(protocol, 1000);
}
};
module.exports = {
arbitrageBuy,
arbitrageSell,
arbitrageSellArt,
updateEthPrice,
updateGasPrice,
};