繁体   English   中英

尝试使用 web3.py 库在以太坊中调用 balanceOf 函数但出现错误

[英]trying to call balanceOf function in ethereum using web3.py library but getting error

    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.fromWei(balance, 'ether'))

但是当我运行此代码时,我收到此错误

web3.exceptions.InvalidAddress: ('Web3.py 只接受校验和地址。给你这个非校验和地址的软件应该被认为是不安全的,请将它作为他们平台上的错误提交。尝试改用 ENS 名称。或者,如果您必须接受较低的安全性,请使用 Web3.toChecksumAddress(lower_case_address)。', '0x2551d2357c8da54b7d330917e0e769d33f1f5b93')--> error at this line

可能的解决方案:

您没有显示您的 Web3 版本,此时fromWei功能已过时并已从文档中删除。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM