From 410cae699cff294f4fed26de9204632389a45a4b Mon Sep 17 00:00:00 2001 From: jankun4 Date: Sun, 7 Apr 2024 19:29:47 +0200 Subject: [PATCH] [#627] add latestTxHash to drep/list and drep/info endpoints --- CHANGELOG.md | 2 +- govtool/backend/sql/get-drep-info.sql | 5 ++++- govtool/backend/sql/list-dreps.sql | 9 ++++++--- govtool/backend/src/VVA/API.hs | 4 +++- govtool/backend/src/VVA/API/Types.hs | 22 +++++++++++++--------- govtool/backend/src/VVA/DRep.hs | 8 +++++--- govtool/backend/src/VVA/Types.hs | 18 ++++++++++-------- 7 files changed, 42 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb3c92587..35d0ff68c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ changes. ## [Unreleased] ### Added - +- addded latestTxHash to the `drep/info` and `drep/list` endpoints [Issue 627](https://github.com/IntersectMBO/govtool/issues/627) - added `epochNo` and `date` to `drep/getVotes` and `proposal/get` - Added `isRegisteredAsSoleVoter` and `wasRegisteredAsSoleVoter` fields to the drep/info response [Issue 212](https://github.com/IntersectMBO/govtool/issues/212) - Abandoning registration as DRep [Issue 151](https://github.com/IntersectMBO/govtool/issues/151) diff --git a/govtool/backend/sql/get-drep-info.sql b/govtool/backend/sql/get-drep-info.sql index cb50e0642..a7b4536a5 100644 --- a/govtool/backend/sql/get-drep-info.sql +++ b/govtool/backend/sql/get-drep-info.sql @@ -6,10 +6,12 @@ LatestRegistrationEntry AS ( SELECT drep_registration.voting_anchor_id AS voting_anchor_id, deposit AS deposit + tx.hash as tx_hash FROM drep_registration CROSS JOIN DrepId JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id + JOIN tx ON tx.id = drep_registration.tx_id WHERE drep_hash.raw = DRepId.raw ORDER BY @@ -95,7 +97,8 @@ SELECT CurrentDeposit.value, CurrentMetadata.url, CurrentMetadata.data_hash, - CurrentVotingPower.amount + CurrentVotingPower.amount, + LatestRegistrationEntry.tx_hash FROM IsRegisteredAsDRep CROSS JOIN IsRegisteredAsSoleVoter diff --git a/govtool/backend/sql/list-dreps.sql b/govtool/backend/sql/list-dreps.sql index 931ae6430..bb2f8fd08 100644 --- a/govtool/backend/sql/list-dreps.sql +++ b/govtool/backend/sql/list-dreps.sql @@ -23,7 +23,8 @@ 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 + 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 FROM drep_hash dh JOIN ( SELECT dr.id, dr.drep_hash_id, dr.deposit, @@ -34,8 +35,10 @@ JOIN ( on dr_deposit.drep_hash_id = dh.id and dr_deposit.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 + 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 ) as dr_voting_anchor on dr_voting_anchor.drep_hash_id = dh.id and dr_voting_anchor.rn = 1 LEFT JOIN ( @@ -65,4 +68,4 @@ on tx_first_register.id = dr_first_register.tx_id JOIN block as block_first_register ON block_first_register.id = tx_first_register.block_id -GROUP BY dh.raw, second_to_newest_drep_registration.voting_anchor_id, dh.view, va.url, va.data_hash, dr_deposit.deposit, DRepDistr.amount, DRepActivity.epoch_no, DRepActivity.drep_activity +GROUP BY dh.raw, second_to_newest_drep_registration.voting_anchor_id, dh.view, va.url, va.data_hash, dr_deposit.deposit, DRepDistr.amount, DRepActivity.epoch_no, DRepActivity.drep_activity, dr_voting_anchor.tx_hash diff --git a/govtool/backend/src/VVA/API.hs b/govtool/backend/src/VVA/API.hs index 293e0c2bd..08b067dd2 100644 --- a/govtool/backend/src/VVA/API.hs +++ b/govtool/backend/src/VVA/API.hs @@ -97,7 +97,8 @@ drepRegistrationToDrep Types.DRepRegistration {..} = dRepDeposit = dRepRegistrationDeposit, dRepVotingPower = dRepRegistrationVotingPower, dRepStatus = mapDRepStatus dRepRegistrationStatus, - dRepType = mapDRepType dRepRegistrationType + dRepType = mapDRepType dRepRegistrationType, + dRepLatestTxHash = HexText <$> dRepRegistrationLatestTxHash } drepList :: App m => Maybe Text -> m [DRep] @@ -208,6 +209,7 @@ drepInfo (unHexText -> dRepId) = do , dRepInfoResponseUrl = dRepInfoUrl , dRepInfoResponseDataHash = HexText <$> dRepInfoDataHash , dRepInfoResponseVotingPower = dRepInfoVotingPower + , dRepInfoResponseLatestTxHash = HexText <$> dRepInfoLatestTxHash } getCurrentDelegation :: App m => HexText -> m (Maybe HexText) diff --git a/govtool/backend/src/VVA/API/Types.hs b/govtool/backend/src/VVA/API/Types.hs index a591305b6..8c1cb49d4 100644 --- a/govtool/backend/src/VVA/API/Types.hs +++ b/govtool/backend/src/VVA/API/Types.hs @@ -441,6 +441,7 @@ data DRepInfoResponse , dRepInfoResponseUrl :: Maybe Text , dRepInfoResponseDataHash :: Maybe HexText , dRepInfoResponseVotingPower :: Maybe Integer + , dRepInfoResponseLatestTxHash :: Maybe HexText } deriving (Generic, Show) @@ -455,7 +456,8 @@ exampleDRepInfoResponse = <> "\"deposit\": 2000000," <> "\"url\": \"https://drep.metadata.xyz\"," <> "\"dataHash\": \"9af10e89979e51b8cdc827c963124a1ef4920d1253eef34a1d5cfe76438e3f11\"," - <> "\"votingPower\": 1000000}" + <> "\"votingPower\": 1000000," + <> "\"latestTxHash\": \"47c14a128cd024f1b990c839d67720825921ad87ed875def42641ddd2169b39c\"}" instance ToSchema DRepInfoResponse where declareNamedSchema proxy = do @@ -632,6 +634,7 @@ data DRep , dRepVotingPower :: Maybe Integer , dRepStatus :: DRepStatus , dRepType :: DRepType + , dRepLatestTxHash :: Maybe HexText } deriving (Generic, Show) @@ -640,14 +643,15 @@ deriveJSON (jsonOptions "dRep") ''DRep exampleDrep :: Text exampleDrep = - "{\"drepId\": \"d3a62ffe9c214e1a6a9809f7ab2a104c117f85e1f171f8f839d94be5\"," - <> "\"view\": \"drep1l8uyy66sm8u82h82gc8hkcy2xu24dl8ffsh58aa0v7d37yp48u8\"," - <> "\"url\": \"https://proposal.metadata.xyz\"," - <> "\"metadataHash\": \"9af10e89979e51b8cdc827c963124a1ef4920d1253eef34a1d5cfe76438e3f11\"," - <> "\"deposit\": 0," - <> "\"votingPower\": 0," - <> "\"status\": \"Active\"," - <> "\"type\": \"DRep\"}" + "{\"drepId\": \"d3a62ffe9c214e1a6a9809f7ab2a104c117f85e1f171f8f839d94be5\"," + <> "\"view\": \"drep1l8uyy66sm8u82h82gc8hkcy2xu24dl8ffsh58aa0v7d37yp48u8\"," + <> "\"url\": \"https://proposal.metadata.xyz\"," + <> "\"metadataHash\": \"9af10e89979e51b8cdc827c963124a1ef4920d1253eef34a1d5cfe76438e3f11\"," + <> "\"deposit\": 0," + <> "\"votingPower\": 0," + <> "\"status\": \"Active\"," + <> "\"type\": \"DRep\"," + <> "\"latestTxHash\": \"47c14a128cd024f1b990c839d67720825921ad87ed875def42641ddd2169b39c\"}" -- ToSchema instance for DRep instance ToSchema DRep where diff --git a/govtool/backend/src/VVA/DRep.hs b/govtool/backend/src/VVA/DRep.hs index eede66a81..9bc5c9e03 100644 --- a/govtool/backend/src/VVA/DRep.hs +++ b/govtool/backend/src/VVA/DRep.hs @@ -60,8 +60,8 @@ listDReps :: listDReps = withPool $ \conn -> do results <- liftIO $ SQL.query_ conn listDRepsSql return - [ DRepRegistration drepHash drepView url dataHash (floor @Scientific deposit) votingPower status drepType - | (drepHash, drepView, url, dataHash, deposit, votingPower, isActive, wasDRep) <- results + [ DRepRegistration drepHash drepView url dataHash (floor @Scientific deposit) votingPower status drepType txHash + | (drepHash, drepView, url, dataHash, deposit, votingPower, isActive, wasDRep, txHash) <- results , let status = case (isActive, deposit) of (_, d) | d < 0 -> Retired (isActive, d) | d >= 0 && isActive -> Active @@ -117,6 +117,7 @@ getDRepInfo drepId = withPool $ \conn -> do , url , dataHash , votingPower + , txHash )] -> return $ DRepInfo { dRepInfoIsRegisteredAsDRep = fromMaybe False isRegisteredAsDRep @@ -127,5 +128,6 @@ getDRepInfo drepId = withPool $ \conn -> do , dRepInfoUrl = url , dRepInfoDataHash = dataHash , dRepInfoVotingPower = votingPower + , dRepInfoLatestTxHash = Just txHash } - [] -> return $ DRepInfo False False False False Nothing Nothing Nothing Nothing + [] -> return $ DRepInfo False False False False Nothing Nothing Nothing Nothing Nothing diff --git a/govtool/backend/src/VVA/Types.hs b/govtool/backend/src/VVA/Types.hs index 15ac3595f..cc2f74f17 100644 --- a/govtool/backend/src/VVA/Types.hs +++ b/govtool/backend/src/VVA/Types.hs @@ -74,6 +74,7 @@ data DRepInfo , dRepInfoUrl :: Maybe Text , dRepInfoDataHash :: Maybe Text , dRepInfoVotingPower :: Maybe Integer + , dRepInfoLatestTxHash :: Maybe Text } data DRepStatus = Retired | Active | Inactive @@ -82,14 +83,15 @@ data DRepType = DRep | SoleVoter data DRepRegistration = DRepRegistration - { dRepRegistrationDRepHash :: Text - , dRepRegistrationView :: Text - , dRepRegistrationUrl :: Maybe Text - , dRepRegistrationDataHash :: Maybe Text - , dRepRegistrationDeposit :: Integer - , dRepRegistrationVotingPower :: Maybe Integer - , dRepRegistrationStatus :: DRepStatus - , dRepRegistrationType :: DRepType + { dRepRegistrationDRepHash :: Text + , dRepRegistrationView :: Text + , dRepRegistrationUrl :: Maybe Text + , dRepRegistrationDataHash :: Maybe Text + , dRepRegistrationDeposit :: Integer + , dRepRegistrationVotingPower :: Maybe Integer + , dRepRegistrationStatus :: DRepStatus + , dRepRegistrationType :: DRepType + , dRepRegistrationLatestTxHash :: Maybe Text } data Proposal