Skip to content

Commit

Permalink
[#212] provide sole voter information in drep/info
Browse files Browse the repository at this point in the history
Extend the drep/info endpoint by adding isRegisteredAsSoleVoter and wasRegisteredAsSoleVoter fields
  • Loading branch information
jankun4 authored and MSzalowski committed Feb 28, 2024
1 parent 0e1216b commit faab2e3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
44 changes: 39 additions & 5 deletions govtool/backend/sql/get-drep-info.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
WITH DRepId AS (
SELECT decode(?, 'hex') as raw
), IsRegistered AS (
), IsRegisteredAsDRep AS (
SELECT (deposit>0) as value,
deposit as deposit
FROM drep_registration
Expand All @@ -9,9 +9,10 @@ WITH DRepId AS (
CROSS JOIN DRepId
WHERE drep_hash.raw = DRepId.raw
and deposit is not null
and drep_registration.voting_anchor_id is not null
ORDER BY drep_registration.tx_id DESC
LIMIT 1
), WasRegistered AS (
), WasRegisteredAsDRep AS (
select (EXISTS (
SELECT *
FROM drep_registration
Expand All @@ -20,9 +21,42 @@ WITH DRepId AS (
CROSS JOIN DRepId
WHERE drep_hash.raw = DRepId.raw
and drep_registration.deposit > 0
and drep_registration.voting_anchor_id is not null
)) as value
), IsRegisteredAsSoleVoter AS (
SELECT (deposit>0) as value,
deposit as deposit
FROM drep_registration
JOIN drep_hash
ON drep_hash.id = drep_registration.drep_hash_id
CROSS JOIN DRepId
WHERE drep_hash.raw = DRepId.raw
and deposit is not null
and drep_registration.voting_anchor_id is null
ORDER BY drep_registration.tx_id DESC
LIMIT 1
), WasRegisteredAsSoleVoter AS (
select (EXISTS (
SELECT *
FROM drep_registration
JOIN drep_hash
ON drep_hash.id = drep_registration.drep_hash_id
CROSS JOIN DRepId
WHERE drep_hash.raw = DRepId.raw
and drep_registration.deposit > 0
and drep_registration.voting_anchor_id is null
)) as value
)
SELECT IsRegistered.value, WasRegistered.value, IsRegistered.deposit
FROM WasRegistered
LEFT JOIN IsRegistered
SELECT
IsRegisteredAsDrep.value,
WasRegisteredAsDRep.value,
IsRegisteredAsSoleVoter.value,
WasRegisteredAsSoleVoter.value,
coalesce(IsRegisteredAsDRep.deposit, IsRegisteredAsSoleVoter.deposit)
FROM WasRegisteredAsDRep
LEFT JOIN IsRegisteredAsDRep
ON 1=1
LEFT JOIN WasRegisteredAsSoleVoter
ON 1=1
LEFT JOIN IsRegisteredAsSoleVoter
ON 1=1
6 changes: 4 additions & 2 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ drepInfo (unHexText -> dRepId) = do
CacheEnv {dRepInfoCache} <- asks vvaCache
Types.DRepInfo {..} <- cacheRequest dRepInfoCache dRepId $ DRep.getDRepInfo dRepId
return $ DRepInfoResponse
{ dRepInfoResponseIsRegistered = dRepInfoIsRegistered
, dRepInfoResponseWasRegistered = dRepInfoWasRegistered
{ dRepInfoResponseIsRegisteredAsDRep = dRepInfoIsRegisteredAsDRep
, dRepInfoResponseWasRegisteredAsDRep = dRepInfoWasRegisteredAsDRep
, dRepInfoResponseIsRegisteredAsSoleVoter = dRepInfoIsRegisteredAsSoleVoter
, dRepInfoResponseWasRegisteredAsSoleVoter = dRepInfoWasRegisteredAsSoleVoter
, dRepInfoResponseDeposit = dRepInfoDeposit
}

Expand Down
12 changes: 8 additions & 4 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,21 @@ instance ToSchema VoteResponse where
?~ toJSON exampleVoteResponse

data DRepInfoResponse = DRepInfoResponse
{ dRepInfoResponseIsRegistered :: Bool
, dRepInfoResponseWasRegistered :: Bool
{ dRepInfoResponseIsRegisteredAsDRep :: Bool
, dRepInfoResponseWasRegisteredAsDRep :: Bool
, dRepInfoResponseIsRegisteredAsSoleVoter :: Bool
, dRepInfoResponseWasRegisteredAsSoleVoter :: Bool
, dRepInfoResponseDeposit :: Maybe Integer
} deriving (Generic, Show)

deriveJSON (jsonOptions "dRepInfoResponse") ''DRepInfoResponse

exampleDRepInfoResponse :: Text
exampleDRepInfoResponse =
"{\"isRegistered\": false,"
<> "\"wasRegistered\": true,"
"{\"isRegisteredAsDRep\": false,"
<> "\"wasRegisteredAsDRep\": true,"
<> "\"wasRegisteredAsSoleVoter\": true,"
<> "\"wasRegisteredAsSoleVoter\": true,"
<> "\"deposit\": 2000000}"

instance ToSchema DRepInfoResponse where
Expand Down
10 changes: 6 additions & 4 deletions govtool/backend/src/VVA/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ getDRepInfo
getDRepInfo drepId = withPool $ \conn -> do
result <- liftIO $ SQL.query conn getDRepInfoSql (SQL.Only drepId)
case result of
[(isRegistered, wasRegistered, deposit)] ->
[(isRegisteredAsDRep, wasRegisteredAsDRep, isRegisteredAsSoleVoter, wasRegisteredAsSoleVoter, deposit)] ->
return $ DRepInfo
{ dRepInfoIsRegistered = fromMaybe False isRegistered
, dRepInfoWasRegistered = fromMaybe False wasRegistered
{ dRepInfoIsRegisteredAsDRep = fromMaybe False isRegisteredAsDRep
, dRepInfoWasRegisteredAsDRep = fromMaybe False wasRegisteredAsDRep
, dRepInfoIsRegisteredAsSoleVoter = fromMaybe False isRegisteredAsSoleVoter
, dRepInfoWasRegisteredAsSoleVoter = fromMaybe False wasRegisteredAsSoleVoter
, dRepInfoDeposit = deposit
}
[] -> return $ DRepInfo False False Nothing
[] -> return $ DRepInfo False False False False Nothing
6 changes: 4 additions & 2 deletions govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ data Vote = Vote
}

data DRepInfo = DRepInfo
{ dRepInfoIsRegistered :: Bool
, dRepInfoWasRegistered :: Bool
{ dRepInfoIsRegisteredAsDRep :: Bool
, dRepInfoWasRegisteredAsDRep :: Bool
, dRepInfoIsRegisteredAsSoleVoter :: Bool
, dRepInfoWasRegisteredAsSoleVoter :: Bool
, dRepInfoDeposit :: Maybe Integer
}

Expand Down

0 comments on commit faab2e3

Please sign in to comment.