tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
414 stars 271 forks source link

Python/Issue with Calculating Total USDT Transactions on Tron Blockchain via API #470

Closed 808culture closed 6 months ago

808culture commented 7 months ago

I am developing a Python script intended to calculate the total amount of incoming USDT (TRC20) transactions to a specific address on the Tron blockchain. The script is supposed to utilize either the TronGrid or TronScan API to fetch transaction data. However, upon executing the script, it returns the total sum as 0, which contradicts the expected results, as there are incoming USDT transactions on the address.

I have already tried various approaches and APIs, but none have yielded the correct outcome. Here's the script I'm using

import requests

def fetch_transactions(address, start=0, limit=50):
    url = f"https://api.trongrid.io/v1/accounts/{address}/transactions/trc20?limit={limit}&only_to=true&min_block_timestamp=0&only_confirmed=true"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json().get("data", [])
    else:
        print(f"Failed to fetch transactions for address: {address}")
        return []

def get_total_usdt(address):
    transactions = fetch_transactions(address)
    total_usdt = 0

    for tx in transactions:
        if tx.get('token_info', {}).get('symbol') == 'USDT':
            total_usdt += int(tx.get('value', 0)) / 10**6  # Adjusting for token's decimal

    return total_usdt

# Example usage (replace 'your_address' with the actual address)
address = "your_address"
total_usdt = get_total_usdt(address)
print(f"Total incoming USDT transactions for address {address}: {total_usdt}")

I have encountered the following issues:

The script always returns a total of 0 USDT, despite there being incoming transactions. I'm unsure if the problem lies with the way I'm using the API, the API itself, or the script. Any advice or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

alextrader007 commented 7 months ago

I used your script locally to test that everything is normal. You can directly request the Trongrid interface in the browser to see if there is data returned.image

alextrader007 commented 7 months ago

The only thing to note is that some address records may be larger than 50. You need to get all the other records. image

image