简体   繁体   English

如何使用 web3.py 在给定的 ETH 地址获取特定的代币余额

[英]How to get the specific token balance available at a give ETH address using web3.py

I am getting started with web.py.我开始使用 web.py。 I have a requirement where I need to get the available token balance at a given ETH address(this address is not the contract owner), going through the docs I stumble upon the following functiion :我有一个要求,我需要在给定的 ETH 地址(该地址不是合同所有者)处获取可用的令牌余额,通过我偶然发现以下功能的文档:

https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.ContractFunction.call https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.ContractFunction.call

token_contract.functions.myBalance().call({'from': web3.eth.accounts[1]})

so on the above lines, I wrote this :所以在上面几行,我写了这个:

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)

I get the following error :我收到以下错误:

"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?')

**Update ** I got rid of the above exception, now I'm getting the following exception : **更新**我摆脱了上述异常,现在我得到以下异常:

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

If I try this :如果我尝试这个:

send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balanceOf.call({'from':send_address})
#Error:
AttributeError: 'balanceOf' object has no attribute 'args'

you have to see if the contract has the function "balance_of".您必须查看合约是否具有“balance_of”功能。 At the time of executing执行时

balance = instance.functions.balanceOf.call(send_address)

You are executing the balance_of function of the contract, if the contract does not have that function it gives the error您正在执行合约的 balance_of 功能,如果合约没有该功能,则会出现错误

In the last code sample try to modify the second line to look like this: balance = instance.functions.balanceOf().call({'from':send_address}) . 在最后一个代码示例中,尝试将第二行修改为: balance = instance.functions.balanceOf().call({'from':send_address}) The parenthesis after balance of are the arguments you are passing to the solidity function. 平衡后的括号是您要传递给solidity函数的参数。

This works for sure as of today:这在今天肯定有效:

send_address = "0xF35A192475527f80EfcfeE5040C8B5BBB596F69A"
balance = instance.functions.balanceOf(send_address).call()

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

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