{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Ledger.Api.State.Query (
  -- * @GetFilteredDelegationsAndRewardAccounts@
  queryStakePoolDelegsAndRewards,

  -- * @GetGovState@
  queryGovState,

  -- * @GetConstitution@
  queryConstitution,

  -- * @GetConstitutionHash@
  queryConstitutionHash,

  -- * @GetDRepState@
  queryDRepState,

  -- * @GetDRepDelegations@
  queryDRepDelegations,

  -- * @GetDRepStakeDistr@
  queryDRepStakeDistr,

  -- * @GetRegisteredDRepStakeDistr@
  queryRegisteredDRepStakeDistr,

  -- * @GetSPOStakeDistr@
  querySPOStakeDistr,

  -- * @GetCommitteeMembersState@
  queryCommitteeMembersState,

  -- * @GetChainAccountState@
  queryChainAccountState,
  CommitteeMemberState (..),
  CommitteeMembersState (..),
  HotCredAuthStatus (..),
  MemberStatus (..),
  NextEpochChange (..),

  -- * @GetCurrentPParams@
  queryCurrentPParams,

  -- * @GetFuturePParams@
  queryFuturePParams,

  -- * @GetProposals@
  queryProposals,

  -- * @GetRatifyState@
  queryRatifyState,

  -- * @GetStakePoolDefaultVote@
  queryStakePoolDefaultVote,
  DefaultVote (..),

  -- * @GetPoolState@
  queryPoolParameters,
  queryPoolState,
  QueryPoolStateResult (..),
  mkQueryPoolStateResult,

  -- * For testing
  getNextEpochCommitteeMembers,
) where

import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
  CommitteeMemberState (..),
  CommitteeMembersState (..),
  HotCredAuthStatus (..),
  MemberStatus (..),
  NextEpochChange (..),
 )
import Cardano.Ledger.BaseTypes (EpochNo, Network, strictMaybeToMaybe)
import Cardano.Ledger.Binary
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
import Cardano.Ledger.Compactible (fromCompact)
import Cardano.Ledger.Conway.Governance (
  Committee (committeeMembers),
  Constitution (constitutionAnchor),
  ConwayEraGov (..),
  DRepPulser (..),
  DRepPulsingState (..),
  DefaultVote (..),
  GovActionId,
  GovActionState (..),
  PulsingSnapshot,
  RatifyState,
  committeeThresholdL,
  defaultStakePoolVote,
  ensCommitteeL,
  finishDRepPulser,
  proposalsDeposits,
  psDRepDistr,
  psPoolDistr,
  psProposalsL,
  rsEnactStateL,
 )
import Cardano.Ledger.Conway.Rules (updateDormantDRepExpiry)
import Cardano.Ledger.Conway.State
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.DRep (credToDRep, dRepToCred)
import Cardano.Ledger.Shelley.LedgerState
import Control.Monad (guard)
import Data.Foldable (foldMap')
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, isJust)
import Data.Sequence (Seq (..))
import qualified Data.Sequence as Seq
import Data.Sequence.Strict (StrictSeq (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Lens.Micro
import Lens.Micro.Extras (view)

-- | Implementation for @GetFilteredDelegationsAndRewardAccounts@ query.
queryStakePoolDelegsAndRewards ::
  EraCertState era =>
  NewEpochState era ->
  Set (Credential Staking) ->
  ( Map (Credential Staking) (KeyHash StakePool)
  , Map (Credential Staking) Coin
  )
queryStakePoolDelegsAndRewards :: forall era.
EraCertState era =>
NewEpochState era
-> Set (Credential Staking)
-> (Map (Credential Staking) (KeyHash StakePool),
    Map (Credential Staking) Coin)
queryStakePoolDelegsAndRewards NewEpochState era
nes Set (Credential Staking)
creds =
  let accountsMap :: Map (Credential Staking) (AccountState era)
accountsMap = NewEpochState era
nes NewEpochState era
-> Getting
     (Map (Credential Staking) (AccountState era))
     (NewEpochState era)
     (Map (Credential Staking) (AccountState era))
-> Map (Credential Staking) (AccountState era)
forall s a. s -> Getting a s a -> a
^. (EpochState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (EpochState era))
-> NewEpochState era
-> Const
     (Map (Credential Staking) (AccountState era)) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era
  -> Const
       (Map (Credential Staking) (AccountState era)) (EpochState era))
 -> NewEpochState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (NewEpochState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> EpochState era
    -> Const
         (Map (Credential Staking) (AccountState era)) (EpochState era))
-> Getting
     (Map (Credential Staking) (AccountState era))
     (NewEpochState era)
     (Map (Credential Staking) (AccountState era))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (LedgerState era))
-> EpochState era
-> Const
     (Map (Credential Staking) (AccountState era)) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era
  -> Const
       (Map (Credential Staking) (AccountState era)) (LedgerState era))
 -> EpochState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (EpochState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> LedgerState era
    -> Const
         (Map (Credential Staking) (AccountState era)) (LedgerState era))
-> (Map (Credential Staking) (AccountState era)
    -> Const
         (Map (Credential Staking) (AccountState era))
         (Map (Credential Staking) (AccountState era)))
-> EpochState era
-> Const
     (Map (Credential Staking) (AccountState era)) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (CertState era))
-> LedgerState era
-> Const
     (Map (Credential Staking) (AccountState era)) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era
  -> Const
       (Map (Credential Staking) (AccountState era)) (CertState era))
 -> LedgerState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (LedgerState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> CertState era
    -> Const
         (Map (Credential Staking) (AccountState era)) (CertState era))
-> (Map (Credential Staking) (AccountState era)
    -> Const
         (Map (Credential Staking) (AccountState era))
         (Map (Credential Staking) (AccountState era)))
-> LedgerState era
-> Const
     (Map (Credential Staking) (AccountState era)) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (DState era))
-> CertState era
-> Const
     (Map (Credential Staking) (AccountState era)) (CertState era)
forall era. EraCertState era => Lens' (CertState era) (DState era)
Lens' (CertState era) (DState era)
certDStateL ((DState era
  -> Const
       (Map (Credential Staking) (AccountState era)) (DState era))
 -> CertState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (CertState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> DState era
    -> Const
         (Map (Credential Staking) (AccountState era)) (DState era))
-> (Map (Credential Staking) (AccountState era)
    -> Const
         (Map (Credential Staking) (AccountState era))
         (Map (Credential Staking) (AccountState era)))
-> CertState era
-> Const
     (Map (Credential Staking) (AccountState era)) (CertState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Accounts era
 -> Const
      (Map (Credential Staking) (AccountState era)) (Accounts era))
-> DState era
-> Const (Map (Credential Staking) (AccountState era)) (DState era)
forall era. Lens' (DState era) (Accounts era)
forall (t :: * -> *) era.
CanSetAccounts t =>
Lens' (t era) (Accounts era)
accountsL ((Accounts era
  -> Const
       (Map (Credential Staking) (AccountState era)) (Accounts era))
 -> DState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (DState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> Accounts era
    -> Const
         (Map (Credential Staking) (AccountState era)) (Accounts era))
-> (Map (Credential Staking) (AccountState era)
    -> Const
         (Map (Credential Staking) (AccountState era))
         (Map (Credential Staking) (AccountState era)))
-> DState era
-> Const (Map (Credential Staking) (AccountState era)) (DState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (Credential Staking) (AccountState era)
 -> Const
      (Map (Credential Staking) (AccountState era))
      (Map (Credential Staking) (AccountState era)))
-> Accounts era
-> Const
     (Map (Credential Staking) (AccountState era)) (Accounts era)
forall era.
EraAccounts era =>
Lens' (Accounts era) (Map (Credential Staking) (AccountState era))
Lens' (Accounts era) (Map (Credential Staking) (AccountState era))
accountsMapL
      accountsMapFiltered :: Map (Credential Staking) (AccountState era)
accountsMapFiltered = Map (Credential Staking) (AccountState era)
accountsMap Map (Credential Staking) (AccountState era)
-> Set (Credential Staking)
-> Map (Credential Staking) (AccountState era)
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (Credential Staking)
creds
   in ( (AccountState era -> Maybe (KeyHash StakePool))
-> Map (Credential Staking) (AccountState era)
-> Map (Credential Staking) (KeyHash StakePool)
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybe (AccountState era
-> Getting
     (Maybe (KeyHash StakePool))
     (AccountState era)
     (Maybe (KeyHash StakePool))
-> Maybe (KeyHash StakePool)
forall s a. s -> Getting a s a -> a
^. Getting
  (Maybe (KeyHash StakePool))
  (AccountState era)
  (Maybe (KeyHash StakePool))
forall era.
EraAccounts era =>
Lens' (AccountState era) (Maybe (KeyHash StakePool))
Lens' (AccountState era) (Maybe (KeyHash StakePool))
stakePoolDelegationAccountStateL) Map (Credential Staking) (AccountState era)
accountsMapFiltered
      , (AccountState era -> Coin)
-> Map (Credential Staking) (AccountState era)
-> Map (Credential Staking) Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact (CompactForm Coin -> Coin)
-> (AccountState era -> CompactForm Coin)
-> AccountState era
-> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountState era
-> Getting (CompactForm Coin) (AccountState era) (CompactForm Coin)
-> CompactForm Coin
forall s a. s -> Getting a s a -> a
^. Getting (CompactForm Coin) (AccountState era) (CompactForm Coin)
forall era.
EraAccounts era =>
Lens' (AccountState era) (CompactForm Coin)
Lens' (AccountState era) (CompactForm Coin)
balanceAccountStateL)) Map (Credential Staking) (AccountState era)
accountsMapFiltered
      )

queryConstitution :: ConwayEraGov era => NewEpochState era -> Constitution era
queryConstitution :: forall era.
ConwayEraGov era =>
NewEpochState era -> Constitution era
queryConstitution = (GovState era
-> Getting (Constitution era) (GovState era) (Constitution era)
-> Constitution era
forall s a. s -> Getting a s a -> a
^. Getting (Constitution era) (GovState era) (Constitution era)
forall era.
ConwayEraGov era =>
Lens' (GovState era) (Constitution era)
Lens' (GovState era) (Constitution era)
constitutionGovStateL) (GovState era -> Constitution era)
-> (NewEpochState era -> GovState era)
-> NewEpochState era
-> Constitution era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NewEpochState era -> GovState era
forall era. NewEpochState era -> GovState era
queryGovState

queryConstitutionHash ::
  ConwayEraGov era =>
  NewEpochState era ->
  SafeHash AnchorData
queryConstitutionHash :: forall era.
ConwayEraGov era =>
NewEpochState era -> SafeHash AnchorData
queryConstitutionHash NewEpochState era
nes =
  Anchor -> SafeHash AnchorData
anchorDataHash (Anchor -> SafeHash AnchorData)
-> (Constitution era -> Anchor)
-> Constitution era
-> SafeHash AnchorData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constitution era -> Anchor
forall era. Constitution era -> Anchor
constitutionAnchor (Constitution era -> SafeHash AnchorData)
-> Constitution era -> SafeHash AnchorData
forall a b. (a -> b) -> a -> b
$ NewEpochState era -> Constitution era
forall era.
ConwayEraGov era =>
NewEpochState era -> Constitution era
queryConstitution NewEpochState era
nes

-- | This query returns all of the state related to governance
queryGovState :: NewEpochState era -> GovState era
queryGovState :: forall era. NewEpochState era -> GovState era
queryGovState NewEpochState era
nes = NewEpochState era
nes NewEpochState era
-> Getting (GovState era) (NewEpochState era) (GovState era)
-> GovState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (GovState era) (EpochState era))
-> NewEpochState era -> Const (GovState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEpochStateL ((EpochState era -> Const (GovState era) (EpochState era))
 -> NewEpochState era -> Const (GovState era) (NewEpochState era))
-> ((GovState era -> Const (GovState era) (GovState era))
    -> EpochState era -> Const (GovState era) (EpochState era))
-> Getting (GovState era) (NewEpochState era) (GovState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GovState era -> Const (GovState era) (GovState era))
-> EpochState era -> Const (GovState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(GovState era -> f (GovState era))
-> EpochState era -> f (EpochState era)
epochStateGovStateL

-- | Query DRep state.
queryDRepState ::
  ConwayEraCertState era =>
  NewEpochState era ->
  -- | Specify a set of DRep credentials whose state should be returned. When this set is
  -- empty, states for all of the DReps will be returned.
  Set (Credential DRepRole) ->
  Map (Credential DRepRole) DRepState
queryDRepState :: forall era.
ConwayEraCertState era =>
NewEpochState era
-> Set (Credential DRepRole) -> Map (Credential DRepRole) DRepState
queryDRepState NewEpochState era
nes Set (Credential DRepRole)
creds
  | Set (Credential DRepRole) -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set (Credential DRepRole)
creds = VState era -> VState era
updateDormantDRepExpiry' VState era
vState VState era
-> Getting
     (Map (Credential DRepRole) DRepState)
     (VState era)
     (Map (Credential DRepRole) DRepState)
-> Map (Credential DRepRole) DRepState
forall s a. s -> Getting a s a -> a
^. Getting
  (Map (Credential DRepRole) DRepState)
  (VState era)
  (Map (Credential DRepRole) DRepState)
forall era (f :: * -> *).
Functor f =>
(Map (Credential DRepRole) DRepState
 -> f (Map (Credential DRepRole) DRepState))
-> VState era -> f (VState era)
vsDRepsL
  | Bool
otherwise = VState era -> VState era
updateDormantDRepExpiry' VState era
vStateFiltered VState era
-> Getting
     (Map (Credential DRepRole) DRepState)
     (VState era)
     (Map (Credential DRepRole) DRepState)
-> Map (Credential DRepRole) DRepState
forall s a. s -> Getting a s a -> a
^. Getting
  (Map (Credential DRepRole) DRepState)
  (VState era)
  (Map (Credential DRepRole) DRepState)
forall era (f :: * -> *).
Functor f =>
(Map (Credential DRepRole) DRepState
 -> f (Map (Credential DRepRole) DRepState))
-> VState era -> f (VState era)
vsDRepsL
  where
    vStateFiltered :: VState era
vStateFiltered = VState era
vState VState era -> (VState era -> VState era) -> VState era
forall a b. a -> (a -> b) -> b
& (Map (Credential DRepRole) DRepState
 -> Identity (Map (Credential DRepRole) DRepState))
-> VState era -> Identity (VState era)
forall era (f :: * -> *).
Functor f =>
(Map (Credential DRepRole) DRepState
 -> f (Map (Credential DRepRole) DRepState))
-> VState era -> f (VState era)
vsDRepsL ((Map (Credential DRepRole) DRepState
  -> Identity (Map (Credential DRepRole) DRepState))
 -> VState era -> Identity (VState era))
-> (Map (Credential DRepRole) DRepState
    -> Map (Credential DRepRole) DRepState)
-> VState era
-> VState era
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Map (Credential DRepRole) DRepState
-> Set (Credential DRepRole) -> Map (Credential DRepRole) DRepState
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (Credential DRepRole)
creds)
    vState :: VState era
vState = NewEpochState era
nes NewEpochState era
-> Getting (VState era) (NewEpochState era) (VState era)
-> VState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (VState era) (EpochState era))
-> NewEpochState era -> Const (VState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era -> Const (VState era) (EpochState era))
 -> NewEpochState era -> Const (VState era) (NewEpochState era))
-> ((VState era -> Const (VState era) (VState era))
    -> EpochState era -> Const (VState era) (EpochState era))
-> Getting (VState era) (NewEpochState era) (VState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (VState era) (LedgerState era))
-> EpochState era -> Const (VState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (VState era) (LedgerState era))
 -> EpochState era -> Const (VState era) (EpochState era))
-> ((VState era -> Const (VState era) (VState era))
    -> LedgerState era -> Const (VState era) (LedgerState era))
-> (VState era -> Const (VState era) (VState era))
-> EpochState era
-> Const (VState era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (VState era) (CertState era))
-> LedgerState era -> Const (VState era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (VState era) (CertState era))
 -> LedgerState era -> Const (VState era) (LedgerState era))
-> ((VState era -> Const (VState era) (VState era))
    -> CertState era -> Const (VState era) (CertState era))
-> (VState era -> Const (VState era) (VState era))
-> LedgerState era
-> Const (VState era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VState era -> Const (VState era) (VState era))
-> CertState era -> Const (VState era) (CertState era)
forall era.
ConwayEraCertState era =>
Lens' (CertState era) (VState era)
Lens' (CertState era) (VState era)
certVStateL
    updateDormantDRepExpiry' :: VState era -> VState era
updateDormantDRepExpiry' = EpochNo -> VState era -> VState era
forall era. EpochNo -> VState era -> VState era
updateDormantDRepExpiry (NewEpochState era
nes NewEpochState era
-> Getting EpochNo (NewEpochState era) EpochNo -> EpochNo
forall s a. s -> Getting a s a -> a
^. Getting EpochNo (NewEpochState era) EpochNo
forall era (f :: * -> *).
Functor f =>
(EpochNo -> f EpochNo)
-> NewEpochState era -> f (NewEpochState era)
nesELL)

-- | Query the delegators delegated to each DRep, including
-- @AlwaysAbstain@ and @NoConfidence@.
queryDRepDelegations ::
  forall era.
  ConwayEraCertState era =>
  NewEpochState era ->
  -- | Specify a set of DReps whose state should be returned. When this set is
  -- empty, states for all of the DReps will be returned.
  Set DRep ->
  Map DRep (Set (Credential Staking))
queryDRepDelegations :: forall era.
ConwayEraCertState era =>
NewEpochState era
-> Set DRep -> Map DRep (Set (Credential Staking))
queryDRepDelegations NewEpochState era
nes Set DRep
dreps =
  case Set DRep -> Maybe (Set (Credential DRepRole))
getDRepCreds Set DRep
dreps of
    Just Set (Credential DRepRole)
creds ->
      (DRepState -> Set (Credential Staking))
-> Map DRep DRepState -> Map DRep (Set (Credential Staking))
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map DRepState -> Set (Credential Staking)
drepDelegs (Map DRep DRepState -> Map DRep (Set (Credential Staking)))
-> Map DRep DRepState -> Map DRep (Set (Credential Staking))
forall a b. (a -> b) -> a -> b
$
        (Credential DRepRole -> DRep)
-> Map (Credential DRepRole) DRepState -> Map DRep DRepState
forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeys Credential DRepRole -> DRep
credToDRep ((VState era
vState VState era
-> Getting
     (Map (Credential DRepRole) DRepState)
     (VState era)
     (Map (Credential DRepRole) DRepState)
-> Map (Credential DRepRole) DRepState
forall s a. s -> Getting a s a -> a
^. Getting
  (Map (Credential DRepRole) DRepState)
  (VState era)
  (Map (Credential DRepRole) DRepState)
forall era (f :: * -> *).
Functor f =>
(Map (Credential DRepRole) DRepState
 -> f (Map (Credential DRepRole) DRepState))
-> VState era -> f (VState era)
vsDRepsL) Map (Credential DRepRole) DRepState
-> Set (Credential DRepRole) -> Map (Credential DRepRole) DRepState
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (Credential DRepRole)
creds)
    Maybe (Set (Credential DRepRole))
Nothing ->
      -- Whenever predefined `AlwaysAbstain` or `AlwaysNoConfidence` are
      -- requested we are forced to iterate over all accounts and find those
      -- delegations.
      (Map DRep (Set (Credential Staking))
 -> Credential Staking
 -> AccountState era
 -> Map DRep (Set (Credential Staking)))
-> Map DRep (Set (Credential Staking))
-> Map (Credential Staking) (AccountState era)
-> Map DRep (Set (Credential Staking))
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey'
        ( \Map DRep (Set (Credential Staking))
m Credential Staking
cred AccountState era
cas ->
            case AccountState era
cas AccountState era
-> Getting (Maybe DRep) (AccountState era) (Maybe DRep)
-> Maybe DRep
forall s a. s -> Getting a s a -> a
^. Getting (Maybe DRep) (AccountState era) (Maybe DRep)
forall era.
ConwayEraAccounts era =>
Lens' (AccountState era) (Maybe DRep)
Lens' (AccountState era) (Maybe DRep)
dRepDelegationAccountStateL of
              Just DRep
drep
                | Set DRep -> Bool
forall a. Set a -> Bool
Set.null Set DRep
dreps Bool -> Bool -> Bool
|| DRep
drep DRep -> Set DRep -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set DRep
dreps ->
                    (Set (Credential Staking)
 -> Set (Credential Staking) -> Set (Credential Staking))
-> DRep
-> Set (Credential Staking)
-> Map DRep (Set (Credential Staking))
-> Map DRep (Set (Credential Staking))
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith Set (Credential Staking)
-> Set (Credential Staking) -> Set (Credential Staking)
forall a. Semigroup a => a -> a -> a
(<>) DRep
drep (Credential Staking -> Set (Credential Staking)
forall a. a -> Set a
Set.singleton Credential Staking
cred) Map DRep (Set (Credential Staking))
m
              Maybe DRep
_ ->
                Map DRep (Set (Credential Staking))
m
        )
        Map DRep (Set (Credential Staking))
forall k a. Map k a
Map.empty
        (DState era
dState DState era
-> Getting
     (Map (Credential Staking) (AccountState era))
     (DState era)
     (Map (Credential Staking) (AccountState era))
-> Map (Credential Staking) (AccountState era)
forall s a. s -> Getting a s a -> a
^. (Accounts era
 -> Const
      (Map (Credential Staking) (AccountState era)) (Accounts era))
-> DState era
-> Const (Map (Credential Staking) (AccountState era)) (DState era)
forall era. Lens' (DState era) (Accounts era)
forall (t :: * -> *) era.
CanSetAccounts t =>
Lens' (t era) (Accounts era)
accountsL ((Accounts era
  -> Const
       (Map (Credential Staking) (AccountState era)) (Accounts era))
 -> DState era
 -> Const
      (Map (Credential Staking) (AccountState era)) (DState era))
-> ((Map (Credential Staking) (AccountState era)
     -> Const
          (Map (Credential Staking) (AccountState era))
          (Map (Credential Staking) (AccountState era)))
    -> Accounts era
    -> Const
         (Map (Credential Staking) (AccountState era)) (Accounts era))
-> Getting
     (Map (Credential Staking) (AccountState era))
     (DState era)
     (Map (Credential Staking) (AccountState era))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (Credential Staking) (AccountState era)
 -> Const
      (Map (Credential Staking) (AccountState era))
      (Map (Credential Staking) (AccountState era)))
-> Accounts era
-> Const
     (Map (Credential Staking) (AccountState era)) (Accounts era)
forall era.
EraAccounts era =>
Lens' (Accounts era) (Map (Credential Staking) (AccountState era))
Lens' (Accounts era) (Map (Credential Staking) (AccountState era))
accountsMapL)
  where
    dState :: DState era
dState = NewEpochState era
nes NewEpochState era
-> Getting (DState era) (NewEpochState era) (DState era)
-> DState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (DState era) (EpochState era))
-> NewEpochState era -> Const (DState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era -> Const (DState era) (EpochState era))
 -> NewEpochState era -> Const (DState era) (NewEpochState era))
-> ((DState era -> Const (DState era) (DState era))
    -> EpochState era -> Const (DState era) (EpochState era))
-> Getting (DState era) (NewEpochState era) (DState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (DState era) (LedgerState era))
-> EpochState era -> Const (DState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (DState era) (LedgerState era))
 -> EpochState era -> Const (DState era) (EpochState era))
-> ((DState era -> Const (DState era) (DState era))
    -> LedgerState era -> Const (DState era) (LedgerState era))
-> (DState era -> Const (DState era) (DState era))
-> EpochState era
-> Const (DState era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (DState era) (CertState era))
-> LedgerState era -> Const (DState era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (DState era) (CertState era))
 -> LedgerState era -> Const (DState era) (LedgerState era))
-> ((DState era -> Const (DState era) (DState era))
    -> CertState era -> Const (DState era) (CertState era))
-> (DState era -> Const (DState era) (DState era))
-> LedgerState era
-> Const (DState era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DState era -> Const (DState era) (DState era))
-> CertState era -> Const (DState era) (CertState era)
forall era. EraCertState era => Lens' (CertState era) (DState era)
Lens' (CertState era) (DState era)
certDStateL
    vState :: VState era
vState = NewEpochState era
nes NewEpochState era
-> Getting (VState era) (NewEpochState era) (VState era)
-> VState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (VState era) (EpochState era))
-> NewEpochState era -> Const (VState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era -> Const (VState era) (EpochState era))
 -> NewEpochState era -> Const (VState era) (NewEpochState era))
-> ((VState era -> Const (VState era) (VState era))
    -> EpochState era -> Const (VState era) (EpochState era))
-> Getting (VState era) (NewEpochState era) (VState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (VState era) (LedgerState era))
-> EpochState era -> Const (VState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (VState era) (LedgerState era))
 -> EpochState era -> Const (VState era) (EpochState era))
-> ((VState era -> Const (VState era) (VState era))
    -> LedgerState era -> Const (VState era) (LedgerState era))
-> (VState era -> Const (VState era) (VState era))
-> EpochState era
-> Const (VState era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (VState era) (CertState era))
-> LedgerState era -> Const (VState era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (VState era) (CertState era))
 -> LedgerState era -> Const (VState era) (LedgerState era))
-> ((VState era -> Const (VState era) (VState era))
    -> CertState era -> Const (VState era) (CertState era))
-> (VState era -> Const (VState era) (VState era))
-> LedgerState era
-> Const (VState era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VState era -> Const (VState era) (VState era))
-> CertState era -> Const (VState era) (CertState era)
forall era.
ConwayEraCertState era =>
Lens' (CertState era) (VState era)
Lens' (CertState era) (VState era)
certVStateL
    -- Find all credentials for requested DReps, but only when we don't care
    -- about predefined DReps
    getDRepCreds :: Set DRep -> Maybe (Set (Credential DRepRole))
getDRepCreds Set DRep
ds = do
      Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Set DRep -> Bool
forall a. Set a -> Bool
Set.null Set DRep
ds
      [Credential DRepRole] -> Set (Credential DRepRole)
forall a. Ord a => [a] -> Set a
Set.fromList ([Credential DRepRole] -> Set (Credential DRepRole))
-> Maybe [Credential DRepRole] -> Maybe (Set (Credential DRepRole))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DRep -> Maybe (Credential DRepRole))
-> [DRep] -> Maybe [Credential DRepRole]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse DRep -> Maybe (Credential DRepRole)
dRepToCred (Set DRep -> [DRep]
forall a. Set a -> [a]
Set.elems Set DRep
ds)

-- | Query DRep stake distribution. Note that this can be an expensive query because there
-- is a chance that current distribution has not been fully computed yet.
queryDRepStakeDistr ::
  ConwayEraGov era =>
  NewEpochState era ->
  -- | Specify DRep Ids whose stake distribution should be returned. When this set is
  -- empty, distributions for all of the DReps will be returned.
  Set DRep ->
  Map DRep Coin
queryDRepStakeDistr :: forall era.
ConwayEraGov era =>
NewEpochState era -> Set DRep -> Map DRep Coin
queryDRepStakeDistr NewEpochState era
nes Set DRep
creds
  | Set DRep -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set DRep
creds = (CompactForm Coin -> Coin)
-> Map DRep (CompactForm Coin) -> Map DRep Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact Map DRep (CompactForm Coin)
distr
  | Bool
otherwise = (CompactForm Coin -> Coin)
-> Map DRep (CompactForm Coin) -> Map DRep Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact (Map DRep (CompactForm Coin) -> Map DRep Coin)
-> Map DRep (CompactForm Coin) -> Map DRep Coin
forall a b. (a -> b) -> a -> b
$ Map DRep (CompactForm Coin)
distr Map DRep (CompactForm Coin)
-> Set DRep -> Map DRep (CompactForm Coin)
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set DRep
creds
  where
    distr :: Map DRep (CompactForm Coin)
distr = PulsingSnapshot era -> Map DRep (CompactForm Coin)
forall era. PulsingSnapshot era -> Map DRep (CompactForm Coin)
psDRepDistr (PulsingSnapshot era -> Map DRep (CompactForm Coin))
-> ((PulsingSnapshot era, RatifyState era) -> PulsingSnapshot era)
-> (PulsingSnapshot era, RatifyState era)
-> Map DRep (CompactForm Coin)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PulsingSnapshot era, RatifyState era) -> PulsingSnapshot era
forall a b. (a, b) -> a
fst ((PulsingSnapshot era, RatifyState era)
 -> Map DRep (CompactForm Coin))
-> (PulsingSnapshot era, RatifyState era)
-> Map DRep (CompactForm Coin)
forall a b. (a -> b) -> a -> b
$ NewEpochState era -> (PulsingSnapshot era, RatifyState era)
forall era.
ConwayEraGov era =>
NewEpochState era -> (PulsingSnapshot era, RatifyState era)
finishedPulserState NewEpochState era
nes

-- | Query the stake distribution of the registered DReps. This does not
-- include the @AlwaysAbstain@ and @NoConfidence@ DReps.
queryRegisteredDRepStakeDistr ::
  (ConwayEraGov era, ConwayEraCertState era) =>
  NewEpochState era ->
  -- | Specify DRep Ids whose stake distribution should be returned. When this set is
  -- empty, distributions for all of the registered DReps will be returned.
  Set (Credential DRepRole) ->
  Map (Credential DRepRole) Coin
queryRegisteredDRepStakeDistr :: forall era.
(ConwayEraGov era, ConwayEraCertState era) =>
NewEpochState era
-> Set (Credential DRepRole) -> Map (Credential DRepRole) Coin
queryRegisteredDRepStakeDistr NewEpochState era
nes Set (Credential DRepRole)
creds =
  (Map (Credential DRepRole) Coin
 -> Credential DRepRole
 -> DRepState
 -> Map (Credential DRepRole) Coin)
-> Map (Credential DRepRole) Coin
-> Map (Credential DRepRole) DRepState
-> Map (Credential DRepRole) Coin
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' Map (Credential DRepRole) Coin
-> Credential DRepRole
-> DRepState
-> Map (Credential DRepRole) Coin
computeDistr Map (Credential DRepRole) Coin
forall a. Monoid a => a
mempty Map (Credential DRepRole) DRepState
selectedDReps
  where
    selectedDReps :: Map (Credential DRepRole) DRepState
selectedDReps
      | Set (Credential DRepRole) -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set (Credential DRepRole)
creds = Map (Credential DRepRole) DRepState
registeredDReps
      | Bool
otherwise = Map (Credential DRepRole) DRepState
registeredDReps Map (Credential DRepRole) DRepState
-> Set (Credential DRepRole) -> Map (Credential DRepRole) DRepState
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (Credential DRepRole)
creds
    registeredDReps :: Map (Credential DRepRole) DRepState
registeredDReps = NewEpochState era
nes NewEpochState era
-> Getting
     (Map (Credential DRepRole) DRepState)
     (NewEpochState era)
     (Map (Credential DRepRole) DRepState)
-> Map (Credential DRepRole) DRepState
forall s a. s -> Getting a s a -> a
^. (EpochState era
 -> Const (Map (Credential DRepRole) DRepState) (EpochState era))
-> NewEpochState era
-> Const (Map (Credential DRepRole) DRepState) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era
  -> Const (Map (Credential DRepRole) DRepState) (EpochState era))
 -> NewEpochState era
 -> Const (Map (Credential DRepRole) DRepState) (NewEpochState era))
-> ((Map (Credential DRepRole) DRepState
     -> Const
          (Map (Credential DRepRole) DRepState)
          (Map (Credential DRepRole) DRepState))
    -> EpochState era
    -> Const (Map (Credential DRepRole) DRepState) (EpochState era))
-> Getting
     (Map (Credential DRepRole) DRepState)
     (NewEpochState era)
     (Map (Credential DRepRole) DRepState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era
 -> Const (Map (Credential DRepRole) DRepState) (LedgerState era))
-> EpochState era
-> Const (Map (Credential DRepRole) DRepState) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era
  -> Const (Map (Credential DRepRole) DRepState) (LedgerState era))
 -> EpochState era
 -> Const (Map (Credential DRepRole) DRepState) (EpochState era))
-> ((Map (Credential DRepRole) DRepState
     -> Const
          (Map (Credential DRepRole) DRepState)
          (Map (Credential DRepRole) DRepState))
    -> LedgerState era
    -> Const (Map (Credential DRepRole) DRepState) (LedgerState era))
-> (Map (Credential DRepRole) DRepState
    -> Const
         (Map (Credential DRepRole) DRepState)
         (Map (Credential DRepRole) DRepState))
-> EpochState era
-> Const (Map (Credential DRepRole) DRepState) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era
 -> Const (Map (Credential DRepRole) DRepState) (CertState era))
-> LedgerState era
-> Const (Map (Credential DRepRole) DRepState) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era
  -> Const (Map (Credential DRepRole) DRepState) (CertState era))
 -> LedgerState era
 -> Const (Map (Credential DRepRole) DRepState) (LedgerState era))
-> ((Map (Credential DRepRole) DRepState
     -> Const
          (Map (Credential DRepRole) DRepState)
          (Map (Credential DRepRole) DRepState))
    -> CertState era
    -> Const (Map (Credential DRepRole) DRepState) (CertState era))
-> (Map (Credential DRepRole) DRepState
    -> Const
         (Map (Credential DRepRole) DRepState)
         (Map (Credential DRepRole) DRepState))
-> LedgerState era
-> Const (Map (Credential DRepRole) DRepState) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VState era
 -> Const (Map (Credential DRepRole) DRepState) (VState era))
-> CertState era
-> Const (Map (Credential DRepRole) DRepState) (CertState era)
forall era.
ConwayEraCertState era =>
Lens' (CertState era) (VState era)
Lens' (CertState era) (VState era)
certVStateL ((VState era
  -> Const (Map (Credential DRepRole) DRepState) (VState era))
 -> CertState era
 -> Const (Map (Credential DRepRole) DRepState) (CertState era))
-> ((Map (Credential DRepRole) DRepState
     -> Const
          (Map (Credential DRepRole) DRepState)
          (Map (Credential DRepRole) DRepState))
    -> VState era
    -> Const (Map (Credential DRepRole) DRepState) (VState era))
-> (Map (Credential DRepRole) DRepState
    -> Const
         (Map (Credential DRepRole) DRepState)
         (Map (Credential DRepRole) DRepState))
-> CertState era
-> Const (Map (Credential DRepRole) DRepState) (CertState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (Credential DRepRole) DRepState
 -> Const
      (Map (Credential DRepRole) DRepState)
      (Map (Credential DRepRole) DRepState))
-> VState era
-> Const (Map (Credential DRepRole) DRepState) (VState era)
forall era (f :: * -> *).
Functor f =>
(Map (Credential DRepRole) DRepState
 -> f (Map (Credential DRepRole) DRepState))
-> VState era -> f (VState era)
vsDRepsL
    computeDistr :: Map (Credential DRepRole) Coin
-> Credential DRepRole
-> DRepState
-> Map (Credential DRepRole) Coin
computeDistr Map (Credential DRepRole) Coin
distrAcc Credential DRepRole
dRepCred (DRepState {Set (Credential Staking)
StrictMaybe Anchor
EpochNo
CompactForm Coin
drepDelegs :: DRepState -> Set (Credential Staking)
drepExpiry :: EpochNo
drepAnchor :: StrictMaybe Anchor
drepDeposit :: CompactForm Coin
drepDelegs :: Set (Credential Staking)
drepAnchor :: DRepState -> StrictMaybe Anchor
drepDeposit :: DRepState -> CompactForm Coin
drepExpiry :: DRepState -> EpochNo
..}) =
      Credential DRepRole
-> Coin
-> Map (Credential DRepRole) Coin
-> Map (Credential DRepRole) Coin
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Credential DRepRole
dRepCred (Set (Credential Staking) -> Coin
totalDelegations Set (Credential Staking)
drepDelegs) Map (Credential DRepRole) Coin
distrAcc
    totalDelegations :: Set (Credential Staking) -> Coin
totalDelegations =
      CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact (CompactForm Coin -> Coin)
-> (Set (Credential Staking) -> CompactForm Coin)
-> Set (Credential Staking)
-> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Credential Staking -> CompactForm Coin)
-> Set (Credential Staking) -> CompactForm Coin
forall m a. Monoid m => (a -> m) -> Set a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Credential Staking -> CompactForm Coin
stakeAndDeposits
    instantStake :: Map (Credential Staking) (CompactForm Coin)
instantStake = NewEpochState era
nes NewEpochState era
-> Getting
     (Map (Credential Staking) (CompactForm Coin))
     (NewEpochState era)
     (Map (Credential Staking) (CompactForm Coin))
-> Map (Credential Staking) (CompactForm Coin)
forall s a. s -> Getting a s a -> a
^. (InstantStake era
 -> Const
      (Map (Credential Staking) (CompactForm Coin)) (InstantStake era))
-> NewEpochState era
-> Const
     (Map (Credential Staking) (CompactForm Coin)) (NewEpochState era)
forall era. Lens' (NewEpochState era) (InstantStake era)
forall (t :: * -> *) era.
CanSetInstantStake t =>
Lens' (t era) (InstantStake era)
instantStakeL ((InstantStake era
  -> Const
       (Map (Credential Staking) (CompactForm Coin)) (InstantStake era))
 -> NewEpochState era
 -> Const
      (Map (Credential Staking) (CompactForm Coin)) (NewEpochState era))
-> ((Map (Credential Staking) (CompactForm Coin)
     -> Const
          (Map (Credential Staking) (CompactForm Coin))
          (Map (Credential Staking) (CompactForm Coin)))
    -> InstantStake era
    -> Const
         (Map (Credential Staking) (CompactForm Coin)) (InstantStake era))
-> Getting
     (Map (Credential Staking) (CompactForm Coin))
     (NewEpochState era)
     (Map (Credential Staking) (CompactForm Coin))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (Credential Staking) (CompactForm Coin)
 -> Const
      (Map (Credential Staking) (CompactForm Coin))
      (Map (Credential Staking) (CompactForm Coin)))
-> InstantStake era
-> Const
     (Map (Credential Staking) (CompactForm Coin)) (InstantStake era)
forall era.
EraStake era =>
Lens'
  (InstantStake era) (Map (Credential Staking) (CompactForm Coin))
Lens'
  (InstantStake era) (Map (Credential Staking) (CompactForm Coin))
instantStakeCredentialsL
    proposalDeposits :: Map (Credential Staking) (CompactForm Coin)
proposalDeposits = Proposals era -> Map (Credential Staking) (CompactForm Coin)
forall era.
Proposals era -> Map (Credential Staking) (CompactForm Coin)
proposalsDeposits (Proposals era -> Map (Credential Staking) (CompactForm Coin))
-> Proposals era -> Map (Credential Staking) (CompactForm Coin)
forall a b. (a -> b) -> a -> b
$ NewEpochState era
nes NewEpochState era
-> Getting (Proposals era) (NewEpochState era) (Proposals era)
-> Proposals era
forall s a. s -> Getting a s a -> a
^. (GovState era -> Const (Proposals era) (GovState era))
-> NewEpochState era -> Const (Proposals era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(GovState era -> f (GovState era))
-> NewEpochState era -> f (NewEpochState era)
newEpochStateGovStateL ((GovState era -> Const (Proposals era) (GovState era))
 -> NewEpochState era -> Const (Proposals era) (NewEpochState era))
-> ((Proposals era -> Const (Proposals era) (Proposals era))
    -> GovState era -> Const (Proposals era) (GovState era))
-> Getting (Proposals era) (NewEpochState era) (Proposals era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Proposals era -> Const (Proposals era) (Proposals era))
-> GovState era -> Const (Proposals era) (GovState era)
forall era.
ConwayEraGov era =>
Lens' (GovState era) (Proposals era)
Lens' (GovState era) (Proposals era)
proposalsGovStateL
    stakeAndDeposits :: Credential Staking -> CompactForm Coin
stakeAndDeposits Credential Staking
stakeCred =
      CompactForm Coin -> Maybe (CompactForm Coin) -> CompactForm Coin
forall a. a -> Maybe a -> a
fromMaybe (Word64 -> CompactForm Coin
CompactCoin Word64
0) (Maybe (CompactForm Coin) -> CompactForm Coin)
-> Maybe (CompactForm Coin) -> CompactForm Coin
forall a b. (a -> b) -> a -> b
$
        Credential Staking
-> Map (Credential Staking) (CompactForm Coin)
-> Maybe (CompactForm Coin)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential Staking
stakeCred Map (Credential Staking) (CompactForm Coin)
instantStake Maybe (CompactForm Coin)
-> Maybe (CompactForm Coin) -> Maybe (CompactForm Coin)
forall a. Semigroup a => a -> a -> a
<> Credential Staking
-> Map (Credential Staking) (CompactForm Coin)
-> Maybe (CompactForm Coin)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential Staking
stakeCred Map (Credential Staking) (CompactForm Coin)
proposalDeposits

-- | Query pool stake distribution.
querySPOStakeDistr ::
  ConwayEraGov era =>
  NewEpochState era ->
  Set (KeyHash StakePool) ->
  -- | Specify pool key hashes whose stake distribution should be returned. When this set is
  -- empty, distributions for all of the pools will be returned.
  Map (KeyHash StakePool) Coin
querySPOStakeDistr :: forall era.
ConwayEraGov era =>
NewEpochState era
-> Set (KeyHash StakePool) -> Map (KeyHash StakePool) Coin
querySPOStakeDistr NewEpochState era
nes Set (KeyHash StakePool)
keys
  | Set (KeyHash StakePool) -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set (KeyHash StakePool)
keys = (CompactForm Coin -> Coin)
-> Map (KeyHash StakePool) (CompactForm Coin)
-> Map (KeyHash StakePool) Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact Map (KeyHash StakePool) (CompactForm Coin)
distr
  | Bool
otherwise = (CompactForm Coin -> Coin)
-> Map (KeyHash StakePool) (CompactForm Coin)
-> Map (KeyHash StakePool) Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact (Map (KeyHash StakePool) (CompactForm Coin)
 -> Map (KeyHash StakePool) Coin)
-> Map (KeyHash StakePool) (CompactForm Coin)
-> Map (KeyHash StakePool) Coin
forall a b. (a -> b) -> a -> b
$ Map (KeyHash StakePool) (CompactForm Coin)
distr Map (KeyHash StakePool) (CompactForm Coin)
-> Set (KeyHash StakePool)
-> Map (KeyHash StakePool) (CompactForm Coin)
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (KeyHash StakePool)
keys
  where
    distr :: Map (KeyHash StakePool) (CompactForm Coin)
distr = PulsingSnapshot era -> Map (KeyHash StakePool) (CompactForm Coin)
forall era.
PulsingSnapshot era -> Map (KeyHash StakePool) (CompactForm Coin)
psPoolDistr (PulsingSnapshot era -> Map (KeyHash StakePool) (CompactForm Coin))
-> ((PulsingSnapshot era, RatifyState era) -> PulsingSnapshot era)
-> (PulsingSnapshot era, RatifyState era)
-> Map (KeyHash StakePool) (CompactForm Coin)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PulsingSnapshot era, RatifyState era) -> PulsingSnapshot era
forall a b. (a, b) -> a
fst ((PulsingSnapshot era, RatifyState era)
 -> Map (KeyHash StakePool) (CompactForm Coin))
-> (PulsingSnapshot era, RatifyState era)
-> Map (KeyHash StakePool) (CompactForm Coin)
forall a b. (a -> b) -> a -> b
$ NewEpochState era -> (PulsingSnapshot era, RatifyState era)
forall era.
ConwayEraGov era =>
NewEpochState era -> (PulsingSnapshot era, RatifyState era)
finishedPulserState NewEpochState era
nes

-- | Query committee members. Whenever the system is in No Confidence mode this query will
-- return `Nothing`.
queryCommitteeMembersState ::
  forall era.
  (ConwayEraGov era, ConwayEraCertState era) =>
  -- | filter by cold credentials (don't filter when empty)
  Set (Credential ColdCommitteeRole) ->
  -- | filter by hot credentials (don't filter when empty)
  Set (Credential HotCommitteeRole) ->
  -- | filter by status (don't filter when empty)
  -- (useful, for discovering, for example, only active members)
  Set MemberStatus ->
  NewEpochState era ->
  CommitteeMembersState
queryCommitteeMembersState :: forall era.
(ConwayEraGov era, ConwayEraCertState era) =>
Set (Credential ColdCommitteeRole)
-> Set (Credential HotCommitteeRole)
-> Set MemberStatus
-> NewEpochState era
-> CommitteeMembersState
queryCommitteeMembersState Set (Credential ColdCommitteeRole)
coldCredsFilter Set (Credential HotCommitteeRole)
hotCredsFilter Set MemberStatus
statusFilter NewEpochState era
nes =
  let
    committee :: StrictMaybe (Committee era)
committee = NewEpochState era -> GovState era
forall era. NewEpochState era -> GovState era
queryGovState NewEpochState era
nes GovState era
-> Getting
     (StrictMaybe (Committee era))
     (GovState era)
     (StrictMaybe (Committee era))
-> StrictMaybe (Committee era)
forall s a. s -> Getting a s a -> a
^. Getting
  (StrictMaybe (Committee era))
  (GovState era)
  (StrictMaybe (Committee era))
forall era.
ConwayEraGov era =>
Lens' (GovState era) (StrictMaybe (Committee era))
Lens' (GovState era) (StrictMaybe (Committee era))
committeeGovStateL
    comMembers :: Map (Credential ColdCommitteeRole) EpochNo
comMembers = (Committee era -> Map (Credential ColdCommitteeRole) EpochNo)
-> StrictMaybe (Committee era)
-> Map (Credential ColdCommitteeRole) EpochNo
forall m a. Monoid m => (a -> m) -> StrictMaybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap' Committee era -> Map (Credential ColdCommitteeRole) EpochNo
forall era.
Committee era -> Map (Credential ColdCommitteeRole) EpochNo
committeeMembers StrictMaybe (Committee era)
committee
    nextComMembers :: Map (Credential ColdCommitteeRole) EpochNo
nextComMembers = NewEpochState era -> Map (Credential ColdCommitteeRole) EpochNo
forall era.
ConwayEraGov era =>
NewEpochState era -> Map (Credential ColdCommitteeRole) EpochNo
getNextEpochCommitteeMembers NewEpochState era
nes
    comStateMembers :: Map (Credential ColdCommitteeRole) CommitteeAuthorization
comStateMembers =
      CommitteeState era
-> Map (Credential ColdCommitteeRole) CommitteeAuthorization
forall era.
CommitteeState era
-> Map (Credential ColdCommitteeRole) CommitteeAuthorization
csCommitteeCreds (CommitteeState era
 -> Map (Credential ColdCommitteeRole) CommitteeAuthorization)
-> CommitteeState era
-> Map (Credential ColdCommitteeRole) CommitteeAuthorization
forall a b. (a -> b) -> a -> b
$
        NewEpochState era
nes NewEpochState era
-> Getting
     (CommitteeState era) (NewEpochState era) (CommitteeState era)
-> CommitteeState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (CommitteeState era) (EpochState era))
-> NewEpochState era
-> Const (CommitteeState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEpochStateL ((EpochState era -> Const (CommitteeState era) (EpochState era))
 -> NewEpochState era
 -> Const (CommitteeState era) (NewEpochState era))
-> ((CommitteeState era
     -> Const (CommitteeState era) (CommitteeState era))
    -> EpochState era -> Const (CommitteeState era) (EpochState era))
-> Getting
     (CommitteeState era) (NewEpochState era) (CommitteeState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (CommitteeState era) (LedgerState era))
-> EpochState era -> Const (CommitteeState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (CommitteeState era) (LedgerState era))
 -> EpochState era -> Const (CommitteeState era) (EpochState era))
-> ((CommitteeState era
     -> Const (CommitteeState era) (CommitteeState era))
    -> LedgerState era -> Const (CommitteeState era) (LedgerState era))
-> (CommitteeState era
    -> Const (CommitteeState era) (CommitteeState era))
-> EpochState era
-> Const (CommitteeState era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (CommitteeState era) (CertState era))
-> LedgerState era -> Const (CommitteeState era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (CommitteeState era) (CertState era))
 -> LedgerState era -> Const (CommitteeState era) (LedgerState era))
-> ((CommitteeState era
     -> Const (CommitteeState era) (CommitteeState era))
    -> CertState era -> Const (CommitteeState era) (CertState era))
-> (CommitteeState era
    -> Const (CommitteeState era) (CommitteeState era))
-> LedgerState era
-> Const (CommitteeState era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VState era -> Const (CommitteeState era) (VState era))
-> CertState era -> Const (CommitteeState era) (CertState era)
forall era.
ConwayEraCertState era =>
Lens' (CertState era) (VState era)
Lens' (CertState era) (VState era)
certVStateL ((VState era -> Const (CommitteeState era) (VState era))
 -> CertState era -> Const (CommitteeState era) (CertState era))
-> ((CommitteeState era
     -> Const (CommitteeState era) (CommitteeState era))
    -> VState era -> Const (CommitteeState era) (VState era))
-> (CommitteeState era
    -> Const (CommitteeState era) (CommitteeState era))
-> CertState era
-> Const (CommitteeState era) (CertState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CommitteeState era
 -> Const (CommitteeState era) (CommitteeState era))
-> VState era -> Const (CommitteeState era) (VState era)
forall era (f :: * -> *).
Functor f =>
(CommitteeState era -> f (CommitteeState era))
-> VState era -> f (VState era)
vsCommitteeStateL

    withFilteredColdCreds :: Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
withFilteredColdCreds Set (Credential ColdCommitteeRole)
s
      | Set (Credential ColdCommitteeRole) -> Bool
forall a. Set a -> Bool
Set.null Set (Credential ColdCommitteeRole)
coldCredsFilter = Set (Credential ColdCommitteeRole)
s
      | Bool
otherwise = Set (Credential ColdCommitteeRole)
s Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
forall a. Ord a => Set a -> Set a -> Set a
`Set.intersection` Set (Credential ColdCommitteeRole)
coldCredsFilter

    relevantColdKeys :: Set (Credential ColdCommitteeRole)
relevantColdKeys
      | Set MemberStatus -> Bool
forall a. Set a -> Bool
Set.null Set MemberStatus
statusFilter Bool -> Bool -> Bool
|| MemberStatus -> Set MemberStatus -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member MemberStatus
Unrecognized Set MemberStatus
statusFilter =
          Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
withFilteredColdCreds (Set (Credential ColdCommitteeRole)
 -> Set (Credential ColdCommitteeRole))
-> Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
forall a b. (a -> b) -> a -> b
$
            [Set (Credential ColdCommitteeRole)]
-> Set (Credential ColdCommitteeRole)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
              [ Map (Credential ColdCommitteeRole) EpochNo
-> Set (Credential ColdCommitteeRole)
forall k a. Map k a -> Set k
Map.keysSet Map (Credential ColdCommitteeRole) EpochNo
comMembers
              , Map (Credential ColdCommitteeRole) CommitteeAuthorization
-> Set (Credential ColdCommitteeRole)
forall k a. Map k a -> Set k
Map.keysSet Map (Credential ColdCommitteeRole) CommitteeAuthorization
comStateMembers
              , Map (Credential ColdCommitteeRole) EpochNo
-> Set (Credential ColdCommitteeRole)
forall k a. Map k a -> Set k
Map.keysSet Map (Credential ColdCommitteeRole) EpochNo
nextComMembers
              ]
      | Bool
otherwise = Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
withFilteredColdCreds (Set (Credential ColdCommitteeRole)
 -> Set (Credential ColdCommitteeRole))
-> Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
forall a b. (a -> b) -> a -> b
$ Map (Credential ColdCommitteeRole) EpochNo
-> Set (Credential ColdCommitteeRole)
forall k a. Map k a -> Set k
Map.keysSet Map (Credential ColdCommitteeRole) EpochNo
comMembers

    relevantHotKeys :: Set (Credential ColdCommitteeRole)
relevantHotKeys =
      [Credential ColdCommitteeRole]
-> Set (Credential ColdCommitteeRole)
forall a. Ord a => [a] -> Set a
Set.fromList
        [ Credential ColdCommitteeRole
ck
        | (Credential ColdCommitteeRole
ck, CommitteeHotCredential Credential HotCommitteeRole
hk) <- Map (Credential ColdCommitteeRole) CommitteeAuthorization
-> [(Credential ColdCommitteeRole, CommitteeAuthorization)]
forall k a. Map k a -> [(k, a)]
Map.toList Map (Credential ColdCommitteeRole) CommitteeAuthorization
comStateMembers
        , Credential HotCommitteeRole
hk Credential HotCommitteeRole
-> Set (Credential HotCommitteeRole) -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set (Credential HotCommitteeRole)
hotCredsFilter
        ]

    relevant :: Set (Credential ColdCommitteeRole)
relevant
      | Set (Credential HotCommitteeRole) -> Bool
forall a. Set a -> Bool
Set.null Set (Credential HotCommitteeRole)
hotCredsFilter = Set (Credential ColdCommitteeRole)
relevantColdKeys
      | Bool
otherwise = Set (Credential ColdCommitteeRole)
relevantColdKeys Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
-> Set (Credential ColdCommitteeRole)
forall a. Ord a => Set a -> Set a -> Set a
`Set.intersection` Set (Credential ColdCommitteeRole)
relevantHotKeys

    cms :: Map (Credential ColdCommitteeRole) CommitteeMemberState
cms = (Maybe CommitteeMemberState -> Maybe CommitteeMemberState)
-> Map (Credential ColdCommitteeRole) (Maybe CommitteeMemberState)
-> Map (Credential ColdCommitteeRole) CommitteeMemberState
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybe Maybe CommitteeMemberState -> Maybe CommitteeMemberState
forall a. a -> a
id (Map (Credential ColdCommitteeRole) (Maybe CommitteeMemberState)
 -> Map (Credential ColdCommitteeRole) CommitteeMemberState)
-> Map (Credential ColdCommitteeRole) (Maybe CommitteeMemberState)
-> Map (Credential ColdCommitteeRole) CommitteeMemberState
forall a b. (a -> b) -> a -> b
$ (Credential ColdCommitteeRole -> Maybe CommitteeMemberState)
-> Set (Credential ColdCommitteeRole)
-> Map (Credential ColdCommitteeRole) (Maybe CommitteeMemberState)
forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet Credential ColdCommitteeRole -> Maybe CommitteeMemberState
mkMaybeMemberState Set (Credential ColdCommitteeRole)
relevant
    currentEpoch :: EpochNo
currentEpoch = NewEpochState era
nes NewEpochState era
-> Getting EpochNo (NewEpochState era) EpochNo -> EpochNo
forall s a. s -> Getting a s a -> a
^. Getting EpochNo (NewEpochState era) EpochNo
forall era (f :: * -> *).
Functor f =>
(EpochNo -> f EpochNo)
-> NewEpochState era -> f (NewEpochState era)
nesELL

    mkMaybeMemberState ::
      Credential ColdCommitteeRole ->
      Maybe CommitteeMemberState
    mkMaybeMemberState :: Credential ColdCommitteeRole -> Maybe CommitteeMemberState
mkMaybeMemberState Credential ColdCommitteeRole
coldCred = do
      let mbExpiry :: Maybe EpochNo
mbExpiry = Credential ColdCommitteeRole
-> Map (Credential ColdCommitteeRole) EpochNo -> Maybe EpochNo
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential ColdCommitteeRole
coldCred Map (Credential ColdCommitteeRole) EpochNo
comMembers
      let status :: MemberStatus
status = case Maybe EpochNo
mbExpiry of
            Maybe EpochNo
Nothing -> MemberStatus
Unrecognized
            Just EpochNo
expiry
              | EpochNo
currentEpoch EpochNo -> EpochNo -> Bool
forall a. Ord a => a -> a -> Bool
> EpochNo
expiry -> MemberStatus
Expired
              | Bool
otherwise -> MemberStatus
Active
      Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Set MemberStatus -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set MemberStatus
statusFilter Bool -> Bool -> Bool
|| MemberStatus
status MemberStatus -> Set MemberStatus -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set MemberStatus
statusFilter)
      let hkStatus :: HotCredAuthStatus
hkStatus =
            case Credential ColdCommitteeRole
-> Map (Credential ColdCommitteeRole) CommitteeAuthorization
-> Maybe CommitteeAuthorization
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential ColdCommitteeRole
coldCred Map (Credential ColdCommitteeRole) CommitteeAuthorization
comStateMembers of
              Maybe CommitteeAuthorization
Nothing -> HotCredAuthStatus
MemberNotAuthorized
              Just (CommitteeMemberResigned StrictMaybe Anchor
anchor) -> Maybe Anchor -> HotCredAuthStatus
MemberResigned (StrictMaybe Anchor -> Maybe Anchor
forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe StrictMaybe Anchor
anchor)
              Just (CommitteeHotCredential Credential HotCommitteeRole
hk) -> Credential HotCommitteeRole -> HotCredAuthStatus
MemberAuthorized Credential HotCommitteeRole
hk
      CommitteeMemberState -> Maybe CommitteeMemberState
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CommitteeMemberState -> Maybe CommitteeMemberState)
-> CommitteeMemberState -> Maybe CommitteeMemberState
forall a b. (a -> b) -> a -> b
$ HotCredAuthStatus
-> MemberStatus
-> Maybe EpochNo
-> NextEpochChange
-> CommitteeMemberState
CommitteeMemberState HotCredAuthStatus
hkStatus MemberStatus
status Maybe EpochNo
mbExpiry (Credential ColdCommitteeRole -> NextEpochChange
nextEpochChange Credential ColdCommitteeRole
coldCred)

    nextEpochChange :: Credential ColdCommitteeRole -> NextEpochChange
    nextEpochChange :: Credential ColdCommitteeRole -> NextEpochChange
nextEpochChange Credential ColdCommitteeRole
ck
      | Bool -> Bool
not Bool
inCurrent Bool -> Bool -> Bool
&& Bool
inNext = NextEpochChange
ToBeEnacted
      | Bool -> Bool
not Bool
inNext = NextEpochChange
ToBeRemoved
      | Just EpochNo
curTerm <- Maybe EpochNo
lookupCurrent
      , Just EpochNo
nextTerm <- Maybe EpochNo
lookupNext
      , EpochNo
curTerm EpochNo -> EpochNo -> Bool
forall a. Eq a => a -> a -> Bool
/= EpochNo
nextTerm
      , -- if the term is adjusted such that it expires in the next epoch,
        -- we set it to ToBeExpired instead of TermAdjusted
        Bool -> Bool
not Bool
expiringNext =
          EpochNo -> NextEpochChange
TermAdjusted EpochNo
nextTerm
      | Bool
expiringCurrent Bool -> Bool -> Bool
|| Bool
expiringNext = NextEpochChange
ToBeExpired
      | Bool
otherwise = NextEpochChange
NoChangeExpected
      where
        lookupCurrent :: Maybe EpochNo
lookupCurrent = Credential ColdCommitteeRole
-> Map (Credential ColdCommitteeRole) EpochNo -> Maybe EpochNo
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential ColdCommitteeRole
ck Map (Credential ColdCommitteeRole) EpochNo
comMembers
        lookupNext :: Maybe EpochNo
lookupNext = Credential ColdCommitteeRole
-> Map (Credential ColdCommitteeRole) EpochNo -> Maybe EpochNo
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Credential ColdCommitteeRole
ck Map (Credential ColdCommitteeRole) EpochNo
nextComMembers
        inCurrent :: Bool
inCurrent = Maybe EpochNo -> Bool
forall a. Maybe a -> Bool
isJust Maybe EpochNo
lookupCurrent
        inNext :: Bool
inNext = Maybe EpochNo -> Bool
forall a. Maybe a -> Bool
isJust Maybe EpochNo
lookupNext
        expiringCurrent :: Bool
expiringCurrent = Maybe EpochNo
lookupCurrent Maybe EpochNo -> Maybe EpochNo -> Bool
forall a. Eq a => a -> a -> Bool
== EpochNo -> Maybe EpochNo
forall a. a -> Maybe a
Just EpochNo
currentEpoch
        expiringNext :: Bool
expiringNext = Maybe EpochNo
lookupNext Maybe EpochNo -> Maybe EpochNo -> Bool
forall a. Eq a => a -> a -> Bool
== EpochNo -> Maybe EpochNo
forall a. a -> Maybe a
Just EpochNo
currentEpoch
   in
    CommitteeMembersState
      { csCommittee :: Map (Credential ColdCommitteeRole) CommitteeMemberState
csCommittee = Map (Credential ColdCommitteeRole) CommitteeMemberState
cms
      , csThreshold :: Maybe UnitInterval
csThreshold = StrictMaybe UnitInterval -> Maybe UnitInterval
forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe (StrictMaybe UnitInterval -> Maybe UnitInterval)
-> StrictMaybe UnitInterval -> Maybe UnitInterval
forall a b. (a -> b) -> a -> b
$ (Committee era
-> Getting UnitInterval (Committee era) UnitInterval
-> UnitInterval
forall s a. s -> Getting a s a -> a
^. Getting UnitInterval (Committee era) UnitInterval
forall era (f :: * -> *).
Functor f =>
(UnitInterval -> f UnitInterval)
-> Committee era -> f (Committee era)
committeeThresholdL) (Committee era -> UnitInterval)
-> StrictMaybe (Committee era) -> StrictMaybe UnitInterval
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictMaybe (Committee era)
committee
      , csEpochNo :: EpochNo
csEpochNo = EpochNo
currentEpoch
      }

queryChainAccountState ::
  NewEpochState era ->
  ChainAccountState
queryChainAccountState :: forall era. NewEpochState era -> ChainAccountState
queryChainAccountState = Getting ChainAccountState (NewEpochState era) ChainAccountState
-> NewEpochState era -> ChainAccountState
forall a s. Getting a s a -> s -> a
view Getting ChainAccountState (NewEpochState era) ChainAccountState
forall era. Lens' (NewEpochState era) ChainAccountState
forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) ChainAccountState
chainAccountStateL

getNextEpochCommitteeMembers ::
  ConwayEraGov era =>
  NewEpochState era ->
  Map (Credential ColdCommitteeRole) EpochNo
getNextEpochCommitteeMembers :: forall era.
ConwayEraGov era =>
NewEpochState era -> Map (Credential ColdCommitteeRole) EpochNo
getNextEpochCommitteeMembers NewEpochState era
nes =
  let ratifyState :: RatifyState era
ratifyState = NewEpochState era -> RatifyState era
forall era.
ConwayEraGov era =>
NewEpochState era -> RatifyState era
queryRatifyState NewEpochState era
nes
      committee :: StrictMaybe (Committee era)
committee = RatifyState era
ratifyState RatifyState era
-> Getting
     (StrictMaybe (Committee era))
     (RatifyState era)
     (StrictMaybe (Committee era))
-> StrictMaybe (Committee era)
forall s a. s -> Getting a s a -> a
^. (EnactState era
 -> Const (StrictMaybe (Committee era)) (EnactState era))
-> RatifyState era
-> Const (StrictMaybe (Committee era)) (RatifyState era)
forall era (f :: * -> *).
Functor f =>
(EnactState era -> f (EnactState era))
-> RatifyState era -> f (RatifyState era)
rsEnactStateL ((EnactState era
  -> Const (StrictMaybe (Committee era)) (EnactState era))
 -> RatifyState era
 -> Const (StrictMaybe (Committee era)) (RatifyState era))
-> ((StrictMaybe (Committee era)
     -> Const
          (StrictMaybe (Committee era)) (StrictMaybe (Committee era)))
    -> EnactState era
    -> Const (StrictMaybe (Committee era)) (EnactState era))
-> Getting
     (StrictMaybe (Committee era))
     (RatifyState era)
     (StrictMaybe (Committee era))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StrictMaybe (Committee era)
 -> Const
      (StrictMaybe (Committee era)) (StrictMaybe (Committee era)))
-> EnactState era
-> Const (StrictMaybe (Committee era)) (EnactState era)
forall era (f :: * -> *).
Functor f =>
(StrictMaybe (Committee era) -> f (StrictMaybe (Committee era)))
-> EnactState era -> f (EnactState era)
ensCommitteeL
   in (Committee era -> Map (Credential ColdCommitteeRole) EpochNo)
-> StrictMaybe (Committee era)
-> Map (Credential ColdCommitteeRole) EpochNo
forall m a. Monoid m => (a -> m) -> StrictMaybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap' Committee era -> Map (Credential ColdCommitteeRole) EpochNo
forall era.
Committee era -> Map (Credential ColdCommitteeRole) EpochNo
committeeMembers StrictMaybe (Committee era)
committee

-- | This is a simple lookup into the state for the values of current protocol
-- parameters. These values can change on the epoch boundary. Use `queryFuturePParams` to
-- see if we are aware of any upcoming changes.
queryCurrentPParams :: EraGov era => NewEpochState era -> PParams era
queryCurrentPParams :: forall era. EraGov era => NewEpochState era -> PParams era
queryCurrentPParams NewEpochState era
nes = NewEpochState era -> GovState era
forall era. NewEpochState era -> GovState era
queryGovState NewEpochState era
nes GovState era
-> Getting (PParams era) (GovState era) (PParams era)
-> PParams era
forall s a. s -> Getting a s a -> a
^. Getting (PParams era) (GovState era) (PParams era)
forall era. EraGov era => Lens' (GovState era) (PParams era)
Lens' (GovState era) (PParams era)
curPParamsGovStateL

-- | This query will return values for protocol parameters that are likely to be adopted
-- at the next epoch boundary. It is only when we passed 2 stability windows before the
-- end of the epoch that users can rely on this query to produce stable results.
queryFuturePParams :: EraGov era => NewEpochState era -> Maybe (PParams era)
queryFuturePParams :: forall era. EraGov era => NewEpochState era -> Maybe (PParams era)
queryFuturePParams NewEpochState era
nes =
  case NewEpochState era -> GovState era
forall era. NewEpochState era -> GovState era
queryGovState NewEpochState era
nes GovState era
-> Getting (FuturePParams era) (GovState era) (FuturePParams era)
-> FuturePParams era
forall s a. s -> Getting a s a -> a
^. Getting (FuturePParams era) (GovState era) (FuturePParams era)
forall era. EraGov era => Lens' (GovState era) (FuturePParams era)
Lens' (GovState era) (FuturePParams era)
futurePParamsGovStateL of
    FuturePParams era
NoPParamsUpdate -> Maybe (PParams era)
forall a. Maybe a
Nothing
    PotentialPParamsUpdate Maybe (PParams era)
mpp -> Maybe (PParams era)
mpp
    DefinitePParamsUpdate PParams era
pp -> PParams era -> Maybe (PParams era)
forall a. a -> Maybe a
Just PParams era
pp

-- | Query proposals that are considered for ratification.
queryProposals ::
  ConwayEraGov era =>
  NewEpochState era ->
  -- | Specify a set of Governance Action IDs to filter the proposals. When this set is
  -- empty, all the proposals considered for ratification will be returned.
  Set GovActionId ->
  Seq (GovActionState era)
queryProposals :: forall era.
ConwayEraGov era =>
NewEpochState era -> Set GovActionId -> Seq (GovActionState era)
queryProposals NewEpochState era
nes Set GovActionId
gids
  | Set GovActionId -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Set GovActionId
gids = Seq (GovActionState era)
proposals
  -- TODO: Add `filter` to `cardano-strict-containers`
  | Bool
otherwise =
      (GovActionState era -> Bool)
-> Seq (GovActionState era) -> Seq (GovActionState era)
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.filter (\GovActionState {Map (KeyHash StakePool) Vote
Map (Credential DRepRole) Vote
Map (Credential HotCommitteeRole) Vote
ProposalProcedure era
GovActionId
EpochNo
gasId :: GovActionId
gasCommitteeVotes :: Map (Credential HotCommitteeRole) Vote
gasDRepVotes :: Map (Credential DRepRole) Vote
gasStakePoolVotes :: Map (KeyHash StakePool) Vote
gasProposalProcedure :: ProposalProcedure era
gasProposedIn :: EpochNo
gasExpiresAfter :: EpochNo
gasExpiresAfter :: forall era. GovActionState era -> EpochNo
gasProposedIn :: forall era. GovActionState era -> EpochNo
gasProposalProcedure :: forall era. GovActionState era -> ProposalProcedure era
gasStakePoolVotes :: forall era. GovActionState era -> Map (KeyHash StakePool) Vote
gasDRepVotes :: forall era. GovActionState era -> Map (Credential DRepRole) Vote
gasCommitteeVotes :: forall era.
GovActionState era -> Map (Credential HotCommitteeRole) Vote
gasId :: forall era. GovActionState era -> GovActionId
..} -> GovActionId
gasId GovActionId -> Set GovActionId -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set GovActionId
gids) Seq (GovActionState era)
proposals
  where
    proposals :: Seq (GovActionState era)
proposals = StrictSeq (GovActionState era) -> Seq (GovActionState era)
forall a. StrictSeq a -> Seq a
fromStrict (StrictSeq (GovActionState era) -> Seq (GovActionState era))
-> StrictSeq (GovActionState era) -> Seq (GovActionState era)
forall a b. (a -> b) -> a -> b
$ case NewEpochState era
nes NewEpochState era
-> Getting
     (DRepPulsingState era) (NewEpochState era) (DRepPulsingState era)
-> DRepPulsingState era
forall s a. s -> Getting a s a -> a
^. (GovState era -> Const (DRepPulsingState era) (GovState era))
-> NewEpochState era
-> Const (DRepPulsingState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(GovState era -> f (GovState era))
-> NewEpochState era -> f (NewEpochState era)
newEpochStateGovStateL ((GovState era -> Const (DRepPulsingState era) (GovState era))
 -> NewEpochState era
 -> Const (DRepPulsingState era) (NewEpochState era))
-> ((DRepPulsingState era
     -> Const (DRepPulsingState era) (DRepPulsingState era))
    -> GovState era -> Const (DRepPulsingState era) (GovState era))
-> Getting
     (DRepPulsingState era) (NewEpochState era) (DRepPulsingState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DRepPulsingState era
 -> Const (DRepPulsingState era) (DRepPulsingState era))
-> GovState era -> Const (DRepPulsingState era) (GovState era)
forall era.
ConwayEraGov era =>
Lens' (GovState era) (DRepPulsingState era)
Lens' (GovState era) (DRepPulsingState era)
drepPulsingStateGovStateL of
      DRComplete PulsingSnapshot era
snap RatifyState era
_rs -> PulsingSnapshot era
snap PulsingSnapshot era
-> Getting
     (StrictSeq (GovActionState era))
     (PulsingSnapshot era)
     (StrictSeq (GovActionState era))
-> StrictSeq (GovActionState era)
forall s a. s -> Getting a s a -> a
^. Getting
  (StrictSeq (GovActionState era))
  (PulsingSnapshot era)
  (StrictSeq (GovActionState era))
forall era (f :: * -> *).
Functor f =>
(StrictSeq (GovActionState era)
 -> f (StrictSeq (GovActionState era)))
-> PulsingSnapshot era -> f (PulsingSnapshot era)
psProposalsL
      DRPulsing DRepPulser {Int
Map (KeyHash StakePool) StakePoolState
Map DRep (CompactForm Coin)
Map (Credential DRepRole) DRepState
Map (Credential Staking) (CompactForm Coin)
Accounts era
CommitteeState era
PoolDistr
InstantStake era
EnactState era
EpochNo
Globals
StrictSeq (GovActionState era)
dpPulseSize :: Int
dpAccounts :: Accounts era
dpIndex :: Int
dpInstantStake :: InstantStake era
dpStakePoolDistr :: PoolDistr
dpDRepDistr :: Map DRep (CompactForm Coin)
dpDRepState :: Map (Credential DRepRole) DRepState
dpCurrentEpoch :: EpochNo
dpCommitteeState :: CommitteeState era
dpEnactState :: EnactState era
dpProposals :: StrictSeq (GovActionState era)
dpProposalDeposits :: Map (Credential Staking) (CompactForm Coin)
dpGlobals :: Globals
dpStakePools :: Map (KeyHash StakePool) StakePoolState
dpStakePools :: forall era ans (m :: * -> *).
DRepPulser era m ans -> Map (KeyHash StakePool) StakePoolState
dpGlobals :: forall era ans (m :: * -> *). DRepPulser era m ans -> Globals
dpProposalDeposits :: forall era ans (m :: * -> *).
DRepPulser era m ans -> Map (Credential Staking) (CompactForm Coin)
dpProposals :: forall era ans (m :: * -> *).
DRepPulser era m ans -> StrictSeq (GovActionState era)
dpEnactState :: forall era ans (m :: * -> *).
DRepPulser era m ans -> EnactState era
dpCommitteeState :: forall era ans (m :: * -> *).
DRepPulser era m ans -> CommitteeState era
dpCurrentEpoch :: forall era ans (m :: * -> *). DRepPulser era m ans -> EpochNo
dpDRepState :: forall era ans (m :: * -> *).
DRepPulser era m ans -> Map (Credential DRepRole) DRepState
dpDRepDistr :: forall era ans (m :: * -> *).
DRepPulser era m ans -> Map DRep (CompactForm Coin)
dpStakePoolDistr :: forall era ans (m :: * -> *). DRepPulser era m ans -> PoolDistr
dpInstantStake :: forall era ans (m :: * -> *).
DRepPulser era m ans -> InstantStake era
dpIndex :: forall era ans (m :: * -> *). DRepPulser era m ans -> Int
dpAccounts :: forall era ans (m :: * -> *). DRepPulser era m ans -> Accounts era
dpPulseSize :: forall era ans (m :: * -> *). DRepPulser era m ans -> Int
..} -> StrictSeq (GovActionState era)
dpProposals

-- | Query ratification state.
queryRatifyState :: ConwayEraGov era => NewEpochState era -> RatifyState era
queryRatifyState :: forall era.
ConwayEraGov era =>
NewEpochState era -> RatifyState era
queryRatifyState = (PulsingSnapshot era, RatifyState era) -> RatifyState era
forall a b. (a, b) -> b
snd ((PulsingSnapshot era, RatifyState era) -> RatifyState era)
-> (NewEpochState era -> (PulsingSnapshot era, RatifyState era))
-> NewEpochState era
-> RatifyState era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NewEpochState era -> (PulsingSnapshot era, RatifyState era)
forall era.
ConwayEraGov era =>
NewEpochState era -> (PulsingSnapshot era, RatifyState era)
finishedPulserState

finishedPulserState ::
  ConwayEraGov era =>
  NewEpochState era ->
  (PulsingSnapshot era, RatifyState era)
finishedPulserState :: forall era.
ConwayEraGov era =>
NewEpochState era -> (PulsingSnapshot era, RatifyState era)
finishedPulserState NewEpochState era
nes = DRepPulsingState era -> (PulsingSnapshot era, RatifyState era)
forall era.
(EraStake era, ConwayEraAccounts era) =>
DRepPulsingState era -> (PulsingSnapshot era, RatifyState era)
finishDRepPulser (NewEpochState era
nes NewEpochState era
-> Getting
     (DRepPulsingState era) (NewEpochState era) (DRepPulsingState era)
-> DRepPulsingState era
forall s a. s -> Getting a s a -> a
^. (GovState era -> Const (DRepPulsingState era) (GovState era))
-> NewEpochState era
-> Const (DRepPulsingState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(GovState era -> f (GovState era))
-> NewEpochState era -> f (NewEpochState era)
newEpochStateGovStateL ((GovState era -> Const (DRepPulsingState era) (GovState era))
 -> NewEpochState era
 -> Const (DRepPulsingState era) (NewEpochState era))
-> ((DRepPulsingState era
     -> Const (DRepPulsingState era) (DRepPulsingState era))
    -> GovState era -> Const (DRepPulsingState era) (GovState era))
-> Getting
     (DRepPulsingState era) (NewEpochState era) (DRepPulsingState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DRepPulsingState era
 -> Const (DRepPulsingState era) (DRepPulsingState era))
-> GovState era -> Const (DRepPulsingState era) (GovState era)
forall era.
ConwayEraGov era =>
Lens' (GovState era) (DRepPulsingState era)
Lens' (GovState era) (DRepPulsingState era)
drepPulsingStateGovStateL)

-- | Query a stake pool's reward account delegatee which determines the pool's default vote
-- in absence of an explicit vote. Note that this is different from the delegatee determined
-- by the credential of the stake pool itself.
queryStakePoolDefaultVote ::
  (EraCertState era, ConwayEraAccounts era) =>
  NewEpochState era ->
  -- | Specify the key hash of the pool whose default vote should be returned.
  KeyHash StakePool ->
  DefaultVote
queryStakePoolDefaultVote :: forall era.
(EraCertState era, ConwayEraAccounts era) =>
NewEpochState era -> KeyHash StakePool -> DefaultVote
queryStakePoolDefaultVote NewEpochState era
nes KeyHash StakePool
poolId =
  KeyHash StakePool
-> Map (KeyHash StakePool) StakePoolState
-> Accounts era
-> DefaultVote
forall era.
ConwayEraAccounts era =>
KeyHash StakePool
-> Map (KeyHash StakePool) StakePoolState
-> Accounts era
-> DefaultVote
defaultStakePoolVote KeyHash StakePool
poolId (NewEpochState era
nes NewEpochState era
-> Getting
     (Map (KeyHash StakePool) StakePoolState)
     (NewEpochState era)
     (Map (KeyHash StakePool) StakePoolState)
-> Map (KeyHash StakePool) StakePoolState
forall s a. s -> Getting a s a -> a
^. (EpochState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
-> NewEpochState era
-> Const
     (Map (KeyHash StakePool) StakePoolState) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era
  -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
 -> NewEpochState era
 -> Const
      (Map (KeyHash StakePool) StakePoolState) (NewEpochState era))
-> ((Map (KeyHash StakePool) StakePoolState
     -> Const
          (Map (KeyHash StakePool) StakePoolState)
          (Map (KeyHash StakePool) StakePoolState))
    -> EpochState era
    -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
-> Getting
     (Map (KeyHash StakePool) StakePoolState)
     (NewEpochState era)
     (Map (KeyHash StakePool) StakePoolState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (KeyHash StakePool) StakePoolState
 -> Const
      (Map (KeyHash StakePool) StakePoolState)
      (Map (KeyHash StakePool) StakePoolState))
-> EpochState era
-> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era)
forall era.
EraCertState era =>
Lens' (EpochState era) (Map (KeyHash StakePool) StakePoolState)
Lens' (EpochState era) (Map (KeyHash StakePool) StakePoolState)
epochStateStakePoolsL) (Accounts era -> DefaultVote) -> Accounts era -> DefaultVote
forall a b. (a -> b) -> a -> b
$
    NewEpochState era
nes NewEpochState era
-> Getting (Accounts era) (NewEpochState era) (Accounts era)
-> Accounts era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (Accounts era) (EpochState era))
-> NewEpochState era -> Const (Accounts era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era -> Const (Accounts era) (EpochState era))
 -> NewEpochState era -> Const (Accounts era) (NewEpochState era))
-> ((Accounts era -> Const (Accounts era) (Accounts era))
    -> EpochState era -> Const (Accounts era) (EpochState era))
-> Getting (Accounts era) (NewEpochState era) (Accounts era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (Accounts era) (LedgerState era))
-> EpochState era -> Const (Accounts era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (Accounts era) (LedgerState era))
 -> EpochState era -> Const (Accounts era) (EpochState era))
-> ((Accounts era -> Const (Accounts era) (Accounts era))
    -> LedgerState era -> Const (Accounts era) (LedgerState era))
-> (Accounts era -> Const (Accounts era) (Accounts era))
-> EpochState era
-> Const (Accounts era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (Accounts era) (CertState era))
-> LedgerState era -> Const (Accounts era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (Accounts era) (CertState era))
 -> LedgerState era -> Const (Accounts era) (LedgerState era))
-> ((Accounts era -> Const (Accounts era) (Accounts era))
    -> CertState era -> Const (Accounts era) (CertState era))
-> (Accounts era -> Const (Accounts era) (Accounts era))
-> LedgerState era
-> Const (Accounts era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DState era -> Const (Accounts era) (DState era))
-> CertState era -> Const (Accounts era) (CertState era)
forall era. EraCertState era => Lens' (CertState era) (DState era)
Lens' (CertState era) (DState era)
certDStateL ((DState era -> Const (Accounts era) (DState era))
 -> CertState era -> Const (Accounts era) (CertState era))
-> ((Accounts era -> Const (Accounts era) (Accounts era))
    -> DState era -> Const (Accounts era) (DState era))
-> (Accounts era -> Const (Accounts era) (Accounts era))
-> CertState era
-> Const (Accounts era) (CertState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Accounts era -> Const (Accounts era) (Accounts era))
-> DState era -> Const (Accounts era) (DState era)
forall era. Lens' (DState era) (Accounts era)
forall (t :: * -> *) era.
CanSetAccounts t =>
Lens' (t era) (Accounts era)
accountsL

-- | Used only for the `queryPoolState` query. This resembles the older way of
-- representing StakePoolState in Ledger.
data QueryPoolStateResult = QueryPoolStateResult
  { QueryPoolStateResult -> Map (KeyHash StakePool) StakePoolParams
qpsrStakePoolParams :: !(Map (KeyHash StakePool) StakePoolParams)
  , QueryPoolStateResult -> Map (KeyHash StakePool) StakePoolParams
qpsrFutureStakePoolParams :: !(Map (KeyHash StakePool) StakePoolParams)
  , QueryPoolStateResult -> Map (KeyHash StakePool) EpochNo
qpsrRetiring :: !(Map (KeyHash StakePool) EpochNo)
  , QueryPoolStateResult -> Map (KeyHash StakePool) Coin
qpsrDeposits :: !(Map (KeyHash StakePool) Coin)
  }
  deriving (Int -> QueryPoolStateResult -> ShowS
[QueryPoolStateResult] -> ShowS
QueryPoolStateResult -> String
(Int -> QueryPoolStateResult -> ShowS)
-> (QueryPoolStateResult -> String)
-> ([QueryPoolStateResult] -> ShowS)
-> Show QueryPoolStateResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> QueryPoolStateResult -> ShowS
showsPrec :: Int -> QueryPoolStateResult -> ShowS
$cshow :: QueryPoolStateResult -> String
show :: QueryPoolStateResult -> String
$cshowList :: [QueryPoolStateResult] -> ShowS
showList :: [QueryPoolStateResult] -> ShowS
Show, QueryPoolStateResult -> QueryPoolStateResult -> Bool
(QueryPoolStateResult -> QueryPoolStateResult -> Bool)
-> (QueryPoolStateResult -> QueryPoolStateResult -> Bool)
-> Eq QueryPoolStateResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: QueryPoolStateResult -> QueryPoolStateResult -> Bool
== :: QueryPoolStateResult -> QueryPoolStateResult -> Bool
$c/= :: QueryPoolStateResult -> QueryPoolStateResult -> Bool
/= :: QueryPoolStateResult -> QueryPoolStateResult -> Bool
Eq)

instance EncCBOR QueryPoolStateResult where
  encCBOR :: QueryPoolStateResult -> Encoding
encCBOR (QueryPoolStateResult Map (KeyHash StakePool) StakePoolParams
a Map (KeyHash StakePool) StakePoolParams
b Map (KeyHash StakePool) EpochNo
c Map (KeyHash StakePool) Coin
d) =
    Word -> Encoding
encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map (KeyHash StakePool) StakePoolParams -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Map (KeyHash StakePool) StakePoolParams
a Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map (KeyHash StakePool) StakePoolParams -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Map (KeyHash StakePool) StakePoolParams
b Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map (KeyHash StakePool) EpochNo -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Map (KeyHash StakePool) EpochNo
c Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map (KeyHash StakePool) Coin -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Map (KeyHash StakePool) Coin
d

instance DecCBOR QueryPoolStateResult where
  decCBOR :: forall s. Decoder s QueryPoolStateResult
decCBOR = Text
-> (QueryPoolStateResult -> Int)
-> Decoder s QueryPoolStateResult
-> Decoder s QueryPoolStateResult
forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"QueryPoolStateResult" (Int -> QueryPoolStateResult -> Int
forall a b. a -> b -> a
const Int
4) (Decoder s QueryPoolStateResult -> Decoder s QueryPoolStateResult)
-> Decoder s QueryPoolStateResult -> Decoder s QueryPoolStateResult
forall a b. (a -> b) -> a -> b
$ do
    qpsrStakePoolParams <- Decoder s (Map (KeyHash StakePool) StakePoolParams)
forall s. Decoder s (Map (KeyHash StakePool) StakePoolParams)
forall a s. DecCBOR a => Decoder s a
decCBOR
    qpsrFutureStakePoolParams <- decCBOR
    qpsrRetiring <- decCBOR
    qpsrDeposits <- decCBOR
    pure
      QueryPoolStateResult {qpsrStakePoolParams, qpsrFutureStakePoolParams, qpsrRetiring, qpsrDeposits}

mkQueryPoolStateResult ::
  (forall x. Map.Map (KeyHash StakePool) x -> Map.Map (KeyHash StakePool) x) ->
  PState era ->
  Network ->
  QueryPoolStateResult
mkQueryPoolStateResult :: forall era.
(forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x)
-> PState era -> Network -> QueryPoolStateResult
mkQueryPoolStateResult forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f PState era
ps Network
network =
  QueryPoolStateResult
    { qpsrStakePoolParams :: Map (KeyHash StakePool) StakePoolParams
qpsrStakePoolParams =
        (KeyHash StakePool -> StakePoolState -> StakePoolParams)
-> Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) StakePoolParams
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey (KeyHash StakePool -> Network -> StakePoolState -> StakePoolParams
`stakePoolStateToStakePoolParams` Network
network) Map (KeyHash StakePool) StakePoolState
restrictedStakePools
    , qpsrFutureStakePoolParams :: Map (KeyHash StakePool) StakePoolParams
qpsrFutureStakePoolParams = Map (KeyHash StakePool) StakePoolParams
-> Map (KeyHash StakePool) StakePoolParams
forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f (Map (KeyHash StakePool) StakePoolParams
 -> Map (KeyHash StakePool) StakePoolParams)
-> Map (KeyHash StakePool) StakePoolParams
-> Map (KeyHash StakePool) StakePoolParams
forall a b. (a -> b) -> a -> b
$ PState era -> Map (KeyHash StakePool) StakePoolParams
forall era. PState era -> Map (KeyHash StakePool) StakePoolParams
psFutureStakePoolParams PState era
ps
    , qpsrRetiring :: Map (KeyHash StakePool) EpochNo
qpsrRetiring = Map (KeyHash StakePool) EpochNo -> Map (KeyHash StakePool) EpochNo
forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f (Map (KeyHash StakePool) EpochNo
 -> Map (KeyHash StakePool) EpochNo)
-> Map (KeyHash StakePool) EpochNo
-> Map (KeyHash StakePool) EpochNo
forall a b. (a -> b) -> a -> b
$ PState era -> Map (KeyHash StakePool) EpochNo
forall era. PState era -> Map (KeyHash StakePool) EpochNo
psRetiring PState era
ps
    , qpsrDeposits :: Map (KeyHash StakePool) Coin
qpsrDeposits = (StakePoolState -> Coin)
-> Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) Coin
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (CompactForm Coin -> Coin
forall a. Compactible a => CompactForm a -> a
fromCompact (CompactForm Coin -> Coin)
-> (StakePoolState -> CompactForm Coin) -> StakePoolState -> Coin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StakePoolState -> CompactForm Coin
spsDeposit) Map (KeyHash StakePool) StakePoolState
restrictedStakePools
    }
  where
    restrictedStakePools :: Map (KeyHash StakePool) StakePoolState
restrictedStakePools = Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) StakePoolState
forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f (Map (KeyHash StakePool) StakePoolState
 -> Map (KeyHash StakePool) StakePoolState)
-> Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) StakePoolState
forall a b. (a -> b) -> a -> b
$ PState era -> Map (KeyHash StakePool) StakePoolState
forall era. PState era -> Map (KeyHash StakePool) StakePoolState
psStakePools PState era
ps

-- | Query the QueryPoolStateResult. This is slightly different from the internal
-- representation used by Ledger and is intended to resemble how the internal
-- representation used to be.
queryPoolState ::
  EraCertState era =>
  NewEpochState era -> Maybe (Set (KeyHash StakePool)) -> Network -> QueryPoolStateResult
queryPoolState :: forall era.
EraCertState era =>
NewEpochState era
-> Maybe (Set (KeyHash StakePool))
-> Network
-> QueryPoolStateResult
queryPoolState NewEpochState era
nes Maybe (Set (KeyHash StakePool))
mPoolKeys Network
network =
  let pstate :: PState era
pstate = NewEpochState era
nes NewEpochState era
-> Getting (PState era) (NewEpochState era) (PState era)
-> PState era
forall s a. s -> Getting a s a -> a
^. (EpochState era -> Const (PState era) (EpochState era))
-> NewEpochState era -> Const (PState era) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era -> Const (PState era) (EpochState era))
 -> NewEpochState era -> Const (PState era) (NewEpochState era))
-> ((PState era -> Const (PState era) (PState era))
    -> EpochState era -> Const (PState era) (EpochState era))
-> Getting (PState era) (NewEpochState era) (PState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era -> Const (PState era) (LedgerState era))
-> EpochState era -> Const (PState era) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era -> Const (PState era) (LedgerState era))
 -> EpochState era -> Const (PState era) (EpochState era))
-> ((PState era -> Const (PState era) (PState era))
    -> LedgerState era -> Const (PState era) (LedgerState era))
-> (PState era -> Const (PState era) (PState era))
-> EpochState era
-> Const (PState era) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era -> Const (PState era) (CertState era))
-> LedgerState era -> Const (PState era) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era -> Const (PState era) (CertState era))
 -> LedgerState era -> Const (PState era) (LedgerState era))
-> ((PState era -> Const (PState era) (PState era))
    -> CertState era -> Const (PState era) (CertState era))
-> (PState era -> Const (PState era) (PState era))
-> LedgerState era
-> Const (PState era) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PState era -> Const (PState era) (PState era))
-> CertState era -> Const (PState era) (CertState era)
forall era. EraCertState era => Lens' (CertState era) (PState era)
Lens' (CertState era) (PState era)
certPStateL
      f :: forall x. Map.Map (KeyHash StakePool) x -> Map.Map (KeyHash StakePool) x
      f :: forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f = case Maybe (Set (KeyHash StakePool))
mPoolKeys of
        Maybe (Set (KeyHash StakePool))
Nothing -> Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
forall a. a -> a
id
        Just Set (KeyHash StakePool)
keys -> (Map (KeyHash StakePool) x
-> Set (KeyHash StakePool) -> Map (KeyHash StakePool) x
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.restrictKeys` Set (KeyHash StakePool)
keys)
   in (forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x)
-> PState era -> Network -> QueryPoolStateResult
forall era.
(forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x)
-> PState era -> Network -> QueryPoolStateResult
mkQueryPoolStateResult Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
forall x. Map (KeyHash StakePool) x -> Map (KeyHash StakePool) x
f PState era
pstate Network
network

-- | Query the current StakePoolParams.
queryPoolParameters ::
  EraCertState era =>
  Network ->
  NewEpochState era ->
  Set (KeyHash StakePool) ->
  Map (KeyHash StakePool) StakePoolParams
queryPoolParameters :: forall era.
EraCertState era =>
Network
-> NewEpochState era
-> Set (KeyHash StakePool)
-> Map (KeyHash StakePool) StakePoolParams
queryPoolParameters Network
network NewEpochState era
nes Set (KeyHash StakePool)
poolKeys =
  let pools :: Map (KeyHash StakePool) StakePoolState
pools = NewEpochState era
nes NewEpochState era
-> Getting
     (Map (KeyHash StakePool) StakePoolState)
     (NewEpochState era)
     (Map (KeyHash StakePool) StakePoolState)
-> Map (KeyHash StakePool) StakePoolState
forall s a. s -> Getting a s a -> a
^. (EpochState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
-> NewEpochState era
-> Const
     (Map (KeyHash StakePool) StakePoolState) (NewEpochState era)
forall era (f :: * -> *).
Functor f =>
(EpochState era -> f (EpochState era))
-> NewEpochState era -> f (NewEpochState era)
nesEsL ((EpochState era
  -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
 -> NewEpochState era
 -> Const
      (Map (KeyHash StakePool) StakePoolState) (NewEpochState era))
-> ((Map (KeyHash StakePool) StakePoolState
     -> Const
          (Map (KeyHash StakePool) StakePoolState)
          (Map (KeyHash StakePool) StakePoolState))
    -> EpochState era
    -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
-> Getting
     (Map (KeyHash StakePool) StakePoolState)
     (NewEpochState era)
     (Map (KeyHash StakePool) StakePoolState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LedgerState era
 -> Const
      (Map (KeyHash StakePool) StakePoolState) (LedgerState era))
-> EpochState era
-> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era)
forall era (f :: * -> *).
Functor f =>
(LedgerState era -> f (LedgerState era))
-> EpochState era -> f (EpochState era)
esLStateL ((LedgerState era
  -> Const
       (Map (KeyHash StakePool) StakePoolState) (LedgerState era))
 -> EpochState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era))
-> ((Map (KeyHash StakePool) StakePoolState
     -> Const
          (Map (KeyHash StakePool) StakePoolState)
          (Map (KeyHash StakePool) StakePoolState))
    -> LedgerState era
    -> Const
         (Map (KeyHash StakePool) StakePoolState) (LedgerState era))
-> (Map (KeyHash StakePool) StakePoolState
    -> Const
         (Map (KeyHash StakePool) StakePoolState)
         (Map (KeyHash StakePool) StakePoolState))
-> EpochState era
-> Const (Map (KeyHash StakePool) StakePoolState) (EpochState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CertState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (CertState era))
-> LedgerState era
-> Const (Map (KeyHash StakePool) StakePoolState) (LedgerState era)
forall era (f :: * -> *).
Functor f =>
(CertState era -> f (CertState era))
-> LedgerState era -> f (LedgerState era)
lsCertStateL ((CertState era
  -> Const (Map (KeyHash StakePool) StakePoolState) (CertState era))
 -> LedgerState era
 -> Const
      (Map (KeyHash StakePool) StakePoolState) (LedgerState era))
-> ((Map (KeyHash StakePool) StakePoolState
     -> Const
          (Map (KeyHash StakePool) StakePoolState)
          (Map (KeyHash StakePool) StakePoolState))
    -> CertState era
    -> Const (Map (KeyHash StakePool) StakePoolState) (CertState era))
-> (Map (KeyHash StakePool) StakePoolState
    -> Const
         (Map (KeyHash StakePool) StakePoolState)
         (Map (KeyHash StakePool) StakePoolState))
-> LedgerState era
-> Const (Map (KeyHash StakePool) StakePoolState) (LedgerState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (PState era))
-> CertState era
-> Const (Map (KeyHash StakePool) StakePoolState) (CertState era)
forall era. EraCertState era => Lens' (CertState era) (PState era)
Lens' (CertState era) (PState era)
certPStateL ((PState era
  -> Const (Map (KeyHash StakePool) StakePoolState) (PState era))
 -> CertState era
 -> Const (Map (KeyHash StakePool) StakePoolState) (CertState era))
-> ((Map (KeyHash StakePool) StakePoolState
     -> Const
          (Map (KeyHash StakePool) StakePoolState)
          (Map (KeyHash StakePool) StakePoolState))
    -> PState era
    -> Const (Map (KeyHash StakePool) StakePoolState) (PState era))
-> (Map (KeyHash StakePool) StakePoolState
    -> Const
         (Map (KeyHash StakePool) StakePoolState)
         (Map (KeyHash StakePool) StakePoolState))
-> CertState era
-> Const (Map (KeyHash StakePool) StakePoolState) (CertState era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map (KeyHash StakePool) StakePoolState
 -> Const
      (Map (KeyHash StakePool) StakePoolState)
      (Map (KeyHash StakePool) StakePoolState))
-> PState era
-> Const (Map (KeyHash StakePool) StakePoolState) (PState era)
forall era (f :: * -> *).
Functor f =>
(Map (KeyHash StakePool) StakePoolState
 -> f (Map (KeyHash StakePool) StakePoolState))
-> PState era -> f (PState era)
psStakePoolsL
   in (KeyHash StakePool -> StakePoolState -> StakePoolParams)
-> Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) StakePoolParams
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey (KeyHash StakePool -> Network -> StakePoolState -> StakePoolParams
`stakePoolStateToStakePoolParams` Network
network) (Map (KeyHash StakePool) StakePoolState
 -> Map (KeyHash StakePool) StakePoolParams)
-> Map (KeyHash StakePool) StakePoolState
-> Map (KeyHash StakePool) StakePoolParams
forall a b. (a -> b) -> a -> b
$ Map (KeyHash StakePool) StakePoolState
-> Set (KeyHash StakePool)
-> Map (KeyHash StakePool) StakePoolState
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Map (KeyHash StakePool) StakePoolState
pools Set (KeyHash StakePool)
poolKeys