Skip to content

Commit

Permalink
Merge pull request #824 from IntersectMBO/smelc/calculate-min-fee-add…
Browse files Browse the repository at this point in the history
…-output-options

Add output options flags to calculate-min-fee
  • Loading branch information
smelc authored Jul 15, 2024
2 parents 13ad36d + 2b292bc commit f5d0056
Show file tree
Hide file tree
Showing 23 changed files with 251 additions and 63 deletions.
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ data QueryLeadershipScheduleCmdArgs = QueryLeadershipScheduleCmdArgs
, vrkSkeyFp :: !(SigningKeyFile In)
, whichSchedule :: !EpochLeadershipSchedule
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down Expand Up @@ -105,7 +105,7 @@ data QueryStakePoolsCmdArgs = QueryStakePoolsCmdArgs
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand All @@ -114,7 +114,7 @@ data QueryStakeDistributionCmdArgs = QueryStakeDistributionCmdArgs
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand All @@ -133,7 +133,7 @@ data QueryUTxOCmdArgs = QueryUTxOCmdArgs
, queryFilter :: !QueryUTxOFilter
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down Expand Up @@ -203,7 +203,7 @@ data QueryRefScriptSizeCmdArgs = QueryRefScriptSizeCmdArgs
, transactionInputs :: !(Set TxIn)
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ data TransactionCalculateMinFeeCmdArgs = TransactionCalculateMinFeeCmdArgs
, txByronWitnessCount :: !TxByronWitnessCount
-- | The total size in bytes of the transaction reference scripts.
, referenceScriptSize :: !ReferenceScriptSize
, outputFormat :: !(Maybe OutputFormatJsonOrText)
, outFile :: !(Maybe (File () Out))
} deriving Show

data TransactionCalculateMinValueCmdArgs era = TransactionCalculateMinValueCmdArgs
Expand Down
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,13 +1642,13 @@ pPoolIdOutputFormat =
, Opt.value IdOutputFormatBech32
]

-- | @pQueryOutputFormat kind@ is a parser to specify in which format
-- | @pOutputFormatJsonOrText kind@ is a parser to specify in which format
-- to view some data (json or text). @kind@ is the kind of data considered.
pQueryOutputFormat :: String -> Parser QueryOutputFormat
pQueryOutputFormat kind =
pOutputFormatJsonOrText :: String -> Parser OutputFormatJsonOrText
pOutputFormatJsonOrText kind =
asum
[ make QueryOutputFormatJson "JSON" "json" (Just " Default format when writing to a file")
, make QueryOutputFormatText "TEXT" "text" (Just " Default format when writing to stdout")
[ make OutputFormatJson "JSON" "json" (Just " Default format when writing to a file")
, make OutputFormatText "TEXT" "text" (Just " Default format when writing to stdout")
]
where
make format desc flag_ extraHelp =
Expand Down
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pQueryUTxOCmd era envCli =
<*> pQueryUTxOFilter
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "utxo")
<*> (optional $ pOutputFormatJsonOrText "utxo")
<*> pMaybeOutputFile

pQueryStakePoolsCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand All @@ -160,7 +160,7 @@ pQueryStakePoolsCmd era envCli =
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "stake-pools")
<*> (optional $ pOutputFormatJsonOrText "stake-pools")
<*> pMaybeOutputFile

pQueryStakeDistributionCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand All @@ -171,7 +171,7 @@ pQueryStakeDistributionCmd era envCli =
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "stake-distribution")
<*> (optional $ pOutputFormatJsonOrText "stake-distribution")
<*> pMaybeOutputFile

pQueryStakeAddressInfoCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down Expand Up @@ -271,7 +271,7 @@ pLeadershipScheduleCmd era envCli =
<*> pVrfSigningKeyFile
<*> pWhichLeadershipSchedule
<*> pTarget era
<*> (optional $ pQueryOutputFormat "leadership-schedule")
<*> (optional $ pOutputFormatJsonOrText "leadership-schedule")
<*> pMaybeOutputFile

pKesPeriodInfoCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down Expand Up @@ -310,7 +310,7 @@ pQueryRefScriptSizeCmd era envCli =
<*> (fromList <$> some pByTxIn)
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "reference inputs")
<*> (optional $ pOutputFormatJsonOrText "reference inputs")
<*> pMaybeOutputFile
where
pByTxIn :: Parser TxIn
Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ pTransactionCalculateMinFee =
<*> pTxShelleyWitnessCount
<*> pTxByronWitnessCount
<*> pReferenceScriptSize
<*> (optional $ pOutputFormatJsonOrText "calculate-min-fee")
<*> optional pOutputFile
-- Deprecated options:
<* optional pNetworkIdDeprecated
<* optional pTxInCountDeprecated
Expand Down
35 changes: 18 additions & 17 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Cardano.CLI.EraBased.Run.Query
, runQueryUTxOCmd

, DelegationsAndRewards(..)
, newOutputFormat
, renderQueryCmdError
, renderOpCertIntervalInformation
, percentage
Expand Down Expand Up @@ -1044,7 +1045,7 @@ writeProtocolState sbe mOutFile ps@(ProtocolState pstate) =
Right chainDepstate -> liftIO . LBS.putStrLn $ encodePretty chainDepstate

writeFilteredUTxOs :: Api.ShelleyBasedEra era
-> Maybe QueryOutputFormat
-> Maybe OutputFormatJsonOrText
-> Maybe (File () Out)
-> UTxO era
-> ExceptT QueryCmdError IO ()
Expand All @@ -1053,8 +1054,8 @@ writeFilteredUTxOs sbe format mOutFile utxo =
firstExceptT QueryCmdWriteFileError . newExceptT .
writeLazyByteStringOutput mOutFile $
case newOutputFormat format mOutFile of
QueryOutputFormatJson -> encodePretty utxo
QueryOutputFormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo
OutputFormatJson -> encodePretty utxo
OutputFormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo

filteredUTxOsToText :: Api.ShelleyBasedEra era -> UTxO era -> Text
filteredUTxOsToText sbe (UTxO utxo) = do
Expand Down Expand Up @@ -1172,7 +1173,7 @@ runQueryStakePoolsCmd

-- TODO: replace with writeFormattedOutput
writeStakePools
:: QueryOutputFormat
:: OutputFormatJsonOrText
-> Maybe (File () Out)
-> Set PoolId
-> ExceptT QueryCmdError IO ()
Expand All @@ -1182,18 +1183,18 @@ writeStakePools format mOutFile stakePools =
where
toWrite :: LBS.ByteString =
case format of
QueryOutputFormatText ->
OutputFormatText ->
LBS.unlines
$ map (strictTextToLazyBytestring . serialiseToBech32)
$ Set.toList stakePools
QueryOutputFormatJson ->
OutputFormatJson ->
encodePretty stakePools

writeFormattedOutput
:: MonadIOTransError QueryCmdError t m
=> ToJSON a
=> Pretty a
=> Maybe QueryOutputFormat
=> Maybe OutputFormatJsonOrText
-> Maybe (File b Out)
-> a
-> t m ()
Expand All @@ -1203,8 +1204,8 @@ writeFormattedOutput mFormat mOutFile value =
where
toWrite :: LBS.ByteString =
case newOutputFormat mFormat mOutFile of
QueryOutputFormatText -> fromString . docToString $ pretty value
QueryOutputFormatJson -> encodePretty value
OutputFormatText -> fromString . docToString $ pretty value
OutputFormatJson -> encodePretty value

runQueryStakeDistributionCmd :: ()
=> Cmd.QueryStakeDistributionCmdArgs
Expand Down Expand Up @@ -1239,7 +1240,7 @@ runQueryStakeDistributionCmd
& onLeft left

writeStakeDistribution
:: QueryOutputFormat
:: OutputFormatJsonOrText
-> Maybe (File () Out)
-> Map PoolId Rational
-> ExceptT QueryCmdError IO ()
Expand All @@ -1249,8 +1250,8 @@ writeStakeDistribution format mOutFile stakeDistrib =
where
toWrite :: LBS.ByteString =
case format of
QueryOutputFormatJson -> encodePretty stakeDistrib
QueryOutputFormatText -> strictTextToLazyBytestring stakeDistributionText
OutputFormatJson -> encodePretty stakeDistrib
OutputFormatText -> strictTextToLazyBytestring stakeDistributionText
stakeDistributionText =
Text.unlines $
[ title
Expand Down Expand Up @@ -1371,9 +1372,9 @@ runQueryLeadershipScheduleCmd
start = SystemStart $ sgSystemStart shelleyGenesis
toWrite =
case newOutputFormat format mOutFile' of
QueryOutputFormatJson ->
OutputFormatJson ->
encodePretty $ leadershipScheduleToJson schedule eInfo start
QueryOutputFormatText ->
OutputFormatText ->
strictTextToLazyBytestring $ leadershipScheduleToText schedule eInfo start

leadershipScheduleToText
Expand Down Expand Up @@ -1660,12 +1661,12 @@ requireEon minEra era =
-- | The output format to use, for commands with a recently introduced --output-[json,text] flag
-- and that used to have the following default: --out-file implies JSON,
-- output to stdout implied text.
newOutputFormat :: Maybe QueryOutputFormat -> Maybe a -> QueryOutputFormat
newOutputFormat :: Maybe OutputFormatJsonOrText -> Maybe a -> OutputFormatJsonOrText
newOutputFormat format mOutFile =
case (format, mOutFile) of
(Just f, _) -> f -- Take flag from CLI if specified
(Nothing, Nothing) -> QueryOutputFormatText -- No CLI flag, writing to stdout: write text
(Nothing, Just _) -> QueryOutputFormatJson -- No CLI flag, writing to a file: write JSON
(Nothing, Nothing) -> OutputFormatText -- No CLI flag, writing to stdout: write text
(Nothing, Just _) -> OutputFormatJson -- No CLI flag, writing to a file: write JSON

strictTextToLazyBytestring :: Text -> LBS.ByteString
strictTextToLazyBytestring t = BS.fromChunks [Text.encodeUtf8 t]
19 changes: 17 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import qualified Cardano.Binary as CBOR
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.CLI.EraBased.Commands.Transaction as Cmd
import Cardano.CLI.EraBased.Run.Genesis
import Cardano.CLI.EraBased.Run.Query
import Cardano.CLI.Json.Friendly (FriendlyFormat (..), friendlyTx, friendlyTxBody)
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
Expand All @@ -57,6 +58,8 @@ import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus
import qualified Ouroboros.Network.Protocol.LocalTxSubmission.Client as Net.Tx

import Control.Monad (forM)
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString as Data.Bytestring
Expand Down Expand Up @@ -1262,6 +1265,8 @@ runTransactionCalculateMinFeeCmd
, txShelleyWitnessCount = TxShelleyWitnessCount nShelleyKeyWitnesses
, txByronWitnessCount = TxByronWitnessCount nByronKeyWitnesses
, referenceScriptSize = ReferenceScriptSize sReferenceScript
, outputFormat
, outFile
} = do

txbodyFile <- liftIO $ fileOrPipe txbodyFilePath
Expand All @@ -1282,8 +1287,18 @@ runTransactionCalculateMinFeeCmd
let byronfee = shelleyBasedEraConstraints sbe $ calculateByronWitnessFees (lpparams ^. L.ppMinFeeAL) nByronKeyWitnesses

let L.Coin fee = shelleyfee + byronfee

liftIO $ putStrLn $ (show fee :: String) <> " Lovelace"
textToWrite = Text.pack ((show fee :: String) <> " Lovelace")
jsonToWrite = encodePretty $ Aeson.object ["fee" .= fee]

case (newOutputFormat outputFormat outFile, outFile) of
(OutputFormatText, Nothing) ->
liftIO $ Text.putStrLn textToWrite
(OutputFormatText, Just file) ->
firstExceptT TxCmdWriteFileError . newExceptT $ writeTextFile file textToWrite
(OutputFormatJson, Nothing) ->
liftIO $ LBS.putStrLn jsonToWrite
(OutputFormatJson, Just file) ->
firstExceptT TxCmdWriteFileError . newExceptT $ writeLazyByteStringFile file jsonToWrite

-- Extra logic to handle byron witnesses.
-- TODO: move this to Cardano.API.Fee.evaluateTransactionFee.
Expand Down
8 changes: 4 additions & 4 deletions cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ data LegacyQueryLeadershipScheduleCmdArgs = LegacyQueryLeadershipScheduleCmdArgs
, poolColdVerKeyFile :: !(VerificationKeyOrHashOrFile StakePoolKey)
, vrkSkeyFp :: !(SigningKeyFile In)
, whichSchedule :: !EpochLeadershipSchedule
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down Expand Up @@ -86,15 +86,15 @@ data LegacyQueryStakePoolsCmdArgs = LegacyQueryStakePoolsCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

data LegacyQueryStakeDistributionCmdArgs = LegacyQueryStakeDistributionCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand All @@ -111,7 +111,7 @@ data LegacyQueryUTxOCmdArgs = LegacyQueryUTxOCmdArgs
, consensusModeParams :: !ConsensusModeParams
, queryFilter :: !QueryUTxOFilter
, networkId :: !NetworkId
, format :: Maybe QueryOutputFormat
, format :: Maybe OutputFormatJsonOrText
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ data LegacyTransactionCmds
TxShelleyWitnessCount
TxByronWitnessCount
ReferenceScriptSize
(Maybe OutputFormatJsonOrText)
(Maybe (File () Out))
-- ^ The total size in bytes of the transaction reference scripts.
| TransactionCalculateMinValueCmd
(EraInEon ShelleyBasedEra)
Expand Down
10 changes: 6 additions & 4 deletions cardano-cli/src/Cardano/CLI/Legacy/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ pTransaction envCli =
<*> pTxShelleyWitnessCount
<*> pTxByronWitnessCount
<*> pReferenceScriptSize
<*> (optional $ pOutputFormatJsonOrText "calculate-min-fee")
<*> optional pOutputFile
-- Deprecated options:
<* optional pNetworkIdDeprecated
<* optional pTxInCountDeprecated
Expand Down Expand Up @@ -652,7 +654,7 @@ pQueryCmds envCli =
<*> pConsensusModeParams
<*> pQueryUTxOFilter
<*> pNetworkId envCli
<*> (optional $ pQueryOutputFormat "utxo")
<*> (optional $ pOutputFormatJsonOrText "utxo")
<*> pMaybeOutputFile

pQueryStakePools :: Parser LegacyQueryCmds
Expand All @@ -662,7 +664,7 @@ pQueryCmds envCli =
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> (optional $ pQueryOutputFormat "stake-pools")
<*> (optional $ pOutputFormatJsonOrText "stake-pools")
<*> pMaybeOutputFile

pQueryStakeDistribution :: Parser LegacyQueryCmds
Expand All @@ -672,7 +674,7 @@ pQueryCmds envCli =
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> (optional $ pQueryOutputFormat "stake-distribution")
<*> (optional $ pOutputFormatJsonOrText "stake-distribution")
<*> pMaybeOutputFile

pQueryStakeAddressInfo :: Parser LegacyQueryCmds
Expand Down Expand Up @@ -766,7 +768,7 @@ pQueryCmds envCli =
<*> pStakePoolVerificationKeyOrHashOrFile Nothing
<*> pVrfSigningKeyFile
<*> pWhichLeadershipSchedule
<*> (optional $ pQueryOutputFormat "leadership-schedule")
<*> (optional $ pOutputFormatJsonOrText "leadership-schedule")
<*> pMaybeOutputFile

pKesPeriodInfo :: Parser LegacyQueryCmds
Expand Down
Loading

0 comments on commit f5d0056

Please sign in to comment.