Returns all allowances for a given owner
Parameters
address
(address as hex string, default: 0x000e7be03f9adbc5065396a09e1b6ddc9da5a8bf)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
token_address
(address as hex string, default: 0x0000000000000000000000000000000000000000)- Optional. Can be used to filter results to a specific token. If unset, defaults to
address(0)
and all tokens are checked.
- Optional. Can be used to filter results to a specific token. 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 (in descending order of block_last_changed
):
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.
spender
(address as hex string)- The address that is permitted to spend
allowance
(int256 as decimal string)- The amount of the allowance normalized by token_decimals. Note that many contracts treat the max value of uint256 (~1.1579×10^77) as an unlimited allowance that doesn't decrement when the spender uses it.
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.
Query
WITH latest_block_number AS (
SELECT token_address,
spender,
chain_id,
MAX(block_number) AS latest_block_number
FROM @sim.erc20_allowance_changes_block
WHERE owner = lower($address)
AND (
chain_id = $chain_id
OR $chain_id = 0
)
AND (
token_address = lower($token_address)
OR $token_address = '0x0000000000000000000000000000000000000000'
)
AND (
block_number <= $block_number
OR $block_number = 0
)
GROUP BY token_address,
owner,
spender,
chain_id
)
SELECT a.chain_id,
a.token_address,
a.token_name,
a.token_symbol,
a.spender,
(1.0 * a.allowance / POWER(10, a.token_decimals)) AS allowance,
a.txn_hash as txn_last_changed,
a.block_number as block_last_changed
FROM @sim.erc20_allowance_changes_block a
INNER JOIN latest_block_number lbn ON a.block_number = lbn.latest_block_number
AND a.token_address = lbn.token_address
AND a.spender = lbn.spender
AND a.chain_id = lbn.chain_id
WHERE a.owner = lower($address)
ORDER BY block_last_changed desc
LIMIT $limit OFFSET $offset