web3-flashbots
web3-flashbots copied to clipboard
Sending bundle with FlashbotsBundleDictTx
I am trying to send a bundle using the FlashbotsBundleDictTx type (based on this line of code from the library), but I am getting the following error: 400 Client Error: Bad Request for url: https://relay-goerli.flashbots.net/
send_bundle.py:
from eth_account.signers.local import LocalAccount
from eth_account.account import Account
from web3 import Web3, HTTPProvider
from web3.types import TxParams
from web3.exceptions import TransactionNotFound
from flashbots import flashbot
from flashbots.flashbots import FlashbotsBundleResponse
from uuid import uuid4
from hexbytes import HexBytes
import json
w3 = Web3(Web3.HTTPProvider(NODE_URL))
tx = {
'from': PUBLIC_KEY,
'nonce': w3.eth.get_transaction_count(PUBLIC_KEY),
'to': '0xDB74803F14e9499F6064D8A68565FA02Bf09f8F6',
'value': Web3.toWei(0.01, 'ether'),
'chainId': 5,
'gas': 50000,
'maxFeePerGas': Web3.toWei(500, 'gwei'),
'maxPriorityFeePerGas': Web3.toWei(200, 'gwei'),
'data': '0x0'
}
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_KEY)
bundle = [
{
'v': signed_victim_tx['v'],
'r': HexBytes(signed_victim_tx['r']),
's': HexBytes(signed_victim_tx['s']),
'nonce': victim_tx['nonce'],
'input': victim_tx['data'],
'value': victim_tx['value'],
'gas': victim_tx['gas'],
'maxFeePerGas': victim_tx['maxFeePerGas'],
'maxPriorityFeePerGas': victim_tx['maxPriorityFeePerGas'],
'accessList': [],
'chainId': victim_tx['chainId'],
'to': victim_tx['to'],
'hash': signed_victim_tx['hash']
}
]
while True:
block_number = w3.eth.block_number
print(f'Simulating on block {block_number}')
try:
w3.flashbots.simulate(bundle, block_number)
print(f'Simulation successful')
except Exception as e:
print(f'Simulation error {e}')
raise e
replacement_uuid = str(uuid4())
res: FlashbotsBundleResponse = w3.flashbots.send_bundle(
bundle,
target_block_number=block_number + 1,
opts={'replacementUuid': replacement_uuid}
)
print(f'Bundle hash: {w3.toHex(res.bundle_hash())}')
res.wait()
try:
receipts = res.receipts()
print(f'Bundle was mined in block {receipts[0].blockNumber}')
break
except TransactionNotFound:
print(f'Bundle not found in block {block_number + 1}')
cancel_res = w3.flashbots.cancel_bundles(replacement_uuid)
print(f'Canceled {cancel_res}')