1

Given the public hash of a fungible token contract, I want to get the amount of tokens owned by a wallet. I want to perform this operation off-chain so that it is free.

FA1.2 (tzip7) has getBalance() and FA2 (tzip12) has balance_of(). Can I call these without actually executing a blockchain transaction?

I have been trying to extract this information from the contract storages, which I can query from javascript using the tezostaquito package. Every contract has its own way of storing account balances though, so a general solution using this approach seems difficult.

I would like to avoid having to hard-code a query function for each token I am interested in. How is this usually done? How do wallets do it?

1 Answer 1

2

A better approach would be to use an Indexer. In general wallets only communicate with the chain directly to inject an operation or to do some sort of highly specific task with a single contract. General tasks like fetching all token balances, xtz balance, lists of available bakers etc is done via indexers, which are purpose built to handle the data in a way frontends expect to have it returned.

As an example here is the API to use to fetch all token balances for a wallet, from TZKT's api: https://api.tzkt.io/#operation/Tokens_GetTokenBalances

FYI, to answer one of your questions above. Using the blockchain RPC doesn't incur a network cost, only injecting an operation does

2
  • Thank you, this has helped a lot. I found out I can make calls to the public endpoint to simulate transactions for me as I understand it. I am using tezostaquito, where contract objects also expose a "views" field through which functions like GetBalance() and balance_of() can be called without injecting an operation.
    – PEC
    Apr 18 at 3:46
  • @PEC yes you CAN get the balances per contract, but with many hundreds of tokens available, and thousands of NFTs, its simply not realistic to query every balance one by one. As I mentioned above, the best way is to query an indexer that was built for this exact purpose
    – Simon McLoughlin
    Apr 19 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.