Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#689] add tx hash to the ada-holder/get-current-delegation #697

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ changes.

### Added

- added drepView and txHash to the `ada-holder/get-current-delegation` [Issue 689](https://github.com/IntersectMBO/govtool/issues/689)
- addded latestTxHash to the `drep/info` and `drep/list` endpoints [Issue 627](https://github.com/IntersectMBO/govtool/issues/627)
- added `txHash` to `drep/getVotes` [Issue 626](https://github.com/IntersectMBO/govtool/issues/626)
- added `references` to all proposal related endpoints
Expand Down
7 changes: 5 additions & 2 deletions govtool/backend/sql/get-current-delegation.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
select
case
when drep_hash.raw is NULL then drep_hash.view
when drep_hash.raw is NULL then NULL
else encode(drep_hash.raw,'hex')
end
end as drep_raw,
drep_hash.view as drep_view,
encode(tx.hash, 'hex')
from delegation_vote
join tx on tx.id = delegation_vote.tx_id
join drep_hash
on drep_hash.id = delegation_vote.drep_hash_id
join stake_address
Expand Down
16 changes: 12 additions & 4 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type VVAApi =
:<|> "drep" :> "get-voting-power" :> Capture "drepId" HexText :> Get '[JSON] Integer
:<|> "drep" :> "getVotes" :> Capture "drepId" HexText :> QueryParams "type" GovernanceActionType :> QueryParam "sort" GovernanceActionSortMode :> Get '[JSON] [VoteResponse]
:<|> "drep" :> "info" :> Capture "drepId" HexText :> Get '[JSON] DRepInfoResponse
:<|> "ada-holder" :> "get-current-delegation" :> Capture "stakeKey" HexText :> Get '[JSON] (Maybe HexText)
:<|> "ada-holder" :> "get-current-delegation" :> Capture "stakeKey" HexText :> Get '[JSON] (Maybe DelegationResponse)
:<|> "ada-holder" :> "get-voting-power" :> Capture "stakeKey" HexText :> Get '[JSON] Integer
:<|> "proposal" :> "list"
:> QueryParams "type" GovernanceActionType
Expand Down Expand Up @@ -101,6 +101,14 @@ drepRegistrationToDrep Types.DRepRegistration {..} =
dRepLatestTxHash = HexText <$> dRepRegistrationLatestTxHash
}

delegationToResponse :: Types.Delegation -> DelegationResponse
delegationToResponse Types.Delegation {..} =
DelegationResponse
{ delegationResponseDRepHash = HexText <$> delegationDRepHash,
delegationResponseDRepView = delegationDRepView,
delegationResponseTxHash = HexText delegationTxHash
}

drepList :: App m => Maybe Text -> m [DRep]
drepList mDRepView = do
CacheEnv {dRepListCache} <- asks vvaCache
Expand Down Expand Up @@ -214,11 +222,11 @@ drepInfo (unHexText -> dRepId) = do
, dRepInfoResponseLatestTxHash = HexText <$> dRepInfoLatestTxHash
}

getCurrentDelegation :: App m => HexText -> m (Maybe HexText)
getCurrentDelegation :: App m => HexText -> m (Maybe DelegationResponse)
getCurrentDelegation (unHexText -> stakeKey) = do
CacheEnv {adaHolderGetCurrentDelegationCache} <- asks vvaCache
result <- cacheRequest adaHolderGetCurrentDelegationCache stakeKey $ AdaHolder.getCurrentDelegation stakeKey
return $ HexText <$> result
delegation <- cacheRequest adaHolderGetCurrentDelegationCache stakeKey $ AdaHolder.getCurrentDelegation stakeKey
return $ delegationToResponse <$> delegation

getStakeKeyVotingPower :: App m => HexText -> m Integer
getStakeKeyVotingPower (unHexText -> stakeKey) = do
Expand Down
17 changes: 17 additions & 0 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,25 @@ instance ToSchema DRep where
& example
?~ toJSON exampleDrep

data DelegationResponse
= DelegationResponse
{ delegationResponseDRepHash :: Maybe HexText
, delegationResponseDRepView :: Text
, delegationResponseTxHash :: HexText
}
deriveJSON (jsonOptions "delegationResponse") ''DelegationResponse

exampleDelegationResponse :: Text
exampleDelegationResponse = "{\"drepHash\": \"b4e4184bfedf920fec53cdc327de4da661ae427784c0ccca9e3c2f50\","
<> "\"drepView\": \"drep1l8uyy66sm8u82h82gc8hkcy2xu24dl8ffsh58aa0v7d37yp48u8\","
<> "\"txHash\": \"47c14a128cd024f1b990c839d67720825921ad87ed875def42641ddd2169b39c\"}"

instance ToSchema DelegationResponse where
declareNamedSchema _ = pure $ NamedSchema (Just "DelegationResponse") $ mempty
& type_ ?~ OpenApiObject
& description ?~ "Delegation Response"
& example
?~ toJSON exampleDelegationResponse

data GetNetworkMetricsResponse
= GetNetworkMetricsResponse
Expand Down
5 changes: 3 additions & 2 deletions govtool/backend/src/VVA/AdaHolder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import qualified Database.PostgreSQL.Simple as SQL

import VVA.Config
import VVA.Pool (ConnectionPool, withPool)
import VVA.Types

sqlFrom :: ByteString -> SQL.Query
sqlFrom bs = fromString $ unpack $ Text.decodeUtf8 bs
Expand All @@ -37,12 +38,12 @@ getCurrentDelegationSql = sqlFrom $(embedFile "sql/get-current-delegation.sql")
getCurrentDelegation ::
(Has ConnectionPool r, Has VVAConfig r, MonadReader r m, MonadIO m) =>
Text ->
m (Maybe Text)
m (Maybe Delegation)
getCurrentDelegation stakeKey = withPool $ \conn -> do
result <- liftIO $ SQL.query conn getCurrentDelegationSql (SQL.Only stakeKey)
case result of
[] -> return Nothing
[SQL.Only delegation] -> return $ Just delegation
[(mDRepHash, dRepView, txHash)] -> return $ Just $ Delegation mDRepHash dRepView txHash
_ -> error ("multiple delegations for stake key: " <> unpack stakeKey)

getVotingPowerSql :: SQL.Query
Expand Down
9 changes: 8 additions & 1 deletion govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ data CacheEnv
, getProposalCache :: Cache.Cache (Text, Integer) Proposal
, currentEpochCache :: Cache.Cache () (Maybe Value)
, adaHolderVotingPowerCache :: Cache.Cache Text Integer
, adaHolderGetCurrentDelegationCache :: Cache.Cache Text (Maybe Text)
, adaHolderGetCurrentDelegationCache :: Cache.Cache Text (Maybe Delegation)
, dRepGetVotesCache :: Cache.Cache Text ([Vote], [Proposal])
, dRepInfoCache :: Cache.Cache Text DRepInfo
, dRepVotingPowerCache :: Cache.Cache Text Integer
Expand All @@ -149,3 +149,10 @@ data NetworkMetrics
, networkMetricsAlwaysAbstainVotingPower :: Integer
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
}

data Delegation
= Delegation
{ delegationDRepHash :: Maybe Text
, delegationDRepView :: Text
, delegationTxHash :: Text
}
Loading