Gianna
Gianna

Reputation: 1

Ethereum Flashbots python code not working correctly with web3.py

Recently I've been having some trouble with the X-Flashbots-Signature header when sending a request to the flashbots endpoint. My python code looks like this:

import requests
import json
import secrets
from eth_account import Account, messages
from web3 import Web3
from math import ceil

rpcUrl = RPC_NODE_PROVIDER
web3 = Web3(Web3.HTTPProvider(rpcUrl))

publicKey = ETH_PUBLIC_KEY
privateKey = ETH_PRIVATE_KEY
contractAddress = TEST_CONTRACT # test contract
data = CONTRACT_DATA # Contract data to execute

signed = []
for _ in range(2):
    nonce = web3.eth.getTransactionCount(publicKey, 'pending')
    checksumAddress = Web3.toChecksumAddress(contractAddress)
    checksumPublic = Web3.toChecksumAddress(publicKey)
    tx = {
        'nonce': nonce,
        'to': checksumAddress,
        'from': checksumPublic,
        'value': 0,
        'gasPrice': web3.toWei(200, 'gwei'),
        'data': data
    }
    gas = web3.eth.estimateGas(tx)
    tx['gas'] = ceil(gas + gas * .1)
    signed_tx = web3.eth.account.signTransaction(tx, privateKey)
    signed.append(Web3.toHex(signed_tx.rawTransaction))

dt = {
    'jsonrpc': '2.0',
    'method': 'eth_sendBundle',
    'params': [
        {
            'txs': [
                signed[0], signed[1] # Signed txs with web3.eth.account.signTransaction
            ],
            'blockNumber': web3.eth.block_number + 1,
        }
    ],
    'id': 1
}
pvk = secrets.token_hex(32)
pbk = Account.from_key(pvk).address

body = json.dumps(dt)
message = messages.encode_defunct(text=Web3.keccak(text=body).hex())
signature = pbk + ':' + Account.sign_message(message, pvk).signature.hex()

hd = {
    'Content-Type': 'application/json',
    'X-Flashbots-Signature': signature,
}

res = requests.post('https://relay.flashbots.net/', json=body, headers=hd,)
print(res.text)

This code is a modified version of code taken straight from the flashbots docs: https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint/#authentication Upon running this code I get a 200 response from flashbots and it gives me a bundlehash, but the transaction never goes through.

When I input the bundlehash into "flashbots_getBundleStatsV2" method, I get this response:

{'id': 1, 'error': {'code': -32600, 'message': 'error in signature check 0xF65f5D0611c5B184D62C9b063585D7246D3F4df8'}, 'jsonrpc': '2.0'}

Any ideas would be greatly appreciated.

Upvotes: 0

Views: 54

Answers (0)

Related Questions