-
Notifications
You must be signed in to change notification settings - Fork 26
/
poolarb.cjs
executable file
·402 lines (355 loc) · 17.4 KB
/
poolarb.cjs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env node
require('dotenv').config()
const { program } = require('commander')
const flashbots = require('@flashbots/ethers-provider-bundle')
const ethers = require('ethers')
const https = require('https')
const fs = require('fs/promises')
program.option('-r, --rpc <url>', 'RPC endpoint URL', 'http://localhost:8545')
.option('-s, --slippage <percentage>', 'slippage tolerance for the swap', '2')
.option('-gs, --swap-gas-limit <gas>', 'gas limit for swap transaction', '400000')
.option('-ga, --arb-gas-limit <gas>', 'gas limit for arb transaction', '600000')
.option('-w, --wallet-file <file>', 'saved wallet for arbitrage transactions', 'wallet.json')
.option('-m, --max-tries <m>', 'number of blocks to attempt to submit bundle for', '2')
program.parse()
const options = program.opts()
const oneEther = ethers.utils.parseUnits('1', 'ether')
const provider = new ethers.providers.JsonRpcProvider(options.rpc)
const rocketStorageAddress = '0x1d8f8f00cfa6758d7bE78336684788Fb0ee0Fa46'
const wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const arbContractAddress = '0x786d8351f419F2Cb076664abcB5F8Ca04e9F1D7D'
const swapRouterAddress = '0x1111111254EEB25477B68fb85Ed929f73A960582'
const oneInchAPIBase = 'https://api.1inch.dev/swap/v5.2/1/'
function oneInchAPI(method, params) {
const url = `${oneInchAPIBase}${method}?${(new URLSearchParams(params)).toString()}`
return new Promise((resolve, reject) => {
const req = https.get(url, {headers: {'Authorization': `Bearer ${process.env.API_KEY}`}},
(res) => {
if (res.statusCode !== 200) {
console.log(`Got bad status ${res.statusCode} for 1inch ${method}`)
reject(res)
}
res.setEncoding('utf8')
let data = ''
res.on('data', (chunk) => data += chunk)
res.on('end', () => resolve(JSON.parse(data)))
})
req.on('error', reject)
})
}
const rocketStorage = new ethers.Contract(
rocketStorageAddress, ["function getAddress(bytes32 key) view returns (address)"], provider)
const arbContract = new ethers.Contract(
arbContractAddress, ["function arb(uint256 rethAmount, uint256 ethAmount, uint256 minProfit, bytes swapData) nonpayable"], provider)
async function run() {
const rocketNodeDepositAddress = await rocketStorage.getAddress(
ethers.utils.keccak256(ethers.utils.toUtf8Bytes("contract.addressrocketNodeDeposit")))
const rethAddress = await rocketStorage.getAddress(
ethers.utils.keccak256(ethers.utils.toUtf8Bytes("contract.addressrocketTokenRETH")))
const rocketDepositSettingsAddress = await rocketStorage.getAddress(
ethers.utils.keccak256(ethers.utils.toUtf8Bytes("contract.addressrocketDAOProtocolSettingsDeposit")))
const rocketDepositPoolAddress = await rocketStorage.getAddress(
ethers.utils.keccak256(ethers.utils.toUtf8Bytes("contract.addressrocketDepositPool")))
console.log(`Using rocketNodeDeposit address ${rocketNodeDepositAddress}`)
console.log(`Using rETH address ${rethAddress}`)
console.log(`Using deposit pool address ${rocketDepositPoolAddress}`)
console.log(`Using deposit settings address ${rocketDepositSettingsAddress}`)
const rethContract = new ethers.Contract(
rethAddress, ["function getRethValue(uint256 ethAmount) view returns (uint256)"], provider)
const rocketDepositPool = new ethers.Contract(
rocketDepositPoolAddress, ["function getBalance() view returns (uint256)"], provider)
const depositSettings = new ethers.Contract(
rocketDepositSettingsAddress,
["function getDepositFee() view returns (uint256)",
"function getMaximumDepositPoolSize() view returns (uint256)",
"function getMinimumDeposit() view returns (uint256)"],
provider)
const dpFee = await depositSettings.getDepositFee()
const dpSize = await depositSettings.getMaximumDepositPoolSize()
const minDeposit = await depositSettings.getMinimumDeposit()
const dpArbMin = minDeposit.mul(20) // TODO: tune
const depositABI = [{"inputs":[{"internalType":"uint256","name":"_bondAmount","type":"uint256"},{"internalType":"uint256","name":"_minimumNodeFee","type":"uint256"},{"internalType":"bytes","name":"_validatorPubkey","type":"bytes"},{"internalType":"bytes","name":"_validatorSignature","type":"bytes"},{"internalType":"bytes32","name":"_depositDataRoot","type":"bytes32"},{"internalType":"uint256","name":"_salt","type":"uint256"},{"internalType":"address","name":"_expectedMinipoolAddress","type":"address"}],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_bondAmount","type":"uint256"},{"internalType":"uint256","name":"_minimumNodeFee","type":"uint256"},{"internalType":"bytes","name":"_validatorPubkey","type":"bytes"},{"internalType":"bytes","name":"_validatorSignature","type":"bytes"},{"internalType":"bytes32","name":"_depositDataRoot","type":"bytes32"},{"internalType":"uint256","name":"_salt","type":"uint256"},{"internalType":"address","name":"_expectedMinipoolAddress","type":"address"}],"name":"depositWithCredit","outputs":[],"stateMutability":"payable","type":"function"}]
const rocketNodeDepositInterface = new ethers.utils.Interface(depositABI)
const validatorEther = ethers.utils.parseEther('32')
const maxArb = validatorEther
const rethBurnInterface = new ethers.utils.Interface(
["function burn(uint256 _rethAmount) nonpayable"])
const network = await provider.getNetwork()
if (network.chainId !== 1) {
console.log(`Only works on Ethereum mainnet (got chainid ${network.chainId})`)
process.exit(1)
}
const signer = (await ethers.Wallet.fromEncryptedJson(
await fs.readFile(options.walletFile, {encoding: 'utf8'}),
process.env.WALLET_PASSWORD)).connect(provider)
console.log(`Using wallet address: ${await signer.getAddress()}`)
// TODO: replace if you want searcher reputation
const randomSigner = ethers.Wallet.createRandom()
const flashbotsProvider = await flashbots.FlashbotsBundleProvider.create(
provider, randomSigner)
const maxTries = parseInt(options.maxTries)
async function makeArbTx(ethAmount) {
const depositFee = ethAmount.mul(dpFee).div(oneEther)
const depositAmount = ethAmount.sub(depositFee)
const rethAmount = await rethContract.getRethValue(depositAmount)
console.log(`Aiming to arb ${ethers.utils.formatUnits(ethAmount, "ether")} ETH via ${ethers.utils.formatUnits(rethAmount, "ether")} rETH`)
const swapParams = {
src: rethAddress,
dst: wethAddress,
from: arbContractAddress,
amount: rethAmount,
slippage: options.slippage,
gasLimit: options.swapGasLimit,
allowPartialFill: false,
disableEstimate: true
}
const swap = await oneInchAPI('swap', swapParams)
const feeData = await provider.getFeeData()
if (!('maxFeePerGas' in feeData) || ethers.BigNumber.from(0).eq(feeData.maxFeePerGas)) {
console.log(`Warning: did not get gas estimate, got ${feeData.maxFeePerGas}, using default 16/2 (-> 24/4)`)
feeData.maxFeePerGas = ethers.BigNumber.from(16)
feeData.maxPriorityFeePerGas = ethers.BigNumber.from(2)
}
// TODO: estimate the gas usage better somehow?
const arbMaxGas = ethers.BigNumber.from(options.arbGasLimit)
const minProfitGas = ethers.BigNumber.from('100000')
const minProfit = feeData.maxFeePerGas.mul(minProfitGas)
if (ethers.utils.getAddress(swap.tx.to) !== swapRouterAddress)
console.log(`Warning: unexpected to address for swap: ${swap.tx.to}`)
console.log(`arb(${rethAmount}, ${ethAmount}, ${minProfit}, ${swap.tx.data})`)
const unsignedArbTx = await arbContract.populateTransaction.arb(
rethAmount, ethAmount, minProfit, swap.tx.data)
unsignedArbTx.chainId = 1
unsignedArbTx.type = 2
unsignedArbTx.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas.mul(4)
unsignedArbTx.maxFeePerGas = feeData.maxFeePerGas.mul(3).div(2)
unsignedArbTx.gasLimit = arbMaxGas
unsignedArbTx.nonce = await signer.getTransactionCount()
return unsignedArbTx
}
async function makeBundle(tx, value) {
const unsignedArbTx = await makeArbTx(value)
if ('gasPrice' in tx && 'maxFeePerGas' in tx) {
console.log('Warning: tx contains both gasPrice and maxFeePerGas; deleting former')
delete tx.gasPrice
}
if ((!('type' in tx) || tx.type === 0) && ('accessList' in tx)) {
console.log('Warning: type 0 tx with accessList; deleting')
delete tx.accessList
}
function getRawTransaction(tx) {
function addKey(acc, key) { if (key in tx) acc[key] = tx[key]; return acc }
const txFields = "accessList chainId data gasPrice gasLimit maxFeePerGas maxPriorityFeePerGas nonce to type value".split(" ")
const sigFields = "v r s".split(" ")
const raw = ethers.utils.serializeTransaction(txFields.reduce(addKey, { }), sigFields.reduce(addKey, { }))
if (ethers.utils.keccak256(raw) !== tx.hash) throw new Error("serializing failed!")
return raw
}
return [
{signedTransaction: getRawTransaction(tx)},
{signer: signer, transaction: unsignedArbTx}
]
}
let canResult
function canParse(iface, tx) {
try {
canResult = iface.parseTransaction(tx)
return true
}
catch { return false }
}
async function processTx(tx, value) {
const bundle = await makeBundle(tx, value.gt(maxArb) ? maxArb : value)
const currentBlockNumber = await provider.getBlockNumber()
const simulateOnly = 0 // 0 = simulate and submit, 1 = submit without bundle, 2 = simulate only
if (simulateOnly === 1) {
console.log('Submitting bundle as individual transactions')
for (const tx of bundle) {
try {
if ('signedTransaction' in tx)
// await provider.sendTransaction(tx.signedTransaction)
console.log('skipping signed tx, assuming already done')
else {
const txr = await tx.signer.sendTransaction(tx.transaction)
await txr.wait()
}
}
catch (e) {
console.log(`transaction failed: ${e.toString()}`)
}
}
}
else if (simulateOnly === 2) {
const targetBlockNumber = currentBlockNumber + 1
console.log(`Target block number: ${targetBlockNumber}`)
const signedBundle = await flashbotsProvider.signBundle(bundle)
const simulation = await flashbotsProvider.simulate(signedBundle, targetBlockNumber)
console.log(JSON.stringify(simulation, null, 2))
const currentBlock = await provider.getBlock(currentBlockNumber)
const bundlePricing = flashbotsProvider.calculateBundlePricing(simulation.results, currentBlock.baseFeePerGas)
console.log(JSON.stringify(bundlePricing, null, 2))
}
else {
const targetBlockNumbers = []
const promises = []
for (let targetBlockNumber = currentBlockNumber + 1; targetBlockNumber <= currentBlockNumber + maxTries; targetBlockNumber++) {
targetBlockNumbers.push(targetBlockNumber)
promises.push(flashbotsProvider.sendBundle(bundle, targetBlockNumber))
}
for (const [i, targetBlockNumber] of targetBlockNumbers.entries()) {
const submission = await promises[i]
console.log(`Target block number: ${targetBlockNumber}`)
if ('error' in submission) {
console.log(`RelayResponseError ${JSON.stringify(submission)}`)
}
else {
const simulateRes = await submission.simulate()
if ('error' in simulateRes) {
console.log(`Error during simulation, aborting: ${simulateRes.error.message}`)
}
else {
console.log(`Simulation used ${simulateRes.totalGasUsed} gas @ ${simulateRes.results[0].gasPrice}`)
if ('firstRevert' in simulateRes && simulateRes.firstRevert !== undefined) {
console.log(JSON.stringify(simulateRes.firstRevert))
console.log('Revert during simulation, aborting')
}
else {
console.log(JSON.stringify(simulateRes))
console.log('Simulation succeeded, waiting for submission')
const resolution = await submission.wait()
console.log(`Resolution: ${flashbots.FlashbotsBundleResolution[resolution]}`)
if (resolution === flashbots.FlashbotsBundleResolution.BundleIncluded) {
console.log('Success!')
break
}
}
}
}
}
}
}
async function watchMempool() {
const filterId = await provider.send("eth_newPendingTransactionFilter", [])
console.log(`installed pending tx filter ${filterId}`)
const skipsOnError = maxTries * 12
const skipsOnRevert = 16
var dpSkips = 0
async function pollForTxs() {
const hashes = await provider.send("eth_getFilterChanges", [filterId])
console.log(`${(new Date()).toLocaleString()}: Got ${hashes.length} pending txs`)
let dropped = 0
let skipped = 0
const dpSpace = dpSize.sub(await rocketDepositPool.getBalance())
for (const hash of hashes) {
const tx = await provider.getTransaction(hash)
if (tx === null) dropped += 1
else if (
tx.to === rocketNodeDepositAddress &&
canParse(rocketNodeDepositInterface, tx)
) {
console.log(`Found ${hash}: a minipool deposit!`)
const mintSpace = validatorEther.sub(tx.value)
await processTx(tx, dpSpace.add(mintSpace))
}
else if (
tx.to === rethAddress &&
canParse(rethBurnInterface, tx)
) {
console.log(`Found ${hash}: an rETH burn!`)
const rethAmount = canResult.args[0]
const rethContractBalance = await provider.getBalance(rethContract.address)
if (rethContractBalance.gte(rethAmount)) {
console.log('But it will not free any DP space')
skipped += 1
continue
}
// know: rethContractBalance < rethAmount
const amount = dpSpace.add(rethAmount.sub(rethContractBalance))
await processTx(tx, amount)
}
else {
skipped += 1
}
}
console.log(`Dropped ${dropped}, Skipped ${skipped}`)
if (dpSkips > 0) {
console.log(`(skipping dp check ${dpSkips})`)
dpSkips--
return
}
if (dpSpace.gt(dpArbMin)) {
console.log(`Found ${ethers.utils.formatUnits(dpSpace, 'ether')} free space in the DP: arbing immediately`)
const unsignedArbTx = await makeArbTx(dpSpace.gt(maxArb) ? maxArb : dpSpace)
console.log('Made tx')
const maxBlockNumber = await provider.getBlockNumber() + maxTries
console.log(`Will submit up till ${maxBlockNumber}`)
const txr = await flashbotsProvider.sendPrivateTransaction(
{transaction: unsignedArbTx, signer: signer},
{maxBlockNumber: maxBlockNumber})
console.log('Sent request to flashbots, now simulating')
const simulateRes = await txr.simulate()
const sentTx = ethers.utils.parseTransaction(txr.transaction.signedTransaction)
console.log(`Got simulation result based on gasLimit ${sentTx.gasLimit} maxFee ${sentTx.maxFeePerGas}`)
if ('error' in simulateRes) {
console.log(`Error during simulation, aborting: ${simulateRes.error.message}`)
// const msgWords = simulateRes.error.message.split(' ')
// const txhash = msgWords.pop()
// const checkWord = msgWords.pop()
// if (checkWord === 'txhash') {
// console.log(`Cancelling ${txhash}`)
// try {
// await flashbotsProvider.cancelPrivateTransaction(txhash)
// }
// catch (e) {
// console.log(`Cancel failed: ${e.toString()}`)
// }
// }
dpSkips = skipsOnError
}
else {
console.log(`Simulation used ${simulateRes.totalGasUsed} gas @ ${simulateRes.results[0].gasPrice}`)
if ('firstRevert' in simulateRes && simulateRes.firstRevert !== undefined) {
if (!('revert' in simulateRes.firstRevert))
console.log(JSON.stringify(simulateRes.firstRevert))
else
console.log(`Revert during simulation, aborting: ${simulateRes.firstRevert.revert}`)
dpSkips = skipsOnRevert
}
else {
console.log('Simulation succeeded, waiting for submission')
const waitRes = await txr.wait()
if (waitRes === flashbots.FlashbotsTransactionResolution.TransactionIncluded) {
console.log('Success!')
}
else if (waitRes === flashbots.FlashbotsTransactionResolution.TransactionDropped) {
console.log('Failed to send flashbots transaction')
dpSkips = skipsOnError
}
else {
console.log(`Something else happened ${waitRes}`)
dpSkips = skipsOnError
}
}
}
}
}
let keepGoing = true
process.on('SIGINT', () => {
if (keepGoing) {
console.log('Got interrupt')
keepGoing = false
}
else {
console.log(`Got another interrupt: leaving ${filterId} installed and exiting now`)
process.exit()
}
})
while (keepGoing)
await new Promise(resolve => setTimeout(resolve, 1000)).then(pollForTxs)
return filterId
}
const filterId = await watchMempool()
const uninstalledFilter = await provider.send("eth_uninstallFilter", [filterId])
console.log(`uninstallFilter ${uninstalledFilter ? 'succeeded' : 'failed'}`)
}
run()