You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用web3.py调用Ethereum Flashbots接口时签名验证失败导致交易未执行问题求助

web3.py调用Ethereum Flashbots接口时签名验证失败导致交易未执行问题求助

最近我在用Python向Flashbots端点发送请求时,碰到了X-Flashbots-Signature请求头相关的问题,交易始终无法正常执行。我的代码如下:

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)

这段代码是我基于Flashbots官方文档里的示例修改而来的。运行后我能收到Flashbots返回的200响应,也能拿到bundlehash,但交易就是一直没有执行。

之后我用flashbots_getBundleStatsV2方法查询这个bundlehash,得到了如下响应:

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

有没有大佬能给我点解决思路呀,非常感谢!

备注:内容来源于stack exchange,提问作者Gianna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:07:58