-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrategy1.js
488 lines (421 loc) · 13.7 KB
/
strategy1.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
- Strategy 1 -
This strategy involves claiming the rewards (AXS tokens) and swapping the AXS tokens to RON and WETH to create LP tokens and deposit the LP tokens into the farm on the Katana DEX for RON rewards, thereby compounding the daily RON yields.
From: https://stake.axieinfinity.com/
To: https://katana.roninchain.com/#/farm
*/
// Import required node modules
const { ethers, BigNumber } = require("ethers");
const scheduler = require("node-schedule");
const figlet = require("figlet");
require("dotenv").config();
const fs = require("fs");
// Import environment variables
const RPC_URL = process.env.RONIN_RPC;
const WALLET_ADDRESS = process.env.USER_ADDRESS;
const USER_AGENT = process.env.USER_AGENT;
const PRIV_KEY = process.env.USER_PRIVATE_KEY;
// State storage object for restakes
var restakes = {
previousRestake: "",
nextRestake: "",
};
// All relevant addresses needed
const AXS = "0x97a9107c1793bc407d6f527b77e7fff4d812bece";
const WETH = "0xc99a6a985ed2cac1ef41640596c5a5f9f4e19ef5";
const WRON = "0xe514d9deb7966c8be0ca922de8a064264ea6bcd4";
const LPtoken = "0x2ecb08f87f075b5769fe543d0e52e40140575ea7";
const katanaDEX = "0x7d0556d55ca1a92708681e2e231733ebd922597d";
const axsStaker = "0x05b0bb3c1c320b280501b86706c3551995bc8571";
const ronStaker = "0xb9072cec557528f81dd25dc474d4d69564956e1e";
// Contract ABIs
const ronStakerABI = ["function stake(uint256)"];
const axsStakerABI = [
"function claimPendingRewards()",
"function getPendingRewards(address) public view returns (uint256)",
];
const erc20ABI = ["function balanceOf(address) view returns (uint256)"];
const katanaDEX_ABI = [
"function getAmountsOut(uint, address[]) public view returns (uint[])",
"function swapExactTokensForRON(uint256,uint256,address[],address,uint256)",
"function swapExactTokensForTokens(uint256,uint256,address[],address,uint256)",
"function addLiquidityRON(address,uint,uint,uint,address,uint) payable returns (uint amountToken, uint amountRON, uint liquidity)",
];
const lpABI = [
"function getReserves() external view returns (uint112, uint112, uint32)",
"function token0() external view returns (address)",
"function token1() external view returns (address)",
].concat(erc20ABI);
// Ethers vars
var wallet,
provider,
axsContract,
lpContract,
katanaRouter,
ronFarmContract,
axsRewardsContract;
// Main Function
const main = async () => {
try {
// hello world
console.log(
figlet.textSync("AXSCompound", {
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default",
width: 80,
whitespaceBreak: true,
})
);
let claimsExists = false;
// get stored values from file
const storedData = JSON.parse(fs.readFileSync("./restakes.json"));
// not first launch, check data
if ("nextRestake" in storedData) {
const nextRestake = new Date(storedData.nextRestake);
// restore claims schedule
if (nextRestake > new Date()) {
console.log("Restored Claim: " + nextRestake);
scheduler.scheduleJob(nextRestake, AXSCompound);
claimsExists = true;
}
}
//no previous launch
if (!claimsExists) {
AXSCompound();
}
} catch (error) {
console.error(error);
}
};
// Ethers vars connect
const connect = () => {
const connection = {
url: RPC_URL,
headers: {
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
"X-Forwarded-For": randomIP(),
"X-Real-Ip": randomIP(),
},
};
// new RPC connection
provider = new ethers.providers.JsonRpcProvider(connection);
console.log(connection.headers["X-Forwarded-For"]);
console.log(connection.headers["X-Real-Ip"]);
wallet = new ethers.Wallet(PRIV_KEY, provider);
axsContract = new ethers.Contract(AXS, erc20ABI, wallet);
lpContract = new ethers.Contract(LPtoken, lpABI, wallet);
katanaRouter = new ethers.Contract(katanaDEX, katanaDEX_ABI, wallet);
ronFarmContract = new ethers.Contract(ronStaker, ronStakerABI, wallet);
axsRewardsContract = new ethers.Contract(axsStaker, axsStakerABI, wallet);
console.log("--> connected\n");
};
// Ethers vars disconnect
const disconnect = () => {
provider = null;
wallet = null;
axsContract = null;
lpContract = null;
katanaRouter = null;
ronFarmContract = null;
axsRewardsContract = null;
console.log("-disconnected-\n");
};
// AXS Compound Function
const AXSCompound = async () => {
console.log("\n--- AXSCompound Start ---");
try {
// start
connect();
const balance = await provider.getBalance(WALLET_ADDRESS);
console.log("RON Balance: " + ethers.utils.formatEther(balance));
// claim AXS rewards, retries 3 times
const axsBalance = await claimAXSrewards(1);
// claims failed throw an exception
if (!axsBalance) throw "AXS claims failed";
// swap the AXS tokens and create LP
const LPtokenBal = await addRewardstoLP(axsBalance);
// stake created LP tokens to farm
await stakeLPintoFarm(LPtokenBal);
return disconnect();
} catch (error) {
console.log("AXSCompound failed!");
// try again tomorrow
console.error(error);
scheduleNext(new Date());
}
return disconnect();
};
// Create LP Function
const addRewardstoLP = async (axsBalance) => {
try {
// calculate amount to swap for RON
axsBalance = BigNumber.from(axsBalance);
const formattedBal = Number(ethers.utils.formatEther(axsBalance));
let amountForRON = Math.floor((formattedBal / 2) * 1000) / 1000;
console.log(`Amount for RON: ${amountForRON} AXS`);
// calculate amount to swap for WETH
amountForRON = ethers.utils.parseEther(amountForRON.toString());
const amountForWETH = axsBalance.sub(amountForRON);
const formattedAmt = ethers.utils.formatEther(amountForWETH);
console.log(`Amount for WETH: ${formattedAmt} AXS`);
// swap half to WETH first
const WETHpath = [AXS, WETH];
const swapWETH = await swapExactTokensForTokens(amountForWETH, WETHpath);
let swapRON = false;
// swap other half for RON
if (swapWETH) {
const RONpath = [AXS, WETH, WRON];
swapRON = await swapExactTokensForTokens(amountForRON, RONpath, "RON");
}
// swaps are both done
if (swapWETH && swapRON) {
console.log("-Both Swaps Successful-");
const randomGas = getRandomNum(400000, 500000);
const keepRON = ethers.utils.parseEther("0.025");
// amount of RON to add to pool
let ronAmt = await provider.getBalance(WALLET_ADDRESS);
ronAmt = ronAmt.sub(keepRON).sub(randomGas);
const ronAmtMin = ronAmt.sub(ronAmt.div(100));
console.log("RON Amount: " + ethers.utils.formatEther(ronAmt));
// msg.value is treated as a amountRONDesired.
const overrideOptions = {
gasLimit: randomGas,
value: ronAmt,
};
// amount of WETH to add to pool
const LPreserves = await getReserves();
const wethAmt = quoteAmount(ronAmt, LPreserves);
const wethAmtMin = wethAmt.sub(wethAmt.div(100));
console.log("WETH Amount: " + ethers.utils.formatEther(wethAmt));
// add amounts into liquidity pool
const deadline = Date.now() + 1000 * 60 * 8;
const addLiquidity = await katanaRouter.addLiquidityRON(
WETH,
wethAmt,
wethAmtMin,
ronAmtMin,
WALLET_ADDRESS,
deadline,
overrideOptions
);
// wait for the transaction to complete
const receipt = await addLiquidity.wait();
if (receipt) {
console.log("ADD LIQUIDITY SUCCESSFUL");
// get current LP token balance of wallet
const lpBal = await lpContract.balanceOf(WALLET_ADDRESS);
console.log("LP Tokens: " + ethers.utils.formatEther(lpBal));
return lpBal;
}
} else {
throw "Swap process failed";
}
} catch (error) {
console.error(error);
}
return false;
};
// Quote Function
const quoteAmount = (ronAmt, LPreserves) => {
try {
// Calculate the quote
ronAmt = BigInt(ronAmt);
const ronReserves = BigInt(LPreserves.ronBalance);
const wethReserves = BigInt(LPreserves.wethBalance);
const wethAmt = (ronAmt * wethReserves) / ronReserves;
return BigNumber.from(wethAmt);
} catch (error) {
console.error(error);
console.log("Failed to get quotes.");
}
return false;
};
// Get Reserves Function
const getReserves = async () => {
try {
// get the LP reserves values
const LPreserves = await lpContract.getReserves();
const token0 = await lpContract.token0();
// assign values based on address
let balances = { ronBalance: 0, wethBalance: 0 };
if (WRON.toLowerCase() === token0.toLowerCase()) {
balances.ronBalance = LPreserves[0];
balances.wethBalance = LPreserves[1];
} else {
balances.ronBalance = LPreserves[1];
balances.wethBalance = LPreserves[0];
}
return balances;
} catch (error) {
console.error(error);
console.log("Failed to get reserves.");
}
return false;
};
// Swaps Function
const swapExactTokensForTokens = async (amountIn, path, mode) => {
try {
// get amount out from katana router
const amtInFormatted = ethers.utils.formatEther(amountIn);
const result = await katanaRouter.getAmountsOut(amountIn, path);
const expectedAmt = result[result.length - 1];
const deadline = Date.now() + 1000 * 60 * 8;
// calculate 1% slippage for ERC20 tokens
const amountOutMin = expectedAmt.sub(expectedAmt.div(100));
const amountOut = ethers.utils.formatEther(amountOutMin);
// console log the details
console.log("Swapping Tokens...");
console.log("Amount In: " + amtInFormatted);
console.log("Amount Out: " + amountOut);
let swap;
// set random gasLimit
const overrideOptions = {
gasLimit: getRandomNum(400000, 500000),
};
// execute the swap using the appropriate function
if (mode === "RON") {
// use the swapExactTokensForRON function
swap = await katanaRouter.swapExactTokensForRON(
amountIn,
amountOutMin,
path,
WALLET_ADDRESS,
deadline,
overrideOptions
);
} else {
// use the swapExactTokensForTokens function
swap = await katanaRouter.swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
WALLET_ADDRESS,
deadline,
overrideOptions
);
}
// wait for transaction to complete
const receipt = await swap.wait();
if (receipt) {
console.log("TOKEN SWAP SUCCESSFUL");
return true;
}
} catch (error) {
console.error(error);
}
return false;
};
// Stake Function
const stakeLPintoFarm = async (LPtokenBal) => {
try {
// set random gasLimit
const overrideOptions = {
gasLimit: getRandomNum(400000, 500000),
};
// execute AXS staking transaction
console.log("Staking LP Tokens...");
const stake = await ronFarmContract.stake(LPtokenBal, overrideOptions);
const receipt = await stake.wait();
// wait for transaction to complete
if (receipt) {
console.log("LP STAKE SUCCESSFUL");
return true;
}
} catch (error) {
console.error(error);
}
return false;
};
// Claims Function
const claimAXSrewards = async (tries) => {
try {
// limit to maximum 13 tries
if (tries > 8) return false;
console.log(`Try #${tries}...`);
console.log("Claiming AXS Rewards...");
// apply delay
await delay();
// get pending rewards amount
const u = await axsRewardsContract.getPendingRewards(WALLET_ADDRESS);
const unclaimed = ethers.utils.formatEther(u);
console.log(`Unclaimed Rewards: ${unclaimed} AXS`);
// set random gasLimit
const overrideOptions = {
gasLimit: getRandomNum(317811, 514229),
};
// execute the AXS claiming transaction
const claim = await axsRewardsContract.claimPendingRewards(overrideOptions);
const receipt = await claim.wait();
// wait for the transaction to complete
if (receipt) {
restakes.previousRestake = new Date().toString();
console.log("AXS CLAIM SUCCESSFUL");
const axsBal = await axsContract.balanceOf(WALLET_ADDRESS);
console.log("AXS Balance: " + ethers.utils.formatEther(axsBal));
// schedule next claim
scheduleNext(new Date());
return axsBal;
}
} catch (error) {
// failed try again
console.error(error);
console.log("Claim Attempt Failed!");
console.log("reconnecting...");
// refresh the connection
disconnect();
connect();
return await claimAXSrewards(++tries);
}
return false;
};
// Job Scheduler Function
const scheduleNext = async (nextDate) => {
// set next job to be 24hrs from now
nextDate.setHours(nextDate.getHours() + 24);
// add randomized buffer delay
const d = getRandomNum(1597, 2584);
nextDate.setSeconds(nextDate.getSeconds() + d);
restakes.nextRestake = nextDate.toString();
console.log("Next Restake: " + nextDate);
// schedule next restake
scheduler.scheduleJob(nextDate, AXSCompound);
storeData();
return;
};
// Data Storage Function
const storeData = async () => {
const data = JSON.stringify(restakes);
fs.writeFile("./restakes.json", data, (err) => {
if (err) {
console.error(err);
} else {
console.log("Data stored:\n", restakes);
}
});
};
// Random IP Function
const randomIP = () => {
const A = getRandomNum(100, 255);
const B = getRandomNum(0, 255);
const C = getRandomNum(0, 255);
const D = getRandomNum(0, 255);
return `${A}.${B}.${C}.${D}`;
};
// Generate Random Num Function
const getRandomNum = (min, max) => {
try {
return Math.floor(Math.random() * (max - min + 1)) + min;
} catch (error) {
console.error(error);
}
return max;
};
// Random Time Delay Function
const delay = () => {
const ms = getRandomNum(75025, 121393);
console.log(`delay(${ms})`);
return new Promise((resolve) => setTimeout(resolve, ms));
};
main();