如何使用Python获取USDT TRC20代币余额?

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月16日 01:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定