-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathronin.js
149 lines (129 loc) · 3.93 KB
/
ronin.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
// Use either this or compound.js not both
const scheduler = require("node-schedule");
const { ethers } = require("ethers");
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 },
})
);
// Staking Contract ABI
const stakingABI = [
"function restakeRewards()",
"function claimPendingRewards()",
"function unstakeAll()",
];
// Setup wallet and contract connections
const contractAddress = "0x05b0bb3c1c320b280501b86706c3551995bc8571";
const contract = new ethers.Contract(contractAddress, stakingABI, provider);
const wallet = new ethers.Wallet(PRIV_KEY, provider);
const connectedContract = contract.connect(wallet);
// Main Function
const main = async () => {
try {
// hello world
console.log(
figlet.textSync("AXSRestake", {
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 restakeExists = 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 restake schedule
if (nextRestake > currentDate) {
console.log("Restored Restake: " + nextRestake);
scheduler.scheduleJob(nextRestake, restake);
restakeExists = true;
}
}
} catch (error) {
console.error(error);
}
// no previous launch
if (!restakeExists) {
restake();
}
} catch (error) {
console.error(error);
}
};
// Restake Function
const restake = async () => {
try {
// set random gasLimit to avoid detection
const randomGas = 400000 + (Math.random() * (99999 - 10000) + 10000);
const overrideOptions = {
gasLimit: Math.floor(randomGas),
};
// execute the restaking transaction
const restake = await connectedContract.restakeRewards(overrideOptions);
const receipt = await restake.wait();
// wait for transaction to complete
if (receipt) {
// restake successful schedule next
restakes.previousRestake = new Date().toString();
console.log("RESTAKE SUCCESSFUL");
scheduleNext(new Date());
return true;
}
} catch (error) {
console.error(error);
// restake failed try again tomorrow
console.log("Restake Attempt Failed!");
console.log("Trying again tomorrow.");
scheduleNext(new Date());
}
return false;
};
// 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);
}
});
};
// Job Scheduler Function
const scheduleNext = async (nextDate) => {
// set the next restake time (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, restake);
storeData();
return;
};
main();