How To Query Token Balances
If you're starting with Web3 Sheets, you might want to call in your ERC20 token balances. This tutorial assumes you already have Web3 Sheets installed.
Web3 Sheets Demo: Token Balances
Requirements
- You need the token symbol or token address.
- You need your wallet address.
Finding the Token Address
=TokenAddressBySymbol("DAI")
will return the token address.- View the token & holder's information on Etherscan or another block explorer.
Basic Example
The structure of the query is as follows:
=TOKENBALANCE(token address, token holder address, block/latest, network)
For example, let's say I want to look up the DAI balance of the GFX Labs wallet.
=TOKENBALANCE("0x6B175474E89094C44Da98b954EedeAC495271d0F", "0xa6e8772af29b29B9202a073f8E36f447689BEef6", "latest", "ethereum")
Historical Token Balances
To make a historical call simply change the "latest" to your desired block number. For example, let's say I want to look up the DAI balance of the GFX Labs wallet at block 16987717. It should return 10965.
=TOKENBALANCE("0x6B175474E89094C44Da98b954EedeAC495271d0F", "0xa6e8772af29b29B9202a073f8E36f447689BEef6", 16987717, "ethereum")
Use =timestampToBlock("blockNumber","chain")
to find the block number nearest to a timestamp!
For example, =TIMESTAMPTOBLOCK("1680760800","ethereum")
will return 16987717.
Extra Info
If you're curious how the token balance function works under the hood, it's just two CALLINT functions pre-packaged.
- tokenDecimals =
=CALLINT("",COPS("0x6B175474E89094C44Da98b954EedeAC495271d0F","latest","ethereum"),"decimals()")
- tokenBalance =
=CALLINT("",COPS("0x6B175474E89094C44Da98b954EedeAC495271d0F",16987717,"ethereum"),"balanceOf(address)","0xa6e8772af29b29B9202a073f8E36f447689BEef6")
- tokenBalance/10^decimals = 10965.52625 DAI