From dd7de90029dd88ff2302cf659b7854ed698c55d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sza=C5=82owski?= Date: Tue, 31 Dec 2024 11:34:25 +0100 Subject: [PATCH] hotfix: fetching latest drep voting anchor --- CHANGELOG.md | 1 + govtool/backend/sql/list-dreps.sql | 10 +++-- .../src/services/requests/getDRepList.ts | 41 +++++++++++-------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8394e9532..2186fff03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ changes. - Revert to drep_distr for providing active voting power - Add rewards amount in the ada holder voting power - Fix nested @value in jsonld metadatas [Issue 2509](https://github.com/IntersectMBO/govtool/issues/2509) +- Fix fetching latest voting anchor after DRep updates ### Changed diff --git a/govtool/backend/sql/list-dreps.sql b/govtool/backend/sql/list-dreps.sql index f5680c319..025764910 100644 --- a/govtool/backend/sql/list-dreps.sql +++ b/govtool/backend/sql/list-dreps.sql @@ -18,7 +18,7 @@ DRepActivity AS ( epoch_no DESC LIMIT 1 ) -SELECT +SELECT DISTINCT ON (dh.raw) encode(dh.raw, 'hex'), dh.view, dh.has_script, @@ -26,7 +26,7 @@ SELECT encode(va.data_hash, 'hex'), dr_deposit.deposit, DRepDistr.amount, - (DRepActivity.epoch_no - COALESCE(block.epoch_no, block_first_register.epoch_no)) <= DRepActivity.drep_activity AS active, + (DRepActivity.epoch_no - newestRegister.epoch_no) <= DRepActivity.drep_activity AS active, encode(dr_voting_anchor.tx_hash, 'hex') AS tx_hash, newestRegister.time AS last_register_time, COALESCE(latestDeposit.deposit, 0), @@ -72,7 +72,7 @@ FROM drep_registration dr JOIN tx ON tx.id = dr.tx_id WHERE - dr.deposit IS NOT NULL AND dr.deposit >= 0 + COALESCE(dr.deposit, 0) >= 0 ) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id AND dr_voting_anchor.rn = 1 LEFT JOIN ( SELECT @@ -122,6 +122,7 @@ FROM LEFT JOIN block ON block.id = tx.block_id LEFT JOIN ( SELECT + block.epoch_no, block.time, dr.drep_hash_id, ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn @@ -130,7 +131,7 @@ FROM JOIN tx ON tx.id = dr.tx_id JOIN block ON block.id = tx.block_id WHERE - NOT (dr.deposit < 0) + COALESCE(dr.deposit, 0) >= 0 ) AS newestRegister ON newestRegister.drep_hash_id = dh.id AND newestRegister.rn = 1 LEFT JOIN ( SELECT @@ -164,6 +165,7 @@ GROUP BY DRepActivity.drep_activity, dr_voting_anchor.tx_hash, newestRegister.time, + newestRegister.epoch_no, latestDeposit.deposit, non_deregister_voting_anchor.url, fetch_error.message, diff --git a/govtool/frontend/src/services/requests/getDRepList.ts b/govtool/frontend/src/services/requests/getDRepList.ts index 5005e1313..835894481 100644 --- a/govtool/frontend/src/services/requests/getDRepList.ts +++ b/govtool/frontend/src/services/requests/getDRepList.ts @@ -1,5 +1,4 @@ import { bech32 } from "bech32"; - import { type Infinite, type DRepStatus, @@ -33,29 +32,35 @@ export const getDRepList = async ({ }: GetDRepListArguments): Promise> => { // DBSync contains wrong representation of DRep view for script based DReps, // but it's still used by BE - const searchPhrase = (() => { - if (rawSearchPhrase.startsWith("drep_script")) { - const { words } = bech32.decode(rawSearchPhrase); - return bech32.encode("drep", words); - } - if (rawSearchPhrase.startsWith("drep")) { - const decodedIdentifier = decodeCIP129Identifier(rawSearchPhrase); - if (decodedIdentifier) { - const isCIP129Identifier = decodedIdentifier.txID.startsWith("22"); - if (isCIP129Identifier) { + const searchPhraseProcessor = async () => { + try { + if (rawSearchPhrase.startsWith("drep_script")) { + const { words } = bech32.decode(rawSearchPhrase); + return bech32.encode("drep", words); + } + if (rawSearchPhrase.startsWith("drep")) { + const decodedIdentifier = decodeCIP129Identifier(rawSearchPhrase); + if (decodedIdentifier) { + const isCIP129Identifier = decodedIdentifier.txID.startsWith("22"); + if (isCIP129Identifier) { + return encodeCIP129Identifier({ + txID: decodedIdentifier.txID.slice(2), + bech32Prefix: "drep", + }); + } return encodeCIP129Identifier({ - txID: decodedIdentifier.txID.slice(2), + txID: decodedIdentifier.txID, bech32Prefix: "drep", }); } - return encodeCIP129Identifier({ - txID: decodedIdentifier.txID, - bech32Prefix: "drep", - }); } + return rawSearchPhrase; + } catch (e) { + return rawSearchPhrase; } - return rawSearchPhrase; - })(); + }; + + const searchPhrase = await searchPhraseProcessor(); const response = await API.get>("/drep/list", { params: {