-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
170 lines (146 loc) · 4.95 KB
/
index.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
import { ethers } from "ethers";
import { CONFIG, ACCOUNTS } from "./config.js";
import {
claimedFailLog,
claimedOkLog,
claimNotStartedLog,
claimStartedLog,
resultsLog, scriptStartedLog,
transferFailLog,
transferOkLog,
welcomeLog
} from "./logs.js";
/* ABI */
import erc20Abi from "./abi/erc20.json" assert { type: "json" };
import distributorAbi from "./abi/distributor.json" assert { type: "json" };
welcomeLog();
class ArbClaimer {
static EXPECTED_PONG_BACK = 10000;
static KEEP_ALIVE_CHECK_INTERVAL = 5000;
claimStarted = false;
pingTimeout = null;
keepAliveInterval = null;
provider;
providerWss;
DistributorContract;
claimsOk = 0;
transfersOk = 0;
totalAccountsDone = 0;
claimStartBlock = 0;
constructor() {
this.provider = new ethers.providers.JsonRpcProvider(
CONFIG.rpc_arb_http
);
this.providerWss = new ethers.providers.WebSocketProvider(
CONFIG.rpc_eth_wss
);
this.DistributorContract = new ethers.Contract(CONFIG.distributorAddress, distributorAbi, this.provider);
}
async claimAccount(account) {
try {
const accountId = account.privateKey.slice(0, 8);
const wallet = new ethers.Wallet(account.privateKey, this.provider);
const ClaimContract = new ethers.Contract(CONFIG.distributorAddress, distributorAbi, wallet);
const ArbTokenContract = new ethers.Contract(CONFIG.arbTokenAddress, erc20Abi, wallet);
const claimableTokens = await ClaimContract.claimableTokens(wallet.address);
const claimableTokensFormattedNum = Number(ethers.utils.formatEther(claimableTokens));
const feeData = await this.provider.getFeeData();
if (claimableTokensFormattedNum > 0) {
const claimTx = await ClaimContract.claim({
gasPrice: feeData.gasPrice,
gasLimit: "2000000"
});
const waitClaimTx = await this.provider.waitForTransaction(claimTx.hash);
if (waitClaimTx.status) {
claimedOkLog(accountId, claimableTokensFormattedNum, claimTx.hash);
this.claimsOk++;
} else {
claimedFailLog(accountId, wallet.address);
}
// Transfer tokens if addressToSendTokens is set
if (waitClaimTx.status && account.addressToSendTokens) {
const balance = await ArbTokenContract.balanceOf(wallet.address);
const transferTx = await ArbTokenContract.transfer(account.addressToSendTokens, balance);
const waitTransferTx = await this.provider.waitForTransaction(transferTx.hash);
if (waitTransferTx.status) {
transferOkLog(accountId, ethers.utils.formatEther(balance), transferTx.hash);
this.transfersOk++;
} else {
transferFailLog(accountId, wallet.address);
}
}
this.totalAccountsDone++;
if (this.totalAccountsDone === ACCOUNTS.length) {
resultsLog({
transfersOk: this.transfersOk,
claimsOk: this.claimsOk,
accountsDone: this.totalAccountsDone,
totalAccounts: ACCOUNTS.length,
});
process.exit(1);
}
}
} catch (e) {
console.log(e);
}
}
async startClaim() {
this.claimStarted = true;
for (let i = 0; i < ACCOUNTS.length; i++) {
this.claimAccount(ACCOUNTS[i]);
}
}
async init() {
this.providerWss._websocket.on("open", async () => {
this.keepAliveInterval = setInterval(() => {
this.providerWss._websocket.ping();
this.pingTimeout = setTimeout(() => {
this.providerWss._websocket.terminate();
}, ArbClaimer.EXPECTED_PONG_BACK);
}, ArbClaimer.KEEP_ALIVE_CHECK_INTERVAL);
const currentBlock = await this.providerWss.getBlockNumber();
this.claimStartBlock = (await this.DistributorContract.claimPeriodStart()).toNumber();
scriptStartedLog(currentBlock, this.claimStartBlock);
if (currentBlock >= this.claimStartBlock) {
this.startClaim();
}
this.providerWss.on("block", async (blockNum) => {
if (this.claimStarted) {
return;
}
if (blockNum >= this.claimStartBlock) {
claimStartedLog(blockNum);
this.startClaim();
} else {
claimNotStartedLog(blockNum);
}
});
});
this.providerWss._websocket.on("close", (code) => {
console.log(
`Connection lost with code ${code}! Attempting reconnect in 1s...`
);
this.restart();
});
this.providerWss._websocket.on("pong", () => {
// console.log(`Connection is alive`);
clearInterval(this.pingTimeout);
});
this.providerWss._websocket.on("error", () => {
console.error(`Unable to connect. Reconnect...`);
this.restart();
});
}
destroy() {
this.providerWss._websocket.terminate();
clearInterval(this.keepAliveInterval);
clearTimeout(this.pingTimeout);
}
restart() {
this.destroy();
new ArbClaimer().init();
}
}
(async () => {
await new ArbClaimer().init();
})()