Skip to content

Commit

Permalink
fix(pools): live saturation calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
1000101 committed May 6, 2024
1 parent 7221231 commit b644d07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- calculation of `live_saturation` in `/pools/{pool_id}` as although ledger specifies the variable as circulating supply, total supply (45B-reserves) is used in this calculation instead

## [2.0.1] - 2024-04-08

| :warning: WARNING |
Expand Down
44 changes: 9 additions & 35 deletions src/sql/pools/pools_pool_id.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* circulating_supply and live_stake_* CTEs are potentially (mainnet) heavy queries => use cache if needed */
/* total_supply and live_stake_* CTEs are potentially (mainnet) heavy queries => use cache if needed */
WITH current_epoch AS (
SELECT b.epoch_no AS "epoch_no"
FROM block b
Expand Down Expand Up @@ -30,39 +30,13 @@ queried_stake AS (
FROM current_epoch
)
),
circulating_supply AS (
SELECT (
(
SELECT COALESCE(SUM(txo.value), 0)
) + (
SELECT COALESCE(SUM(amount), 0)
FROM reward
WHERE spendable_epoch <= (
SELECT epoch_no
FROM current_epoch
)
) + (
SELECT COALESCE(SUM(amount), 0)
FROM instant_reward
WHERE spendable_epoch <= (
SELECT epoch_no
FROM current_epoch
)
) - (
SELECT COALESCE(SUM(amount), 0)
FROM withdrawal
)
) AS "circulating_supply"
/*
circulating_supply = SUM of all utxos + withdrawables
withdrawables = rewards (all types including rewards + refunds + treasury + reserves) - withdrawals
*/
FROM tx_out txo
LEFT JOIN tx_in txi ON (txo.tx_id = txi.tx_out_id)
AND (txo.index = txi.tx_out_index)
WHERE txi IS NULL
),
queried_addr AS (
-- although it's called circulation in the ledger code, we actually need total_supply instead for the calculations
total_supply AS (
SELECT 45000000000000000 - reserves
FROM ada_pots
ORDER BY epoch_no desc
LIMIT 1
), queried_addr AS (
SELECT id
FROM (
(
Expand Down Expand Up @@ -337,7 +311,7 @@ SELECT ph.view AS "pool_id",
) / (
(
SELECT *
FROM circulating_supply
FROM total_supply
) / (
SELECT optimal_pool_count
FROM epoch_param
Expand Down

0 comments on commit b644d07

Please sign in to comment.