英文:
How I can get USDT TRC20 token balance with python?
问题
我使用Tron API生成的代码,并获得一个200响应代码('ok'),但我没有获取到有关余额的任何信息。
import requests
url = "https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
print(response.text)
响应:
{
"data": [],
"meta": {
"at": 1676483294906,
"page_size": 0
},
"success": true
}
英文:
I use the code that the Tron api generates and get a 200 response code ('ok'), but I don't get any information about the balances.
Code
import requests
#wallet = 'TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD'
url = "https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
print(response.text)
Response
{
"data": [],
"meta": {
"at": 1676483294906,
"page_size": 0
},
"success": true
}
答案1
得分: 3
你所使用的API端点似乎出现了问题:https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens
我使用了一个不同的API端点,问题得到了解决:https://apilist.tronscan.org/api/account?address=TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD&includeToken=true.
import requests
contract_address = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' # USDT TRC20 contract address
wallet_address = 'TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD' # wallet address
url = f"https://apilist.tronscan.org/api/account?address={wallet_address}&includeToken=true"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
data = response.json()
if 'error' in data:
print(f"Error: {data['error']}")
else:
usdt_balance = None
for token in data['trc20token_balances']:
if token['tokenName'] == 'Tether USD':
usdt_balance = round(float(token['balance'])*pow(10,-token['tokenDecimal']),6)
break
if usdt_balance is not None:
print(f'USDT TRC20 balance in {wallet_address}: {usdt_balance}')
else:
print(f'USDT TRC20 token not found in {wallet_address}')
这是结果:
USDT TRC20 token balance
英文:
There seems to be a problem with the API endpoint you used: https://api.trongrid.io/v1/contracts/TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD/tokens
Here, I used a different API endpoint and it fixed the problem: https://apilist.tronscan.org/api/account?address=TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD&includeToken=true.
import requests
contract_address = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' # USDT TRC20 contract address
wallet_address = 'TWMsYUtqEAPxs7ZXuANkpABqGcixK3XZJD' # wallet address
url = f"https://apilist.tronscan.org/api/account?address={wallet_address}&includeToken=true"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
data = response.json()
if 'error' in data:
print(f"Error: {data['error']}")
else:
usdt_balance = None
for token in data['trc20token_balances']:
if token['tokenName'] == 'Tether USD':
usdt_balance = round(float(token['balance'])*pow(10,-token['tokenDecimal']),6)
break
if usdt_balance is not None:
print(f'USDT TRC20 balance in {wallet_address}: {usdt_balance}')
else:
print(f'USDT TRC20 token not found in {wallet_address}')
Here is the result:
USDT TRC20 token balance
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论