BalanceOf function for the contract itself

CIS-2 contracts have a function ‘balanceOf’ to invoke to ask for token balances for specific addresses.

However, is there a function to invoke that returns the balance for the contract itself?

Surely that balance is also part of the contract state?

Hi there

I am not sure which balance you are interested in, CIS-2 or CCD and whether you need this information within a smart contract or off chain.

If you want this information off-chain:

  • For the CCD balance of a contract use get_instance_info (named similar in other SDKs) API call.
  • For the CIS-2 balance for a contract, it works just like for an account address, where you query the CIS-2 Token smart contract using BalanceOf this time you simply provide a contract address instead of the account address.

If you need the information within a smart contract:

  • CCD balance of the contract itself can be read from the context
  • Querying the CCD balance of another contract can be done using contract_balance method on the host object.
  • CIS-2 balances uses BalanceOf similar to the off-chain approach, here we provide a CIS-2 smart contract library.

Thank you! Reading your response made me realize I asked the wrong question. For reference, I’ve already implemented both methods (GetInstanceInfo and BalanceOf) in my python sdk for ccdexplorer.io and use both heavily.

The question I wanted to ask is: how can I retrieve the amount of CIS-2 token outstanding through a function call? I can count up all mint logged events and subtract all burn logged events, but ideally I would like to get this from the contract itself?

@limemloh would you still be able to answer this? Thanks.

CIS-2 does not require the smart contract to provide a function for getting the total supply, so the only way in the general case is to index the Mint and Burn events off-chain.

If you control the token contract, then you can just add this functionality to the contract.

2 Likes