From 06b678027f9122c8ccdf66feb23f165187096404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sza=C5=82owski?= Date: Fri, 27 Dec 2024 15:24:22 +0100 Subject: [PATCH] fix: count rewards with drep voting power --- CHANGELOG.md | 2 + .../sql/get-stake-key-voting-power.sql | 52 ++++++++++++++++--- govtool/backend/sql/get-voting-power.sql | 28 +++------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74962dffc..b33789fb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ changes. - Fix calculating DRep live voting power [Issue 2460](https://github.com/IntersectMBO/govtool/issues/2460) - Fix link and description validation [Issue 2403](https://github.com/IntersectMBO/govtool/issues/2403) +- Revert to drep_distr for providing active voting power +- Add rewards amount in the ada holder voting power ### Changed diff --git a/govtool/backend/sql/get-stake-key-voting-power.sql b/govtool/backend/sql/get-stake-key-voting-power.sql index abb0fd1af..a6cdbfe43 100644 --- a/govtool/backend/sql/get-stake-key-voting-power.sql +++ b/govtool/backend/sql/get-stake-key-voting-power.sql @@ -1,6 +1,46 @@ -SELECT COALESCE(SUM(utxo_view.value::numeric), 0), - encode(stake_address.hash_raw, 'hex') -FROM stake_address -JOIN utxo_view ON utxo_view.stake_address_id = stake_address.id -WHERE stake_address.hash_raw = decode(?, 'hex') -GROUP BY stake_address.hash_raw; +WITH RewardRest AS ( + SELECT + SUM(amount) AS amount, + addr_id + FROM + reward_rest + GROUP BY + addr_id +), +Reward AS ( + SELECT + SUM(amount) AS amount, + addr_id + FROM + reward + GROUP BY + addr_id +), +Balance AS ( + SELECT + COALESCE(SUM(uv.value), 0) AS amount, + sa.id AS addr_id, + encode(sa.hash_raw, 'hex') AS addr_raw + FROM + stake_address sa + JOIN utxo_view uv ON uv.stake_address_id = sa.id + GROUP BY + addr_id, + addr_raw +) +SELECT + (COALESCE(rr.amount, 0) + COALESCE(r.amount, 0) + COALESCE(b.amount, 0)) AS total_balance, + b.addr_raw +FROM + Balance b +LEFT JOIN + RewardRest rr ON rr.addr_id = b.addr_id +LEFT JOIN + Reward r ON r.addr_id = rr.addr_id +WHERE + b.addr_id = (SELECT id FROM stake_address WHERE hash_raw = decode(?, 'hex')) +GROUP BY + b.addr_raw, + rr.amount, + r.amount, + b.amount \ No newline at end of file diff --git a/govtool/backend/sql/get-voting-power.sql b/govtool/backend/sql/get-voting-power.sql index 2a39edf1d..a6b225f3e 100644 --- a/govtool/backend/sql/get-voting-power.sql +++ b/govtool/backend/sql/get-voting-power.sql @@ -1,21 +1,7 @@ -WITH LatestDelegationVote AS ( - SELECT - addr_id, - MAX(id) AS latest_vote_id - FROM - delegation_vote - GROUP BY - addr_id -) -SELECT - SUM(uv.value) AS total_value -FROM - utxo_view uv -JOIN - stake_address sa ON sa.id = uv.stake_address_id -JOIN - LatestDelegationVote ldv ON uv.stake_address_id = ldv.addr_id -JOIN - delegation_vote dv ON dv.id = ldv.latest_vote_id -WHERE - dv.drep_hash_id = (SELECT id FROM drep_hash WHERE raw = decode(?,'hex')) +SELECT + amount +FROM + drep_distr +WHERE + hash_id = (SELECT id FROM drep_hash WHERE raw = decode(?,'hex')) +ORDER BY epoch_no DESC LIMIT 1 \ No newline at end of file