Skip to content

Instantly share code, notes, and snippets.

@trepca
Last active August 20, 2023 18:15
Show Gist options
  • Save trepca/64d22f7a96f7fe7b5c98eee52a291ea3 to your computer and use it in GitHub Desktop.
Save trepca/64d22f7a96f7fe7b5c98eee52a291ea3 to your computer and use it in GitHub Desktop.
Get CAT balance for a given puzzle hash. This query can be run on https://mojonode.com/explorer
select
-- second parameter of CAT_v2.clsp puzzle is TAIL, so we take it here (indexing starts from 0)
parent.puzzle->'a'->>1 as TAIL,
-- we sum the amount of all unspent coins we fetch
sum(amount)::bigint as amount,
-- we also provide the count of all coins for convenience
count(*) as number_of_coins
from chia.coin_spends parent
-- here we join with coin_records to get unspent coins
-- (we need to also use block name here due to how these tables are distributed in the cluster)
inner join chia.coin_records coin on coin.parent_coin_name = parent.name
and parent.spent_block_name = coin.confirmed_block_name
where
-- first we want to find all coins that have a hint to given puzzle
-- hash this address: xch1xjf7nsf3gsusyc0rwd50t889ntkadk6ghfvwsur669ekusl4mu6qsdrtmd
coin.hint = '\x3493e9c13144390261e37368f59ce59aedd6db48ba58e8707ad1736e43f5df34'
-- they should be unspent
and coin.is_spent=false
-- their parent puzzle should be a CAT puzzle
and parent.puzzle->>'m'='37bef360ee858133b69d595a906dc45d01af50379dad515eb9518abb7c1d2a7a'
group by TAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment