{-# LANGUAGE NamedFieldPuns #-}

module Cardano.CLI.Run.Debug.TransactionView
  ( runTransactionViewCmd
  )
where

import           Cardano.Api

import           Cardano.CLI.Commands.Debug.TransactionView
import           Cardano.CLI.Json.Friendly (friendlyTx, friendlyTxBody,
                   viewOutputFormatToFriendlyFormat)
import           Cardano.CLI.Read
import           Cardano.CLI.Types.Common
import           Cardano.CLI.Types.Errors.TxCmdError

import           Data.Function ((&))

runTransactionViewCmd
  :: ()
  => TransactionViewCmdArgs
  -> ExceptT TxCmdError IO ()
runTransactionViewCmd :: TransactionViewCmdArgs -> ExceptT TxCmdError IO ()
runTransactionViewCmd
  TransactionViewCmdArgs
    { ViewOutputFormat
outputFormat :: ViewOutputFormat
outputFormat :: TransactionViewCmdArgs -> ViewOutputFormat
outputFormat
    , Maybe (File () 'Out)
mOutFile :: Maybe (File () 'Out)
mOutFile :: TransactionViewCmdArgs -> Maybe (File () 'Out)
mOutFile
    , InputTxBodyOrTxFile
inputTxBodyOrTxFile :: InputTxBodyOrTxFile
inputTxBodyOrTxFile :: TransactionViewCmdArgs -> InputTxBodyOrTxFile
inputTxBodyOrTxFile
    } =
    case InputTxBodyOrTxFile
inputTxBodyOrTxFile of
      InputTxBodyFile (File FilePath
txbodyFilePath) -> do
        FileOrPipe
txbodyFile <- IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe
forall a. IO a -> ExceptT TxCmdError IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe)
-> IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txbodyFilePath
        IncompleteCddlTxBody
unwitnessed <-
          (FileError TextEnvelopeCddlError -> TxCmdError)
-> ExceptT
     (FileError TextEnvelopeCddlError) IO IncompleteCddlTxBody
-> ExceptT TxCmdError IO IncompleteCddlTxBody
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeCddlError -> TxCmdError
TxCmdTextEnvCddlError (ExceptT (FileError TextEnvelopeCddlError) IO IncompleteCddlTxBody
 -> ExceptT TxCmdError IO IncompleteCddlTxBody)
-> (IO
      (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
    -> ExceptT
         (FileError TextEnvelopeCddlError) IO IncompleteCddlTxBody)
-> IO
     (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
-> ExceptT TxCmdError IO IncompleteCddlTxBody
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
-> ExceptT
     (FileError TextEnvelopeCddlError) IO IncompleteCddlTxBody
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
 -> ExceptT TxCmdError IO IncompleteCddlTxBody)
-> IO
     (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
-> ExceptT TxCmdError IO IncompleteCddlTxBody
forall a b. (a -> b) -> a -> b
$
            FileOrPipe
-> IO
     (Either (FileError TextEnvelopeCddlError) IncompleteCddlTxBody)
readFileTxBody FileOrPipe
txbodyFile
        InAnyShelleyBasedEra ShelleyBasedEra era
era TxBody era
txbody <- InAnyShelleyBasedEra TxBody
-> ExceptT TxCmdError IO (InAnyShelleyBasedEra TxBody)
forall a. a -> ExceptT TxCmdError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InAnyShelleyBasedEra TxBody
 -> ExceptT TxCmdError IO (InAnyShelleyBasedEra TxBody))
-> InAnyShelleyBasedEra TxBody
-> ExceptT TxCmdError IO (InAnyShelleyBasedEra TxBody)
forall a b. (a -> b) -> a -> b
$ IncompleteCddlTxBody -> InAnyShelleyBasedEra TxBody
unIncompleteCddlTxBody IncompleteCddlTxBody
unwitnessed
        -- Why are we differentiating between a transaction body and a transaction?
        -- In the case of a transaction body, we /could/ simply call @makeSignedTransaction []@
        -- to get a transaction which would allow us to reuse friendlyTxBS. However,
        -- this would mean that we'd have an empty list of witnesses mentioned in the output, which
        -- is arguably not part of the transaction body.
        (FileError () -> TxCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT TxCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> TxCmdError
TxCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT TxCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT TxCmdError IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError ()) ()) -> ExceptT TxCmdError IO ())
-> IO (Either (FileError ()) ()) -> ExceptT TxCmdError IO ()
forall a b. (a -> b) -> a -> b
$
          FriendlyFormat
-> Maybe (File () 'Out)
-> CardanoEra era
-> TxBody era
-> IO (Either (FileError ()) ())
forall (m :: * -> *) era e.
MonadIO m =>
FriendlyFormat
-> Maybe (File () 'Out)
-> CardanoEra era
-> TxBody era
-> m (Either (FileError e) ())
friendlyTxBody (ViewOutputFormat -> FriendlyFormat
viewOutputFormatToFriendlyFormat ViewOutputFormat
outputFormat) Maybe (File () 'Out)
mOutFile (ShelleyBasedEra era -> CardanoEra era
forall era. ShelleyBasedEra era -> CardanoEra era
forall (eon :: * -> *) era.
ToCardanoEra eon =>
eon era -> CardanoEra era
toCardanoEra ShelleyBasedEra era
era) TxBody era
txbody
      InputTxFile (File FilePath
txFilePath) -> do
        FileOrPipe
txFile <- IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe
forall a. IO a -> ExceptT TxCmdError IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe)
-> IO FileOrPipe -> ExceptT TxCmdError IO FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txFilePath
        InAnyShelleyBasedEra ShelleyBasedEra era
era Tx era
tx <- IO
  (Either
     (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
-> ExceptT
     TxCmdError
     IO
     (Either
        (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
forall (m :: * -> *) a. Monad m => m a -> ExceptT TxCmdError m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (FileOrPipe
-> IO
     (Either
        (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
readFileTx FileOrPipe
txFile) ExceptT
  TxCmdError
  IO
  (Either
     (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
-> (ExceptT
      TxCmdError
      IO
      (Either
         (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
    -> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx))
-> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx)
forall a b. a -> (a -> b) -> b
& (FileError TextEnvelopeCddlError
 -> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx))
-> ExceptT
     TxCmdError
     IO
     (Either
        (FileError TextEnvelopeCddlError) (InAnyShelleyBasedEra Tx))
-> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx)
forall e x (m :: * -> *) a.
Monad m =>
(e -> ExceptT x m a) -> ExceptT x m (Either e a) -> ExceptT x m a
onLeft (TxCmdError -> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx)
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (TxCmdError -> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx))
-> (FileError TextEnvelopeCddlError -> TxCmdError)
-> FileError TextEnvelopeCddlError
-> ExceptT TxCmdError IO (InAnyShelleyBasedEra Tx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileError TextEnvelopeCddlError -> TxCmdError
TxCmdTextEnvCddlError)
        (FileError () -> TxCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT TxCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> TxCmdError
TxCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT TxCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT TxCmdError IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError ()) ()) -> ExceptT TxCmdError IO ())
-> IO (Either (FileError ()) ()) -> ExceptT TxCmdError IO ()
forall a b. (a -> b) -> a -> b
$
          FriendlyFormat
-> Maybe (File () 'Out)
-> CardanoEra era
-> Tx era
-> IO (Either (FileError ()) ())
forall (m :: * -> *) era e.
MonadIO m =>
FriendlyFormat
-> Maybe (File () 'Out)
-> CardanoEra era
-> Tx era
-> m (Either (FileError e) ())
friendlyTx (ViewOutputFormat -> FriendlyFormat
viewOutputFormatToFriendlyFormat ViewOutputFormat
outputFormat) Maybe (File () 'Out)
mOutFile (ShelleyBasedEra era -> CardanoEra era
forall era. ShelleyBasedEra era -> CardanoEra era
forall (eon :: * -> *) era.
ToCardanoEra eon =>
eon era -> CardanoEra era
toCardanoEra ShelleyBasedEra era
era) Tx era
tx