SRC-20 User Balances

How do we query the user balance of an SRC-20?

I understand given the UTXO model we would have to add up all the transactions.

In Ethereum, we have to track the balances in storage due to the account-based model.

What is the best way to go about this on-chain?

1 Like

Hey @alphaK3Y, to query the asset balance of an address, you can follow the docs for the TS SDK and GraphQL API.
And if you want to do it on-chain, you can have a look at a similar forum question here. lmk if you have any more questions

3 Likes

Hey @alphaK3Y, it’s not possible to fully read a user’s token balances on-chain (in a smart-contract or script), since the chain only has access to the UTXOs included in the transactions.

However, smart-contracts store their token balances using an accounts model within the smart contract. So a smart contract can read the balance of another smart contract (assuming the contract was included in the transaction).

2 Likes

@Nazeeh21 How would one go about listing all addresses that hold a specific SRC-20 token?

1 Like

This is probably something that needs an indexer and would be possible once we have SubSquid or HyperFuel ready

5 Likes

Hey @crypt0mate, we would be keen to assist you with this use case.

This is a good use case for an indexer such as HyperIndex for Fuel. A handler to process transfers for asset, but FVM token transfers don’t emit a log, they emit a transfer receipt which we don’t support just yet. Mint, Burn, Transfer, and TransferOut to be supported soon.

Another way to do this, is to use HyperFuel to query for getting transfer receipts of a specific asset. You’d just need to then process these receipts to construct a current list of addresses that have a balance of the token.
e.g. hyperfuel-client-python/examples/received-asset.py at main · enviodev/hyperfuel-client-python · GitHub

1 Like