-
Notifications
You must be signed in to change notification settings - Fork 4
/
claimer.js
244 lines (206 loc) · 7.95 KB
/
claimer.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
const ethers = require("ethers");
// const { CONTRACTS, PROVIDERS, SIGNER, ABI } = require("./constants/index")
const { SIGNER } = require("./constants/providers.js");
const { ADDRESS } = require("./constants/address.js");
const { ABI } = require("./constants/abi.js");
const { CONFIG } = require("./constants/config.js");
const { FetchApiPrizes } = require("./functions/fetchApiPrizes.js");
const { GetWinners } = require("./functions/winners.js");
const { GetRecentClaims } = require("./functions/getRecentClaims.js");
const { SendClaims } = require("./functions/sendClaims.js");
const chalk = require("chalk");
const { GetPrizePoolData } = require("./functions/getPrizePoolData.js");
const { GeckoIDPrices } = require("./utilities/geckoFetch.js");
const { GetPricesForToken } = require("./utilities/1inch.js");
const { CollectRewards } = require("./collectRewards.js");
//const settings = require('./constants/liquidator-config');
const { minTimeInMilliseconds, maxTimeInMilliseconds, useCoinGecko } = CONFIG;
// covalent, not accurate to get twab players
// const FetchPlayers = require("./utilities/players.js");
const section = chalk.hex("#47FDFB");
const claimerContract = new ethers.Contract(
ADDRESS[CONFIG.CHAINNAME].CLAIMER,
ABI.CLAIMER,
SIGNER
);
async function go() {
console.log(section("----- starting claim bot ------"));
console.log("time logged | ", Date.now());
const claimsPromise = GetRecentClaims(CONFIG.CHAINID);
const prizePoolDataPromise = GetPrizePoolData();
// Set up the third promise based on the useCoinGecko flag
const priceFetchPromise = useCoinGecko
? GeckoIDPrices(["pooltogether", "ethereum"])
: GetPricesForToken(ADDRESS[CONFIG.CHAINNAME].PRIZETOKEN.ADDRESS);
// Use Promise.all to wait for all three promises to resolve
const [claims, prizePoolData, priceData] = await Promise.all([
claimsPromise,
prizePoolDataPromise,
priceFetchPromise,
]);
console.log("got " + claims.length + " claim events ", "\n");
// Extract the necessary data from prizePoolData
const {
lastDrawId,
numberOfTiers,
tierTimestamps,
prizesForTier,
maxFee,
tierPrizeValues,
tierRemainingLiquidites,
reserve,
} = prizePoolData;
let prizeTokenPrice, ethPrice;
// If useCoinGecko is true, priceData is from GeckoIDPrices, otherwise it's the direct prize token price
if (useCoinGecko) {
prizeTokenPrice = priceData[0];
ethPrice = priceData[1];
} else {
// If not using CoinGecko, priceData is directly the prizeTokenPrice
prizeTokenPrice = priceData;
// Assume you have another way to get ethPrice if necessary or it's not needed in this branch
}
console.log(section("----- contract data ------"));
maxFee.forEach((fee, index) => {
console.log("max fee for tier ", index, " -> ", parseInt(fee) / 1e18);
});
// console.log("prizes for Tier ",prizesForTier)
let newWinners;
console.log(section("----- getting winners -----"));
if (!CONFIG.USEAPI) {
// await SendClaims(claimerContract, lastDrawId, []);
newWinners = await GetWinners(
CONFIG.CHAINNAME,
numberOfTiers,
lastDrawId,
tierTimestamps,
CONFIG.TIERSTOCLAIM,
prizesForTier,
"latest"
);
} else {
console.log("using api for winner calculations");
newWinners = await FetchApiPrizes(
CONFIG.CHAINID,
lastDrawId,
CONFIG.TIERSTOCLAIM,
claims
);
}
if (newWinners === null) {
console.log("ERROR fetching API");
} else {
console.log("winners before removing claims", newWinners.length);
let winVsClaimStats
({ updatedWinners: newWinners, stats:winVsClaimStats } = removeAlreadyClaimed(newWinners, claims, lastDrawId))
console.log("won vs claimed",winVsClaimStats)
console.log("winners before removing claims", newWinners.length);
console.log("winners after removing claims", newWinners.length);
// newWinners = removeAlreadyClaimed(newWinners, claims, lastDrawId);
// console.log("winners after removing claims", newWinners.length);
// console.log(section("---- checking profitability -----"));
// console.log("time logged | ",Date.now())
await SendClaims(
claimerContract,
lastDrawId,
newWinners,
maxFee,
prizeTokenPrice,
ethPrice
);
console.log("");
console.log(section("----- rewards check and claim ------"));
await CollectRewards(prizeTokenPrice, ethPrice);
}
console.log(
"-------------------------------------------bot will run again in " +
parseInt(minTimeInMilliseconds / 60000) +
"min - " +
parseInt(maxTimeInMilliseconds / 60000) +
"min------------ "
);
}
/*
function removeAlreadyClaimed(winners, claims, drawId) {
// Filter the claims for the specified drawId and tier
const relevantClaims = claims.filter((claim) => claim.drawId === drawId);
console.log(winners[0])
return winners
.map((winner) => {
const [vault, person, tier, prizeIndices] = winner;
// Check each prize index to see if it's been claimed
const unclaimedPrizeIndices = prizeIndices.filter(
(prizeIndex) =>
!relevantClaims.some(
(claim) =>
claim.vault.toLowerCase() === vault.toLowerCase() &&
claim.winner.toLowerCase() === person.toLowerCase() &&
claim.index === prizeIndex
)
);
// Return the winner with only unclaimed prize indices
return [vault, person, tier, unclaimedPrizeIndices];
})
.filter((winner) => winner[3].length > 0); // Remove winners with no unclaimed prizes
}*/
function removeAlreadyClaimed(winners, claims, drawId) {
// Initialize statistics objects
const tierStats = {
totalPrizesByTier: {},
claimedPrizesByTier: {},
};
// Filter the claims for the specified drawId
const relevantClaims = claims.filter((claim) => claim.drawId === drawId);
// Initialize the count of total prizes by tier from winners
winners.forEach(([,, tier, prizeIndices]) => {
// Increment total prizes for each prize index in the tier
tierStats.totalPrizesByTier[tier] = (tierStats.totalPrizesByTier[tier] || 0) + prizeIndices.length;
});
// Increment claims for each tier
relevantClaims.forEach((claim) => {
// console.log(`Claim Tier: ${claim.tier}, Current Count: ${tierStats.claimedPrizesByTier[claim.tier] || 0}`);
tierStats.claimedPrizesByTier[claim.tier] = (tierStats.claimedPrizesByTier[claim.tier] || 0) + 1;
});
const updatedWinners = winners.map((winner) => {
const [vault, person, tier, prizeIndices] = winner;
// Filter out claimed prizes
const unclaimedPrizeIndices = prizeIndices.filter(
(prizeIndex) =>
!relevantClaims.some(
(claim) =>
claim.vault.toLowerCase() === vault.toLowerCase() &&
claim.winner.toLowerCase() === person.toLowerCase() &&
claim.index === prizeIndex
)
);
/*
// Adjust the count of claimed prizes for the tier based on the difference
if (prizeIndices.length !== unclaimedPrizeIndices.length) {
tierStats.claimedPrizesByTier[tier] = (tierStats.claimedPrizesByTier[tier] || 0) + (prizeIndices.length - unclaimedPrizeIndices.length);
}*/
// Return the winner with only unclaimed prize indices
return [vault, person, tier, unclaimedPrizeIndices];
}).filter((winner) => winner[3].length > 0); // Remove winners with no unclaimed prizes
// Return both the updated winners list and the statistics
return {
updatedWinners,
stats: tierStats
};
}
async function executeAfterRandomTime(minTime, maxTime) {
// Calculate a random time between minTime and maxTime
const randomTime = minTime + Math.random() * (maxTime - minTime);
setTimeout(async () => {
try {
await go();
} catch (error) {
console.error("Error occurred:", error);
}
// Recursively call the function to continue the cycle
executeAfterRandomTime(minTime, maxTime);
}, randomTime);
}
// go once
go();
// go randomly after init
executeAfterRandomTime(minTimeInMilliseconds, maxTimeInMilliseconds);