【问题标题】:How to get all ENS domains for one wallet address with web3.py如何使用 web3.py 获取一个钱包地址的所有 ENS 域
【发布时间】:2023-02-21 05:17:50
【问题描述】:

我正在尝试用 python 编写一段代码,返回给定钱包地址的 ENS 域 web3.py,但是当钱包注册了多个 ENS 时我遇到了问题,我在文档中找不到任何关于此的内容问题。

这是一个示例代码:

infura_endpoint = f'https://mainnet.infura.io/v3/{infura_api_key}'

w3 = Web3(HTTPProvider(infura_endpoint))
print(w3.isConnected())  # This returns True
ns = ns.fromWeb3(web3=w3)

domain = ns.name('0xC99c2bdA0BEaA0B4c9774B48B81307C00e19CAde')
print(domain)  # This prints None

# This try/except block prints "Something went wrong" because the domain variable is None
try:
    assert ns.address(domain) == '0xC99c2bdA0BEaA0B4c9774B48B81307C00e19CAde'
except AssertionError:
    print('Something went wrong')

print(ns.address('seeds.eth'))  # "seeds.eth" is one of the domains that should be returned

如果您能给我任何帮助,我将不胜感激。

【问题讨论】:

    标签: python web3py


    【解决方案1】:

    以下代码对我有用(通过 Alchemy 连接到 web3)并利用了 ENS 包。

    from ens import ENS
    from web3 import Web3
    
    ALCHEMY_KEY = "<YOUR_KEY>"
    
    alchemy_url = f"https://eth-mainnet.g.alchemy.com/v2/{ALCHEMY_KEY}"
    w3 = Web3(Web3.HTTPProvider(alchemy_url))
    ns = ENS.fromWeb3(w3)
    
    addr = "0xD2Af803ad747ea12Acf5Ae468056703aE48785b5"
    wallet_name = ns.name(addr)
    
    print(wallet_name)
    

    使用的地址应解析为whaleshark.eth

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2022-11-22
      • 2021-10-07
      • 2021-07-31
      • 2021-09-06
      • 2022-01-18
      • 2022-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多