Returns ERC20 token balances for all tokens in which the given address has ever held a position
Parameters
address
(address as hex string, default: 0x0000000000000000000000000000000000002c64)- The address for which we want to check the balances.
chain_id
(uint64, default: 0)- Optional. Can be used to view balances from a certain chain. If unset, defaults to
0
and returns results from all chains.
- Optional. Can be used to view balances from a certain chain. If unset, defaults to
block_number
(uint64, default: 0)- Optional. Can be used to view allowances at a particular block. If unset, defaults to
0
and returns latest.
- Optional. Can be used to view allowances at a particular block. If unset, defaults to
limit
(uint32, default: 100)- Number of records you want returned.
offset
(uint32, default: 0)- For pagination. If you're showing ten records per page, choose offset = 9 for the second page.
Returns
Array with the following fields:
address
(address as hex string)- The address for which we checked the balances.
chain_id
(int64)- The chain for the token
token_address
(address as hex string)- The address of the token.
token_name
(string)- The name of the token.
token_symbol
(string)- The symbol of the token.
balance
(int256 as decimal string)- The balance at the specified block, normalized using that token's
decimals
.
- The balance at the specified block, normalized using that token's
txn_last_changed
(address as hex string)- The most recent transaction that impacted the balance.
block_last_changed
(int64)- The most recent block that impacted the balance.
The array is sorted in descending order of block_last_changed
.
Query
WITH latest_block_number AS (
SELECT chain_id,
token_address,
MAX(block_number) AS latest_block_number
FROM @sim.erc20_balance_changes_block
WHERE account_address = lower($address)
AND (
chain_id = $chain_id
OR $chain_id = 0
)
AND (
block_number <= $block_number
OR $block_number = 0
)
GROUP BY token_address,
chain_id
)
SELECT b.account_address AS address,
b.chain_id,
b.token_address,
b.token_name,
b.token_symbol,
(1.0 * b.balance / POWER(10, b.token_decimals)) AS balance,
b.txn_hash AS txn_last_changed,
b.block_number AS block_last_changed
FROM @sim.erc20_balance_changes_block b
INNER JOIN latest_block_number lbn ON b.token_address = lbn.token_address
AND b.block_number = lbn.latest_block_number
AND b.chain_id = lbn.chain_id
WHERE b.account_address = lower($address)
ORDER BY block_last_changed DESC
LIMIT $limit OFFSET $offset