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

标签 python ethereum web3

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

最佳答案

可能的解决方案:

您没有显示您的 Web3 版本,此时 来自魏功能过时并从文档中删除。
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

关于python - 尝试使用 web3.py 库在以太坊中调用 balanceOf 函数但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335994/

相关文章:

javascript - 智能合约方法不是 web3 中的函数

javascript - Solidity 中的这个语法是什么意思?为了 (;;) {}

error-handling - 没有在给定选项或默认选项中指定有效的 “from”地址

node.js - Web3 通过钱包地址获取所有代币

javascript - 如何修复 "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default"错误?

c# - 让 C# 与 Python 通信的最快方法是什么?

python - Flask/SQLAlchemy : Incompatible collection type: None is not list-like

Python,如何在 lambda 中展开元组?

python - 在Python3中将unicode序列转换为字符串但允许字符串中的路径

transactions - 我应该在以太坊上有多少个确认?