From 904f329010ed5e98cdcbad562f1364b04ad9fc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sza=C5=82owski?= Date: Tue, 24 Dec 2024 16:12:18 +0100 Subject: [PATCH] fix(#2460): fix calculating live voting power --- CHANGELOG.md | 2 +- govtool/backend/sql/get-voting-power.sql | 27 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cebca540..5b074cb93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ changes. ### Fixed -- +- Fix calculating DRep live voting power [Issue 2460](https://github.com/IntersectMBO/govtool/issues/2460) ### Changed diff --git a/govtool/backend/sql/get-voting-power.sql b/govtool/backend/sql/get-voting-power.sql index 14662c65d..2a39edf1d 100644 --- a/govtool/backend/sql/get-voting-power.sql +++ b/govtool/backend/sql/get-voting-power.sql @@ -1,6 +1,21 @@ -select coalesce(sum(uv.value), 0) as amount -from utxo_view uv -join delegation_vote dv on uv.stake_address_id = dv.addr_id -join drep_hash dh on dv.drep_hash_id = dh.id -where dh.raw = decode(?,'hex') -and dv.cert_index != 0 \ No newline at end of file +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'))