Returns ERC1155 token balances for all tokens in which the given address has ever held a position

Parameters

  1. address (address as hex string, default: 0x001e7e862b9b2aa56c75f5ff2247068b56f675eb)
    1. The address for which we want to check the balances.
  2. chain_id (uint64, default: 0)
    1. Optional. Can be used to view balances from a certain chain. If unset, defaults to 0 and returns results from all chains.
  3. block_number (uint64, default: 0)
    1. Optional. Can be used to view allowances at a particular block. If unset, defaults to 0 and returns latest.
  4. limit (uint32, default: 100)
    1. Number of records you want returned.
  5. offset (uint32, default: 0)
    1. For pagination. If you're showing ten records per page, choose offset = 9 for the second page.

Returns

Array with the following fields:

  1. address (address as hex string)
    1. The address for which we checked the balances.
  2. chain_id (int64)
    1. The chain for the token
  3. token_address (address as hex string)
    1. The address of the token.
  4. token_name (string)
    1. The name of the token.
  5. token_symbol (string)
    1. The symbol of the token.
  6. token_id (int256 as decimal string)
    1. The token's id.
  7. balance (int256 as decimal string)
    1. The balance at the specified block
  8. txn_last_changed (address as hex string)
    1. The most recent transaction that impacted the balance.
  9. block_last_changed (int64)
    1. 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,
    token_id,
    MAX(block_number) AS latest_block_number
  FROM @sim.erc1155_balance_changes_block
  WHERE account_address = lower($owner)
    AND (
      chain_id = $chain_id
      OR $chain_id = 0
    )
    AND (
      block_number <= $block_number
      OR $block_number = 0
    )
  GROUP BY token_address, token_id, chain_id
)
SELECT b.account_address AS address,
  b.chain_id,
  b.token_address,
  b.token_name,
  b.token_symbol,
  b.token_id,
  b.balance,
  b.txn_hash AS txn_last_changed,
  b.block_number AS block_last_changed
FROM @sim.erc1155_balance_changes_block b
  INNER JOIN latest_block_number lbn ON b.token_address = lbn.token_address
  AND b.token_id = lbn.token_id
  AND b.block_number = lbn.latest_block_number
  AND b.chain_id = lbn.chain_id
WHERE b.account_address = lower($owner)
ORDER BY block_last_changed DESC
LIMIT $limit OFFSET $offset