andelf / tronpy

TRON Python Client Library.
MIT License
204 stars 96 forks source link

Error while trying to query how much usdt is in a wallet #123

Closed 30ThreeDegrees closed 8 months ago

30ThreeDegrees commented 8 months ago

Hi,

Im getting a hex error while trying to query how much usdt is in wallet here is my code

from tronpy import Tron, keys
from tronpy.providers import HTTPProvider

def get_usdt_balance(tron, usdt_contract_address, tron_address):
    try:
        # Convert TRON address to hex format
        address = keys.to_hex_address(tron_address)
        print(address)

        result = tron.trigger_constant_contract(
            contract_address=usdt_contract_address,
            owner_address="TGQVLckg1gDZS5wUwPTrPgRG4U8MKC4jcP",
            function_selector="balanceOf(address)",
            parameter=[{"type": "address", "value": address}],
        )

        # Extract the balance from the result
        balance = int(result["constant_result"][0], 16)

        return balance

    except Exception as e:
        print(f"Error checking balance: {e}")
        return None

def main():
    tron = Tron(
        HTTPProvider(api_key="xxxxx"), network="nile"
    )

    # USDT contract address on the TRON network (replace with the actual USDT contract address)
    usdt_contract_address = "TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj"

    tron_address = "4153213d0cd0058dc92642bd286e4e1a104a87fcdc"

    # Get and print the USDT balance
    usdt_balance = get_usdt_balance(tron, usdt_contract_address, tron_address)
    if usdt_balance is not None:
        print(f"USDT Balance for {tron_address}: {usdt_balance}")

if __name__ == "__main__":
    main()

And this is the error I keep encountering: Error checking balance: ('class org.bouncycastle.util.encoders.DecoderException : exception decoding Hex string: invalid characters encountered in Hex string', 'OTHER_ERROR')

Any help on where im going wrong or if this is a bug how to work around it would be appriciated.

30ThreeDegrees commented 8 months ago

I found the issue and now have this working