Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify toTxOutInAnyEra #393

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-api
tag: 9281f5ed808ca4841373da8adf55250072a8614c
subdir: cardano-api
9 changes: 0 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ runCmds = \case
runTransactionCmds cmd
& firstExceptT CmdTransactionError

-- TODO smelc Move me to cardano-api. Or is there another way? I'd be surprised
-- this is the first time we need this.
shelleyToAlonzoEraToShelleyToBabbageEra :: ShelleyToAlonzoEra era -> ShelleyToBabbageEra era
shelleyToAlonzoEraToShelleyToBabbageEra = \case
ShelleyToAlonzoEraShelley -> ShelleyToBabbageEraShelley
ShelleyToAlonzoEraAllegra -> ShelleyToBabbageEraAllegra
ShelleyToAlonzoEraMary -> ShelleyToBabbageEraMary
ShelleyToAlonzoEraAlonzo -> ShelleyToBabbageEraAlonzo

runGovernanceCmds :: ()
=> GovernanceCmds era
-> ExceptT CmdError IO ()
Expand Down
74 changes: 22 additions & 52 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
{-# LANGUAGE TupleSections #-}

{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Unused LANGUAGE pragma" #-}

{- HLINT ignore "Unused LANGUAGE pragma" -}
{- HLINT ignore "Avoid lambda using `infix`" -}

module Cardano.CLI.EraBased.Run.Transaction
( runTransactionCmds
Expand Down Expand Up @@ -728,76 +730,44 @@ toTxOutInAnyEra :: CardanoEra era
toTxOutInAnyEra era (TxOutAnyEra addr' val' mDatumHash refScriptFp) = do
addr <- hoistEither $ toAddressInAnyEra era addr'
val <- hoistEither $ toTxOutValueInAnyEra era val'
(datum, refScript)
<- caseByronToMaryOrAlonzoEraOnwards
(const $ pure (TxOutDatumNone, ReferenceScriptNone))
(\w ->
caseAlonzoOnlyOrBabbageEraOnwards
(\wa ->
(,)
<$> toTxAlonzoDatum (alonzoEraOnlyToAlonzoEraOnwards wa) mDatumHash
<*> pure ReferenceScriptNone
)
(\wbo -> toTxDatumReferenceScriptBabbage w wbo mDatumHash refScriptFp)
w
)
era

datum <- caseByronToMaryOrAlonzoEraOnwards
(const (pure TxOutDatumNone))
(\wa -> toTxAlonzoDatum wa mDatumHash)
Fixed Show fixed Hide fixed
era

refScript <- caseByronToAlonzoOrBabbageEraOnwards
(const (pure ReferenceScriptNone))
(\wb -> getReferenceScript wb refScriptFp)
Fixed Show fixed Hide fixed
era
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good example of how you could use eons to handle each feature independently which simplifies code a lot.


pure $ TxOut addr val datum refScript

where
getReferenceScript :: ()
=> ReferenceScriptAnyEra
-> BabbageEraOnwards era
-> ExceptT TxCmdError IO (ReferenceScript era)
getReferenceScript ReferenceScriptAnyEraNone _ = return ReferenceScriptNone
getReferenceScript (ReferenceScriptAnyEra fp) supp = do
ReferenceScript supp
<$> firstExceptT TxCmdScriptFileError (readFileScriptInAnyLang fp)

toTxDatumReferenceScriptBabbage :: ()
=> AlonzoEraOnwards era
-> BabbageEraOnwards era
-> TxOutDatumAnyEra
=> BabbageEraOnwards era
-> ReferenceScriptAnyEra
-> ExceptT TxCmdError IO (TxOutDatum CtxTx era, ReferenceScript era)
toTxDatumReferenceScriptBabbage sDataSupp inlineRefSupp cliDatum refScriptFp' = do
refScript <- getReferenceScript refScriptFp' inlineRefSupp
case cliDatum of
TxOutDatumByNone -> do
pure (TxOutDatumNone, refScript)
TxOutDatumByHashOnly dh -> do
pure (TxOutDatumHash sDataSupp dh, refScript)
TxOutDatumByHashOf fileOrSdata -> do
sData <- firstExceptT TxCmdScriptDataError
$ readScriptDataOrFile fileOrSdata
pure (TxOutDatumHash sDataSupp $ hashScriptDataBytes sData, refScript)
TxOutDatumByValue fileOrSdata -> do
sData <- firstExceptT TxCmdScriptDataError
$ readScriptDataOrFile fileOrSdata
pure (TxOutDatumInTx sDataSupp sData, refScript)
TxOutInlineDatumByValue fileOrSdata -> do
sData <- firstExceptT TxCmdScriptDataError
$ readScriptDataOrFile fileOrSdata
pure (TxOutDatumInline inlineRefSupp sData, refScript)
-> ExceptT TxCmdError IO (ReferenceScript era)
getReferenceScript w = \case
ReferenceScriptAnyEraNone -> return ReferenceScriptNone
ReferenceScriptAnyEra fp -> ReferenceScript w <$> firstExceptT TxCmdScriptFileError (readFileScriptInAnyLang fp)

toTxAlonzoDatum :: ()
=> AlonzoEraOnwards era
-> TxOutDatumAnyEra
-> ExceptT TxCmdError IO (TxOutDatum CtxTx era)
toTxAlonzoDatum supp cliDatum =
case cliDatum of
TxOutDatumByNone -> pure TxOutDatumNone
TxOutDatumByHashOnly h -> pure (TxOutDatumHash supp h)
TxOutDatumByHashOf sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError
$ readScriptDataOrFile sDataOrFile
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumHash supp $ hashScriptDataBytes sData)
TxOutDatumByValue sDataOrFile -> do
sData <- firstExceptT TxCmdScriptDataError
$ readScriptDataOrFile sDataOrFile
sData <- firstExceptT TxCmdScriptDataError $ readScriptDataOrFile sDataOrFile
pure (TxOutDatumInTx supp sData)
TxOutInlineDatumByValue _ ->
txFeatureMismatch era TxFeatureInlineDatums
TxOutDatumByNone -> pure TxOutDatumNone


-- TODO: Currently we specify the policyId with the '--mint' option on the cli
Expand Down