[英]How to get the specific token balance available at a give ETH address using web3.py
我开始使用 web.py。 我有一个要求,我需要在给定的 ETH 地址(该地址不是合同所有者)处获取可用的令牌余额,通过我偶然发现以下功能的文档:
https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.ContractFunction.call
token_contract.functions.myBalance().call({'from': web3.eth.accounts[1]})
所以在上面几行,我写了这个:
from web3 import HTTPProvider, Web3
import requests
w3 = Web3(HTTPProvider('https://ropsten.infura.io/RPw9nHRS7Ue47RaKVvHM'))
url_eth = "https://api.etherscan.io/api"
contract_address = '0x0CdCdA4E7FCDD461Ba82B61cBc4163E1022f22e4'
API_ENDPOINT = url_eth+"?module=contract&action=getabi&address="+str(contract_address)
r = requests.get(url = API_ENDPOINT)
response = r.json()
instance = w3.eth.contract(
address=Web3.toChecksumAddress(contract_address),
abi = response["result"]
)
send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balance_of.call({'from':send_address})
#balnce = instance.functions.myBalance.call({'from':send_address})
print("----------------------",balance)
我收到以下错误:
"Are you sure you provided the correct contract abi?"
web3.exceptions.MismatchedABI: ("The function 'balance_of' was not foundin this contract's abi. ", 'Are you sure you provided the correct contract abi?')
**更新**我摆脱了上述异常,现在我得到以下异常:
send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balanceOf.call(send_address)
#Error :
call_transaction = dict(**transaction)
TypeError: type object argument after ** must be a mapping, not str
如果我尝试这个:
send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balanceOf.call({'from':send_address})
#Error:
AttributeError: 'balanceOf' object has no attribute 'args'
您必须查看合约是否具有“balance_of”功能。 执行时
balance = instance.functions.balanceOf.call(send_address)
您正在执行合约的 balance_of 功能,如果合约没有该功能,则会出现错误
在最后一个代码示例中,尝试将第二行修改为: balance = instance.functions.balanceOf().call({'from':send_address})
。 平衡后的括号是您要传递给solidity函数的参数。
这在今天肯定有效:
send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balanceOf(send_address).call()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.