{-# LANGUAGE DeriveAnyClass           #-}
{-# LANGUAGE DeriveGeneric            #-}
{-# LANGUAGE DerivingVia              #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE FlexibleContexts         #-}
{-# LANGUAGE FlexibleInstances        #-}
{-# LANGUAGE GADTs                    #-}
{-# LANGUAGE LambdaCase               #-}
{-# LANGUAGE NamedFieldPuns           #-}
{-# LANGUAGE OverloadedLists          #-}
{-# LANGUAGE OverloadedStrings        #-}
{-# LANGUAGE TypeApplications         #-}

module Ledger.Tx.Internal
    ( module Ledger.Tx.Internal
    , Language(..)
    , TxOut (..)
    , TxOutRef (..)
    , Versioned(..)
    ) where

import Cardano.Api qualified as C
import Cardano.Api.Shelley qualified as C hiding (toShelleyTxOut)
import Cardano.Binary qualified as C
import Cardano.Ledger.Alonzo.Genesis ()
import Codec.Serialise (Serialise, decode, encode)

import Cardano.Ledger.Core qualified as Ledger (TxOut)
import Cardano.Ledger.Serialization qualified as Ledger (Sized, mkSized)
import Ouroboros.Consensus.Shelley.Eras qualified as Ledger

import Control.Lens qualified as L
import Data.Aeson (FromJSON, ToJSON)
import Data.Default (def)
import Data.Map (Map)
import Data.Map qualified as Map
import GHC.Generics (Generic)

import Ledger.Address (CardanoAddress, cardanoPubKeyHash)
import Ledger.Contexts.Orphans ()
import Ledger.Crypto
import Ledger.DCert.Orphans ()
import Ledger.Tx.CardanoAPITemp qualified as C
import Ledger.Tx.Orphans ()
import Ledger.Tx.Orphans.V2 ()

import Plutus.Script.Utils.Scripts
import Plutus.V1.Ledger.Api (Credential, DCert, dataToBuiltinData)
import Plutus.V1.Ledger.Scripts
import Plutus.V1.Ledger.Tx hiding (TxIn (..), TxInType (..), TxOut (..), inRef, inType, pubKeyTxIn, scriptTxIn)
import PlutusTx (FromData (..))
import PlutusTx.Prelude qualified as PlutusTx
import Prettyprinter (Pretty (..), viaShow)

cardanoTxOutValue :: C.TxOut ctx era -> C.Value
cardanoTxOutValue :: TxOut ctx era -> Value
cardanoTxOutValue (C.TxOut AddressInEra era
_aie TxOutValue era
tov TxOutDatum ctx era
_tod ReferenceScript era
_rs) =
  TxOutValue era -> Value
forall era. TxOutValue era -> Value
C.txOutValueToValue TxOutValue era
tov

txOutValue :: TxOut -> C.Value
txOutValue :: TxOut -> Value
txOutValue = TxOut CtxTx BabbageEra -> Value
forall ctx era. TxOut ctx era -> Value
cardanoTxOutValue (TxOut CtxTx BabbageEra -> Value)
-> (TxOut -> TxOut CtxTx BabbageEra) -> TxOut -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> TxOut CtxTx BabbageEra
getTxOut

outValue :: L.Lens TxOut TxOut C.Value (C.TxOutValue C.BabbageEra)
outValue :: (Value -> f (TxOutValue BabbageEra)) -> TxOut -> f TxOut
outValue = (TxOut -> Value)
-> (TxOut -> TxOutValue BabbageEra -> TxOut)
-> Lens TxOut TxOut Value (TxOutValue BabbageEra)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
L.lens
  TxOut -> Value
txOutValue
  (\(TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
_ TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs)) TxOutValue BabbageEra
tov -> TxOut CtxTx BabbageEra -> TxOut
TxOut (AddressInEra BabbageEra
-> TxOutValue BabbageEra
-> TxOutDatum CtxTx BabbageEra
-> ReferenceScript BabbageEra
-> TxOut CtxTx BabbageEra
forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs))

outValue' :: L.Lens' TxOut (C.TxOutValue C.BabbageEra)
outValue' :: (TxOutValue BabbageEra -> f (TxOutValue BabbageEra))
-> TxOut -> f TxOut
outValue' = (TxOut -> TxOutValue BabbageEra)
-> (TxOut -> TxOutValue BabbageEra -> TxOut)
-> Lens TxOut TxOut (TxOutValue BabbageEra) (TxOutValue BabbageEra)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
L.lens
  (\(TxOut (C.TxOut AddressInEra BabbageEra
_aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
_tod ReferenceScript BabbageEra
_rs)) -> TxOutValue BabbageEra
tov)
  (\(TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
_ TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs)) TxOutValue BabbageEra
tov -> TxOut CtxTx BabbageEra -> TxOut
TxOut (AddressInEra BabbageEra
-> TxOutValue BabbageEra
-> TxOutDatum CtxTx BabbageEra
-> ReferenceScript BabbageEra
-> TxOut CtxTx BabbageEra
forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs))


-- | Stake withdrawal, if applicable the script should be included in txScripts.
data Withdrawal = Withdrawal
  { Withdrawal -> Credential
withdrawalCredential :: Credential      -- ^ staking credential
  , Withdrawal -> Integer
withdrawalAmount     :: Integer         -- ^ amount of withdrawal in Lovelace, must withdraw all eligible amount
  , Withdrawal -> Maybe Redeemer
withdrawalRedeemer   :: Maybe Redeemer  -- ^ redeemer for script credential
  }
  deriving stock (Int -> Withdrawal -> ShowS
[Withdrawal] -> ShowS
Withdrawal -> String
(Int -> Withdrawal -> ShowS)
-> (Withdrawal -> String)
-> ([Withdrawal] -> ShowS)
-> Show Withdrawal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Withdrawal] -> ShowS
$cshowList :: [Withdrawal] -> ShowS
show :: Withdrawal -> String
$cshow :: Withdrawal -> String
showsPrec :: Int -> Withdrawal -> ShowS
$cshowsPrec :: Int -> Withdrawal -> ShowS
Show, Withdrawal -> Withdrawal -> Bool
(Withdrawal -> Withdrawal -> Bool)
-> (Withdrawal -> Withdrawal -> Bool) -> Eq Withdrawal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Withdrawal -> Withdrawal -> Bool
$c/= :: Withdrawal -> Withdrawal -> Bool
== :: Withdrawal -> Withdrawal -> Bool
$c== :: Withdrawal -> Withdrawal -> Bool
Eq, (forall x. Withdrawal -> Rep Withdrawal x)
-> (forall x. Rep Withdrawal x -> Withdrawal) -> Generic Withdrawal
forall x. Rep Withdrawal x -> Withdrawal
forall x. Withdrawal -> Rep Withdrawal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Withdrawal x -> Withdrawal
$cfrom :: forall x. Withdrawal -> Rep Withdrawal x
Generic)
  deriving anyclass ([Withdrawal] -> Encoding
[Withdrawal] -> Value
Withdrawal -> Encoding
Withdrawal -> Value
(Withdrawal -> Value)
-> (Withdrawal -> Encoding)
-> ([Withdrawal] -> Value)
-> ([Withdrawal] -> Encoding)
-> ToJSON Withdrawal
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Withdrawal] -> Encoding
$ctoEncodingList :: [Withdrawal] -> Encoding
toJSONList :: [Withdrawal] -> Value
$ctoJSONList :: [Withdrawal] -> Value
toEncoding :: Withdrawal -> Encoding
$ctoEncoding :: Withdrawal -> Encoding
toJSON :: Withdrawal -> Value
$ctoJSON :: Withdrawal -> Value
ToJSON, Value -> Parser [Withdrawal]
Value -> Parser Withdrawal
(Value -> Parser Withdrawal)
-> (Value -> Parser [Withdrawal]) -> FromJSON Withdrawal
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Withdrawal]
$cparseJSONList :: Value -> Parser [Withdrawal]
parseJSON :: Value -> Parser Withdrawal
$cparseJSON :: Value -> Parser Withdrawal
FromJSON, [Withdrawal] -> Encoding
Withdrawal -> Encoding
(Withdrawal -> Encoding)
-> (forall s. Decoder s Withdrawal)
-> ([Withdrawal] -> Encoding)
-> (forall s. Decoder s [Withdrawal])
-> Serialise Withdrawal
forall s. Decoder s [Withdrawal]
forall s. Decoder s Withdrawal
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [Withdrawal]
$cdecodeList :: forall s. Decoder s [Withdrawal]
encodeList :: [Withdrawal] -> Encoding
$cencodeList :: [Withdrawal] -> Encoding
decode :: Decoder s Withdrawal
$cdecode :: forall s. Decoder s Withdrawal
encode :: Withdrawal -> Encoding
$cencode :: Withdrawal -> Encoding
Serialise)

instance Pretty Withdrawal where
    pretty :: Withdrawal -> Doc ann
pretty = Withdrawal -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow

data Certificate = Certificate
  { Certificate -> DCert
certificateDcert    :: DCert
  , Certificate -> Maybe Redeemer
certificateRedeemer :: Maybe Redeemer           -- ^ redeemer for script credential
  }
  deriving stock (Int -> Certificate -> ShowS
[Certificate] -> ShowS
Certificate -> String
(Int -> Certificate -> ShowS)
-> (Certificate -> String)
-> ([Certificate] -> ShowS)
-> Show Certificate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Certificate] -> ShowS
$cshowList :: [Certificate] -> ShowS
show :: Certificate -> String
$cshow :: Certificate -> String
showsPrec :: Int -> Certificate -> ShowS
$cshowsPrec :: Int -> Certificate -> ShowS
Show, Certificate -> Certificate -> Bool
(Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Bool) -> Eq Certificate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Certificate -> Certificate -> Bool
$c/= :: Certificate -> Certificate -> Bool
== :: Certificate -> Certificate -> Bool
$c== :: Certificate -> Certificate -> Bool
Eq, (forall x. Certificate -> Rep Certificate x)
-> (forall x. Rep Certificate x -> Certificate)
-> Generic Certificate
forall x. Rep Certificate x -> Certificate
forall x. Certificate -> Rep Certificate x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Certificate x -> Certificate
$cfrom :: forall x. Certificate -> Rep Certificate x
Generic)
  deriving anyclass ([Certificate] -> Encoding
[Certificate] -> Value
Certificate -> Encoding
Certificate -> Value
(Certificate -> Value)
-> (Certificate -> Encoding)
-> ([Certificate] -> Value)
-> ([Certificate] -> Encoding)
-> ToJSON Certificate
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Certificate] -> Encoding
$ctoEncodingList :: [Certificate] -> Encoding
toJSONList :: [Certificate] -> Value
$ctoJSONList :: [Certificate] -> Value
toEncoding :: Certificate -> Encoding
$ctoEncoding :: Certificate -> Encoding
toJSON :: Certificate -> Value
$ctoJSON :: Certificate -> Value
ToJSON, Value -> Parser [Certificate]
Value -> Parser Certificate
(Value -> Parser Certificate)
-> (Value -> Parser [Certificate]) -> FromJSON Certificate
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Certificate]
$cparseJSONList :: Value -> Parser [Certificate]
parseJSON :: Value -> Parser Certificate
$cparseJSON :: Value -> Parser Certificate
FromJSON, [Certificate] -> Encoding
Certificate -> Encoding
(Certificate -> Encoding)
-> (forall s. Decoder s Certificate)
-> ([Certificate] -> Encoding)
-> (forall s. Decoder s [Certificate])
-> Serialise Certificate
forall s. Decoder s [Certificate]
forall s. Decoder s Certificate
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [Certificate]
$cdecodeList :: forall s. Decoder s [Certificate]
encodeList :: [Certificate] -> Encoding
$cencodeList :: [Certificate] -> Encoding
decode :: Decoder s Certificate
$cdecode :: forall s. Decoder s Certificate
encode :: Certificate -> Encoding
$cencode :: Certificate -> Encoding
Serialise)

instance Pretty Certificate where
    pretty :: Certificate -> Doc ann
pretty = Certificate -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow

newtype TxOut = TxOut {TxOut -> TxOut CtxTx BabbageEra
getTxOut :: C.TxOut C.CtxTx C.BabbageEra}
    deriving stock (Int -> TxOut -> ShowS
[TxOut] -> ShowS
TxOut -> String
(Int -> TxOut -> ShowS)
-> (TxOut -> String) -> ([TxOut] -> ShowS) -> Show TxOut
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxOut] -> ShowS
$cshowList :: [TxOut] -> ShowS
show :: TxOut -> String
$cshow :: TxOut -> String
showsPrec :: Int -> TxOut -> ShowS
$cshowsPrec :: Int -> TxOut -> ShowS
Show, TxOut -> TxOut -> Bool
(TxOut -> TxOut -> Bool) -> (TxOut -> TxOut -> Bool) -> Eq TxOut
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxOut -> TxOut -> Bool
$c/= :: TxOut -> TxOut -> Bool
== :: TxOut -> TxOut -> Bool
$c== :: TxOut -> TxOut -> Bool
Eq, (forall x. TxOut -> Rep TxOut x)
-> (forall x. Rep TxOut x -> TxOut) -> Generic TxOut
forall x. Rep TxOut x -> TxOut
forall x. TxOut -> Rep TxOut x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxOut x -> TxOut
$cfrom :: forall x. TxOut -> Rep TxOut x
Generic)
    deriving anyclass ([TxOut] -> Encoding
[TxOut] -> Value
TxOut -> Encoding
TxOut -> Value
(TxOut -> Value)
-> (TxOut -> Encoding)
-> ([TxOut] -> Value)
-> ([TxOut] -> Encoding)
-> ToJSON TxOut
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxOut] -> Encoding
$ctoEncodingList :: [TxOut] -> Encoding
toJSONList :: [TxOut] -> Value
$ctoJSONList :: [TxOut] -> Value
toEncoding :: TxOut -> Encoding
$ctoEncoding :: TxOut -> Encoding
toJSON :: TxOut -> Value
$ctoJSON :: TxOut -> Value
ToJSON, Value -> Parser [TxOut]
Value -> Parser TxOut
(Value -> Parser TxOut)
-> (Value -> Parser [TxOut]) -> FromJSON TxOut
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxOut]
$cparseJSONList :: Value -> Parser [TxOut]
parseJSON :: Value -> Parser TxOut
$cparseJSON :: Value -> Parser TxOut
FromJSON)
    deriving newtype ([TxOut] -> Doc ann
TxOut -> Doc ann
(forall ann. TxOut -> Doc ann)
-> (forall ann. [TxOut] -> Doc ann) -> Pretty TxOut
forall ann. [TxOut] -> Doc ann
forall ann. TxOut -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
prettyList :: [TxOut] -> Doc ann
$cprettyList :: forall ann. [TxOut] -> Doc ann
pretty :: TxOut -> Doc ann
$cpretty :: forall ann. TxOut -> Doc ann
Pretty)

instance C.ToCBOR TxOut where
  toCBOR :: TxOut -> Encoding
toCBOR (TxOut TxOut CtxTx BabbageEra
txout) = TxOut (BabbageEra StandardCrypto) -> Encoding
forall a. ToCBOR a => a -> Encoding
C.toCBOR (TxOut (BabbageEra StandardCrypto) -> Encoding)
-> TxOut (BabbageEra StandardCrypto) -> Encoding
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra BabbageEra
-> TxOut CtxTx BabbageEra -> TxOut (BabbageEra StandardCrypto)
forall era ledgerera.
(ShelleyLedgerEra era ~ ledgerera, IsShelleyBasedEra era) =>
ShelleyBasedEra era -> TxOut CtxTx era -> TxOut ledgerera
C.toShelleyTxOut ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage TxOut CtxTx BabbageEra
txout

instance C.FromCBOR TxOut where
  fromCBOR :: Decoder s TxOut
fromCBOR = do
    TxOut (BabbageEra StandardCrypto)
txout <- Decoder s (TxOut (BabbageEra StandardCrypto))
forall a s. FromCBOR a => Decoder s a
C.fromCBOR
    TxOut -> Decoder s TxOut
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TxOut -> Decoder s TxOut) -> TxOut -> Decoder s TxOut
forall a b. (a -> b) -> a -> b
$ TxOut CtxTx BabbageEra -> TxOut
TxOut (TxOut CtxTx BabbageEra -> TxOut)
-> TxOut CtxTx BabbageEra -> TxOut
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra BabbageEra
-> TxOut (BabbageEra StandardCrypto) -> TxOut CtxTx BabbageEra
forall era ledgerera ctx.
(ShelleyLedgerEra era ~ ledgerera) =>
ShelleyBasedEra era -> TxOut ledgerera -> TxOut ctx era
C.fromShelleyTxOut ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage TxOut (BabbageEra StandardCrypto)
TxOut (BabbageEra StandardCrypto)
txout

instance Serialise TxOut where
  encode :: TxOut -> Encoding
encode = TxOut -> Encoding
forall a. ToCBOR a => a -> Encoding
C.toCBOR
  decode :: Decoder s TxOut
decode = Decoder s TxOut
forall a s. FromCBOR a => Decoder s a
C.fromCBOR

toSizedTxOut :: TxOut -> Ledger.Sized (Ledger.TxOut Ledger.StandardBabbage)
toSizedTxOut :: TxOut -> Sized (TxOut (BabbageEra StandardCrypto))
toSizedTxOut = TxOut (BabbageEra StandardCrypto)
-> Sized (TxOut (BabbageEra StandardCrypto))
forall a. ToCBOR a => a -> Sized a
Ledger.mkSized (TxOut (BabbageEra StandardCrypto)
 -> Sized (TxOut (BabbageEra StandardCrypto)))
-> (TxOut -> TxOut (BabbageEra StandardCrypto))
-> TxOut
-> Sized (TxOut (BabbageEra StandardCrypto))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShelleyBasedEra BabbageEra
-> TxOut CtxTx BabbageEra -> TxOut (BabbageEra StandardCrypto)
forall era ledgerera.
(ShelleyLedgerEra era ~ ledgerera, IsShelleyBasedEra era) =>
ShelleyBasedEra era -> TxOut CtxTx era -> TxOut ledgerera
C.toShelleyTxOut ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage (TxOut CtxTx BabbageEra -> TxOut (BabbageEra StandardCrypto))
-> (TxOut -> TxOut CtxTx BabbageEra)
-> TxOut
-> TxOut (BabbageEra StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> TxOut CtxTx BabbageEra
getTxOut

toCtxUTxOTxOut :: TxOut -> C.TxOut C.CtxUTxO C.BabbageEra
toCtxUTxOTxOut :: TxOut -> TxOut CtxUTxO BabbageEra
toCtxUTxOTxOut = TxOut CtxTx BabbageEra -> TxOut CtxUTxO BabbageEra
forall era. TxOut CtxTx era -> TxOut CtxUTxO era
C.toCtxUTxOTxOut (TxOut CtxTx BabbageEra -> TxOut CtxUTxO BabbageEra)
-> (TxOut -> TxOut CtxTx BabbageEra)
-> TxOut
-> TxOut CtxUTxO BabbageEra
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> TxOut CtxTx BabbageEra
getTxOut


type ScriptsMap = Map ScriptHash (Versioned Script)
type MintingWitnessesMap = Map MintingPolicyHash (Redeemer, Maybe (Versioned TxOutRef))

-- | Get a hash from the stored TxOutDatum (either directly or by hashing the inlined datum)
txOutDatumHash :: TxOut -> Maybe DatumHash
txOutDatumHash :: TxOut -> Maybe DatumHash
txOutDatumHash (TxOut (C.TxOut AddressInEra BabbageEra
_aie TxOutValue BabbageEra
_tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
_rs)) =
  case TxOutDatum CtxTx BabbageEra
tod of
    TxOutDatum CtxTx BabbageEra
C.TxOutDatumNone ->
      Maybe DatumHash
forall a. Maybe a
Nothing
    C.TxOutDatumHash ScriptDataSupportedInEra BabbageEra
_era Hash ScriptData
scriptDataHash ->
      DatumHash -> Maybe DatumHash
forall a. a -> Maybe a
Just (DatumHash -> Maybe DatumHash) -> DatumHash -> Maybe DatumHash
forall a b. (a -> b) -> a -> b
$ BuiltinByteString -> DatumHash
DatumHash (BuiltinByteString -> DatumHash) -> BuiltinByteString -> DatumHash
forall a b. (a -> b) -> a -> b
$ ByteString -> BuiltinByteString
forall a arep. ToBuiltin a arep => a -> arep
PlutusTx.toBuiltin (Hash ScriptData -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
C.serialiseToRawBytes Hash ScriptData
scriptDataHash)
    C.TxOutDatumInline ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
_era ScriptData
scriptData ->
      DatumHash -> Maybe DatumHash
forall a. a -> Maybe a
Just (DatumHash -> Maybe DatumHash) -> DatumHash -> Maybe DatumHash
forall a b. (a -> b) -> a -> b
$ Datum -> DatumHash
datumHash (Datum -> DatumHash) -> Datum -> DatumHash
forall a b. (a -> b) -> a -> b
$ BuiltinData -> Datum
Datum (BuiltinData -> Datum) -> BuiltinData -> Datum
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ ScriptData -> Data
C.toPlutusData ScriptData
scriptData
    C.TxOutDatumInTx ScriptDataSupportedInEra BabbageEra
_era ScriptData
scriptData ->
      DatumHash -> Maybe DatumHash
forall a. a -> Maybe a
Just (DatumHash -> Maybe DatumHash) -> DatumHash -> Maybe DatumHash
forall a b. (a -> b) -> a -> b
$ Datum -> DatumHash
datumHash (Datum -> DatumHash) -> Datum -> DatumHash
forall a b. (a -> b) -> a -> b
$ BuiltinData -> Datum
Datum (BuiltinData -> Datum) -> BuiltinData -> Datum
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ ScriptData -> Data
C.toPlutusData ScriptData
scriptData

txOutDatum :: forall d . FromData d => TxOut -> Maybe d
txOutDatum :: TxOut -> Maybe d
txOutDatum (TxOut (C.TxOut AddressInEra BabbageEra
_aie TxOutValue BabbageEra
_tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
_rs)) =
  case TxOutDatum CtxTx BabbageEra
tod of
    TxOutDatum CtxTx BabbageEra
C.TxOutDatumNone ->
      Maybe d
forall a. Maybe a
Nothing
    C.TxOutDatumHash ScriptDataSupportedInEra BabbageEra
_era Hash ScriptData
_scriptDataHash ->
      Maybe d
forall a. Maybe a
Nothing
    C.TxOutDatumInline ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
_era ScriptData
scriptData ->
      FromData d => BuiltinData -> Maybe d
forall a. FromData a => BuiltinData -> Maybe a
fromBuiltinData @d  (BuiltinData -> Maybe d) -> BuiltinData -> Maybe d
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ ScriptData -> Data
C.toPlutusData ScriptData
scriptData
    C.TxOutDatumInTx ScriptDataSupportedInEra BabbageEra
_era ScriptData
scriptData ->
      FromData d => BuiltinData -> Maybe d
forall a. FromData a => BuiltinData -> Maybe a
fromBuiltinData @d  (BuiltinData -> Maybe d) -> BuiltinData -> Maybe d
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ ScriptData -> Data
C.toPlutusData ScriptData
scriptData


cardanoTxOutDatumHash :: C.TxOutDatum C.CtxUTxO C.BabbageEra -> Maybe (C.Hash C.ScriptData)
cardanoTxOutDatumHash :: TxOutDatum CtxUTxO BabbageEra -> Maybe (Hash ScriptData)
cardanoTxOutDatumHash = \case
  TxOutDatum CtxUTxO BabbageEra
C.TxOutDatumNone ->
    Maybe (Hash ScriptData)
forall a. Maybe a
Nothing
  C.TxOutDatumHash ScriptDataSupportedInEra BabbageEra
_era Hash ScriptData
scriptDataHash ->
    Hash ScriptData -> Maybe (Hash ScriptData)
forall a. a -> Maybe a
Just Hash ScriptData
scriptDataHash
  C.TxOutDatumInline ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra
_era ScriptData
scriptData -> Hash ScriptData -> Maybe (Hash ScriptData)
forall a. a -> Maybe a
Just (Hash ScriptData -> Maybe (Hash ScriptData))
-> Hash ScriptData -> Maybe (Hash ScriptData)
forall a b. (a -> b) -> a -> b
$ ScriptData -> Hash ScriptData
C.hashScriptData ScriptData
scriptData


txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutPubKey (TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
_ TxOutDatum CtxTx BabbageEra
_ ReferenceScript BabbageEra
_)) = AddressInEra BabbageEra -> Maybe PubKeyHash
forall era. AddressInEra era -> Maybe PubKeyHash
cardanoPubKeyHash AddressInEra BabbageEra
aie

txOutAddress :: TxOut -> CardanoAddress
txOutAddress :: TxOut -> AddressInEra BabbageEra
txOutAddress (TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
_tov TxOutDatum CtxTx BabbageEra
_tod ReferenceScript BabbageEra
_rs)) = AddressInEra BabbageEra
aie

outAddress :: L.Lens' TxOut (C.AddressInEra C.BabbageEra)
outAddress :: (AddressInEra BabbageEra -> f (AddressInEra BabbageEra))
-> TxOut -> f TxOut
outAddress = (TxOut -> AddressInEra BabbageEra)
-> (TxOut -> AddressInEra BabbageEra -> TxOut)
-> Lens
     TxOut TxOut (AddressInEra BabbageEra) (AddressInEra BabbageEra)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
L.lens
  TxOut -> AddressInEra BabbageEra
txOutAddress
  (\(TxOut (C.TxOut AddressInEra BabbageEra
_ TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs)) AddressInEra BabbageEra
aie -> TxOut CtxTx BabbageEra -> TxOut
TxOut (AddressInEra BabbageEra
-> TxOutValue BabbageEra
-> TxOutDatum CtxTx BabbageEra
-> ReferenceScript BabbageEra
-> TxOut CtxTx BabbageEra
forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs))

outDatumHash :: L.Lens TxOut TxOut (Maybe DatumHash) (C.TxOutDatum C.CtxTx C.BabbageEra)
outDatumHash :: (Maybe DatumHash -> f (TxOutDatum CtxTx BabbageEra))
-> TxOut -> f TxOut
outDatumHash = (TxOut -> Maybe DatumHash)
-> (TxOut -> TxOutDatum CtxTx BabbageEra -> TxOut)
-> Lens TxOut TxOut (Maybe DatumHash) (TxOutDatum CtxTx BabbageEra)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
L.lens
  TxOut -> Maybe DatumHash
txOutDatumHash
  (\(TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
_ ReferenceScript BabbageEra
rs)) TxOutDatum CtxTx BabbageEra
tod -> TxOut CtxTx BabbageEra -> TxOut
TxOut (AddressInEra BabbageEra
-> TxOutValue BabbageEra
-> TxOutDatum CtxTx BabbageEra
-> ReferenceScript BabbageEra
-> TxOut CtxTx BabbageEra
forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs))

type ReferenceScript = C.ReferenceScript C.BabbageEra

txOutReferenceScript :: TxOut -> ReferenceScript
txOutReferenceScript :: TxOut -> ReferenceScript BabbageEra
txOutReferenceScript (TxOut (C.TxOut AddressInEra BabbageEra
_aie TxOutValue BabbageEra
_tov TxOutDatum CtxTx BabbageEra
_tod ReferenceScript BabbageEra
rs)) = ReferenceScript BabbageEra
rs

outReferenceScript :: L.Lens' TxOut ReferenceScript
outReferenceScript :: (ReferenceScript BabbageEra -> f (ReferenceScript BabbageEra))
-> TxOut -> f TxOut
outReferenceScript = (TxOut -> ReferenceScript BabbageEra)
-> (TxOut -> ReferenceScript BabbageEra -> TxOut)
-> Lens
     TxOut
     TxOut
     (ReferenceScript BabbageEra)
     (ReferenceScript BabbageEra)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
L.lens
  TxOut -> ReferenceScript BabbageEra
txOutReferenceScript
  (\(TxOut (C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
_)) ReferenceScript BabbageEra
rs -> TxOut CtxTx BabbageEra -> TxOut
TxOut (AddressInEra BabbageEra
-> TxOutValue BabbageEra
-> TxOutDatum CtxTx BabbageEra
-> ReferenceScript BabbageEra
-> TxOut CtxTx BabbageEra
forall ctx era.
AddressInEra era
-> TxOutValue era
-> TxOutDatum ctx era
-> ReferenceScript era
-> TxOut ctx era
C.TxOut AddressInEra BabbageEra
aie TxOutValue BabbageEra
tov TxOutDatum CtxTx BabbageEra
tod ReferenceScript BabbageEra
rs))

lookupScript :: ScriptsMap -> ScriptHash -> Maybe (Versioned Script)
lookupScript :: ScriptsMap -> ScriptHash -> Maybe (Versioned Script)
lookupScript ScriptsMap
txScripts ScriptHash
hash = ScriptHash -> ScriptsMap -> Maybe (Versioned Script)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ScriptHash
hash ScriptsMap
txScripts

lookupValidator :: ScriptsMap -> ValidatorHash -> Maybe (Versioned Validator)
lookupValidator :: ScriptsMap -> ValidatorHash -> Maybe (Versioned Validator)
lookupValidator ScriptsMap
txScripts = ((Versioned Script -> Versioned Validator)
-> Maybe (Versioned Script) -> Maybe (Versioned Validator)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Versioned Script -> Versioned Validator)
 -> Maybe (Versioned Script) -> Maybe (Versioned Validator))
-> ((Script -> Validator)
    -> Versioned Script -> Versioned Validator)
-> (Script -> Validator)
-> Maybe (Versioned Script)
-> Maybe (Versioned Validator)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Script -> Validator) -> Versioned Script -> Versioned Validator
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Script -> Validator
Validator (Maybe (Versioned Script) -> Maybe (Versioned Validator))
-> (ValidatorHash -> Maybe (Versioned Script))
-> ValidatorHash
-> Maybe (Versioned Validator)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptsMap -> ScriptHash -> Maybe (Versioned Script)
lookupScript ScriptsMap
txScripts (ScriptHash -> Maybe (Versioned Script))
-> (ValidatorHash -> ScriptHash)
-> ValidatorHash
-> Maybe (Versioned Script)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValidatorHash -> ScriptHash
toScriptHash
    where
        toScriptHash :: ValidatorHash -> ScriptHash
toScriptHash (ValidatorHash BuiltinByteString
b) = BuiltinByteString -> ScriptHash
ScriptHash BuiltinByteString
b

lookupMintingPolicy :: ScriptsMap -> MintingPolicyHash -> Maybe (Versioned MintingPolicy)
lookupMintingPolicy :: ScriptsMap -> MintingPolicyHash -> Maybe (Versioned MintingPolicy)
lookupMintingPolicy ScriptsMap
txScripts = ((Versioned Script -> Versioned MintingPolicy)
-> Maybe (Versioned Script) -> Maybe (Versioned MintingPolicy)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Versioned Script -> Versioned MintingPolicy)
 -> Maybe (Versioned Script) -> Maybe (Versioned MintingPolicy))
-> ((Script -> MintingPolicy)
    -> Versioned Script -> Versioned MintingPolicy)
-> (Script -> MintingPolicy)
-> Maybe (Versioned Script)
-> Maybe (Versioned MintingPolicy)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Script -> MintingPolicy)
-> Versioned Script -> Versioned MintingPolicy
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Script -> MintingPolicy
MintingPolicy (Maybe (Versioned Script) -> Maybe (Versioned MintingPolicy))
-> (MintingPolicyHash -> Maybe (Versioned Script))
-> MintingPolicyHash
-> Maybe (Versioned MintingPolicy)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptsMap -> ScriptHash -> Maybe (Versioned Script)
lookupScript ScriptsMap
txScripts (ScriptHash -> Maybe (Versioned Script))
-> (MintingPolicyHash -> ScriptHash)
-> MintingPolicyHash
-> Maybe (Versioned Script)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MintingPolicyHash -> ScriptHash
toScriptHash
    where
        toScriptHash :: MintingPolicyHash -> ScriptHash
toScriptHash (MintingPolicyHash BuiltinByteString
b) = BuiltinByteString -> ScriptHash
ScriptHash BuiltinByteString
b

lookupStakeValidator :: ScriptsMap -> StakeValidatorHash -> Maybe (Versioned StakeValidator)
lookupStakeValidator :: ScriptsMap
-> StakeValidatorHash -> Maybe (Versioned StakeValidator)
lookupStakeValidator ScriptsMap
txScripts = ((Versioned Script -> Versioned StakeValidator)
-> Maybe (Versioned Script) -> Maybe (Versioned StakeValidator)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Versioned Script -> Versioned StakeValidator)
 -> Maybe (Versioned Script) -> Maybe (Versioned StakeValidator))
-> ((Script -> StakeValidator)
    -> Versioned Script -> Versioned StakeValidator)
-> (Script -> StakeValidator)
-> Maybe (Versioned Script)
-> Maybe (Versioned StakeValidator)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Script -> StakeValidator)
-> Versioned Script -> Versioned StakeValidator
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Script -> StakeValidator
StakeValidator (Maybe (Versioned Script) -> Maybe (Versioned StakeValidator))
-> (StakeValidatorHash -> Maybe (Versioned Script))
-> StakeValidatorHash
-> Maybe (Versioned StakeValidator)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptsMap -> ScriptHash -> Maybe (Versioned Script)
lookupScript ScriptsMap
txScripts (ScriptHash -> Maybe (Versioned Script))
-> (StakeValidatorHash -> ScriptHash)
-> StakeValidatorHash
-> Maybe (Versioned Script)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StakeValidatorHash -> ScriptHash
toScriptHash
    where
        toScriptHash :: StakeValidatorHash -> ScriptHash
toScriptHash (StakeValidatorHash BuiltinByteString
b) = BuiltinByteString -> ScriptHash
ScriptHash BuiltinByteString
b

emptyTxBodyContent :: C.TxBodyContent C.BuildTx C.BabbageEra
emptyTxBodyContent :: TxBodyContent BuildTx BabbageEra
emptyTxBodyContent = TxBodyContent :: forall build era.
TxIns build era
-> TxInsCollateral era
-> TxInsReference build era
-> [TxOut CtxTx era]
-> TxTotalCollateral era
-> TxReturnCollateral CtxTx era
-> TxFee era
-> (TxValidityLowerBound era, TxValidityUpperBound era)
-> TxMetadataInEra era
-> TxAuxScripts era
-> TxExtraKeyWitnesses era
-> BuildTxWith build (Maybe ProtocolParameters)
-> TxWithdrawals build era
-> TxCertificates build era
-> TxUpdateProposal era
-> TxMintValue build era
-> TxScriptValidity era
-> TxBodyContent build era
C.TxBodyContent
   { txIns :: TxIns BuildTx BabbageEra
txIns = []
   , txInsCollateral :: TxInsCollateral BabbageEra
txInsCollateral = TxInsCollateral BabbageEra
forall era. TxInsCollateral era
C.TxInsCollateralNone
   , txMintValue :: TxMintValue BuildTx BabbageEra
txMintValue = TxMintValue BuildTx BabbageEra
forall build era. TxMintValue build era
C.TxMintNone
   , txFee :: TxFee BabbageEra
txFee = TxFeesExplicitInEra BabbageEra -> Lovelace -> TxFee BabbageEra
forall era. TxFeesExplicitInEra era -> Lovelace -> TxFee era
C.TxFeeExplicit TxFeesExplicitInEra BabbageEra
C.TxFeesExplicitInBabbageEra Lovelace
0
   , txOuts :: [TxOut CtxTx BabbageEra]
txOuts = []
   , txProtocolParams :: BuildTxWith BuildTx (Maybe ProtocolParameters)
txProtocolParams = Maybe ProtocolParameters
-> BuildTxWith BuildTx (Maybe ProtocolParameters)
forall a. a -> BuildTxWith BuildTx a
C.BuildTxWith (Maybe ProtocolParameters
 -> BuildTxWith BuildTx (Maybe ProtocolParameters))
-> Maybe ProtocolParameters
-> BuildTxWith BuildTx (Maybe ProtocolParameters)
forall a b. (a -> b) -> a -> b
$ ProtocolParameters -> Maybe ProtocolParameters
forall a. a -> Maybe a
Just (ProtocolParameters -> Maybe ProtocolParameters)
-> ProtocolParameters -> Maybe ProtocolParameters
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra BabbageEra
-> PParams (ShelleyLedgerEra BabbageEra) -> ProtocolParameters
forall era.
ShelleyBasedEra era
-> PParams (ShelleyLedgerEra era) -> ProtocolParameters
C.fromLedgerPParams ShelleyBasedEra BabbageEra
C.ShelleyBasedEraBabbage PParams (ShelleyLedgerEra BabbageEra)
forall a. Default a => a
def
   , txInsReference :: TxInsReference BuildTx BabbageEra
txInsReference = TxInsReference BuildTx BabbageEra
forall build era. TxInsReference build era
C.TxInsReferenceNone
   , txTotalCollateral :: TxTotalCollateral BabbageEra
txTotalCollateral = TxTotalCollateral BabbageEra
forall era. TxTotalCollateral era
C.TxTotalCollateralNone
   , txReturnCollateral :: TxReturnCollateral CtxTx BabbageEra
txReturnCollateral = TxReturnCollateral CtxTx BabbageEra
forall ctx era. TxReturnCollateral ctx era
C.TxReturnCollateralNone
   , txValidityRange :: (TxValidityLowerBound BabbageEra, TxValidityUpperBound BabbageEra)
txValidityRange = ( TxValidityLowerBound BabbageEra
forall era. TxValidityLowerBound era
C.TxValidityNoLowerBound
                       , ValidityNoUpperBoundSupportedInEra BabbageEra
-> TxValidityUpperBound BabbageEra
forall era.
ValidityNoUpperBoundSupportedInEra era -> TxValidityUpperBound era
C.TxValidityNoUpperBound ValidityNoUpperBoundSupportedInEra BabbageEra
C.ValidityNoUpperBoundInBabbageEra)
   , txScriptValidity :: TxScriptValidity BabbageEra
txScriptValidity = TxScriptValidity BabbageEra
forall era. TxScriptValidity era
C.TxScriptValidityNone
   , txExtraKeyWits :: TxExtraKeyWitnesses BabbageEra
txExtraKeyWits = TxExtraKeyWitnesses BabbageEra
forall era. TxExtraKeyWitnesses era
C.TxExtraKeyWitnessesNone
   , txMetadata :: TxMetadataInEra BabbageEra
txMetadata = TxMetadataInEra BabbageEra
forall era. TxMetadataInEra era
C.TxMetadataNone
   , txAuxScripts :: TxAuxScripts BabbageEra
txAuxScripts = TxAuxScripts BabbageEra
forall era. TxAuxScripts era
C.TxAuxScriptsNone
   , txWithdrawals :: TxWithdrawals BuildTx BabbageEra
txWithdrawals = TxWithdrawals BuildTx BabbageEra
forall build era. TxWithdrawals build era
C.TxWithdrawalsNone
   , txCertificates :: TxCertificates BuildTx BabbageEra
txCertificates = TxCertificates BuildTx BabbageEra
forall build era. TxCertificates build era
C.TxCertificatesNone
   , txUpdateProposal :: TxUpdateProposal BabbageEra
txUpdateProposal = TxUpdateProposal BabbageEra
forall era. TxUpdateProposal era
C.TxUpdateProposalNone
   }