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

如何预生成以太坊合约地址?Web3.py实现问题排查

问题解决:合约预期地址与实际部署地址不一致
  • 核心问题:你使用的sender地址格式错误,RLP编码要求传入20字节的原始地址(bytes类型),而非字符串格式的地址,这是导致地址不匹配的根本原因,和normalize_transaction无关。
  • 修正步骤:
    1. 将字符串格式的sender地址转换为bytes(去除0x前缀后解码)
    2. 确保RLP编码的元素类型正确:[部署者地址bytes, nonce整数]
  • 修正后的代码:
from eth_utils import to_bytes
from rlp import encode

sender = w3.eth.default_account
# 把字符串地址转换为20字节的bytes类型
sender_bytes = to_bytes(hexstr=sender)
expected_address = w3.keccak(encode([sender_bytes, 0]))[12:]
print("Expected address:")
print(expected_address.hex())
  • 额外验证:可以通过w3.eth.get_transaction_count(sender)确认部署前的nonce值,确保确实为0。

内容的提问来源于stack exchange,提问作者Goober117

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:27:44