From d0b2d6eca52d592d9e442adfd4d92daa6f42b2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 1 Jul 2024 10:09:09 +0200 Subject: [PATCH] calculate-min-fee: add outputs option --- .../CLI/EraBased/Commands/Transaction.hs | 2 ++ .../CLI/EraBased/Options/Transaction.hs | 2 ++ .../src/Cardano/CLI/EraBased/Run/Query.hs | 1 + .../Cardano/CLI/EraBased/Run/Transaction.hs | 19 +++++++++++++++++-- .../CLI/Legacy/Commands/Transaction.hs | 2 ++ cardano-cli/src/Cardano/CLI/Legacy/Options.hs | 2 ++ .../src/Cardano/CLI/Legacy/Run/Transaction.hs | 12 +++++++++--- 7 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs index 435f66a81f..d25b2a8bc1 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs @@ -220,6 +220,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 diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs index 199034b096..7ee1d0cc83 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs @@ -329,6 +329,8 @@ pTransactionCalculateMinFee = <*> pTxShelleyWitnessCount <*> pTxByronWitnessCount <*> pReferenceScriptSize + <*> (optional $ pOutputFormatJsonOrText "calculate-min-fee") + <*> optional pOutputFile -- Deprecated options: <* optional pNetworkIdDeprecated <* optional pTxInCountDeprecated diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs index 478d9bb450..ef9c7c1801 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs @@ -33,6 +33,7 @@ module Cardano.CLI.EraBased.Run.Query , runQueryUTxOCmd , DelegationsAndRewards(..) + , newOutputFormat , renderQueryCmdError , renderOpCertIntervalInformation , percentage diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs index bf29374254..07533360a9 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs @@ -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 @@ -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 @@ -1258,6 +1261,8 @@ runTransactionCalculateMinFeeCmd , txShelleyWitnessCount = TxShelleyWitnessCount nShelleyKeyWitnesses , txByronWitnessCount = TxByronWitnessCount nByronKeyWitnesses , referenceScriptSize = ReferenceScriptSize sReferenceScript + , outputFormat + , outFile } = do txbodyFile <- liftIO $ fileOrPipe txbodyFilePath @@ -1278,8 +1283,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. diff --git a/cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs b/cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs index 9bb6b865e8..35e16e0bf5 100644 --- a/cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs @@ -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) diff --git a/cardano-cli/src/Cardano/CLI/Legacy/Options.hs b/cardano-cli/src/Cardano/CLI/Legacy/Options.hs index 4db09e354d..69766ffb52 100644 --- a/cardano-cli/src/Cardano/CLI/Legacy/Options.hs +++ b/cardano-cli/src/Cardano/CLI/Legacy/Options.hs @@ -419,6 +419,8 @@ pTransaction envCli = <*> pTxShelleyWitnessCount <*> pTxByronWitnessCount <*> pReferenceScriptSize + <*> (optional $ pOutputFormatJsonOrText "calculate-min-fee") + <*> optional pOutputFile -- Deprecated options: <* optional pNetworkIdDeprecated <* optional pTxInCountDeprecated diff --git a/cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs b/cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs index 1a0fc007ed..0e67c1fd06 100644 --- a/cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs @@ -43,8 +43,8 @@ runLegacyTransactionCmds = \case runLegacyTransactionSignCmd txinfile skfiles network txoutfile TransactionSubmitCmd mNodeSocketPath consensusModeParams network txFp -> runLegacyTransactionSubmitCmd mNodeSocketPath consensusModeParams network txFp - TransactionCalculateMinFeeCmd txbody pParamsFile nShelleyKeyWitnesses nByronKeyWitnesses referenceScriptSize -> - runLegacyTransactionCalculateMinFeeCmd txbody pParamsFile nShelleyKeyWitnesses nByronKeyWitnesses referenceScriptSize + TransactionCalculateMinFeeCmd txbody pParamsFile nShelleyKeyWitnesses nByronKeyWitnesses referenceScriptSize format mOutFile -> + runLegacyTransactionCalculateMinFeeCmd txbody pParamsFile nShelleyKeyWitnesses nByronKeyWitnesses referenceScriptSize format mOutFile TransactionCalculateMinValueCmd (EraInEon sbe) pParamsFile txOuts' -> runLegacyTransactionCalculateMinValueCmd (AnyShelleyBasedEra sbe) pParamsFile txOuts' TransactionHashScriptDataCmd scriptDataOrFile -> @@ -230,13 +230,17 @@ runLegacyTransactionCalculateMinFeeCmd :: () -> TxShelleyWitnessCount -> TxByronWitnessCount -> ReferenceScriptSize + -> Maybe OutputFormatJsonOrText + -> Maybe (File () Out) -> ExceptT TxCmdError IO () runLegacyTransactionCalculateMinFeeCmd txbodyFile pParamsFile txShelleyWitnessCount txByronWitnessCount - referenceScriptSize = + referenceScriptSize + outputFormat + outFile = runTransactionCalculateMinFeeCmd ( Cmd.TransactionCalculateMinFeeCmdArgs txbodyFile @@ -244,6 +248,8 @@ runLegacyTransactionCalculateMinFeeCmd txShelleyWitnessCount txByronWitnessCount referenceScriptSize + outputFormat + outFile ) runLegacyTransactionCalculateMinValueCmd :: ()