This github issue on web3.py is "Document how to set up ENS for non-mainnet addresses."

This question (that I filed) is me thrashing around trying to do so.

How do you set up ENS for non-mainnet addresses? For example, Rinkeby?

1 Answer 1


Got an answer. You have to inject some middleware at exactly the right spot.

from web3 import Web3
from web3.auto.infura import (
    INFURA_MAINNET_DOMAIN, build_http_headers, build_infura_url)
from web3.auto.infura.endpoints import INFURA_RINKEBY_DOMAIN
from web3.providers.auto import load_provider_from_uri

from ens import ENS
from eth_typing import ChecksumAddress

_headers = build_http_headers()

# NOTE: build_infura_url requires WEB3_INFURA_PROJECT_ID
# and WEB3_INFURA_API_SECRET environment variables set.
if False:
    # Works with mainnet
    _infura_url = build_infura_url(INFURA_MAINNET_DOMAIN)
    # Doesn't work with Rinkeby
    # "web3.exceptions.ExtraDataLengthError: The field extraData is 97 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."
    _infura_url = build_infura_url(INFURA_RINKEBY_DOMAIN)

w3 = Web3(load_provider_from_uri(_infura_url, _headers))
ens = ENS.fromWeb3(w3)

from web3.middleware import geth_poa_middleware
# NOTE: have to inject this into ens.web3, not w3
ens.web3.middleware_onion.inject(geth_poa_middleware, layer=0)

the_ens = ens.name(
print(the_ens)  # Returns None, presumably not mapped on Rinkeby

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.