{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}

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

import Cardano.Api

import Cardano.CLI.Compatible.Exception
import Cardano.CLI.Compatible.Json.Friendly
  ( friendlyTx
  , friendlyTxBody
  )
import Cardano.CLI.EraIndependent.Debug.TransactionView.Command
import Cardano.CLI.Read
import Cardano.CLI.Type.Common

runTransactionViewCmd
  :: ()
  => TransactionViewCmdArgs
  -> CIO e ()
runTransactionViewCmd :: forall e. TransactionViewCmdArgs -> CIO e ()
runTransactionViewCmd
  TransactionViewCmdArgs
    { Vary '[FormatJson, FormatYaml]
outputFormat :: Vary '[FormatJson, FormatYaml]
outputFormat :: TransactionViewCmdArgs -> Vary '[FormatJson, FormatYaml]
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 -> RIO e FileOrPipe
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> RIO e FileOrPipe)
-> IO FileOrPipe -> RIO e FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txbodyFilePath
        IncompleteTxBody
unwitnessed <-
          IO (Either (FileError TextEnvelopeError) IncompleteTxBody)
-> RIO e IncompleteTxBody
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli (IO (Either (FileError TextEnvelopeError) IncompleteTxBody)
 -> RIO e IncompleteTxBody)
-> IO (Either (FileError TextEnvelopeError) IncompleteTxBody)
-> RIO e IncompleteTxBody
forall a b. (a -> b) -> a -> b
$
            FileOrPipe
-> IO (Either (FileError TextEnvelopeError) IncompleteTxBody)
readFileTxBody FileOrPipe
txbodyFile
        InAnyShelleyBasedEra ShelleyBasedEra era
era TxBody era
txbody <- InAnyShelleyBasedEra TxBody -> RIO e (InAnyShelleyBasedEra TxBody)
forall a. a -> RIO e a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InAnyShelleyBasedEra TxBody
 -> RIO e (InAnyShelleyBasedEra TxBody))
-> InAnyShelleyBasedEra TxBody
-> RIO e (InAnyShelleyBasedEra TxBody)
forall a b. (a -> b) -> a -> b
$ IncompleteTxBody -> InAnyShelleyBasedEra TxBody
unIncompleteTxBody IncompleteTxBody
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.
        forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
          Vary '[FormatJson, FormatYaml]
-> Maybe (File () 'Out)
-> ShelleyBasedEra era
-> TxBody era
-> IO (Either (FileError ()) ())
forall (m :: * -> *) era e.
MonadIO m =>
Vary '[FormatJson, FormatYaml]
-> Maybe (File () 'Out)
-> ShelleyBasedEra era
-> TxBody era
-> m (Either (FileError e) ())
friendlyTxBody Vary '[FormatJson, FormatYaml]
outputFormat Maybe (File () 'Out)
mOutFile ShelleyBasedEra era
era TxBody era
txbody
      InputTxFile (File FilePath
txFilePath) -> do
        FileOrPipe
txFile <- IO FileOrPipe -> RIO e FileOrPipe
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> RIO e FileOrPipe)
-> IO FileOrPipe -> RIO e FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txFilePath
        InAnyShelleyBasedEra ShelleyBasedEra era
era Tx era
tx <- IO (Either (FileError TextEnvelopeError) (InAnyShelleyBasedEra Tx))
-> RIO e (InAnyShelleyBasedEra Tx)
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli (FileOrPipe
-> IO
     (Either (FileError TextEnvelopeError) (InAnyShelleyBasedEra Tx))
readFileTx FileOrPipe
txFile)
        forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
          Vary '[FormatJson, FormatYaml]
-> Maybe (File () 'Out)
-> ShelleyBasedEra era
-> Tx era
-> IO (Either (FileError ()) ())
forall (m :: * -> *) era e.
MonadIO m =>
Vary '[FormatJson, FormatYaml]
-> Maybe (File () 'Out)
-> ShelleyBasedEra era
-> Tx era
-> m (Either (FileError e) ())
friendlyTx Vary '[FormatJson, FormatYaml]
outputFormat Maybe (File () 'Out)
mOutFile ShelleyBasedEra era
era Tx era
tx