-
Notifications
You must be signed in to change notification settings - Fork 4
/
compound.js
388 lines (343 loc) · 11.3 KB
/
compound.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
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: "",
};
// Initialize ethers components
const provider = new ethers.getDefaultProvider(
RPC_URL,
(request_kwargs = {
headers: { "content-type": "application/json", "user-agent": USER_AGENT },
})
);
// All relevant addresses needed
const AXS = "0x97a9107c1793bc407d6f527b77e7fff4d812bece";
const SLP = "0xa8754b9fa15fc18bb59458815510e40a12cd2014";
const WETH = "0xc99a6a985ed2cac1ef41640596c5a5f9f4e19ef5";
const LPtoken = "0x306a28279d04a47468ed83d55088d0dcd1369294";
const katanaDEX = "0x7d0556d55ca1a92708681e2e231733ebd922597d";
const axsStaker = "0x05b0bb3c1c320b280501b86706c3551995bc8571";
const slpStaker = "0xd4640c26c1a31cd632d8ae1a96fe5ac135d1eb52";
// Contract ABIs
const slpStakerABI = ["function stake(uint256)"];
const axsStakerABI = ["function claimPendingRewards()"];
const erc20ABI = ["function balanceOf(address) view returns (uint256)"];
const lpABI = [
"function getReserves() external view returns (uint112, uint112, uint32)",
"function token0() external view returns (address)",
"function token1() external view returns (address)",
].concat(erc20ABI);
const katanaDEX_ABI = [
"function getAmountsOut(uint, address[]) public view returns (uint[])",
"function swapExactTokensForTokens(uint256,uint256,address[],address,uint256)",
"function addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256)",
];
// Setup wallet and contract connections
const wallet = new ethers.Wallet(PRIV_KEY, provider);
const axsContract = new ethers.Contract(AXS, erc20ABI, provider);
const slpContract = new ethers.Contract(SLP, erc20ABI, provider);
const lpContract = new ethers.Contract(LPtoken, lpABI, provider);
const katanaRouter = new ethers.Contract(katanaDEX, katanaDEX_ABI, wallet);
const slpFarmContract = new ethers.Contract(slpStaker, slpStakerABI, wallet);
const axsRewardsContract = new ethers.Contract(axsStaker, axsStakerABI, wallet);
// Main Function
const main = async () => {
try {
// hello world
console.log(
figlet.textSync("AXSCompound", {
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default",
width: 80,
whitespaceBreak: true,
})
);
// current ronin balance
const balance = await provider.getBalance(WALLET_ADDRESS);
console.log("RON Balance: " + ethers.utils.formatEther(balance));
let claimsExists = false;
try {
// 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);
const currentDate = new Date();
// restore claims schedule
if (nextRestake > currentDate) {
console.log("Restored Claim: " + nextRestake);
scheduler.scheduleJob(nextRestake, AXSCompound);
claimsExists = true;
}
}
} catch (error) {
console.error(error);
}
//no previous launch
if (!claimsExists) {
AXSCompound();
}
} catch (error) {
console.error(error);
}
};
// AXS Compound Function
const AXSCompound = async () => {
try {
// claim AXS rewards and swap to create LP
const axsBalance = await claimAXSrewards();
const LPtokenBal = await addRewardstoLP(axsBalance);
// stake created LP tokens to farm
return stakeLPintoFarm(LPtokenBal);
} catch (error) {
console.error(error);
}
return false;
};
// Create LP Function
const addRewardstoLP = async (axsBalance) => {
try {
// calculate amount to swap for SLP
axsBalance = BigNumber.from(axsBalance);
const formattedBal = Number(ethers.utils.formatEther(axsBalance));
let amountForSLP = Math.floor((formattedBal / 2) * 1000) / 1000;
console.log(`Amount for SLP: ${amountForSLP} AXS`);
// calculate amount to swap for WETH
amountForSLP = ethers.utils.parseEther(amountForSLP.toString());
const amountForWETH = axsBalance.sub(amountForSLP);
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 swapSLP = false;
// swap other half for SLP
if (swapWETH) {
const SLPpath = [AXS, WETH, SLP];
swapSLP = await swapExactTokensForTokens(amountForSLP, SLPpath, "SLP");
}
// swaps are both done
if (swapWETH && swapSLP) {
console.log("Both Swaps Successful");
// amount of SLP to add to pool
const slpAmt = await slpContract.balanceOf(WALLET_ADDRESS);
const slpAmtMin = BigNumber.from(Math.floor(Number(slpAmt) * 0.99));
console.log("SLP Amount: " + slpAmtMin);
// amonut of WETH to add to pool
const LPreserves = await getReserves();
const wethAmt = quoteAmount(slpAmt, LPreserves);
const wethAmtMin = wethAmt.sub(wethAmt.div(100));
console.log("WETH Amount: " + ethers.utils.formatEther(wethAmtMin));
// set gasLimit
const overrideOptions = {
gasLimit: getRandomGas(400000, 500000),
};
// add amounts into liquidity pool
const deadline = Date.now() + 1000 * 60 * 8;
const addLiquidity = await katanaRouter.addLiquidity(
SLP,
WETH,
slpAmt,
wethAmt,
slpAmtMin,
wethAmtMin,
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 = (slpAmt, LPreserves) => {
try {
// Calculate the quote
slpAmt = BigInt(slpAmt);
const slpReserves = BigInt(LPreserves.slpBalance);
const wethReserves = BigInt(LPreserves.wethBalance);
const wethAmt = (slpAmt * wethReserves) / slpReserves;
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 = { slpBalance: 0, wethBalance: 0 };
if (SLP.toLowerCase() === token0.toLowerCase()) {
balances.slpBalance = LPreserves[0];
balances.wethBalance = LPreserves[1];
} else {
balances.slpBalance = LPreserves[1];
balances.wethBalance = LPreserves[0];
}
return balances;
} catch (error) {
console.error(error);
}
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;
let amountOutMin, amountOut;
// calculate input variables
if (mode === "SLP") {
// calculate 1% slippage amount for SLP
amountOut = Math.floor(Number(expectedAmt) * 0.99);
amountOutMin = BigNumber.from(amountOut);
} else {
// calculate 1% slippage for other ERC20
amountOutMin = expectedAmt.sub(expectedAmt.div(100));
amountOut = ethers.utils.formatEther(amountOutMin);
}
// console log the details
console.log("Swapping Tokens...");
console.log("Path: " + path);
console.log("Amount In: " + amtInFormatted);
console.log("Amount Out: " + amountOut);
// set random gasLimit
const overrideOptions = {
gasLimit: getRandomGas(400000, 500000),
};
// execute the swapping transaction
const 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: getRandomGas(400000, 500000),
};
// execute AXS staking transaction
console.log("Staking LP Tokens...");
const stake = await slpFarmContract.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 () => {
try {
// set random gasLimit
const overrideOptions = {
gasLimit: getRandomGas(400000, 500000),
};
// 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) {
console.error(error);
console.log("Claims Attempt Failed!");
console.log("Trying again tomorrow.");
// claims failed try again tomorrow
scheduleNext(new Date());
}
return false;
};
// Job Scheduler Function
const scheduleNext = async (nextDate) => {
// set next (24hrs, 1min, 30sec from now)
nextDate.setHours(nextDate.getHours() + 24);
nextDate.setMinutes(nextDate.getMinutes() + 1);
nextDate.setSeconds(nextDate.getSeconds() + 30);
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" + data);
}
});
};
// Generate random GAS Function
const getRandomGas = (min, max) => {
try {
return Math.floor(Math.random() * (max - min + 1)) + min;
} catch (error) {
console.error(error);
}
return max;
};
main();