尝试使用web3.py库调用以太坊中的balanceOf函数,但是

2024-03-28 20:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

    import json
    from web3 import Web3
    infura_url="https://mainnet.infura.io/v3/5b314a9b373442fc8ed0c9cd184e838f"
    web3 =Web3(Web3.HTTPProvider(infura_url))
    abi=json.loads('[{"constant":true,"inputs":..........] large array')
    address="0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
    contract=web3.eth.contract(address=address,abi=abi)
    totalSupply=contract.functions.totalSupply().call()
    print(totalSupply)
    print(contract.functions.name().call())
    print(contract.functions.symbol().call())
    balance=contract.functions.balanceOf('0x2551d2357c8da54b7d330917e0e769d33f1f5b93').call()
    print(web3.feromWei(balance,ether))

但是当我运行这个代码时,我得到了这个错误

web3.exceptions.InvalidAddress: ('Web3.py only accepts checksum addresses. The software that gave you this non-checksum address should be considered unsafe, please file it as a bug on their platform. Try using an ENS name instead. Or, if you must accept lower safety, use Web3.toChecksumAddress(lower_case_address).', '0x2551d2357c8da54b7d330917e0e769d33f1f5b93')--> error at this line


Tags: nameimportjsonurladdresscallfunctionsweb3
1条回答
网友
1楼 · 发布于 2024-03-28 20:57:14

可能的解决方案:

您没有显示您的Web3版本,此时fromWei函数已过时并从文档中删除。你知道吗

contract.functions.balanceOf('0x2551d2357c8da54b7d330917e0e769d33f1f5b93').call()

上面的函数出错,因为插入的地址不是校验和地址。如果你不明白什么是校验和地址,here你有一个很好的解释,在这种情况下该怎么办?显然,您需要将在contract.functions.balanceOf上插入的地址转换为checksum address。你知道吗

address2 = Web3.toChecksumAddress('0x2551d2357c8da54b7d330917e0e769d33f1f5b93')
balance=contract.functions.balanceOf(address2).call()

#Don't use fromWei function if its not defined on your Web3 documentation

相关问题 更多 >