【问题标题】:Get Avalanche block data by block hash with web3.py使用 web3.py 通过块哈希获取 Avalanche 块数据
【发布时间】:2021-11-18 21:47:46
【问题描述】:

如何通过区块哈希获取区块数据?我有兴趣获取每个新区块的区块时间戳。

from web3 import Web3

avalanche_url = 'https://api.avax.network/ext/bc/C/rpc'
provider = Web3(Web3.HTTPProvider(avalanche_url))

new_block_filter = provider.eth.filter('latest')

while True:
    block_hashes = new_block_filter.get_new_entries()
    
    for block_hash in block_hashes:
        block = provider.eth.get_block(block_hash.hex())
        print(block)

这会导致错误:

web3.exceptions.ExtraDataLengthError: The field extraData is 80 bytes, but should be 32. It is quite likely that you are connected to a POA chain. Refer to http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority for more details. The full extraData is: HexBytes('0x0000000000000000000000000001edd400000000000000000000000000000000000000000000000000000000002cb3970000000000000000000000000005902b00000000000000000000000000000000')

同样的查询也适用于以太坊。

【问题讨论】:

    标签: python ethereum web3py avalanche


    【解决方案1】:

    添加这个对我有用:

    from web3.middleware import geth_poa_middleware
    w3 = Web3(Web3.HTTPProvider('https://api.avax.network/ext/bc/C/rpc'))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)
    

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 2021-10-21
      • 2021-10-07
      • 2022-07-19
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多