11{-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE GADTs #-}
4+ {-# LANGUAGE NamedFieldPuns #-}
35{-# LANGUAGE OverloadedStrings #-}
6+ {-# LANGUAGE RankNTypes #-}
47{-# LANGUAGE RecordWildCards #-}
58{-# LANGUAGE ScopedTypeVariables #-}
9+ {-# LANGUAGE TypeFamilies #-}
10+ {-# LANGUAGE UndecidableInstances #-}
611
712module Cardano.Ledger.Api.State.Query (
813 -- * @GetFilteredDelegationsAndRewardAccounts@
@@ -58,10 +63,16 @@ module Cardano.Ledger.Api.State.Query (
5863 -- * @GetRatifyState@
5964 queryRatifyState ,
6065
61- -- * @GetStakePoolDefaultVote
66+ -- * @GetStakePoolDefaultVote@
6267 queryStakePoolDefaultVote ,
6368 DefaultVote (.. ),
6469
70+ -- * @GetPoolState@
71+ queryPoolParameters ,
72+ queryPoolState ,
73+ QueryPoolStateResult (.. ),
74+ mkQueryPoolStateResult ,
75+
6576 -- * For testing
6677 getNextEpochCommitteeMembers ,
6778) where
@@ -74,6 +85,7 @@ import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
7485 NextEpochChange (.. ),
7586 )
7687import Cardano.Ledger.BaseTypes (EpochNo , strictMaybeToMaybe )
88+ import Cardano.Ledger.Binary
7789import Cardano.Ledger.Coin (Coin (.. ), CompactForm (.. ))
7890import Cardano.Ledger.Compactible (fromCompact )
7991import Cardano.Ledger.Conway.Governance (
@@ -426,3 +438,61 @@ queryStakePoolDefaultVote ::
426438queryStakePoolDefaultVote nes poolId =
427439 defaultStakePoolVote poolId (nes ^. nesEsL . epochStateStakePoolsL) $
428440 nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
441+
442+ -- | Used only for the `queryPoolState` query. This resembles the older way of
443+ -- representing StakePoolState in Ledger.
444+ data QueryPoolStateResult = QueryPoolStateResult
445+ { qpsrStakePoolParams :: ! (Map (KeyHash 'StakePool) PoolParams )
446+ , qpsrFutureStakePoolParams :: ! (Map (KeyHash 'StakePool) PoolParams )
447+ , qpsrRetiring :: ! (Map (KeyHash 'StakePool) EpochNo )
448+ , qpsrDeposits :: ! (Map (KeyHash 'StakePool) Coin )
449+ }
450+ deriving (Show , Eq )
451+
452+ instance EncCBOR QueryPoolStateResult where
453+ encCBOR (QueryPoolStateResult a b c d) =
454+ encodeListLen 4 <> encCBOR a <> encCBOR b <> encCBOR c <> encCBOR d
455+
456+ instance DecCBOR QueryPoolStateResult where
457+ decCBOR = decodeRecordNamed " QueryPoolStateResult" (const 4 ) $ do
458+ qpsrStakePoolParams <- decCBOR
459+ qpsrFutureStakePoolParams <- decCBOR
460+ qpsrRetiring <- decCBOR
461+ qpsrDeposits <- decCBOR
462+ pure
463+ QueryPoolStateResult {qpsrStakePoolParams, qpsrFutureStakePoolParams, qpsrRetiring, qpsrDeposits}
464+
465+ mkQueryPoolStateResult ::
466+ (forall x . Map. Map (KeyHash 'StakePool) x -> Map. Map (KeyHash 'StakePool) x ) ->
467+ PState era ->
468+ QueryPoolStateResult
469+ mkQueryPoolStateResult f ps =
470+ QueryPoolStateResult
471+ { qpsrStakePoolParams = Map. mapWithKey stakePoolStateToPoolParams restrictedStakePools
472+ , qpsrFutureStakePoolParams = Map. mapWithKey stakePoolStateToPoolParams restrictedStakePools
473+ , qpsrRetiring = f $ psRetiring ps
474+ , qpsrDeposits = Map. map (fromCompact . spsDeposit) restrictedStakePools
475+ }
476+ where
477+ restrictedStakePools = f $ psStakePools ps
478+
479+ -- | Query the QueryPoolStateResult. This is slightly different from the internal
480+ -- representation used by Ledger and is intended to resemble how the internal
481+ -- representation used to be.
482+ queryPoolState ::
483+ EraCertState era => NewEpochState era -> Maybe (Set (KeyHash 'StakePool)) -> QueryPoolStateResult
484+ queryPoolState nes mPoolKeys =
485+ let pstate = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL
486+ f :: forall x . Map. Map (KeyHash 'StakePool) x -> Map. Map (KeyHash 'StakePool) x
487+ f = case mPoolKeys of
488+ Nothing -> id
489+ Just keys -> (`Map.restrictKeys` keys)
490+ in mkQueryPoolStateResult f pstate
491+
492+ -- | Query the current PoolParams.
493+ queryPoolParameters ::
494+ EraCertState era =>
495+ NewEpochState era -> Set (KeyHash 'StakePool) -> Map (KeyHash 'StakePool) PoolParams
496+ queryPoolParameters nes poolKeys =
497+ let pools = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
498+ in Map. mapWithKey stakePoolStateToPoolParams $ Map. restrictKeys pools poolKeys
0 commit comments