{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE GADTs              #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE NamedFieldPuns     #-}
{-# LANGUAGE OverloadedLists    #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RankNTypes         #-}
{-# LANGUAGE RecordWildCards    #-}
{-# LANGUAGE TupleSections      #-}

{-|

Interface to the transaction types from 'cardano-api'

-}
module Ledger.Tx.CardanoAPI(
  module Ledger.Tx.CardanoAPI.Internal
  , CardanoBuildTx(..)
  , CardanoTx(..)
  , fromCardanoTxInsCollateral
  , fromCardanoTotalCollateral
  , fromCardanoReturnCollateral
  , toCardanoTotalCollateral
  , toCardanoReturnCollateral
  , toCardanoDatumWitness
  , toCardanoTxInReferenceWitnessHeader
  , toCardanoTxInScriptWitnessHeader
  , toCardanoMintWitness
  , ToCardanoError(..)
  , FromCardanoError(..)
  , getRequiredSigners
  -- * Conversion from Plutus types
  , fromPlutusIndex
  , fromPlutusTxOut
  , fromPlutusTxOutRef
) where

import Cardano.Api qualified as C
import Cardano.Api.Shelley qualified as C
import Cardano.Ledger.Alonzo.Tx (ValidatedTx (..))
import Cardano.Ledger.Babbage qualified as Babbage
import Cardano.Ledger.Babbage.TxBody (TxBody (TxBody, reqSignerHashes))
import Cardano.Ledger.BaseTypes (mkTxIxPartial)
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Shelley.API qualified as C.Ledger
import Ledger.Address qualified as P
import Ledger.Index.Internal qualified as P
import Ledger.Scripts qualified as P
import Ledger.Tx.CardanoAPI.Internal
import Ledger.Tx.Internal qualified as P
import Plutus.V1.Ledger.Api qualified as PV1


toCardanoMintWitness :: PV1.Redeemer -> Maybe (P.Versioned PV1.TxOutRef) -> Maybe (P.Versioned PV1.MintingPolicy) -> Either ToCardanoError (C.ScriptWitness C.WitCtxMint C.BabbageEra)
toCardanoMintWitness :: Redeemer
-> Maybe (Versioned TxOutRef)
-> Maybe (Versioned MintingPolicy)
-> Either ToCardanoError (ScriptWitness WitCtxMint BabbageEra)
toCardanoMintWitness Redeemer
_ Maybe (Versioned TxOutRef)
Nothing Maybe (Versioned MintingPolicy)
Nothing = ToCardanoError
-> Either ToCardanoError (ScriptWitness WitCtxMint BabbageEra)
forall a b. a -> Either a b
Left ToCardanoError
MissingMintingPolicy
toCardanoMintWitness Redeemer
redeemer (Just Versioned TxOutRef
ref) Maybe (Versioned MintingPolicy)
_ =
  ScriptDatum WitCtxMint
-> Redeemer
-> Either (Versioned Script) (Versioned TxOutRef)
-> Either ToCardanoError (ScriptWitness WitCtxMint BabbageEra)
forall a witctx.
ToData a =>
ScriptDatum witctx
-> a
-> Either (Versioned Script) (Versioned TxOutRef)
-> Either ToCardanoError (ScriptWitness witctx BabbageEra)
toCardanoScriptWitness ScriptDatum WitCtxMint
C.NoScriptDatumForMint Redeemer
redeemer (Versioned TxOutRef
-> Either (Versioned Script) (Versioned TxOutRef)
forall a b. b -> Either a b
Right Versioned TxOutRef
ref)
toCardanoMintWitness Redeemer
redeemer Maybe (Versioned TxOutRef)
_ (Just Versioned MintingPolicy
script) =
  ScriptDatum WitCtxMint
-> Redeemer
-> Either (Versioned Script) (Versioned TxOutRef)
-> Either ToCardanoError (ScriptWitness WitCtxMint BabbageEra)
forall a witctx.
ToData a =>
ScriptDatum witctx
-> a
-> Either (Versioned Script) (Versioned TxOutRef)
-> Either ToCardanoError (ScriptWitness witctx BabbageEra)
toCardanoScriptWitness ScriptDatum WitCtxMint
C.NoScriptDatumForMint Redeemer
redeemer (Versioned Script -> Either (Versioned Script) (Versioned TxOutRef)
forall a b. a -> Either a b
Left ((MintingPolicy -> Script)
-> Versioned MintingPolicy -> Versioned Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MintingPolicy -> Script
P.getMintingPolicy Versioned MintingPolicy
script))

toCardanoScriptWitness :: PV1.ToData a =>
  C.ScriptDatum witctx
  -> a
  -> Either (P.Versioned PV1.Script) (P.Versioned PV1.TxOutRef)
  -> Either ToCardanoError (C.ScriptWitness witctx C.BabbageEra)
toCardanoScriptWitness :: ScriptDatum witctx
-> a
-> Either (Versioned Script) (Versioned TxOutRef)
-> Either ToCardanoError (ScriptWitness witctx BabbageEra)
toCardanoScriptWitness ScriptDatum witctx
datum a
redeemer Either (Versioned Script) (Versioned TxOutRef)
scriptOrRef = (case Language
lang of
    Language
P.PlutusV1 ->
      ScriptLanguageInEra PlutusScriptV1 BabbageEra
-> PlutusScriptVersion PlutusScriptV1
-> PlutusScriptOrReferenceInput PlutusScriptV1
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx BabbageEra
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV1 BabbageEra
C.PlutusScriptV1InBabbage PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1
          (PlutusScriptOrReferenceInput PlutusScriptV1
 -> ScriptDatum witctx
 -> ScriptRedeemer
 -> ExecutionUnits
 -> ScriptWitness witctx BabbageEra)
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV1)
-> Either
     ToCardanoError
     (ScriptDatum witctx
      -> ScriptRedeemer
      -> ExecutionUnits
      -> ScriptWitness witctx BabbageEra)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (case Either (Versioned Script) (Versioned TxOutRef)
scriptOrRef of
            Left (P.Versioned Script
script Language
_) -> (PlutusScript PlutusScriptV1
 -> PlutusScriptOrReferenceInput PlutusScriptV1)
-> Either ToCardanoError (PlutusScript PlutusScriptV1)
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PlutusScript PlutusScriptV1
-> PlutusScriptOrReferenceInput PlutusScriptV1
forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript (AsType (PlutusScript PlutusScriptV1)
-> Script -> Either ToCardanoError (PlutusScript PlutusScriptV1)
forall plutusScript.
SerialiseAsRawBytes plutusScript =>
AsType plutusScript -> Script -> Either ToCardanoError plutusScript
toCardanoPlutusScript (AsType PlutusScriptV1 -> AsType (PlutusScript PlutusScriptV1)
forall lang. AsType lang -> AsType (PlutusScript lang)
C.AsPlutusScript AsType PlutusScriptV1
C.AsPlutusScriptV1) Script
script)
            Right (P.Versioned TxOutRef
ref Language
_) -> (TxIn
 -> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV1)
-> Maybe ScriptHash
-> TxIn
-> PlutusScriptOrReferenceInput PlutusScriptV1
forall a b c. (a -> b -> c) -> b -> a -> c
flip TxIn
-> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV1
forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript Maybe ScriptHash
forall a. Maybe a
Nothing (TxIn -> PlutusScriptOrReferenceInput PlutusScriptV1)
-> Either ToCardanoError TxIn
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxOutRef -> Either ToCardanoError TxIn
toCardanoTxIn TxOutRef
ref
          )
    Language
P.PlutusV2 ->
      ScriptLanguageInEra PlutusScriptV2 BabbageEra
-> PlutusScriptVersion PlutusScriptV2
-> PlutusScriptOrReferenceInput PlutusScriptV2
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx BabbageEra
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2
          (PlutusScriptOrReferenceInput PlutusScriptV2
 -> ScriptDatum witctx
 -> ScriptRedeemer
 -> ExecutionUnits
 -> ScriptWitness witctx BabbageEra)
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV2)
-> Either
     ToCardanoError
     (ScriptDatum witctx
      -> ScriptRedeemer
      -> ExecutionUnits
      -> ScriptWitness witctx BabbageEra)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (case Either (Versioned Script) (Versioned TxOutRef)
scriptOrRef of
            Left (P.Versioned Script
script Language
_) -> (PlutusScript PlutusScriptV2
 -> PlutusScriptOrReferenceInput PlutusScriptV2)
-> Either ToCardanoError (PlutusScript PlutusScriptV2)
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PlutusScript PlutusScriptV2
-> PlutusScriptOrReferenceInput PlutusScriptV2
forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript (AsType (PlutusScript PlutusScriptV2)
-> Script -> Either ToCardanoError (PlutusScript PlutusScriptV2)
forall plutusScript.
SerialiseAsRawBytes plutusScript =>
AsType plutusScript -> Script -> Either ToCardanoError plutusScript
toCardanoPlutusScript (AsType PlutusScriptV2 -> AsType (PlutusScript PlutusScriptV2)
forall lang. AsType lang -> AsType (PlutusScript lang)
C.AsPlutusScript AsType PlutusScriptV2
C.AsPlutusScriptV2) Script
script)
            Right (P.Versioned TxOutRef
ref Language
_) -> (TxIn
 -> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV2)
-> Maybe ScriptHash
-> TxIn
-> PlutusScriptOrReferenceInput PlutusScriptV2
forall a b c. (a -> b -> c) -> b -> a -> c
flip TxIn
-> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV2
forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript Maybe ScriptHash
forall a. Maybe a
Nothing (TxIn -> PlutusScriptOrReferenceInput PlutusScriptV2)
-> Either ToCardanoError TxIn
-> Either
     ToCardanoError (PlutusScriptOrReferenceInput PlutusScriptV2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxOutRef -> Either ToCardanoError TxIn
toCardanoTxIn TxOutRef
ref
          )
  ) Either
  ToCardanoError
  (ScriptDatum witctx
   -> ScriptRedeemer
   -> ExecutionUnits
   -> ScriptWitness witctx BabbageEra)
-> Either ToCardanoError (ScriptDatum witctx)
-> Either
     ToCardanoError
     (ScriptRedeemer
      -> ExecutionUnits -> ScriptWitness witctx BabbageEra)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ScriptDatum witctx -> Either ToCardanoError (ScriptDatum witctx)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScriptDatum witctx
datum
    Either
  ToCardanoError
  (ScriptRedeemer
   -> ExecutionUnits -> ScriptWitness witctx BabbageEra)
-> Either ToCardanoError ScriptRedeemer
-> Either
     ToCardanoError (ExecutionUnits -> ScriptWitness witctx BabbageEra)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ScriptRedeemer -> Either ToCardanoError ScriptRedeemer
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Data -> ScriptRedeemer
C.fromPlutusData (Data -> ScriptRedeemer) -> Data -> ScriptRedeemer
forall a b. (a -> b) -> a -> b
$ a -> Data
forall a. ToData a => a -> Data
PV1.toData a
redeemer)
    Either
  ToCardanoError (ExecutionUnits -> ScriptWitness witctx BabbageEra)
-> Either ToCardanoError ExecutionUnits
-> Either ToCardanoError (ScriptWitness witctx BabbageEra)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ExecutionUnits -> Either ToCardanoError ExecutionUnits
forall (f :: * -> *) a. Applicative f => a -> f a
pure ExecutionUnits
zeroExecutionUnits
  where
    lang :: Language
lang = (Versioned Script -> Language)
-> (Versioned TxOutRef -> Language)
-> Either (Versioned Script) (Versioned TxOutRef)
-> Language
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Versioned Script -> Language
forall script. Versioned script -> Language
P.version Versioned TxOutRef -> Language
forall script. Versioned script -> Language
P.version Either (Versioned Script) (Versioned TxOutRef)
scriptOrRef

fromCardanoTxInsCollateral :: C.TxInsCollateral era -> [C.TxIn]
fromCardanoTxInsCollateral :: TxInsCollateral era -> [TxIn]
fromCardanoTxInsCollateral TxInsCollateral era
C.TxInsCollateralNone       = []
fromCardanoTxInsCollateral (C.TxInsCollateral CollateralSupportedInEra era
_ [TxIn]
txIns) = [TxIn]
txIns

toCardanoDatumWitness :: Maybe PV1.Datum -> C.ScriptDatum C.WitCtxTxIn
toCardanoDatumWitness :: Maybe Datum -> ScriptDatum WitCtxTxIn
toCardanoDatumWitness = ScriptDatum WitCtxTxIn
-> (Datum -> ScriptDatum WitCtxTxIn)
-> Maybe Datum
-> ScriptDatum WitCtxTxIn
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ScriptDatum WitCtxTxIn
C.InlineScriptDatum (ScriptRedeemer -> ScriptDatum WitCtxTxIn
C.ScriptDatumForTxIn (ScriptRedeemer -> ScriptDatum WitCtxTxIn)
-> (Datum -> ScriptRedeemer) -> Datum -> ScriptDatum WitCtxTxIn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinData -> ScriptRedeemer
toCardanoScriptData (BuiltinData -> ScriptRedeemer)
-> (Datum -> BuiltinData) -> Datum -> ScriptRedeemer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Datum -> BuiltinData
PV1.getDatum)

type WitnessHeader = C.ScriptDatum C.WitCtxTxIn -> C.ScriptRedeemer -> C.ExecutionUnits -> C.ScriptWitness C.WitCtxTxIn C.BabbageEra

toCardanoTxInReferenceWitnessHeader :: P.Versioned PV1.TxOutRef -> Either ToCardanoError WitnessHeader
toCardanoTxInReferenceWitnessHeader :: Versioned TxOutRef -> Either ToCardanoError WitnessHeader
toCardanoTxInReferenceWitnessHeader (P.Versioned TxOutRef
ref Language
lang) = do
    TxIn
txIn <- TxOutRef -> Either ToCardanoError TxIn
toCardanoTxIn TxOutRef
ref
    WitnessHeader -> Either ToCardanoError WitnessHeader
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WitnessHeader -> Either ToCardanoError WitnessHeader)
-> WitnessHeader -> Either ToCardanoError WitnessHeader
forall a b. (a -> b) -> a -> b
$ case Language
lang of
        Language
P.PlutusV1 ->
            ScriptLanguageInEra PlutusScriptV1 BabbageEra
-> PlutusScriptVersion PlutusScriptV1
-> PlutusScriptOrReferenceInput PlutusScriptV1
-> WitnessHeader
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV1 BabbageEra
C.PlutusScriptV1InBabbage PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 (PlutusScriptOrReferenceInput PlutusScriptV1 -> WitnessHeader)
-> PlutusScriptOrReferenceInput PlutusScriptV1 -> WitnessHeader
forall a b. (a -> b) -> a -> b
$
                TxIn
-> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV1
forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript TxIn
txIn Maybe ScriptHash
forall a. Maybe a
Nothing
        Language
P.PlutusV2 ->
            ScriptLanguageInEra PlutusScriptV2 BabbageEra
-> PlutusScriptVersion PlutusScriptV2
-> PlutusScriptOrReferenceInput PlutusScriptV2
-> WitnessHeader
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (PlutusScriptOrReferenceInput PlutusScriptV2 -> WitnessHeader)
-> PlutusScriptOrReferenceInput PlutusScriptV2 -> WitnessHeader
forall a b. (a -> b) -> a -> b
$
                TxIn
-> Maybe ScriptHash -> PlutusScriptOrReferenceInput PlutusScriptV2
forall lang.
TxIn -> Maybe ScriptHash -> PlutusScriptOrReferenceInput lang
C.PReferenceScript TxIn
txIn Maybe ScriptHash
forall a. Maybe a
Nothing

toCardanoTxInScriptWitnessHeader :: P.Versioned PV1.Script -> Either ToCardanoError WitnessHeader
toCardanoTxInScriptWitnessHeader :: Versioned Script -> Either ToCardanoError WitnessHeader
toCardanoTxInScriptWitnessHeader (P.Versioned Script
script Language
lang) =
    case Language
lang of
        Language
P.PlutusV1 ->
            ScriptLanguageInEra PlutusScriptV1 BabbageEra
-> PlutusScriptVersion PlutusScriptV1
-> PlutusScriptOrReferenceInput PlutusScriptV1
-> WitnessHeader
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV1 BabbageEra
C.PlutusScriptV1InBabbage PlutusScriptVersion PlutusScriptV1
C.PlutusScriptV1 (PlutusScriptOrReferenceInput PlutusScriptV1 -> WitnessHeader)
-> (PlutusScript PlutusScriptV1
    -> PlutusScriptOrReferenceInput PlutusScriptV1)
-> PlutusScript PlutusScriptV1
-> WitnessHeader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlutusScript PlutusScriptV1
-> PlutusScriptOrReferenceInput PlutusScriptV1
forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript (PlutusScript PlutusScriptV1 -> WitnessHeader)
-> Either ToCardanoError (PlutusScript PlutusScriptV1)
-> Either ToCardanoError WitnessHeader
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                AsType (PlutusScript PlutusScriptV1)
-> Script -> Either ToCardanoError (PlutusScript PlutusScriptV1)
forall plutusScript.
SerialiseAsRawBytes plutusScript =>
AsType plutusScript -> Script -> Either ToCardanoError plutusScript
toCardanoPlutusScript (AsType PlutusScriptV1 -> AsType (PlutusScript PlutusScriptV1)
forall lang. AsType lang -> AsType (PlutusScript lang)
C.AsPlutusScript AsType PlutusScriptV1
C.AsPlutusScriptV1) Script
script
        Language
P.PlutusV2 ->
            ScriptLanguageInEra PlutusScriptV2 BabbageEra
-> PlutusScriptVersion PlutusScriptV2
-> PlutusScriptOrReferenceInput PlutusScriptV2
-> WitnessHeader
forall lang era witctx.
ScriptLanguageInEra lang era
-> PlutusScriptVersion lang
-> PlutusScriptOrReferenceInput lang
-> ScriptDatum witctx
-> ScriptRedeemer
-> ExecutionUnits
-> ScriptWitness witctx era
C.PlutusScriptWitness ScriptLanguageInEra PlutusScriptV2 BabbageEra
C.PlutusScriptV2InBabbage PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 (PlutusScriptOrReferenceInput PlutusScriptV2 -> WitnessHeader)
-> (PlutusScript PlutusScriptV2
    -> PlutusScriptOrReferenceInput PlutusScriptV2)
-> PlutusScript PlutusScriptV2
-> WitnessHeader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlutusScript PlutusScriptV2
-> PlutusScriptOrReferenceInput PlutusScriptV2
forall lang. PlutusScript lang -> PlutusScriptOrReferenceInput lang
C.PScript (PlutusScript PlutusScriptV2 -> WitnessHeader)
-> Either ToCardanoError (PlutusScript PlutusScriptV2)
-> Either ToCardanoError WitnessHeader
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                AsType (PlutusScript PlutusScriptV2)
-> Script -> Either ToCardanoError (PlutusScript PlutusScriptV2)
forall plutusScript.
SerialiseAsRawBytes plutusScript =>
AsType plutusScript -> Script -> Either ToCardanoError plutusScript
toCardanoPlutusScript (AsType PlutusScriptV2 -> AsType (PlutusScript PlutusScriptV2)
forall lang. AsType lang -> AsType (PlutusScript lang)
C.AsPlutusScript AsType PlutusScriptV2
C.AsPlutusScriptV2) Script
script

fromCardanoTotalCollateral :: C.TxTotalCollateral C.BabbageEra -> Maybe C.Lovelace
fromCardanoTotalCollateral :: TxTotalCollateral BabbageEra -> Maybe Lovelace
fromCardanoTotalCollateral TxTotalCollateral BabbageEra
C.TxTotalCollateralNone    = Maybe Lovelace
forall a. Maybe a
Nothing
fromCardanoTotalCollateral (C.TxTotalCollateral TxTotalAndReturnCollateralSupportedInEra BabbageEra
_ Lovelace
lv) = Lovelace -> Maybe Lovelace
forall a. a -> Maybe a
Just Lovelace
lv

toCardanoTotalCollateral :: Maybe C.Lovelace -> C.TxTotalCollateral C.BabbageEra
toCardanoTotalCollateral :: Maybe Lovelace -> TxTotalCollateral BabbageEra
toCardanoTotalCollateral Maybe Lovelace
totalCollateral =
  case CardanoEra BabbageEra
-> Maybe (TxTotalAndReturnCollateralSupportedInEra BabbageEra)
forall era.
CardanoEra era
-> Maybe (TxTotalAndReturnCollateralSupportedInEra era)
C.totalAndReturnCollateralSupportedInEra CardanoEra BabbageEra
C.BabbageEra of
    Just TxTotalAndReturnCollateralSupportedInEra BabbageEra
txTotalAndReturnCollateralInBabbageEra ->
      TxTotalCollateral BabbageEra
-> (Lovelace -> TxTotalCollateral BabbageEra)
-> Maybe Lovelace
-> TxTotalCollateral BabbageEra
forall b a. b -> (a -> b) -> Maybe a -> b
maybe TxTotalCollateral BabbageEra
forall era. TxTotalCollateral era
C.TxTotalCollateralNone (TxTotalAndReturnCollateralSupportedInEra BabbageEra
-> Lovelace -> TxTotalCollateral BabbageEra
forall era.
TxTotalAndReturnCollateralSupportedInEra era
-> Lovelace -> TxTotalCollateral era
C.TxTotalCollateral TxTotalAndReturnCollateralSupportedInEra BabbageEra
txTotalAndReturnCollateralInBabbageEra) Maybe Lovelace
totalCollateral
    Maybe (TxTotalAndReturnCollateralSupportedInEra BabbageEra)
Nothing -> TxTotalCollateral BabbageEra
forall era. TxTotalCollateral era
C.TxTotalCollateralNone

fromCardanoReturnCollateral :: C.TxReturnCollateral C.CtxTx C.BabbageEra -> Maybe P.TxOut
fromCardanoReturnCollateral :: TxReturnCollateral CtxTx BabbageEra -> Maybe TxOut
fromCardanoReturnCollateral TxReturnCollateral CtxTx BabbageEra
C.TxReturnCollateralNone       = Maybe TxOut
forall a. Maybe a
Nothing
fromCardanoReturnCollateral (C.TxReturnCollateral TxTotalAndReturnCollateralSupportedInEra BabbageEra
_ TxOut CtxTx BabbageEra
txOut) = TxOut -> Maybe TxOut
forall a. a -> Maybe a
Just (TxOut -> Maybe TxOut) -> TxOut -> Maybe TxOut
forall a b. (a -> b) -> a -> b
$ TxOut CtxTx BabbageEra -> TxOut
P.TxOut TxOut CtxTx BabbageEra
txOut

toCardanoReturnCollateral :: Maybe P.TxOut -> C.TxReturnCollateral C.CtxTx C.BabbageEra
toCardanoReturnCollateral :: Maybe TxOut -> TxReturnCollateral CtxTx BabbageEra
toCardanoReturnCollateral Maybe TxOut
returnCollateral =
  case CardanoEra BabbageEra
-> Maybe (TxTotalAndReturnCollateralSupportedInEra BabbageEra)
forall era.
CardanoEra era
-> Maybe (TxTotalAndReturnCollateralSupportedInEra era)
C.totalAndReturnCollateralSupportedInEra CardanoEra BabbageEra
C.BabbageEra of
    Just TxTotalAndReturnCollateralSupportedInEra BabbageEra
txTotalAndReturnCollateralInBabbageEra ->
      TxReturnCollateral CtxTx BabbageEra
-> (TxOut -> TxReturnCollateral CtxTx BabbageEra)
-> Maybe TxOut
-> TxReturnCollateral CtxTx BabbageEra
forall b a. b -> (a -> b) -> Maybe a -> b
maybe TxReturnCollateral CtxTx BabbageEra
forall ctx era. TxReturnCollateral ctx era
C.TxReturnCollateralNone (TxTotalAndReturnCollateralSupportedInEra BabbageEra
-> TxOut CtxTx BabbageEra -> TxReturnCollateral CtxTx BabbageEra
forall era ctx.
TxTotalAndReturnCollateralSupportedInEra era
-> TxOut ctx era -> TxReturnCollateral ctx era
C.TxReturnCollateral TxTotalAndReturnCollateralSupportedInEra BabbageEra
txTotalAndReturnCollateralInBabbageEra (TxOut CtxTx BabbageEra -> TxReturnCollateral CtxTx BabbageEra)
-> (TxOut -> TxOut CtxTx BabbageEra)
-> TxOut
-> TxReturnCollateral CtxTx BabbageEra
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> TxOut CtxTx BabbageEra
P.getTxOut) Maybe TxOut
returnCollateral
    Maybe (TxTotalAndReturnCollateralSupportedInEra BabbageEra)
Nothing -> TxReturnCollateral CtxTx BabbageEra
forall ctx era. TxReturnCollateral ctx era
C.TxReturnCollateralNone

getRequiredSigners :: C.Tx C.BabbageEra -> [P.PaymentPubKeyHash]
getRequiredSigners :: Tx BabbageEra -> [PaymentPubKeyHash]
getRequiredSigners (C.ShelleyTx ShelleyBasedEra BabbageEra
_ (ValidatedTx TxBody { reqSignerHashes = rsq } _ _ _)) =
  (KeyHash 'Witness StandardCrypto -> [PaymentPubKeyHash])
-> Set (KeyHash 'Witness StandardCrypto) -> [PaymentPubKeyHash]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (PaymentPubKeyHash -> [PaymentPubKeyHash]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaymentPubKeyHash -> [PaymentPubKeyHash])
-> (KeyHash 'Witness StandardCrypto -> PaymentPubKeyHash)
-> KeyHash 'Witness StandardCrypto
-> [PaymentPubKeyHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubKeyHash -> PaymentPubKeyHash
P.PaymentPubKeyHash (PubKeyHash -> PaymentPubKeyHash)
-> (KeyHash 'Witness StandardCrypto -> PubKeyHash)
-> KeyHash 'Witness StandardCrypto
-> PaymentPubKeyHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash PaymentKey -> PubKeyHash
P.toPlutusPubKeyHash (Hash PaymentKey -> PubKeyHash)
-> (KeyHash 'Witness StandardCrypto -> Hash PaymentKey)
-> KeyHash 'Witness StandardCrypto
-> PubKeyHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash 'Payment StandardCrypto -> Hash PaymentKey
C.PaymentKeyHash (KeyHash 'Payment StandardCrypto -> Hash PaymentKey)
-> (KeyHash 'Witness StandardCrypto
    -> KeyHash 'Payment StandardCrypto)
-> KeyHash 'Witness StandardCrypto
-> Hash PaymentKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash 'Witness StandardCrypto -> KeyHash 'Payment StandardCrypto
forall (a :: KeyRole -> * -> *) (r :: KeyRole) crypto
       (r' :: KeyRole).
HasKeyRole a =>
a r crypto -> a r' crypto
C.Ledger.coerceKeyRole) Set (KeyHash 'Witness StandardCrypto)
Set (KeyHash 'Witness (Crypto (BabbageEra StandardCrypto)))
rsq

fromPlutusIndex :: P.UtxoIndex -> C.Ledger.UTxO (Babbage.BabbageEra StandardCrypto)
fromPlutusIndex :: UtxoIndex -> UTxO (BabbageEra StandardCrypto)
fromPlutusIndex = ShelleyBasedEra BabbageEra
-> UtxoIndex -> UTxO (BabbageEra StandardCrypto)
forall era ledgerera.
(ShelleyLedgerEra era ~ ledgerera,
 Crypto ledgerera ~ StandardCrypto) =>
ShelleyBasedEra era -> UTxO era -> UTxO ledgerera
C.toLedgerUTxO ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage

fromPlutusTxOutRef :: P.TxOutRef -> Either ToCardanoError (C.Ledger.TxIn StandardCrypto)
fromPlutusTxOutRef :: TxOutRef -> Either ToCardanoError (TxIn StandardCrypto)
fromPlutusTxOutRef (P.TxOutRef TxId
txId Integer
i) = TxId StandardCrypto -> TxIx -> TxIn StandardCrypto
forall crypto. TxId crypto -> TxIx -> TxIn crypto
C.Ledger.TxIn (TxId StandardCrypto -> TxIx -> TxIn StandardCrypto)
-> Either ToCardanoError (TxId StandardCrypto)
-> Either ToCardanoError (TxIx -> TxIn StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxId -> Either ToCardanoError (TxId StandardCrypto)
fromPlutusTxId TxId
txId Either ToCardanoError (TxIx -> TxIn StandardCrypto)
-> Either ToCardanoError TxIx
-> Either ToCardanoError (TxIn StandardCrypto)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TxIx -> Either ToCardanoError TxIx
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HasCallStack => Integer -> TxIx
Integer -> TxIx
mkTxIxPartial Integer
i)

fromPlutusTxId :: PV1.TxId -> Either ToCardanoError (C.Ledger.TxId StandardCrypto)
fromPlutusTxId :: TxId -> Either ToCardanoError (TxId StandardCrypto)
fromPlutusTxId = (TxId -> TxId StandardCrypto)
-> Either ToCardanoError TxId
-> Either ToCardanoError (TxId StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TxId -> TxId StandardCrypto
C.toShelleyTxId (Either ToCardanoError TxId
 -> Either ToCardanoError (TxId StandardCrypto))
-> (TxId -> Either ToCardanoError TxId)
-> TxId
-> Either ToCardanoError (TxId StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxId -> Either ToCardanoError TxId
toCardanoTxId

fromPlutusTxOut :: P.TxOut -> Babbage.TxOut (Babbage.BabbageEra StandardCrypto)
fromPlutusTxOut :: TxOut -> TxOut (BabbageEra StandardCrypto)
fromPlutusTxOut = ShelleyBasedEra BabbageEra
-> TxOut CtxUTxO BabbageEra -> TxOut (BabbageEra StandardCrypto)
forall era ledgerera.
(ShelleyLedgerEra era ~ ledgerera) =>
ShelleyBasedEra era -> TxOut CtxUTxO era -> TxOut ledgerera
C.toShelleyTxOut ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage (TxOut CtxUTxO BabbageEra -> TxOut (BabbageEra StandardCrypto))
-> (TxOut -> TxOut CtxUTxO BabbageEra)
-> TxOut
-> TxOut (BabbageEra StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> TxOut CtxUTxO BabbageEra
P.toCtxUTxOTxOut