-
Notifications
You must be signed in to change notification settings - Fork 4
/
relayAuction.js
291 lines (251 loc) · 9.18 KB
/
relayAuction.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
const { CONTRACTS } = require("./constants/contracts");
const { CONFIG } = require("./constants/config");
const { PROVIDERS, MAINNETSIGNER } = require("./constants/providers.js");
const { ADDRESS, ADDRESS_AUCTION } = require("./constants/address.js");
const ethers = require("ethers");
const { GeckoIDPrices } = require("./utilities/geckoFetch.js");
const { Multicall } = require("./utilities/multicall.js");
const profitMarginUsd = 1.1
let transactionAttempted = false;
console.log("relay address ", ADDRESS_AUCTION["MAINNET"].RNGAUCTIONRELAY);
// ----------------------------------------------------------------- RELAY
async function checkAndCompleteRelay() {
if (transactionAttempted) {
console.log("Transaction already attempted, exiting.");
return;
}
let isAuctionOpen = await CONTRACTS.RNGAUCTION.MAINNET.isAuctionOpen();
isAuctionOpen = true;
if (isAuctionOpen) {
console.log("auction is open");
const relayEvents = await getRelayEvents();
if (relayEvents > 1) {
console.log("relay already compelted", relayEvents);
} else {
const calls = [
CONTRACTS.PRIZEPOOL[CONFIG.CHAINNAME].reserve(),
CONTRACTS.PRIZEPOOL[CONFIG.CHAINNAME].pendingReserveContributions(),
];
const [prices, fetchedCalls] = await Promise.all([
GeckoIDPrices(["pooltogether", "weth"]),
Multicall(calls, [CONFIG.CHAINNAME]),
]);
const poolPrice = prices[0];
const ethPrice = prices[1];
const [reserveForOpenDraw, reserve] = fetchedCalls;
//console.log("reserve open draw",reserveForOpenDraw)
const totalReserve = reserve.add(reserveForOpenDraw);
console.log(
"total reserve",
ethers.utils.formatUnits(
totalReserve,
ADDRESS[CONFIG.CHAINNAME].PRIZETOKEN.DECIMALS
)
);
const totalReserveFormatted = ethers.utils.formatUnits(
totalReserve,
ADDRESS[CONFIG.CHAINNAME].PRIZETOKEN.DECIMALS
);
console.log("token pricing - POOL $", poolPrice, " ETH $", ethPrice);
//[0] looks like the token required
const relayAuction = await relayRewards();
const reserveRemaining =
totalReserveFormatted - relayAuction.lastAuction / 1e18;
console.log(
"reserve remaining after last auction",
reserveRemaining.toFixed(2),
" POOL"
);
const relayEstimateReward =
reserveRemaining * (relayAuction.rewardFraction / 1e18);
console.log("");
const poolRewardUsd = relayEstimateReward * poolPrice;
console.log(
"estimated reward for relay",
relayEstimateReward.toFixed(2),
" POOL",
" | $",
poolRewardUsd.toFixed(2)
);
const relayGasLimit = 69420;
//const relayTx = await CONTRACTS.RNGAUCTIONRELAYWITHSIGNER.MAINNET.populateTransaction.relay("0xa8f85bAB964D7e6bE938B54Bf4b29A247A88CD9d",CONFIG.CHAINID.toString(),"0x38449a6b7bb76638452273925c9a2BA818bD130d","0xF4c47dacFda99bE38793181af9Fd1A2Ec7576bBF",CONFIG.WALLET)
const relayGas =
await CONTRACTS.RNGAUCTIONRELAYWITHSIGNER.MAINNET.estimateGas.relay(
ADDRESS_AUCTION.MAINNET.MESSAGEDISPATCHER,
CONFIG.CHAINID.toString(),
ADDRESS[CONFIG.CHAINNAME].REMOTEOWNER,
ADDRESS[CONFIG.CHAINNAME].RNGRELAYAUCTION,
CONFIG.WALLET,
relayGasLimit
);
const mainnetGasNow = await PROVIDERS.MAINNET.getGasPrice();
/*
// 1159 tx
let feeData = await PROVIDERS.MAINNET.getFeeData();
const lastBaseFeePerGas = Number(feeData.lastBaseFeePerGas);
const maxFeePerGas = Number(feeData.maxFeePerGas);
const maxPriorityFeePerGas = Number(feeData.maxPriorityFeePerGas);
const gasPrice = Number(feeData.gasPrice);
// Calculate the gas cost in Ether
const gasCost = relayGas.mul(feeData.maxFeePerGas);
// Convert gas cost to USD
const gasCostUsd = (parseFloat(ethers.utils.formatEther(gasCost)) * ethPrice);
console.log(
"relay gas ",
relayGas.toString(),
" ",
ethers.utils.formatEther(gasCost),
" ETH | ",
gasCostUsd.toFixed(4),
" USD"
);
const poolRewardUsd = relayEstimateReward * poolPrice;
console.log("pool reward value $", poolRewardUsd.toFixed(2));
if (poolRewardUsd > gasCostUsd + profitMarginUsd) {
try {
console.log("profitable yea");
return
// Replace 'return' with the transaction sending logic if the condition is true
let tx = await CONTRACTS.RNGAUCTIONRELAYWITHSIGNER.MAINNET.relay(
ADDRESS_AUCTION.MAINNET.MESSAGEDISPATCHER,
CONFIG.CHAINID.toString(),
ADDRESS[CONFIG.CHAINNAME].REMOTEOWNER,
ADDRESS[CONFIG.CHAINNAME].RNGRELAYAUCTION,
CONFIG.WALLET,
relayGasLimit,
{
maxFeePerGas: ethers.utils.parseUnits(maxFeePerGas.toString(), 'gwei'),
maxPriorityFeePerGas: ethers.utils.parseUnits(maxPriorityFeePerGas.toString(), 'gwei'),
type: 2, // Indicate EIP-1559 transaction
gasLimit: 600000
}
);
*/
//pre 1159 tx
const gasCost = relayGas.mul(mainnetGasNow);
const gasCostUsd = (gasCost / 1e18) * ethPrice;
console.log(
"relay gas ",
(mainnetGasNow / 1e9).toFixed(2),
"gwei | ",
(gasCost / 1e18).toFixed(5),
" ETH | $",
gasCostUsd.toFixed(2)
);
if (poolRewardUsd > gasCostUsd + profitMarginUsd) {
try {
console.log("profitable yehhaaaaaaaa");
return
let tx = await CONTRACTS.RNGAUCTIONRELAYWITHSIGNER.MAINNET.relay(
ADDRESS_AUCTION.MAINNET.MESSAGEDISPATCHER,
CONFIG.CHAINID.toString(),
ADDRESS[CONFIG.CHAINNAME].REMOTEOWNER,
ADDRESS[CONFIG.CHAINNAME].RNGRELAYAUCTION,
CONFIG.WALLET,
relayGasLimit,
{ gasPrice: mainnetGasNow, gasLimit: 600000 }
);
const receipt = await tx.wait();
// console.log("relayed!", receipt);
console.log("relayed!", receipt.transactionHash);
transactionAttempted = true;
} catch (e) {
console.log(e);
transactionAttempted = true;
}
} else {
console.log("");
console.log(
"$",
(poolRewardUsd - gasCostUsd).toFixed(2),
"not enough profit to send relay, threshold $",
profitMarginUsd
);
}
}
} else {
console.log("auction is closed");
}
}
async function relayRewards() {
const recentRngEvent = await getMostRecentEvent();
//console.log("most recent event block number", recentRngEvent.blockNumber);
const eventBlock = await getTimestampForBlock(recentRngEvent.blockNumber);
const timeElapsed = Math.round(Date.now() / 1000 - eventBlock);
try {
const [rewardFraction, lastResult] = await Promise.all([
CONTRACTS.RNGRELAYAUCTION[CONFIG.CHAINNAME].computeRewardFraction(timeElapsed),
CONTRACTS.RNGAUCTION.MAINNET.getLastAuctionResult()
]);
/* console.log(
"reward fraction:",
rewardFraction.toString(),
parseInt(rewardFraction) / 1e18
);*/
//console.log("last result", lastResult);
const auctionResult = {
rewardFraction: rewardFraction,
recipient: CONFIG.WALLET,
};
const auctionResults = [];
auctionResults[0] = lastResult;
auctionResults[1] = auctionResult;
const reward = await CONTRACTS.RNGRELAYAUCTION[
CONFIG.CHAINNAME
].computeRewards(auctionResults);
const results = {
lastAuction: reward[0],
newAuction: reward[1],
rewardFraction: rewardFraction,
};
//console.log("last auction result fraction (rng)",lastAuctionResult, " ",lastAuctionResult/1e18)
//console.log("relay auction fraction ",relayAuctionResult," ",relayAuctionResult/1e18)
return results;
} catch (e) {
console.log("error getting relay reward fraction", e);
}
}
async function getRelayEvents() {
const relayFilter = {
address: ADDRESS_AUCTION["MAINNET"].RNGAUCTIONRELAY,
topics: [
"0x0aaaf9835b678c952fbddc44da49e66cffaa813453ff984c4b8d497a1e5708dc",
],
fromBlock: -3000, //blocks to search for events
toBlock: "latest",
};
const relayLogs = await PROVIDERS["MAINNET"].getLogs(relayFilter);
// console.log("relay logs",relayLogs)
return relayLogs.length;
}
async function getMostRecentEvent() {
const RNGAUCTION_ADDRESS = ADDRESS[CONFIG.CHAINNAME].RNGAUCTION;
const topic =
"0xf037bfa85a373d893a047acc295492dfc8adaa0e779a717827e4ac1f67aca9b5";
const logs = await PROVIDERS.MAINNET.getLogs({
address: RNGAUCTION_ADDRESS,
topics: [topic],
fromBlock: 0, // adjust as needed
toBlock: "latest",
});
if (logs.length === 0) return null;
const mostRecentEvent = logs[logs.length - 1];
// console.log(mostRecentEvent)
return mostRecentEvent;
}
async function getTimestampForBlock(blockNumber) {
const block = await PROVIDERS.MAINNET.getBlock(blockNumber);
return block.timestamp;
}
// USE THESE FUNCTIONS TO RUN AUCTION
checkAndCompleteRelay();
// Call the async function every 30 seconds
setInterval(async () => {
try {
await checkAndCompleteRelay();
} catch (error) {
console.error("Error executing function:", error);
}
}, 120000);
// If you ever want to stop the interval, you can use:
// clearInterval(intervalId);