gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:02:15 77 4
gpt4 key购买 nike

我开始使用 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 = instance.functions.balanceOf().call({'from':send_address})。 balance of 之后的括号是您传递给 solidity 函数的参数。

关于python - 如何使用 web3.py 获取给定 ETH 地址的特定代币余额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528001/

77 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com