-
Notifications
You must be signed in to change notification settings - Fork 4
/
collectRewards.js
90 lines (79 loc) · 2.96 KB
/
collectRewards.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
const { CONTRACTS } = require("./constants/contracts");
const { CONFIG } = require("./constants/config");
//const { web3GasEstimate } = require("./utilities/web3");
const { ADDRESS } = require("./constants/address");
const { GasEstimate } = require("./utilities/gas")
const maxGas = 60;
const minClaim = CONFIG.minToClaim;
const CLAIM_COST_AS_PERCENTAGE = 1; // 2 = 2%
const MIN_REWARDS_CLAIM = 5;
async function CollectRewards(prizeTokenPrice, ethPrice) {
PRIZEPOOL_CONTRACT = CONTRACTS.PRIZEPOOLWITHSIGNER[CONFIG.CHAINNAME];
try {
// Get the balance of claim rewards
const awardsBalance = await CONTRACTS.PRIZEPOOL[
CONFIG.CHAINNAME
].rewardBalance(CONFIG.WALLET);
console.log(
"Awards Balance:",
awardsBalance.toString(),
" ",
(parseInt(awardsBalance) / 1e18).toFixed(4),
" POOL ($",
(parseInt(awardsBalance) / 1e18) * prizeTokenPrice,
")"
);
if (parseInt(awardsBalance) / 1e18 < minClaim) {
console.log("not enough rewards accumulated to claim...(", minClaim, ")");
} else {
const functionName = "withdrawRewards";
const args = [CONFIG.WALLET, awardsBalance];
// Encode the function call
const data = PRIZEPOOL_CONTRACT.interface.encodeFunctionData(
functionName,
args
);
// calculate total gas cost in wei
/*const web3TotalGasCost = await web3GasEstimate(
data,
CONFIG.CHAINID,
CONFIG.WALLET,
ADDRESS[CONFIG.CHAINNAME].PRIZEPOOL
);*/
const web3TotalGasCost = await GasEstimate(PRIZEPOOL_CONTRACT,functionName,args,CONFIG.PRIORITYFEE)
const web3TotalGasCostUSD = (Number(web3TotalGasCost) * ethPrice) / 1e18;
console.log("Gas Estimate: $" + web3TotalGasCostUSD.toFixed(2))
const awardBalanceFormatted =
parseInt(awardsBalance) /
Math.pow(10, ADDRESS[CONFIG.CHAINNAME].PRIZETOKEN.DECIMALS);
if (
web3TotalGasCostUSD >
(awardBalanceFormatted * prizeTokenPrice * CLAIM_COST_AS_PERCENTAGE) /
100
) {
console.log("gas cost to claim rewards is too high");
} else if (awardBalanceFormatted < MIN_REWARDS_CLAIM) {
console.log("not meeting MIN_REWARDS_CLAIM");
} else {
//console.log("ready to claim but returning for testing");return
// Sign and send the transaction
const submittedTx = await PRIZEPOOL_CONTRACT.withdrawRewards(
CONFIG.WALLET,
awardsBalance,
{ maxPriorityFeePerGas: "1000011" }
);
const receipt = await submittedTx.wait();
if (receipt.status === 0) {
throw new Error("Approve transaction failed");
}
// console.log('Transaction Receipt:', receipt);
console.log("claimed POOL awards! tx hash ", receipt.transactionHash);
}
}
} catch (error) {
console.error("Error:", error);
}
}
module.exports = { CollectRewards };
// run in isolation (prizetokenprice,ethprice)
//CollectRewards(.49,3300)