diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs index 2f77c9a81d..1008a46944 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs @@ -25,6 +25,7 @@ module Cardano.CLI.EraBased.Commands.Query , QueryDRepStateCmdArgs(..) , QueryDRepStakeDistributionCmdArgs(..) , renderQueryCmds + , IncludeStake (..) ) where import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..)) @@ -188,9 +189,15 @@ data QueryDRepStateCmdArgs era = QueryDRepStateCmdArgs , consensusModeParams :: !ConsensusModeParams , networkId :: !NetworkId , drepKeys :: !(AllOrOnly (VerificationKeyOrHashOrFile DRepKey)) + , includeStake :: !IncludeStake , mOutFile :: !(Maybe (File () Out)) } deriving Show +-- | Whether to include the stake, as queried by drep-stake-distribution, in +-- the output of drep-state. This is (computationally) expensive, but sometimes +-- convenient. +data IncludeStake = WithStake | NoStake deriving Show + data QueryDRepStakeDistributionCmdArgs era = QueryDRepStakeDistributionCmdArgs { eon :: !(ConwayEraOnwards era) , nodeSocketPath :: !SocketPath diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs index bb05deb85b..4f4855a470 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs @@ -323,6 +323,16 @@ pQueryDRepStateCmd era envCli = do <*> pConsensusModeParams <*> pNetworkId envCli <*> pAllOrOnlyDRepVerificationKeyOrHashOrFile + <*> Opt.flag WithStake NoStake (mconcat + [ Opt.long "include-stake" + , Opt.help $ mconcat + [ "Also return the stake associated with each DRep. " + , "The result is the same as with \"drep-stake-distribution\"; " + , "this is a convenience option to obtain all information concerning a DRep at once. " + , "This is a potentially expensive query." + ] + ] + ) <*> optional pOutputFile pQueryDRepStakeDistributionCmd :: () diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs index 7a0e37fa64..2970b98479 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs @@ -8,6 +8,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeOperators #-} @@ -1394,6 +1395,7 @@ runQueryDRepState , Cmd.consensusModeParams , Cmd.networkId , Cmd.drepKeys = drepKeys' + , Cmd.includeStake , Cmd.mOutFile } = conwayEraOnwardsConstraints eon $ do let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath @@ -1401,17 +1403,32 @@ runQueryDRepState let drepKeys = case drepKeys' of All -> [] Only l -> l - drepCreds <- Set.fromList <$> mapM (firstExceptT QueryCmdDRepKeyError . getDRepCredentialFromVerKeyHashOrFile) drepKeys + drepCreds <- mapM (firstExceptT QueryCmdDRepKeyError . getDRepCredentialFromVerKeyHashOrFile) drepKeys + + drepState <- runQuery localNodeConnInfo $ queryDRepState eon $ Set.fromList drepCreds + + drepStakeDistribution <- + case includeStake of + Cmd.WithStake -> runQuery localNodeConnInfo $ + queryDRepStakeDistribution eon (Set.fromList $ Ledger.DRepCredential <$> drepCreds) + Cmd.NoStake -> return mempty - drepState <- runQuery localNodeConnInfo $ queryDRepState eon drepCreds writeOutput mOutFile $ - second drepStateToJson <$> Map.assocs drepState + drepStateToJson drepStakeDistribution <$> Map.assocs drepState where - drepStateToJson ds = A.object - [ "expiry" .= (ds ^. Ledger.drepExpiryL) - , "anchor" .= (ds ^. Ledger.drepAnchorL) - , "deposit" .= (ds ^. Ledger.drepDepositL) - ] + drepStateToJson stakeDistr (cred, ds) = (cred,) . A.object $ + if Map.null stakeDistr + then + [ "expiry" .= (ds ^. Ledger.drepExpiryL) + , "anchor" .= (ds ^. Ledger.drepAnchorL) + , "deposit" .= (ds ^. Ledger.drepDepositL) + ] + else + [ "expiry" .= (ds ^. Ledger.drepExpiryL) + , "anchor" .= (ds ^. Ledger.drepAnchorL) + , "deposit" .= (ds ^. Ledger.drepDepositL) + , "stake" .= Map.lookup (Ledger.DRepCredential cred) stakeDistr + ] runQueryDRepStakeDistribution :: Cmd.QueryDRepStakeDistributionCmdArgs era diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index fe3fba7d21..59edd5ff12 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -6721,6 +6721,7 @@ Usage: cardano-cli conway query drep-state --socket-path SOCKET_PATH | --drep-key-hash HASH ) ) + [--include-stake] [--out-file FILE] Get the DRep state. If no DRep credentials are provided, return states for all diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_drep-state.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_drep-state.cli index 3a55d2a8d9..8dcf2728d1 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_drep-state.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_drep-state.cli @@ -11,6 +11,7 @@ Usage: cardano-cli conway query drep-state --socket-path SOCKET_PATH | --drep-key-hash HASH ) ) + [--include-stake] [--out-file FILE] Get the DRep state. If no DRep credentials are provided, return states for all @@ -37,5 +38,10 @@ Available options: Filepath of the DRep verification key. --drep-key-hash HASH DRep verification key hash (either Bech32-encoded or hex-encoded). + --include-stake Also return the stake associated with each DRep. The + result is the same as with "drep-stake-distribution"; + this is a convenience option to obtain all + information concerning a DRep at once. This is a + potentially expensive query. --out-file FILE The output file. -h,--help Show this help text