Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check TRC20 token balance? #73

Closed
Endogen opened this issue Jun 12, 2020 · 4 comments
Closed

How to check TRC20 token balance? #73

Endogen opened this issue Jun 12, 2020 · 4 comments

Comments

@Endogen
Copy link

Endogen commented Jun 12, 2020

How do i check the balance of a TRC20 token for a given TRX address?

@Endogen
Copy link
Author

Endogen commented Jun 15, 2020

Derived from what i found in the README i think the way to get the balance of a TRC20 token (in this case the WIN token) should look like this:

from tronapi import Tron

# Source for ABI: https://tronscan.org/#/token20/TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7/code
contract_abi = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from","type":"address"},{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"value","type":"uint256"}],"name":"burn","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from","type":"address"},{"name":"value","type":"uint256"}],"name":"burnFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"account","type":"address"}],"name":"addMinter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"renounceMinter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"isMinter","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"owner","type":"address"},{"name":"spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"name","type":"string"},{"name":"symbol","type":"string"},{"name":"decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"account","type":"address"}],"name":"MinterAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"account","type":"address"}],"name":"MinterRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]'

tron = Tron()
contract = tron.trx.contract("TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7", abi=contract_abi)
balance = contract.functions.balanceOf("TXRZqGMEXsGTX6AQtcSgYknos93hqw18P7")

print(balance)

But by calling that i get this error:
eth_abi.exceptions.NoEntriesFound: No matching entries for 'address' in encoder registry

What am i doing wrong here?

@Endogen
Copy link
Author

Endogen commented Jun 19, 2020

Turns out i was on the wrong path with my approach... Here is how to send WIN token - which will be easy to change to get the balance:

        trx_kwargs = dict()
        trx_kwargs["private_key"] = your_privkey
        trx_kwargs["default_address"] = your_base58_address

        tron = Tron(**trx_kwargs)

        kwargs = dict()
        kwargs["contract_address"] = tron.address.to_hex(wink_base58_contract_address)
        kwargs["function_selector"] = "transfer(address,uint256)"
        kwargs["fee_limit"] = 100000
        kwargs["call_value"] = 0
        kwargs["parameters"] = [
            {
                'type': 'address',
                'value': tron.address.to_hex(recipients_base58_address)
            },
            {
                'type': 'uint256',
                'value': 8000000
            }
        ]

        raw_tx = tron.transaction_builder.trigger_smart_contract(**kwargs)
        sign = tron.trx.sign(raw_tx["transaction"])
        result = tron.trx.broadcast(sign)
        
        print("RESULT", result)

@Endogen Endogen closed this as completed Jun 19, 2020
@Arjun-S05
Copy link

@Endogen Can you please share the code which can be used for querying TRC20 balance?
Thanks a lot

@Endogen
Copy link
Author

Endogen commented Mar 2, 2021

https://stackoverflow.com/questions/62398207/how-to-query-the-balance-of-a-trc20-token-for-a-given-trx-address

But a TRX account needs to be activated first. Means you need to have, i think at least, 1 TRX in that address or otherwise the TRC20 balance will not be shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@Endogen @Arjun-S05 and others