Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Avalanche block data by block hash with web3.py

How do I get block data by block hash? I'm interested in getting block timestamp for each new block.

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)

This causes an error:

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')

The same query works on Ethereum.

like image 741
MikkoP Avatar asked Oct 12 '25 09:10

MikkoP


1 Answers

Adding geth_poa_middleware worked for me:

from web3 import Web3
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)
like image 54
Alkhara Avatar answered Oct 15 '25 10:10

Alkhara