Last active
November 1, 2022 23:51
-
-
Save SmaugPool/892f5bc18bf5c6e52245e1b7bd0bed84 to your computer and use it in GitHub Desktop.
Pool Live Stake with cardano-db-sync 11.0.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH stake AS | |
(SELECT d1.addr_id | |
FROM delegation d1, pool_hash | |
WHERE pool_hash.id=d1.pool_hash_id | |
AND pool_hash.hash_raw='\xabacadaba9f12a8b5382fc370e4e7e69421fb59831bb4ecca3a11d9b' | |
AND NOT EXISTS | |
(SELECT TRUE | |
FROM delegation d2 | |
WHERE d2.addr_id=d1.addr_id | |
AND d2.tx_id>d1.tx_id) | |
AND NOT EXISTS | |
(SELECT TRUE | |
FROM stake_deregistration | |
WHERE stake_deregistration.addr_id=d1.addr_id | |
AND stake_deregistration.tx_id>d1.tx_id)) | |
SELECT sum(total) | |
FROM | |
(SELECT sum(value) total | |
FROM utxo_view | |
INNER JOIN stake ON utxo_view.stake_address_id=stake.addr_id | |
UNION SELECT sum(amount) | |
FROM reward | |
INNER JOIN stake ON reward.addr_id=stake.addr_id | |
WHERE reward.spendable_epoch <= (SELECT MAX(epoch_no) FROM block) | |
UNION SELECT -sum(amount) | |
FROM withdrawal | |
INNER JOIN stake ON withdrawal.addr_id=stake.addr_id | |
) AS t; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A pool live stake is generally considered as the sum of all wallets balances as currently seen by their users. If you use
earned_epoch < current_epoch
at epoch n, again, before the end of the epoch you will get new rows included for the rewards earned epoch n-1 and being calculated progressively during the epoch but not yet spendable (they will be spendable epoch n+1).This would be a different definition of a pool live stake and it would include before the end of epochs some rewards that no one has in their wallet yet. You could use
earned_epoch < current_epoch - 1
, but then it's exactly the same asspendable_epoch <= current_epoch
as used in the current query.