Skip to content

Commit

Permalink
calculate-min-fee: add outputs option
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Jul 5, 2024
1 parent a3b9161 commit d0b2d6e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
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 @@ -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
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 @@ -329,6 +329,8 @@ pTransactionCalculateMinFee =
<*> pTxShelleyWitnessCount
<*> pTxByronWitnessCount
<*> pReferenceScriptSize
<*> (optional $ pOutputFormatJsonOrText "calculate-min-fee")
<*> optional pOutputFile
-- Deprecated options:
<* optional pNetworkIdDeprecated
<* optional pTxInCountDeprecated
Expand Down
1 change: 1 addition & 0 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
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 @@ -1258,6 +1261,8 @@ runTransactionCalculateMinFeeCmd
, txShelleyWitnessCount = TxShelleyWitnessCount nShelleyKeyWitnesses
, txByronWitnessCount = TxByronWitnessCount nByronKeyWitnesses
, referenceScriptSize = ReferenceScriptSize sReferenceScript
, outputFormat
, outFile
} = do

txbodyFile <- liftIO $ fileOrPipe txbodyFilePath
Expand All @@ -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.
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
2 changes: 2 additions & 0 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
12 changes: 9 additions & 3 deletions cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -230,20 +230,26 @@ 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
pParamsFile
txShelleyWitnessCount
txByronWitnessCount
referenceScriptSize
outputFormat
outFile
)

runLegacyTransactionCalculateMinValueCmd :: ()
Expand Down

0 comments on commit d0b2d6e

Please sign in to comment.