{-# LANGUAGE DeriveAnyClass     #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts   #-}
{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE NamedFieldPuns     #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}
{-# LANGUAGE TemplateHaskell    #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}

module Plutus.V1.Ledger.Tx(
    -- * Transactions
    Tx(..),
    inputs,
    collateralInputs,
    outputs,
    spentOutputs,
    updateUtxoCollateral,
    validValuesTx,
    mintScripts,
    signatures,
    datumWitnesses,
    redeemers,
    lookupSignature,
    lookupDatum,
    lookupRedeemer,
    mint,
    fee,
    ScriptTag (..),
    RedeemerPtr (..),
    Redeemers,
    -- ** Stripped transactions
    TxStripped(..),
    strip,
    -- * Transaction outputs
    TxOut(..),
    TxOutTx(..),
    TxOutRef(..),
    isPubKeyOut,
    isPayToScriptOut,
    outAddress,
    outValue,
    txOutPubKey,
    txOutDatum,
    pubKeyHashTxOut,
    txOutTxDatum,
    -- * Transaction inputs
    TxInType(..),
    TxIn(..),
    inRef,
    inType,
    inScripts,
    validRange,
    pubKeyTxIn,
    scriptTxIn,
    pubKeyTxIns,
    scriptTxIns,
    -- * Addresses
    Address
    ) where

import Codec.CBOR.Write qualified as Write
import Codec.Serialise.Class (Serialise, encode)
import Control.DeepSeq (NFData)
import Control.Lens
import Data.Aeson (FromJSON, FromJSONKey (..), ToJSON, ToJSONKey (..))
import Data.ByteArray qualified as BA
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (isJust)
import Data.Set qualified as Set
import GHC.Generics (Generic)
import Prettyprinter

import PlutusTx qualified
import PlutusTx.Bool qualified as PlutusTx
import PlutusTx.Eq qualified as PlutusTx
import PlutusTx.Lattice

import Plutus.V1.Ledger.Address
import Plutus.V1.Ledger.Crypto
import Plutus.V1.Ledger.Orphans ()
import Plutus.V1.Ledger.Scripts
import Plutus.V1.Ledger.Slot
import Plutus.V1.Ledger.TxId
import Plutus.V1.Ledger.Value
import Plutus.V1.Ledger.Value qualified as V

{- Note [Serialisation and hashing]

We use cryptonite for generating hashes, which requires us to serialise values
to a strict ByteString (to implement `Data.ByteArray.ByteArrayAccess`).

Binary serialisation could be achieved via

1. The `binary` package
2. The `cbor` package

(1) is used in the cardano-sl repository, and (2) is used in the
`plutus-core` project in this repository.

In this module we use (2) because of the precedent. This means however that we
may generate different hashes for the same transactions compared to cardano-sl.
This might become a problem if/when we want to support "imports" of some real
blockchain state into the emulator.

However, it should be easy to change the serialisation mechanism later on,
especially because we only need one direction (to binary).

-}

-- | A transaction, including witnesses for its inputs.
data Tx = Tx {
    Tx -> Set TxIn
txInputs      :: Set.Set TxIn,
    -- ^ The inputs to this transaction.
    Tx -> Set TxIn
txCollateral  :: Set.Set TxIn,
    -- ^ The collateral inputs to cover the fees in case validation of the transaction fails.
    Tx -> [TxOut]
txOutputs     :: [TxOut],
    -- ^ The outputs of this transaction, ordered so they can be referenced by index.
    Tx -> Value
txMint        :: !Value,
    -- ^ The 'Value' minted by this transaction.
    Tx -> Value
txFee         :: !Value,
    -- ^ The fee for this transaction.
    Tx -> SlotRange
txValidRange  :: !SlotRange,
    -- ^ The 'SlotRange' during which this transaction may be validated.
    Tx -> Set MintingPolicy
txMintScripts :: Set.Set MintingPolicy,
    -- ^ The scripts that must be run to check minting conditions.
    Tx -> Map PubKey Signature
txSignatures  :: Map PubKey Signature,
    -- ^ Signatures of this transaction.
    Tx -> Redeemers
txRedeemers   :: Redeemers,
    -- ^ Redeemers of the minting scripts.
    Tx -> Map DatumHash Datum
txData        :: Map DatumHash Datum
    -- ^ Datum objects recorded on this transaction.
    } deriving stock (Int -> Tx -> ShowS
[Tx] -> ShowS
Tx -> String
(Int -> Tx -> ShowS)
-> (Tx -> String) -> ([Tx] -> ShowS) -> Show Tx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tx] -> ShowS
$cshowList :: [Tx] -> ShowS
show :: Tx -> String
$cshow :: Tx -> String
showsPrec :: Int -> Tx -> ShowS
$cshowsPrec :: Int -> Tx -> ShowS
Show, Tx -> Tx -> Bool
(Tx -> Tx -> Bool) -> (Tx -> Tx -> Bool) -> Eq Tx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tx -> Tx -> Bool
$c/= :: Tx -> Tx -> Bool
== :: Tx -> Tx -> Bool
$c== :: Tx -> Tx -> Bool
Eq, (forall x. Tx -> Rep Tx x)
-> (forall x. Rep Tx x -> Tx) -> Generic Tx
forall x. Rep Tx x -> Tx
forall x. Tx -> Rep Tx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Tx x -> Tx
$cfrom :: forall x. Tx -> Rep Tx x
Generic)
      deriving anyclass ([Tx] -> Encoding
[Tx] -> Value
Tx -> Encoding
Tx -> Value
(Tx -> Value)
-> (Tx -> Encoding)
-> ([Tx] -> Value)
-> ([Tx] -> Encoding)
-> ToJSON Tx
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Tx] -> Encoding
$ctoEncodingList :: [Tx] -> Encoding
toJSONList :: [Tx] -> Value
$ctoJSONList :: [Tx] -> Value
toEncoding :: Tx -> Encoding
$ctoEncoding :: Tx -> Encoding
toJSON :: Tx -> Value
$ctoJSON :: Tx -> Value
ToJSON, Value -> Parser [Tx]
Value -> Parser Tx
(Value -> Parser Tx) -> (Value -> Parser [Tx]) -> FromJSON Tx
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Tx]
$cparseJSONList :: Value -> Parser [Tx]
parseJSON :: Value -> Parser Tx
$cparseJSON :: Value -> Parser Tx
FromJSON, [Tx] -> Encoding
Tx -> Encoding
(Tx -> Encoding)
-> (forall s. Decoder s Tx)
-> ([Tx] -> Encoding)
-> (forall s. Decoder s [Tx])
-> Serialise Tx
forall s. Decoder s [Tx]
forall s. Decoder s Tx
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [Tx]
$cdecodeList :: forall s. Decoder s [Tx]
encodeList :: [Tx] -> Encoding
$cencodeList :: [Tx] -> Encoding
decode :: Decoder s Tx
$cdecode :: forall s. Decoder s Tx
encode :: Tx -> Encoding
$cencode :: Tx -> Encoding
Serialise, Tx -> ()
(Tx -> ()) -> NFData Tx
forall a. (a -> ()) -> NFData a
rnf :: Tx -> ()
$crnf :: Tx -> ()
NFData)

instance Semigroup Tx where
    Tx
tx1 <> :: Tx -> Tx -> Tx
<> Tx
tx2 = Tx :: Set TxIn
-> Set TxIn
-> [TxOut]
-> Value
-> Value
-> SlotRange
-> Set MintingPolicy
-> Map PubKey Signature
-> Redeemers
-> Map DatumHash Datum
-> Tx
Tx {
        txInputs :: Set TxIn
txInputs = Tx -> Set TxIn
txInputs Tx
tx1 Set TxIn -> Set TxIn -> Set TxIn
forall a. Semigroup a => a -> a -> a
<> Tx -> Set TxIn
txInputs Tx
tx2,
        txCollateral :: Set TxIn
txCollateral = Tx -> Set TxIn
txCollateral Tx
tx1 Set TxIn -> Set TxIn -> Set TxIn
forall a. Semigroup a => a -> a -> a
<> Tx -> Set TxIn
txCollateral Tx
tx2,
        txOutputs :: [TxOut]
txOutputs = Tx -> [TxOut]
txOutputs Tx
tx1 [TxOut] -> [TxOut] -> [TxOut]
forall a. Semigroup a => a -> a -> a
<> Tx -> [TxOut]
txOutputs Tx
tx2,
        txMint :: Value
txMint = Tx -> Value
txMint Tx
tx1 Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Tx -> Value
txMint Tx
tx2,
        txFee :: Value
txFee = Tx -> Value
txFee Tx
tx1 Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Tx -> Value
txFee Tx
tx2,
        txValidRange :: SlotRange
txValidRange = Tx -> SlotRange
txValidRange Tx
tx1 SlotRange -> SlotRange -> SlotRange
forall a. MeetSemiLattice a => a -> a -> a
/\ Tx -> SlotRange
txValidRange Tx
tx2,
        txMintScripts :: Set MintingPolicy
txMintScripts = Tx -> Set MintingPolicy
txMintScripts Tx
tx1 Set MintingPolicy -> Set MintingPolicy -> Set MintingPolicy
forall a. Semigroup a => a -> a -> a
<> Tx -> Set MintingPolicy
txMintScripts Tx
tx2,
        txSignatures :: Map PubKey Signature
txSignatures = Tx -> Map PubKey Signature
txSignatures Tx
tx1 Map PubKey Signature
-> Map PubKey Signature -> Map PubKey Signature
forall a. Semigroup a => a -> a -> a
<> Tx -> Map PubKey Signature
txSignatures Tx
tx2,
        txRedeemers :: Redeemers
txRedeemers = Tx -> Redeemers
txRedeemers Tx
tx1 Redeemers -> Redeemers -> Redeemers
forall a. Semigroup a => a -> a -> a
<> Tx -> Redeemers
txRedeemers Tx
tx2,
        txData :: Map DatumHash Datum
txData = Tx -> Map DatumHash Datum
txData Tx
tx1 Map DatumHash Datum -> Map DatumHash Datum -> Map DatumHash Datum
forall a. Semigroup a => a -> a -> a
<> Tx -> Map DatumHash Datum
txData Tx
tx2
        }

instance Monoid Tx where
    mempty :: Tx
mempty = Set TxIn
-> Set TxIn
-> [TxOut]
-> Value
-> Value
-> SlotRange
-> Set MintingPolicy
-> Map PubKey Signature
-> Redeemers
-> Map DatumHash Datum
-> Tx
Tx Set TxIn
forall a. Monoid a => a
mempty Set TxIn
forall a. Monoid a => a
mempty [TxOut]
forall a. Monoid a => a
mempty Value
forall a. Monoid a => a
mempty Value
forall a. Monoid a => a
mempty SlotRange
forall a. BoundedMeetSemiLattice a => a
top Set MintingPolicy
forall a. Monoid a => a
mempty Map PubKey Signature
forall a. Monoid a => a
mempty Redeemers
forall a. Monoid a => a
mempty Map DatumHash Datum
forall a. Monoid a => a
mempty

instance BA.ByteArrayAccess Tx where
    length :: Tx -> Int
length        = ByteString -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length (ByteString -> Int) -> (Tx -> ByteString) -> Tx -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString) -> (Tx -> Encoding) -> Tx -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tx -> Encoding
forall a. Serialise a => a -> Encoding
encode
    withByteArray :: Tx -> (Ptr p -> IO a) -> IO a
withByteArray = ByteString -> (Ptr p -> IO a) -> IO a
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
BA.withByteArray (ByteString -> (Ptr p -> IO a) -> IO a)
-> (Tx -> ByteString) -> Tx -> (Ptr p -> IO a) -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString) -> (Tx -> Encoding) -> Tx -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tx -> Encoding
forall a. Serialise a => a -> Encoding
encode

-- | The inputs of a transaction.
inputs :: Lens' Tx (Set.Set TxIn)
inputs :: (Set TxIn -> f (Set TxIn)) -> Tx -> f Tx
inputs = (Tx -> Set TxIn)
-> (Tx -> Set TxIn -> Tx) -> Lens Tx Tx (Set TxIn) (Set TxIn)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Set TxIn
g Tx -> Set TxIn -> Tx
s where
    g :: Tx -> Set TxIn
g = Tx -> Set TxIn
txInputs
    s :: Tx -> Set TxIn -> Tx
s Tx
tx Set TxIn
i = Tx
tx { txInputs :: Set TxIn
txInputs = Set TxIn
i }

-- | The collateral inputs of a transaction for paying fees when validating the transaction fails.
collateralInputs :: Lens' Tx (Set.Set TxIn)
collateralInputs :: (Set TxIn -> f (Set TxIn)) -> Tx -> f Tx
collateralInputs = (Tx -> Set TxIn)
-> (Tx -> Set TxIn -> Tx) -> Lens Tx Tx (Set TxIn) (Set TxIn)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Set TxIn
g Tx -> Set TxIn -> Tx
s where
    g :: Tx -> Set TxIn
g = Tx -> Set TxIn
txCollateral
    s :: Tx -> Set TxIn -> Tx
s Tx
tx Set TxIn
i = Tx
tx { txCollateral :: Set TxIn
txCollateral = Set TxIn
i }

-- | The outputs of a transaction.
outputs :: Lens' Tx [TxOut]
outputs :: ([TxOut] -> f [TxOut]) -> Tx -> f Tx
outputs = (Tx -> [TxOut])
-> (Tx -> [TxOut] -> Tx) -> Lens Tx Tx [TxOut] [TxOut]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> [TxOut]
g Tx -> [TxOut] -> Tx
s where
    g :: Tx -> [TxOut]
g = Tx -> [TxOut]
txOutputs
    s :: Tx -> [TxOut] -> Tx
s Tx
tx [TxOut]
o = Tx
tx { txOutputs :: [TxOut]
txOutputs = [TxOut]
o }

-- | The validity range of a transaction.
validRange :: Lens' Tx SlotRange
validRange :: (SlotRange -> f SlotRange) -> Tx -> f Tx
validRange = (Tx -> SlotRange)
-> (Tx -> SlotRange -> Tx) -> Lens Tx Tx SlotRange SlotRange
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> SlotRange
g Tx -> SlotRange -> Tx
s where
    g :: Tx -> SlotRange
g = Tx -> SlotRange
txValidRange
    s :: Tx -> SlotRange -> Tx
s Tx
tx SlotRange
o = Tx
tx { txValidRange :: SlotRange
txValidRange = SlotRange
o }

signatures :: Lens' Tx (Map PubKey Signature)
signatures :: (Map PubKey Signature -> f (Map PubKey Signature)) -> Tx -> f Tx
signatures = (Tx -> Map PubKey Signature)
-> (Tx -> Map PubKey Signature -> Tx)
-> Lens Tx Tx (Map PubKey Signature) (Map PubKey Signature)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Map PubKey Signature
g Tx -> Map PubKey Signature -> Tx
s where
    g :: Tx -> Map PubKey Signature
g = Tx -> Map PubKey Signature
txSignatures
    s :: Tx -> Map PubKey Signature -> Tx
s Tx
tx Map PubKey Signature
sig = Tx
tx { txSignatures :: Map PubKey Signature
txSignatures = Map PubKey Signature
sig }

fee :: Lens' Tx Value
fee :: (Value -> f Value) -> Tx -> f Tx
fee = (Tx -> Value) -> (Tx -> Value -> Tx) -> Lens Tx Tx Value Value
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Value
g Tx -> Value -> Tx
s where
    g :: Tx -> Value
g = Tx -> Value
txFee
    s :: Tx -> Value -> Tx
s Tx
tx Value
v = Tx
tx { txFee :: Value
txFee = Value
v }

mint :: Lens' Tx Value
mint :: (Value -> f Value) -> Tx -> f Tx
mint = (Tx -> Value) -> (Tx -> Value -> Tx) -> Lens Tx Tx Value Value
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Value
g Tx -> Value -> Tx
s where
    g :: Tx -> Value
g = Tx -> Value
txMint
    s :: Tx -> Value -> Tx
s Tx
tx Value
v = Tx
tx { txMint :: Value
txMint = Value
v }

mintScripts :: Lens' Tx (Set.Set MintingPolicy)
mintScripts :: (Set MintingPolicy -> f (Set MintingPolicy)) -> Tx -> f Tx
mintScripts = (Tx -> Set MintingPolicy)
-> (Tx -> Set MintingPolicy -> Tx)
-> Lens Tx Tx (Set MintingPolicy) (Set MintingPolicy)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Set MintingPolicy
g Tx -> Set MintingPolicy -> Tx
s where
    g :: Tx -> Set MintingPolicy
g = Tx -> Set MintingPolicy
txMintScripts
    s :: Tx -> Set MintingPolicy -> Tx
s Tx
tx Set MintingPolicy
fs = Tx
tx { txMintScripts :: Set MintingPolicy
txMintScripts = Set MintingPolicy
fs }

redeemers :: Lens' Tx Redeemers
redeemers :: (Redeemers -> f Redeemers) -> Tx -> f Tx
redeemers = (Tx -> Redeemers)
-> (Tx -> Redeemers -> Tx) -> Lens Tx Tx Redeemers Redeemers
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Redeemers
g Tx -> Redeemers -> Tx
s where
    g :: Tx -> Redeemers
g = Tx -> Redeemers
txRedeemers
    s :: Tx -> Redeemers -> Tx
s Tx
tx Redeemers
reds = Tx
tx { txRedeemers :: Redeemers
txRedeemers = Redeemers
reds }

datumWitnesses :: Lens' Tx (Map DatumHash Datum)
datumWitnesses :: (Map DatumHash Datum -> f (Map DatumHash Datum)) -> Tx -> f Tx
datumWitnesses = (Tx -> Map DatumHash Datum)
-> (Tx -> Map DatumHash Datum -> Tx)
-> Lens Tx Tx (Map DatumHash Datum) (Map DatumHash Datum)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens Tx -> Map DatumHash Datum
g Tx -> Map DatumHash Datum -> Tx
s where
    g :: Tx -> Map DatumHash Datum
g = Tx -> Map DatumHash Datum
txData
    s :: Tx -> Map DatumHash Datum -> Tx
s Tx
tx Map DatumHash Datum
dat = Tx
tx { txData :: Map DatumHash Datum
txData = Map DatumHash Datum
dat }

lookupSignature :: PubKey -> Tx -> Maybe Signature
lookupSignature :: PubKey -> Tx -> Maybe Signature
lookupSignature PubKey
s Tx{Map PubKey Signature
txSignatures :: Map PubKey Signature
txSignatures :: Tx -> Map PubKey Signature
txSignatures} = PubKey -> Map PubKey Signature -> Maybe Signature
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup PubKey
s Map PubKey Signature
txSignatures

lookupDatum :: Tx -> DatumHash -> Maybe Datum
lookupDatum :: Tx -> DatumHash -> Maybe Datum
lookupDatum Tx{Map DatumHash Datum
txData :: Map DatumHash Datum
txData :: Tx -> Map DatumHash Datum
txData} DatumHash
h = DatumHash -> Map DatumHash Datum -> Maybe Datum
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DatumHash
h Map DatumHash Datum
txData

lookupRedeemer :: Tx -> RedeemerPtr -> Maybe Redeemer
lookupRedeemer :: Tx -> RedeemerPtr -> Maybe Redeemer
lookupRedeemer Tx
tx RedeemerPtr
p = RedeemerPtr -> Redeemers -> Maybe Redeemer
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup RedeemerPtr
p (Tx -> Redeemers
txRedeemers Tx
tx)

-- | Check that all values in a transaction are non-negative.
validValuesTx :: Tx -> Bool
validValuesTx :: Tx -> Bool
validValuesTx Tx{[TxOut]
Map DatumHash Datum
Map PubKey Signature
Redeemers
Set MintingPolicy
Set TxIn
SlotRange
Value
txData :: Map DatumHash Datum
txRedeemers :: Redeemers
txSignatures :: Map PubKey Signature
txMintScripts :: Set MintingPolicy
txValidRange :: SlotRange
txFee :: Value
txMint :: Value
txOutputs :: [TxOut]
txCollateral :: Set TxIn
txInputs :: Set TxIn
txData :: Tx -> Map DatumHash Datum
txRedeemers :: Tx -> Redeemers
txSignatures :: Tx -> Map PubKey Signature
txMintScripts :: Tx -> Set MintingPolicy
txValidRange :: Tx -> SlotRange
txFee :: Tx -> Value
txMint :: Tx -> Value
txOutputs :: Tx -> [TxOut]
txCollateral :: Tx -> Set TxIn
txInputs :: Tx -> Set TxIn
..}
  = (TxOut -> Bool) -> [TxOut] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Value -> Bool
nonNegative (Value -> Bool) -> (TxOut -> Value) -> TxOut -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> Value
txOutValue) [TxOut]
txOutputs  Bool -> Bool -> Bool
&& Value -> Bool
nonNegative Value
txFee
    where
      nonNegative :: Value -> Bool
nonNegative Value
i = Value -> Value -> Bool
V.geq Value
i Value
forall a. Monoid a => a
mempty

-- | A transaction without witnesses for its inputs.
data TxStripped = TxStripped {
    TxStripped -> Set TxOutRef
txStrippedInputs  :: Set.Set TxOutRef,
    -- ^ The inputs to this transaction, as transaction output references only.
    TxStripped -> [TxOut]
txStrippedOutputs :: [TxOut],
    -- ^ The outputs of this transation.
    TxStripped -> Value
txStrippedMint    :: !Value,
    -- ^ The 'Value' minted by this transaction.
    TxStripped -> Value
txStrippedFee     :: !Value
    -- ^ The fee for this transaction.
    } deriving (Int -> TxStripped -> ShowS
[TxStripped] -> ShowS
TxStripped -> String
(Int -> TxStripped -> ShowS)
-> (TxStripped -> String)
-> ([TxStripped] -> ShowS)
-> Show TxStripped
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxStripped] -> ShowS
$cshowList :: [TxStripped] -> ShowS
show :: TxStripped -> String
$cshow :: TxStripped -> String
showsPrec :: Int -> TxStripped -> ShowS
$cshowsPrec :: Int -> TxStripped -> ShowS
Show, TxStripped -> TxStripped -> Bool
(TxStripped -> TxStripped -> Bool)
-> (TxStripped -> TxStripped -> Bool) -> Eq TxStripped
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxStripped -> TxStripped -> Bool
$c/= :: TxStripped -> TxStripped -> Bool
== :: TxStripped -> TxStripped -> Bool
$c== :: TxStripped -> TxStripped -> Bool
Eq, (forall x. TxStripped -> Rep TxStripped x)
-> (forall x. Rep TxStripped x -> TxStripped) -> Generic TxStripped
forall x. Rep TxStripped x -> TxStripped
forall x. TxStripped -> Rep TxStripped x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxStripped x -> TxStripped
$cfrom :: forall x. TxStripped -> Rep TxStripped x
Generic, [TxStripped] -> Encoding
TxStripped -> Encoding
(TxStripped -> Encoding)
-> (forall s. Decoder s TxStripped)
-> ([TxStripped] -> Encoding)
-> (forall s. Decoder s [TxStripped])
-> Serialise TxStripped
forall s. Decoder s [TxStripped]
forall s. Decoder s TxStripped
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxStripped]
$cdecodeList :: forall s. Decoder s [TxStripped]
encodeList :: [TxStripped] -> Encoding
$cencodeList :: [TxStripped] -> Encoding
decode :: Decoder s TxStripped
$cdecode :: forall s. Decoder s TxStripped
encode :: TxStripped -> Encoding
$cencode :: TxStripped -> Encoding
Serialise)

strip :: Tx -> TxStripped
strip :: Tx -> TxStripped
strip Tx{[TxOut]
Map DatumHash Datum
Map PubKey Signature
Redeemers
Set MintingPolicy
Set TxIn
SlotRange
Value
txData :: Map DatumHash Datum
txRedeemers :: Redeemers
txSignatures :: Map PubKey Signature
txMintScripts :: Set MintingPolicy
txValidRange :: SlotRange
txFee :: Value
txMint :: Value
txOutputs :: [TxOut]
txCollateral :: Set TxIn
txInputs :: Set TxIn
txData :: Tx -> Map DatumHash Datum
txRedeemers :: Tx -> Redeemers
txSignatures :: Tx -> Map PubKey Signature
txMintScripts :: Tx -> Set MintingPolicy
txValidRange :: Tx -> SlotRange
txFee :: Tx -> Value
txMint :: Tx -> Value
txOutputs :: Tx -> [TxOut]
txCollateral :: Tx -> Set TxIn
txInputs :: Tx -> Set TxIn
..} = Set TxOutRef -> [TxOut] -> Value -> Value -> TxStripped
TxStripped Set TxOutRef
i [TxOut]
txOutputs Value
txMint Value
txFee where
    i :: Set TxOutRef
i = (TxIn -> TxOutRef) -> Set TxIn -> Set TxOutRef
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map TxIn -> TxOutRef
txInRef Set TxIn
txInputs

-- | A tag indicating the type of script that we are pointing to.
-- NOTE: Cert/Reward are not supported right now.
data ScriptTag = Spend | Mint | Cert | Reward
    deriving stock (Int -> ScriptTag -> ShowS
[ScriptTag] -> ShowS
ScriptTag -> String
(Int -> ScriptTag -> ShowS)
-> (ScriptTag -> String)
-> ([ScriptTag] -> ShowS)
-> Show ScriptTag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ScriptTag] -> ShowS
$cshowList :: [ScriptTag] -> ShowS
show :: ScriptTag -> String
$cshow :: ScriptTag -> String
showsPrec :: Int -> ScriptTag -> ShowS
$cshowsPrec :: Int -> ScriptTag -> ShowS
Show, ScriptTag -> ScriptTag -> Bool
(ScriptTag -> ScriptTag -> Bool)
-> (ScriptTag -> ScriptTag -> Bool) -> Eq ScriptTag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScriptTag -> ScriptTag -> Bool
$c/= :: ScriptTag -> ScriptTag -> Bool
== :: ScriptTag -> ScriptTag -> Bool
$c== :: ScriptTag -> ScriptTag -> Bool
Eq, Eq ScriptTag
Eq ScriptTag
-> (ScriptTag -> ScriptTag -> Ordering)
-> (ScriptTag -> ScriptTag -> Bool)
-> (ScriptTag -> ScriptTag -> Bool)
-> (ScriptTag -> ScriptTag -> Bool)
-> (ScriptTag -> ScriptTag -> Bool)
-> (ScriptTag -> ScriptTag -> ScriptTag)
-> (ScriptTag -> ScriptTag -> ScriptTag)
-> Ord ScriptTag
ScriptTag -> ScriptTag -> Bool
ScriptTag -> ScriptTag -> Ordering
ScriptTag -> ScriptTag -> ScriptTag
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ScriptTag -> ScriptTag -> ScriptTag
$cmin :: ScriptTag -> ScriptTag -> ScriptTag
max :: ScriptTag -> ScriptTag -> ScriptTag
$cmax :: ScriptTag -> ScriptTag -> ScriptTag
>= :: ScriptTag -> ScriptTag -> Bool
$c>= :: ScriptTag -> ScriptTag -> Bool
> :: ScriptTag -> ScriptTag -> Bool
$c> :: ScriptTag -> ScriptTag -> Bool
<= :: ScriptTag -> ScriptTag -> Bool
$c<= :: ScriptTag -> ScriptTag -> Bool
< :: ScriptTag -> ScriptTag -> Bool
$c< :: ScriptTag -> ScriptTag -> Bool
compare :: ScriptTag -> ScriptTag -> Ordering
$ccompare :: ScriptTag -> ScriptTag -> Ordering
$cp1Ord :: Eq ScriptTag
Ord, (forall x. ScriptTag -> Rep ScriptTag x)
-> (forall x. Rep ScriptTag x -> ScriptTag) -> Generic ScriptTag
forall x. Rep ScriptTag x -> ScriptTag
forall x. ScriptTag -> Rep ScriptTag x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ScriptTag x -> ScriptTag
$cfrom :: forall x. ScriptTag -> Rep ScriptTag x
Generic)
    deriving anyclass ([ScriptTag] -> Encoding
ScriptTag -> Encoding
(ScriptTag -> Encoding)
-> (forall s. Decoder s ScriptTag)
-> ([ScriptTag] -> Encoding)
-> (forall s. Decoder s [ScriptTag])
-> Serialise ScriptTag
forall s. Decoder s [ScriptTag]
forall s. Decoder s ScriptTag
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [ScriptTag]
$cdecodeList :: forall s. Decoder s [ScriptTag]
encodeList :: [ScriptTag] -> Encoding
$cencodeList :: [ScriptTag] -> Encoding
decode :: Decoder s ScriptTag
$cdecode :: forall s. Decoder s ScriptTag
encode :: ScriptTag -> Encoding
$cencode :: ScriptTag -> Encoding
Serialise, [ScriptTag] -> Encoding
[ScriptTag] -> Value
ScriptTag -> Encoding
ScriptTag -> Value
(ScriptTag -> Value)
-> (ScriptTag -> Encoding)
-> ([ScriptTag] -> Value)
-> ([ScriptTag] -> Encoding)
-> ToJSON ScriptTag
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ScriptTag] -> Encoding
$ctoEncodingList :: [ScriptTag] -> Encoding
toJSONList :: [ScriptTag] -> Value
$ctoJSONList :: [ScriptTag] -> Value
toEncoding :: ScriptTag -> Encoding
$ctoEncoding :: ScriptTag -> Encoding
toJSON :: ScriptTag -> Value
$ctoJSON :: ScriptTag -> Value
ToJSON, Value -> Parser [ScriptTag]
Value -> Parser ScriptTag
(Value -> Parser ScriptTag)
-> (Value -> Parser [ScriptTag]) -> FromJSON ScriptTag
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ScriptTag]
$cparseJSONList :: Value -> Parser [ScriptTag]
parseJSON :: Value -> Parser ScriptTag
$cparseJSON :: Value -> Parser ScriptTag
FromJSON, ScriptTag -> ()
(ScriptTag -> ()) -> NFData ScriptTag
forall a. (a -> ()) -> NFData a
rnf :: ScriptTag -> ()
$crnf :: ScriptTag -> ()
NFData)

-- | A redeemer pointer is a pair of a script type tag t and an index i, picking out the ith
-- script of type t in the transaction.
data RedeemerPtr = RedeemerPtr ScriptTag Integer
    deriving stock (Int -> RedeemerPtr -> ShowS
[RedeemerPtr] -> ShowS
RedeemerPtr -> String
(Int -> RedeemerPtr -> ShowS)
-> (RedeemerPtr -> String)
-> ([RedeemerPtr] -> ShowS)
-> Show RedeemerPtr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RedeemerPtr] -> ShowS
$cshowList :: [RedeemerPtr] -> ShowS
show :: RedeemerPtr -> String
$cshow :: RedeemerPtr -> String
showsPrec :: Int -> RedeemerPtr -> ShowS
$cshowsPrec :: Int -> RedeemerPtr -> ShowS
Show, RedeemerPtr -> RedeemerPtr -> Bool
(RedeemerPtr -> RedeemerPtr -> Bool)
-> (RedeemerPtr -> RedeemerPtr -> Bool) -> Eq RedeemerPtr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RedeemerPtr -> RedeemerPtr -> Bool
$c/= :: RedeemerPtr -> RedeemerPtr -> Bool
== :: RedeemerPtr -> RedeemerPtr -> Bool
$c== :: RedeemerPtr -> RedeemerPtr -> Bool
Eq, Eq RedeemerPtr
Eq RedeemerPtr
-> (RedeemerPtr -> RedeemerPtr -> Ordering)
-> (RedeemerPtr -> RedeemerPtr -> Bool)
-> (RedeemerPtr -> RedeemerPtr -> Bool)
-> (RedeemerPtr -> RedeemerPtr -> Bool)
-> (RedeemerPtr -> RedeemerPtr -> Bool)
-> (RedeemerPtr -> RedeemerPtr -> RedeemerPtr)
-> (RedeemerPtr -> RedeemerPtr -> RedeemerPtr)
-> Ord RedeemerPtr
RedeemerPtr -> RedeemerPtr -> Bool
RedeemerPtr -> RedeemerPtr -> Ordering
RedeemerPtr -> RedeemerPtr -> RedeemerPtr
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RedeemerPtr -> RedeemerPtr -> RedeemerPtr
$cmin :: RedeemerPtr -> RedeemerPtr -> RedeemerPtr
max :: RedeemerPtr -> RedeemerPtr -> RedeemerPtr
$cmax :: RedeemerPtr -> RedeemerPtr -> RedeemerPtr
>= :: RedeemerPtr -> RedeemerPtr -> Bool
$c>= :: RedeemerPtr -> RedeemerPtr -> Bool
> :: RedeemerPtr -> RedeemerPtr -> Bool
$c> :: RedeemerPtr -> RedeemerPtr -> Bool
<= :: RedeemerPtr -> RedeemerPtr -> Bool
$c<= :: RedeemerPtr -> RedeemerPtr -> Bool
< :: RedeemerPtr -> RedeemerPtr -> Bool
$c< :: RedeemerPtr -> RedeemerPtr -> Bool
compare :: RedeemerPtr -> RedeemerPtr -> Ordering
$ccompare :: RedeemerPtr -> RedeemerPtr -> Ordering
$cp1Ord :: Eq RedeemerPtr
Ord, (forall x. RedeemerPtr -> Rep RedeemerPtr x)
-> (forall x. Rep RedeemerPtr x -> RedeemerPtr)
-> Generic RedeemerPtr
forall x. Rep RedeemerPtr x -> RedeemerPtr
forall x. RedeemerPtr -> Rep RedeemerPtr x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RedeemerPtr x -> RedeemerPtr
$cfrom :: forall x. RedeemerPtr -> Rep RedeemerPtr x
Generic)
    deriving anyclass ([RedeemerPtr] -> Encoding
RedeemerPtr -> Encoding
(RedeemerPtr -> Encoding)
-> (forall s. Decoder s RedeemerPtr)
-> ([RedeemerPtr] -> Encoding)
-> (forall s. Decoder s [RedeemerPtr])
-> Serialise RedeemerPtr
forall s. Decoder s [RedeemerPtr]
forall s. Decoder s RedeemerPtr
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [RedeemerPtr]
$cdecodeList :: forall s. Decoder s [RedeemerPtr]
encodeList :: [RedeemerPtr] -> Encoding
$cencodeList :: [RedeemerPtr] -> Encoding
decode :: Decoder s RedeemerPtr
$cdecode :: forall s. Decoder s RedeemerPtr
encode :: RedeemerPtr -> Encoding
$cencode :: RedeemerPtr -> Encoding
Serialise, [RedeemerPtr] -> Encoding
[RedeemerPtr] -> Value
RedeemerPtr -> Encoding
RedeemerPtr -> Value
(RedeemerPtr -> Value)
-> (RedeemerPtr -> Encoding)
-> ([RedeemerPtr] -> Value)
-> ([RedeemerPtr] -> Encoding)
-> ToJSON RedeemerPtr
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [RedeemerPtr] -> Encoding
$ctoEncodingList :: [RedeemerPtr] -> Encoding
toJSONList :: [RedeemerPtr] -> Value
$ctoJSONList :: [RedeemerPtr] -> Value
toEncoding :: RedeemerPtr -> Encoding
$ctoEncoding :: RedeemerPtr -> Encoding
toJSON :: RedeemerPtr -> Value
$ctoJSON :: RedeemerPtr -> Value
ToJSON, Value -> Parser [RedeemerPtr]
Value -> Parser RedeemerPtr
(Value -> Parser RedeemerPtr)
-> (Value -> Parser [RedeemerPtr]) -> FromJSON RedeemerPtr
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [RedeemerPtr]
$cparseJSONList :: Value -> Parser [RedeemerPtr]
parseJSON :: Value -> Parser RedeemerPtr
$cparseJSON :: Value -> Parser RedeemerPtr
FromJSON, ToJSONKeyFunction [RedeemerPtr]
ToJSONKeyFunction RedeemerPtr
ToJSONKeyFunction RedeemerPtr
-> ToJSONKeyFunction [RedeemerPtr] -> ToJSONKey RedeemerPtr
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [RedeemerPtr]
$ctoJSONKeyList :: ToJSONKeyFunction [RedeemerPtr]
toJSONKey :: ToJSONKeyFunction RedeemerPtr
$ctoJSONKey :: ToJSONKeyFunction RedeemerPtr
ToJSONKey, FromJSONKeyFunction [RedeemerPtr]
FromJSONKeyFunction RedeemerPtr
FromJSONKeyFunction RedeemerPtr
-> FromJSONKeyFunction [RedeemerPtr] -> FromJSONKey RedeemerPtr
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [RedeemerPtr]
$cfromJSONKeyList :: FromJSONKeyFunction [RedeemerPtr]
fromJSONKey :: FromJSONKeyFunction RedeemerPtr
$cfromJSONKey :: FromJSONKeyFunction RedeemerPtr
FromJSONKey, RedeemerPtr -> ()
(RedeemerPtr -> ()) -> NFData RedeemerPtr
forall a. (a -> ()) -> NFData a
rnf :: RedeemerPtr -> ()
$crnf :: RedeemerPtr -> ()
NFData)

type Redeemers = Map RedeemerPtr Redeemer

-- | A reference to a transaction output. This is a
-- pair of a transaction reference, and an index indicating which of the outputs
-- of that transaction we are referring to.
data TxOutRef = TxOutRef {
    TxOutRef -> TxId
txOutRefId  :: TxId,
    TxOutRef -> Integer
txOutRefIdx :: Integer -- ^ Index into the referenced transaction's outputs
    }
    deriving stock (Int -> TxOutRef -> ShowS
[TxOutRef] -> ShowS
TxOutRef -> String
(Int -> TxOutRef -> ShowS)
-> (TxOutRef -> String) -> ([TxOutRef] -> ShowS) -> Show TxOutRef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxOutRef] -> ShowS
$cshowList :: [TxOutRef] -> ShowS
show :: TxOutRef -> String
$cshow :: TxOutRef -> String
showsPrec :: Int -> TxOutRef -> ShowS
$cshowsPrec :: Int -> TxOutRef -> ShowS
Show, TxOutRef -> TxOutRef -> Bool
(TxOutRef -> TxOutRef -> Bool)
-> (TxOutRef -> TxOutRef -> Bool) -> Eq TxOutRef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxOutRef -> TxOutRef -> Bool
$c/= :: TxOutRef -> TxOutRef -> Bool
== :: TxOutRef -> TxOutRef -> Bool
$c== :: TxOutRef -> TxOutRef -> Bool
Eq, Eq TxOutRef
Eq TxOutRef
-> (TxOutRef -> TxOutRef -> Ordering)
-> (TxOutRef -> TxOutRef -> Bool)
-> (TxOutRef -> TxOutRef -> Bool)
-> (TxOutRef -> TxOutRef -> Bool)
-> (TxOutRef -> TxOutRef -> Bool)
-> (TxOutRef -> TxOutRef -> TxOutRef)
-> (TxOutRef -> TxOutRef -> TxOutRef)
-> Ord TxOutRef
TxOutRef -> TxOutRef -> Bool
TxOutRef -> TxOutRef -> Ordering
TxOutRef -> TxOutRef -> TxOutRef
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TxOutRef -> TxOutRef -> TxOutRef
$cmin :: TxOutRef -> TxOutRef -> TxOutRef
max :: TxOutRef -> TxOutRef -> TxOutRef
$cmax :: TxOutRef -> TxOutRef -> TxOutRef
>= :: TxOutRef -> TxOutRef -> Bool
$c>= :: TxOutRef -> TxOutRef -> Bool
> :: TxOutRef -> TxOutRef -> Bool
$c> :: TxOutRef -> TxOutRef -> Bool
<= :: TxOutRef -> TxOutRef -> Bool
$c<= :: TxOutRef -> TxOutRef -> Bool
< :: TxOutRef -> TxOutRef -> Bool
$c< :: TxOutRef -> TxOutRef -> Bool
compare :: TxOutRef -> TxOutRef -> Ordering
$ccompare :: TxOutRef -> TxOutRef -> Ordering
$cp1Ord :: Eq TxOutRef
Ord, (forall x. TxOutRef -> Rep TxOutRef x)
-> (forall x. Rep TxOutRef x -> TxOutRef) -> Generic TxOutRef
forall x. Rep TxOutRef x -> TxOutRef
forall x. TxOutRef -> Rep TxOutRef x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxOutRef x -> TxOutRef
$cfrom :: forall x. TxOutRef -> Rep TxOutRef x
Generic)
    deriving anyclass ([TxOutRef] -> Encoding
TxOutRef -> Encoding
(TxOutRef -> Encoding)
-> (forall s. Decoder s TxOutRef)
-> ([TxOutRef] -> Encoding)
-> (forall s. Decoder s [TxOutRef])
-> Serialise TxOutRef
forall s. Decoder s [TxOutRef]
forall s. Decoder s TxOutRef
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxOutRef]
$cdecodeList :: forall s. Decoder s [TxOutRef]
encodeList :: [TxOutRef] -> Encoding
$cencodeList :: [TxOutRef] -> Encoding
decode :: Decoder s TxOutRef
$cdecode :: forall s. Decoder s TxOutRef
encode :: TxOutRef -> Encoding
$cencode :: TxOutRef -> Encoding
Serialise, [TxOutRef] -> Encoding
[TxOutRef] -> Value
TxOutRef -> Encoding
TxOutRef -> Value
(TxOutRef -> Value)
-> (TxOutRef -> Encoding)
-> ([TxOutRef] -> Value)
-> ([TxOutRef] -> Encoding)
-> ToJSON TxOutRef
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxOutRef] -> Encoding
$ctoEncodingList :: [TxOutRef] -> Encoding
toJSONList :: [TxOutRef] -> Value
$ctoJSONList :: [TxOutRef] -> Value
toEncoding :: TxOutRef -> Encoding
$ctoEncoding :: TxOutRef -> Encoding
toJSON :: TxOutRef -> Value
$ctoJSON :: TxOutRef -> Value
ToJSON, Value -> Parser [TxOutRef]
Value -> Parser TxOutRef
(Value -> Parser TxOutRef)
-> (Value -> Parser [TxOutRef]) -> FromJSON TxOutRef
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxOutRef]
$cparseJSONList :: Value -> Parser [TxOutRef]
parseJSON :: Value -> Parser TxOutRef
$cparseJSON :: Value -> Parser TxOutRef
FromJSON, ToJSONKeyFunction [TxOutRef]
ToJSONKeyFunction TxOutRef
ToJSONKeyFunction TxOutRef
-> ToJSONKeyFunction [TxOutRef] -> ToJSONKey TxOutRef
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [TxOutRef]
$ctoJSONKeyList :: ToJSONKeyFunction [TxOutRef]
toJSONKey :: ToJSONKeyFunction TxOutRef
$ctoJSONKey :: ToJSONKeyFunction TxOutRef
ToJSONKey, FromJSONKeyFunction [TxOutRef]
FromJSONKeyFunction TxOutRef
FromJSONKeyFunction TxOutRef
-> FromJSONKeyFunction [TxOutRef] -> FromJSONKey TxOutRef
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [TxOutRef]
$cfromJSONKeyList :: FromJSONKeyFunction [TxOutRef]
fromJSONKey :: FromJSONKeyFunction TxOutRef
$cfromJSONKey :: FromJSONKeyFunction TxOutRef
FromJSONKey, TxOutRef -> ()
(TxOutRef -> ()) -> NFData TxOutRef
forall a. (a -> ()) -> NFData a
rnf :: TxOutRef -> ()
$crnf :: TxOutRef -> ()
NFData)

instance Pretty TxOutRef where
    pretty :: TxOutRef -> Doc ann
pretty TxOutRef{TxId
txOutRefId :: TxId
txOutRefId :: TxOutRef -> TxId
txOutRefId, Integer
txOutRefIdx :: Integer
txOutRefIdx :: TxOutRef -> Integer
txOutRefIdx} = TxId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty TxId
txOutRefId Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"!" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Integer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Integer
txOutRefIdx

instance PlutusTx.Eq TxOutRef where
    {-# INLINABLE (==) #-}
    TxOutRef
l == :: TxOutRef -> TxOutRef -> Bool
== TxOutRef
r =
        TxOutRef -> TxId
txOutRefId TxOutRef
l TxId -> TxId -> Bool
forall a. Eq a => a -> a -> Bool
PlutusTx.== TxOutRef -> TxId
txOutRefId TxOutRef
r
        Bool -> Bool -> Bool
PlutusTx.&& TxOutRef -> Integer
txOutRefIdx TxOutRef
l Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
PlutusTx.== TxOutRef -> Integer
txOutRefIdx TxOutRef
r

-- | The type of a transaction input.
data TxInType =
      -- TODO: these should all be hashes, with the validators and data segregated to the side
      ConsumeScriptAddress !Validator !Redeemer !Datum -- ^ A transaction input that consumes a script address with the given validator, redeemer, and datum.
    | ConsumePublicKeyAddress -- ^ A transaction input that consumes a public key address.
    | ConsumeSimpleScriptAddress -- ^ Consume a simple script
    deriving stock (Int -> TxInType -> ShowS
[TxInType] -> ShowS
TxInType -> String
(Int -> TxInType -> ShowS)
-> (TxInType -> String) -> ([TxInType] -> ShowS) -> Show TxInType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxInType] -> ShowS
$cshowList :: [TxInType] -> ShowS
show :: TxInType -> String
$cshow :: TxInType -> String
showsPrec :: Int -> TxInType -> ShowS
$cshowsPrec :: Int -> TxInType -> ShowS
Show, TxInType -> TxInType -> Bool
(TxInType -> TxInType -> Bool)
-> (TxInType -> TxInType -> Bool) -> Eq TxInType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxInType -> TxInType -> Bool
$c/= :: TxInType -> TxInType -> Bool
== :: TxInType -> TxInType -> Bool
$c== :: TxInType -> TxInType -> Bool
Eq, Eq TxInType
Eq TxInType
-> (TxInType -> TxInType -> Ordering)
-> (TxInType -> TxInType -> Bool)
-> (TxInType -> TxInType -> Bool)
-> (TxInType -> TxInType -> Bool)
-> (TxInType -> TxInType -> Bool)
-> (TxInType -> TxInType -> TxInType)
-> (TxInType -> TxInType -> TxInType)
-> Ord TxInType
TxInType -> TxInType -> Bool
TxInType -> TxInType -> Ordering
TxInType -> TxInType -> TxInType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TxInType -> TxInType -> TxInType
$cmin :: TxInType -> TxInType -> TxInType
max :: TxInType -> TxInType -> TxInType
$cmax :: TxInType -> TxInType -> TxInType
>= :: TxInType -> TxInType -> Bool
$c>= :: TxInType -> TxInType -> Bool
> :: TxInType -> TxInType -> Bool
$c> :: TxInType -> TxInType -> Bool
<= :: TxInType -> TxInType -> Bool
$c<= :: TxInType -> TxInType -> Bool
< :: TxInType -> TxInType -> Bool
$c< :: TxInType -> TxInType -> Bool
compare :: TxInType -> TxInType -> Ordering
$ccompare :: TxInType -> TxInType -> Ordering
$cp1Ord :: Eq TxInType
Ord, (forall x. TxInType -> Rep TxInType x)
-> (forall x. Rep TxInType x -> TxInType) -> Generic TxInType
forall x. Rep TxInType x -> TxInType
forall x. TxInType -> Rep TxInType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxInType x -> TxInType
$cfrom :: forall x. TxInType -> Rep TxInType x
Generic)
    deriving anyclass ([TxInType] -> Encoding
TxInType -> Encoding
(TxInType -> Encoding)
-> (forall s. Decoder s TxInType)
-> ([TxInType] -> Encoding)
-> (forall s. Decoder s [TxInType])
-> Serialise TxInType
forall s. Decoder s [TxInType]
forall s. Decoder s TxInType
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxInType]
$cdecodeList :: forall s. Decoder s [TxInType]
encodeList :: [TxInType] -> Encoding
$cencodeList :: [TxInType] -> Encoding
decode :: Decoder s TxInType
$cdecode :: forall s. Decoder s TxInType
encode :: TxInType -> Encoding
$cencode :: TxInType -> Encoding
Serialise, [TxInType] -> Encoding
[TxInType] -> Value
TxInType -> Encoding
TxInType -> Value
(TxInType -> Value)
-> (TxInType -> Encoding)
-> ([TxInType] -> Value)
-> ([TxInType] -> Encoding)
-> ToJSON TxInType
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxInType] -> Encoding
$ctoEncodingList :: [TxInType] -> Encoding
toJSONList :: [TxInType] -> Value
$ctoJSONList :: [TxInType] -> Value
toEncoding :: TxInType -> Encoding
$ctoEncoding :: TxInType -> Encoding
toJSON :: TxInType -> Value
$ctoJSON :: TxInType -> Value
ToJSON, Value -> Parser [TxInType]
Value -> Parser TxInType
(Value -> Parser TxInType)
-> (Value -> Parser [TxInType]) -> FromJSON TxInType
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxInType]
$cparseJSONList :: Value -> Parser [TxInType]
parseJSON :: Value -> Parser TxInType
$cparseJSON :: Value -> Parser TxInType
FromJSON, TxInType -> ()
(TxInType -> ()) -> NFData TxInType
forall a. (a -> ()) -> NFData a
rnf :: TxInType -> ()
$crnf :: TxInType -> ()
NFData)

-- | A transaction input, consisting of a transaction output reference and an input type.
data TxIn = TxIn {
    TxIn -> TxOutRef
txInRef  :: !TxOutRef,
    TxIn -> Maybe TxInType
txInType :: Maybe TxInType
    }
    deriving stock (Int -> TxIn -> ShowS
[TxIn] -> ShowS
TxIn -> String
(Int -> TxIn -> ShowS)
-> (TxIn -> String) -> ([TxIn] -> ShowS) -> Show TxIn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxIn] -> ShowS
$cshowList :: [TxIn] -> ShowS
show :: TxIn -> String
$cshow :: TxIn -> String
showsPrec :: Int -> TxIn -> ShowS
$cshowsPrec :: Int -> TxIn -> ShowS
Show, TxIn -> TxIn -> Bool
(TxIn -> TxIn -> Bool) -> (TxIn -> TxIn -> Bool) -> Eq TxIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxIn -> TxIn -> Bool
$c/= :: TxIn -> TxIn -> Bool
== :: TxIn -> TxIn -> Bool
$c== :: TxIn -> TxIn -> Bool
Eq, Eq TxIn
Eq TxIn
-> (TxIn -> TxIn -> Ordering)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> TxIn)
-> (TxIn -> TxIn -> TxIn)
-> Ord TxIn
TxIn -> TxIn -> Bool
TxIn -> TxIn -> Ordering
TxIn -> TxIn -> TxIn
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TxIn -> TxIn -> TxIn
$cmin :: TxIn -> TxIn -> TxIn
max :: TxIn -> TxIn -> TxIn
$cmax :: TxIn -> TxIn -> TxIn
>= :: TxIn -> TxIn -> Bool
$c>= :: TxIn -> TxIn -> Bool
> :: TxIn -> TxIn -> Bool
$c> :: TxIn -> TxIn -> Bool
<= :: TxIn -> TxIn -> Bool
$c<= :: TxIn -> TxIn -> Bool
< :: TxIn -> TxIn -> Bool
$c< :: TxIn -> TxIn -> Bool
compare :: TxIn -> TxIn -> Ordering
$ccompare :: TxIn -> TxIn -> Ordering
$cp1Ord :: Eq TxIn
Ord, (forall x. TxIn -> Rep TxIn x)
-> (forall x. Rep TxIn x -> TxIn) -> Generic TxIn
forall x. Rep TxIn x -> TxIn
forall x. TxIn -> Rep TxIn x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxIn x -> TxIn
$cfrom :: forall x. TxIn -> Rep TxIn x
Generic)
    deriving anyclass ([TxIn] -> Encoding
TxIn -> Encoding
(TxIn -> Encoding)
-> (forall s. Decoder s TxIn)
-> ([TxIn] -> Encoding)
-> (forall s. Decoder s [TxIn])
-> Serialise TxIn
forall s. Decoder s [TxIn]
forall s. Decoder s TxIn
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxIn]
$cdecodeList :: forall s. Decoder s [TxIn]
encodeList :: [TxIn] -> Encoding
$cencodeList :: [TxIn] -> Encoding
decode :: Decoder s TxIn
$cdecode :: forall s. Decoder s TxIn
encode :: TxIn -> Encoding
$cencode :: TxIn -> Encoding
Serialise, [TxIn] -> Encoding
[TxIn] -> Value
TxIn -> Encoding
TxIn -> Value
(TxIn -> Value)
-> (TxIn -> Encoding)
-> ([TxIn] -> Value)
-> ([TxIn] -> Encoding)
-> ToJSON TxIn
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxIn] -> Encoding
$ctoEncodingList :: [TxIn] -> Encoding
toJSONList :: [TxIn] -> Value
$ctoJSONList :: [TxIn] -> Value
toEncoding :: TxIn -> Encoding
$ctoEncoding :: TxIn -> Encoding
toJSON :: TxIn -> Value
$ctoJSON :: TxIn -> Value
ToJSON, Value -> Parser [TxIn]
Value -> Parser TxIn
(Value -> Parser TxIn) -> (Value -> Parser [TxIn]) -> FromJSON TxIn
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxIn]
$cparseJSONList :: Value -> Parser [TxIn]
parseJSON :: Value -> Parser TxIn
$cparseJSON :: Value -> Parser TxIn
FromJSON, TxIn -> ()
(TxIn -> ()) -> NFData TxIn
forall a. (a -> ()) -> NFData a
rnf :: TxIn -> ()
$crnf :: TxIn -> ()
NFData)

instance Pretty TxIn where
    pretty :: TxIn -> Doc ann
pretty TxIn{TxOutRef
txInRef :: TxOutRef
txInRef :: TxIn -> TxOutRef
txInRef,Maybe TxInType
txInType :: Maybe TxInType
txInType :: TxIn -> Maybe TxInType
txInType} =
                let rest :: Doc ann
rest =
                        case Maybe TxInType
txInType of
                            Just (ConsumeScriptAddress Validator
_ Redeemer
redeemer Datum
_) ->
                                Redeemer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Redeemer
redeemer
                            Maybe TxInType
_ -> Doc ann
forall a. Monoid a => a
mempty
                in Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
hang Int
2 (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
"-" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TxOutRef -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty TxOutRef
txInRef, Doc ann
forall ann. Doc ann
rest]

-- | The 'TxOutRef' spent by a transaction input.
inRef :: Lens' TxIn TxOutRef
inRef :: (TxOutRef -> f TxOutRef) -> TxIn -> f TxIn
inRef = (TxIn -> TxOutRef)
-> (TxIn -> TxOutRef -> TxIn) -> Lens TxIn TxIn TxOutRef TxOutRef
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TxIn -> TxOutRef
txInRef TxIn -> TxOutRef -> TxIn
s where
    s :: TxIn -> TxOutRef -> TxIn
s TxIn
txi TxOutRef
r = TxIn
txi { txInRef :: TxOutRef
txInRef = TxOutRef
r }

-- | The type of a transaction input.
inType :: Lens' TxIn (Maybe TxInType)
inType :: (Maybe TxInType -> f (Maybe TxInType)) -> TxIn -> f TxIn
inType = (TxIn -> Maybe TxInType)
-> (TxIn -> Maybe TxInType -> TxIn)
-> Lens TxIn TxIn (Maybe TxInType) (Maybe TxInType)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TxIn -> Maybe TxInType
txInType TxIn -> Maybe TxInType -> TxIn
s where
    s :: TxIn -> Maybe TxInType -> TxIn
s TxIn
txi Maybe TxInType
t = TxIn
txi { txInType :: Maybe TxInType
txInType = Maybe TxInType
t }

-- | Validator, redeemer, and data scripts of a transaction input that spends a
--   "pay to script" output.
inScripts :: TxIn -> Maybe (Validator, Redeemer, Datum)
inScripts :: TxIn -> Maybe (Validator, Redeemer, Datum)
inScripts TxIn{ txInType :: TxIn -> Maybe TxInType
txInType = Maybe TxInType
t } = case Maybe TxInType
t of
    Just (ConsumeScriptAddress Validator
v Redeemer
r Datum
d) -> (Validator, Redeemer, Datum) -> Maybe (Validator, Redeemer, Datum)
forall a. a -> Maybe a
Just (Validator
v, Redeemer
r, Datum
d)
    Maybe TxInType
_                                 -> Maybe (Validator, Redeemer, Datum)
forall a. Maybe a
Nothing

-- | A transaction input that spends a "pay to public key" output, given the witness.
pubKeyTxIn :: TxOutRef -> TxIn
pubKeyTxIn :: TxOutRef -> TxIn
pubKeyTxIn TxOutRef
r = TxOutRef -> Maybe TxInType -> TxIn
TxIn TxOutRef
r (TxInType -> Maybe TxInType
forall a. a -> Maybe a
Just TxInType
ConsumePublicKeyAddress)

-- | A transaction input that spends a "pay to script" output, given witnesses.
scriptTxIn :: TxOutRef -> Validator -> Redeemer -> Datum -> TxIn
scriptTxIn :: TxOutRef -> Validator -> Redeemer -> Datum -> TxIn
scriptTxIn TxOutRef
ref Validator
v Redeemer
r Datum
d = TxOutRef -> Maybe TxInType -> TxIn
TxIn TxOutRef
ref (Maybe TxInType -> TxIn)
-> (TxInType -> Maybe TxInType) -> TxInType -> TxIn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInType -> Maybe TxInType
forall a. a -> Maybe a
Just (TxInType -> TxIn) -> TxInType -> TxIn
forall a b. (a -> b) -> a -> b
$ Validator -> Redeemer -> Datum -> TxInType
ConsumeScriptAddress Validator
v Redeemer
r Datum
d

-- | Filter to get only the pubkey inputs.
pubKeyTxIns :: Fold (Set.Set TxIn) TxIn
pubKeyTxIns :: (TxIn -> f TxIn) -> Set TxIn -> f (Set TxIn)
pubKeyTxIns = (Set TxIn -> Set TxIn) -> Fold (Set TxIn) TxIn
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding ((TxIn -> Bool) -> Set TxIn -> Set TxIn
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (\TxIn{ txInType :: TxIn -> Maybe TxInType
txInType = Maybe TxInType
t } -> Maybe TxInType
t Maybe TxInType -> Maybe TxInType -> Bool
forall a. Eq a => a -> a -> Bool
== TxInType -> Maybe TxInType
forall a. a -> Maybe a
Just TxInType
ConsumePublicKeyAddress))

-- | Filter to get only the script inputs.
scriptTxIns :: Fold (Set.Set TxIn) TxIn
scriptTxIns :: (TxIn -> f TxIn) -> Set TxIn -> f (Set TxIn)
scriptTxIns = (\Set TxIn -> Set TxIn
x -> (Set TxIn -> Set TxIn) -> Fold (Set TxIn) TxIn
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding Set TxIn -> Set TxIn
x) ((Set TxIn -> Set TxIn)
 -> (TxIn -> f TxIn) -> Set TxIn -> f (Set TxIn))
-> ((TxIn -> Bool) -> Set TxIn -> Set TxIn)
-> (TxIn -> Bool)
-> (TxIn -> f TxIn)
-> Set TxIn
-> f (Set TxIn)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxIn -> Bool) -> Set TxIn -> Set TxIn
forall a. (a -> Bool) -> Set a -> Set a
Set.filter ((TxIn -> Bool) -> (TxIn -> f TxIn) -> Set TxIn -> f (Set TxIn))
-> (TxIn -> Bool) -> (TxIn -> f TxIn) -> Set TxIn -> f (Set TxIn)
forall a b. (a -> b) -> a -> b
$ \case
    TxIn{ txInType :: TxIn -> Maybe TxInType
txInType = Just ConsumeScriptAddress{} } -> Bool
True
    TxIn
_                                              -> Bool
False

-- | A transaction output, consisting of a target address, a value, and optionally a datum hash.
data TxOut = TxOut {
    TxOut -> Address
txOutAddress   :: Address,
    TxOut -> Value
txOutValue     :: Value,
    TxOut -> Maybe DatumHash
txOutDatumHash :: Maybe DatumHash
    }
    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 -> Encoding
(TxOut -> Encoding)
-> (forall s. Decoder s TxOut)
-> ([TxOut] -> Encoding)
-> (forall s. Decoder s [TxOut])
-> Serialise TxOut
forall s. Decoder s [TxOut]
forall s. Decoder s TxOut
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxOut]
$cdecodeList :: forall s. Decoder s [TxOut]
encodeList :: [TxOut] -> Encoding
$cencodeList :: [TxOut] -> Encoding
decode :: Decoder s TxOut
$cdecode :: forall s. Decoder s TxOut
encode :: TxOut -> Encoding
$cencode :: TxOut -> Encoding
Serialise, [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, TxOut -> ()
(TxOut -> ()) -> NFData TxOut
forall a. (a -> ()) -> NFData a
rnf :: TxOut -> ()
$crnf :: TxOut -> ()
NFData)

instance Pretty TxOut where
    pretty :: TxOut -> Doc ann
pretty TxOut{Address
txOutAddress :: Address
txOutAddress :: TxOut -> Address
txOutAddress, Value
txOutValue :: Value
txOutValue :: TxOut -> Value
txOutValue} =
                Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
hang Int
2 (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall a b. (a -> b) -> a -> b
$ [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
"-" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Value -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Value
txOutValue Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"addressed to", Address -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Address
txOutAddress]

instance PlutusTx.Eq TxOut where
    {-# INLINABLE (==) #-}
    TxOut
l == :: TxOut -> TxOut -> Bool
== TxOut
r =
        TxOut -> Address
txOutAddress TxOut
l Address -> Address -> Bool
forall a. Eq a => a -> a -> Bool
PlutusTx.== TxOut -> Address
txOutAddress TxOut
r
        Bool -> Bool -> Bool
PlutusTx.&& TxOut -> Value
txOutValue TxOut
l Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
PlutusTx.== TxOut -> Value
txOutValue TxOut
r
        Bool -> Bool -> Bool
PlutusTx.&& TxOut -> Maybe DatumHash
txOutDatumHash TxOut
l Maybe DatumHash -> Maybe DatumHash -> Bool
forall a. Eq a => a -> a -> Bool
PlutusTx.== TxOut -> Maybe DatumHash
txOutDatumHash TxOut
r

-- | The datum attached to a 'TxOut', if there is one.
txOutDatum :: TxOut -> Maybe DatumHash
txOutDatum :: TxOut -> Maybe DatumHash
txOutDatum TxOut{Maybe DatumHash
txOutDatumHash :: Maybe DatumHash
txOutDatumHash :: TxOut -> Maybe DatumHash
txOutDatumHash} = Maybe DatumHash
txOutDatumHash

-- | The public key attached to a 'TxOut', if there is one.
txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutPubKey TxOut{Address
txOutAddress :: Address
txOutAddress :: TxOut -> Address
txOutAddress} = Address -> Maybe PubKeyHash
toPubKeyHash Address
txOutAddress

-- | The address of a transaction output.
outAddress :: Lens' TxOut Address
outAddress :: (Address -> f Address) -> TxOut -> f TxOut
outAddress = (TxOut -> Address)
-> (TxOut -> Address -> TxOut) -> Lens TxOut TxOut Address Address
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TxOut -> Address
txOutAddress TxOut -> Address -> TxOut
s where
    s :: TxOut -> Address -> TxOut
s TxOut
tx Address
a = TxOut
tx { txOutAddress :: Address
txOutAddress = Address
a }

-- | The value of a transaction output.
-- | TODO: Compute address again
outValue :: Lens' TxOut Value
outValue :: (Value -> f Value) -> TxOut -> f TxOut
outValue = (TxOut -> Value)
-> (TxOut -> Value -> TxOut) -> Lens TxOut TxOut Value Value
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TxOut -> Value
txOutValue TxOut -> Value -> TxOut
s where
    s :: TxOut -> Value -> TxOut
s TxOut
tx Value
v = TxOut
tx { txOutValue :: Value
txOutValue = Value
v }

-- | Whether the output is a pay-to-pubkey output.
isPubKeyOut :: TxOut -> Bool
isPubKeyOut :: TxOut -> Bool
isPubKeyOut = Maybe PubKeyHash -> Bool
forall a. Maybe a -> Bool
isJust (Maybe PubKeyHash -> Bool)
-> (TxOut -> Maybe PubKeyHash) -> TxOut -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> Maybe PubKeyHash
txOutPubKey

-- | Whether the output is a pay-to-script output.
isPayToScriptOut :: TxOut -> Bool
isPayToScriptOut :: TxOut -> Bool
isPayToScriptOut = Maybe DatumHash -> Bool
forall a. Maybe a -> Bool
isJust (Maybe DatumHash -> Bool)
-> (TxOut -> Maybe DatumHash) -> TxOut -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxOut -> Maybe DatumHash
txOutDatum

-- | A 'TxOut' along with the 'Tx' it comes from, which may have additional information e.g.
-- the full data script that goes with the 'TxOut'.
data TxOutTx = TxOutTx { TxOutTx -> Tx
txOutTxTx :: Tx, TxOutTx -> TxOut
txOutTxOut :: TxOut }
    deriving stock (Int -> TxOutTx -> ShowS
[TxOutTx] -> ShowS
TxOutTx -> String
(Int -> TxOutTx -> ShowS)
-> (TxOutTx -> String) -> ([TxOutTx] -> ShowS) -> Show TxOutTx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxOutTx] -> ShowS
$cshowList :: [TxOutTx] -> ShowS
show :: TxOutTx -> String
$cshow :: TxOutTx -> String
showsPrec :: Int -> TxOutTx -> ShowS
$cshowsPrec :: Int -> TxOutTx -> ShowS
Show, TxOutTx -> TxOutTx -> Bool
(TxOutTx -> TxOutTx -> Bool)
-> (TxOutTx -> TxOutTx -> Bool) -> Eq TxOutTx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxOutTx -> TxOutTx -> Bool
$c/= :: TxOutTx -> TxOutTx -> Bool
== :: TxOutTx -> TxOutTx -> Bool
$c== :: TxOutTx -> TxOutTx -> Bool
Eq, (forall x. TxOutTx -> Rep TxOutTx x)
-> (forall x. Rep TxOutTx x -> TxOutTx) -> Generic TxOutTx
forall x. Rep TxOutTx x -> TxOutTx
forall x. TxOutTx -> Rep TxOutTx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxOutTx x -> TxOutTx
$cfrom :: forall x. TxOutTx -> Rep TxOutTx x
Generic)
    deriving anyclass ([TxOutTx] -> Encoding
TxOutTx -> Encoding
(TxOutTx -> Encoding)
-> (forall s. Decoder s TxOutTx)
-> ([TxOutTx] -> Encoding)
-> (forall s. Decoder s [TxOutTx])
-> Serialise TxOutTx
forall s. Decoder s [TxOutTx]
forall s. Decoder s TxOutTx
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [TxOutTx]
$cdecodeList :: forall s. Decoder s [TxOutTx]
encodeList :: [TxOutTx] -> Encoding
$cencodeList :: [TxOutTx] -> Encoding
decode :: Decoder s TxOutTx
$cdecode :: forall s. Decoder s TxOutTx
encode :: TxOutTx -> Encoding
$cencode :: TxOutTx -> Encoding
Serialise, [TxOutTx] -> Encoding
[TxOutTx] -> Value
TxOutTx -> Encoding
TxOutTx -> Value
(TxOutTx -> Value)
-> (TxOutTx -> Encoding)
-> ([TxOutTx] -> Value)
-> ([TxOutTx] -> Encoding)
-> ToJSON TxOutTx
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TxOutTx] -> Encoding
$ctoEncodingList :: [TxOutTx] -> Encoding
toJSONList :: [TxOutTx] -> Value
$ctoJSONList :: [TxOutTx] -> Value
toEncoding :: TxOutTx -> Encoding
$ctoEncoding :: TxOutTx -> Encoding
toJSON :: TxOutTx -> Value
$ctoJSON :: TxOutTx -> Value
ToJSON, Value -> Parser [TxOutTx]
Value -> Parser TxOutTx
(Value -> Parser TxOutTx)
-> (Value -> Parser [TxOutTx]) -> FromJSON TxOutTx
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [TxOutTx]
$cparseJSONList :: Value -> Parser [TxOutTx]
parseJSON :: Value -> Parser TxOutTx
$cparseJSON :: Value -> Parser TxOutTx
FromJSON)

txOutTxDatum :: TxOutTx -> Maybe Datum
txOutTxDatum :: TxOutTx -> Maybe Datum
txOutTxDatum (TxOutTx Tx
tx TxOut
out) = TxOut -> Maybe DatumHash
txOutDatum TxOut
out Maybe DatumHash -> (DatumHash -> Maybe Datum) -> Maybe Datum
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Tx -> DatumHash -> Maybe Datum
lookupDatum Tx
tx

-- | Create a transaction output locked by a public key.
pubKeyHashTxOut :: Value -> PubKeyHash -> TxOut
pubKeyHashTxOut :: Value -> PubKeyHash -> TxOut
pubKeyHashTxOut Value
v PubKeyHash
pkh = Address -> Value -> Maybe DatumHash -> TxOut
TxOut (PubKeyHash -> Address
pubKeyHashAddress PubKeyHash
pkh) Value
v Maybe DatumHash
forall a. Maybe a
Nothing

-- | The transaction output references consumed by a transaction.
spentOutputs :: Tx -> Set.Set TxOutRef
spentOutputs :: Tx -> Set TxOutRef
spentOutputs = (TxIn -> TxOutRef) -> Set TxIn -> Set TxOutRef
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map TxIn -> TxOutRef
txInRef (Set TxIn -> Set TxOutRef)
-> (Tx -> Set TxIn) -> Tx -> Set TxOutRef
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tx -> Set TxIn
txInputs

-- | Update a map of unspent transaction outputs and signatures
--   for a failed transaction using its collateral inputs.
updateUtxoCollateral :: Tx -> Map TxOutRef TxOut -> Map TxOutRef TxOut
updateUtxoCollateral :: Tx -> Map TxOutRef TxOut -> Map TxOutRef TxOut
updateUtxoCollateral Tx
tx Map TxOutRef TxOut
unspent = Map TxOutRef TxOut
unspent Map TxOutRef TxOut -> Set TxOutRef -> Map TxOutRef TxOut
forall k a. Ord k => Map k a -> Set k -> Map k a
`Map.withoutKeys` ((TxIn -> TxOutRef) -> Set TxIn -> Set TxOutRef
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map TxIn -> TxOutRef
txInRef (Set TxIn -> Set TxOutRef)
-> (Tx -> Set TxIn) -> Tx -> Set TxOutRef
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tx -> Set TxIn
txCollateral (Tx -> Set TxOutRef) -> Tx -> Set TxOutRef
forall a b. (a -> b) -> a -> b
$ Tx
tx)

PlutusTx.makeIsDataIndexed ''TxOut [('TxOut,0)]
PlutusTx.makeLift ''TxOut

PlutusTx.makeIsDataIndexed ''TxOutRef [('TxOutRef,0)]
PlutusTx.makeLift ''TxOutRef