From 90f1e02ab7411d2da637564e2fd7c83beece44ca Mon Sep 17 00:00:00 2001 From: jankun4 Date: Mon, 13 May 2024 10:36:03 +0200 Subject: [PATCH] [#957] drep/list drep type fix --- CHANGELOG.md | 1 + govtool/backend/sql/list-dreps.sql | 32 +++++++++++++++++++++++++++--- govtool/backend/src/VVA/DRep.hs | 9 ++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e637a8779..a94b2f2e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ changes. ### Fixed +- drep/list sql fix (now the drep type is correct) [Issue 957](https://github.com/IntersectMBO/govtool/issues/957) - drep/list sql fix (now the latest tx date is correct) [Issue 826](https://github.com/IntersectMBO/govtool/issues/826) - drep/info no longer returns null values [Issue 720](https://github.com/IntersectMBO/govtool/issues/720) - drep/getVotes no longer returns 500 [Issue 685](https://github.com/IntersectMBO/govtool/issues/685) diff --git a/govtool/backend/sql/list-dreps.sql b/govtool/backend/sql/list-dreps.sql index 74def83c8..399b50874 100644 --- a/govtool/backend/sql/list-dreps.sql +++ b/govtool/backend/sql/list-dreps.sql @@ -26,9 +26,10 @@ SELECT dr_deposit.deposit, DRepDistr.amount, (DRepActivity.epoch_no - Max(coalesce(block.epoch_no, block_first_register.epoch_no))) <= DRepActivity.drep_activity AS active, - second_to_newest_drep_registration.voting_anchor_id IS NOT NULL AS has_voting_anchor, encode(dr_voting_anchor.tx_hash, 'hex') AS tx_hash, - newestRegister.time AS last_register_time + newestRegister.time AS last_register_time, + COALESCE(latestDeposit.deposit, 0), + non_deregister_voting_anchor.url IS NOT NULL AS has_non_deregister_voting_anchor FROM drep_hash dh JOIN ( @@ -42,6 +43,15 @@ FROM WHERE dr.deposit IS NOT NULL) AS dr_deposit ON dr_deposit.drep_hash_id = dh.id AND dr_deposit.rn = 1 + JOIN ( + SELECT + dr.id, + dr.drep_hash_id, + dr.deposit, + ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn + FROM + drep_registration dr) AS latestDeposit ON latestDeposit.drep_hash_id = dh.id + AND latestDeposit.rn = 1 LEFT JOIN ( SELECT dr.id, @@ -53,6 +63,19 @@ FROM drep_registration dr JOIN tx ON tx.id = dr.tx_id) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id AND dr_voting_anchor.rn = 1 + LEFT JOIN ( + SELECT + dr.id, + dr.drep_hash_id, + dr.voting_anchor_id, + ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn, + tx.hash AS tx_hash + FROM + drep_registration dr + JOIN tx ON tx.id = dr.tx_id + WHERE dr.deposit is not null + AND dr.deposit >= 0) AS dr_non_deregister_voting_anchor ON dr_non_deregister_voting_anchor.drep_hash_id = dh.id + AND dr_non_deregister_voting_anchor.rn = 1 LEFT JOIN ( SELECT dr.id, @@ -65,6 +88,7 @@ FROM LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id AND DRepDistr.rn = 1 LEFT JOIN voting_anchor va ON va.id = dr_voting_anchor.voting_anchor_id + LEFT JOIN voting_anchor non_deregister_voting_anchor on non_deregister_voting_anchor.id = dr_non_deregister_voting_anchor.voting_anchor_id CROSS JOIN DRepActivity LEFT JOIN voting_procedure AS voting_procedure ON voting_procedure.drep_voter = dh.id LEFT JOIN tx AS tx ON tx.id = voting_procedure.tx_id @@ -102,4 +126,6 @@ GROUP BY DRepActivity.epoch_no, DRepActivity.drep_activity, dr_voting_anchor.tx_hash, - newestRegister.time + newestRegister.time, + latestDeposit.deposit, + non_deregister_voting_anchor.url diff --git a/govtool/backend/src/VVA/DRep.hs b/govtool/backend/src/VVA/DRep.hs index d5db8d115..347527337 100644 --- a/govtool/backend/src/VVA/DRep.hs +++ b/govtool/backend/src/VVA/DRep.hs @@ -62,13 +62,16 @@ listDReps = withPool $ \conn -> do timeZone <- liftIO getCurrentTimeZone return [ DRepRegistration drepHash drepView url dataHash (floor @Scientific deposit) votingPower status drepType txHash (localTimeToUTC timeZone date) - | (drepHash, drepView, url, dataHash, deposit, votingPower, isActive, wasDRep, txHash, date) <- results + | (drepHash, drepView, url, dataHash, deposit, votingPower, isActive, txHash, date, latestDeposit, latestNonDeregisterVotingAnchorWasNotNull) <- results , let status = case (isActive, deposit) of (_, d) | d < 0 -> Retired (isActive, d) | d >= 0 && isActive -> Active | d >= 0 && not isActive -> Inactive - , let drepType | isNothing url && wasDRep = DRep - | isNothing url && not wasDRep = SoleVoter + , let latestDeposit' = floor @Scientific latestDeposit :: Integer + , let drepType | latestDeposit' >= 0 && isNothing url = SoleVoter + | latestDeposit' >= 0 && not (isNothing url) = DRep + | latestDeposit' < 0 && not latestNonDeregisterVotingAnchorWasNotNull = SoleVoter + | latestDeposit' < 0 && latestNonDeregisterVotingAnchorWasNotNull = DRep | Data.Maybe.isJust url = DRep ]