-- editorconfig-checker-disable-file
{-# LANGUAGE DerivingVia       #-}
{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE ViewPatterns      #-}

{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
{-# OPTIONS_GHC -fno-strictness #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}

module PlutusLedgerApi.V2.Contexts
    (
    -- * Pending transactions and related types
      TxInfo(..)
    , ScriptContext(..)
    , ScriptPurpose(..)
    , TxId (..)
    , TxOut(..)
    , TxOutRef(..)
    , TxInInfo(..)
    , findOwnInput
    , findDatum
    , findDatumHash
    , findTxInByTxOutRef
    , findContinuingOutputs
    , getContinuingOutputs
    -- * Validator functions
    , pubKeyOutputsAt
    , valuePaidTo
    , spendsOutput
    , txSignedBy
    , valueSpent
    , valueProduced
    , ownCurrencySymbol
    ) where

import GHC.Generics (Generic)
import PlutusTx
import PlutusTx.AssocMap hiding (filter, mapMaybe)
import PlutusTx.Prelude hiding (toList)
import Prettyprinter (Pretty (..), nest, vsep, (<+>))

import PlutusLedgerApi.V1.Address (Address (..))
import PlutusLedgerApi.V1.Contexts (ScriptPurpose (..))
import PlutusLedgerApi.V1.Credential (Credential (..), StakingCredential)
import PlutusLedgerApi.V1.Crypto (PubKeyHash (..))
import PlutusLedgerApi.V1.DCert (DCert (..))
import PlutusLedgerApi.V1.Scripts
import PlutusLedgerApi.V1.Time (POSIXTimeRange)
import PlutusLedgerApi.V1.Value (CurrencySymbol, Value)
import PlutusLedgerApi.V2.Tx (TxId (..), TxOut (..), TxOutRef (..))

import Prelude qualified as Haskell

-- | An input of a pending transaction.
data TxInInfo = TxInInfo
    { TxInInfo -> TxOutRef
txInInfoOutRef   :: TxOutRef
    , TxInInfo -> TxOut
txInInfoResolved :: TxOut
    } deriving stock (forall x. Rep TxInInfo x -> TxInInfo
forall x. TxInInfo -> Rep TxInInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxInInfo x -> TxInInfo
$cfrom :: forall x. TxInInfo -> Rep TxInInfo x
Generic, Int -> TxInInfo -> ShowS
[TxInInfo] -> ShowS
TxInInfo -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxInInfo] -> ShowS
$cshowList :: [TxInInfo] -> ShowS
show :: TxInInfo -> String
$cshow :: TxInInfo -> String
showsPrec :: Int -> TxInInfo -> ShowS
$cshowsPrec :: Int -> TxInInfo -> ShowS
Haskell.Show, TxInInfo -> TxInInfo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxInInfo -> TxInInfo -> Bool
$c/= :: TxInInfo -> TxInInfo -> Bool
== :: TxInInfo -> TxInInfo -> Bool
$c== :: TxInInfo -> TxInInfo -> Bool
Haskell.Eq)

instance Eq TxInInfo where
    TxInInfo TxOutRef
ref TxOut
res == :: TxInInfo -> TxInInfo -> Bool
== TxInInfo TxOutRef
ref' TxOut
res' = TxOutRef
ref forall a. Eq a => a -> a -> Bool
== TxOutRef
ref' Bool -> Bool -> Bool
&& TxOut
res forall a. Eq a => a -> a -> Bool
== TxOut
res'

instance Pretty TxInInfo where
    pretty :: forall ann. TxInInfo -> Doc ann
pretty TxInInfo{TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef, TxOut
txInInfoResolved :: TxOut
txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved} =
        forall a ann. Pretty a => a -> Doc ann
pretty TxOutRef
txInInfoOutRef forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"->" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty TxOut
txInInfoResolved

-- | A pending transaction. This is the view as seen by validator scripts, so some details are stripped out.
data TxInfo = TxInfo
    { TxInfo -> [TxInInfo]
txInfoInputs          :: [TxInInfo] -- ^ Transaction inputs
    , TxInfo -> [TxInInfo]
txInfoReferenceInputs :: [TxInInfo] -- ^ Transaction reference inputs
    , TxInfo -> [TxOut]
txInfoOutputs         :: [TxOut] -- ^ Transaction outputs
    , TxInfo -> Value
txInfoFee             :: Value -- ^ The fee paid by this transaction.
    , TxInfo -> Value
txInfoMint            :: Value -- ^ The 'Value' minted by this transaction.
    , TxInfo -> [DCert]
txInfoDCert           :: [DCert] -- ^ Digests of certificates included in this transaction
    , TxInfo -> Map StakingCredential Integer
txInfoWdrl            :: Map StakingCredential Integer -- ^ Withdrawals
    , TxInfo -> POSIXTimeRange
txInfoValidRange      :: POSIXTimeRange -- ^ The valid range for the transaction.
    , TxInfo -> [PubKeyHash]
txInfoSignatories     :: [PubKeyHash] -- ^ Signatures provided with the transaction, attested that they all signed the tx
    , TxInfo -> Map ScriptPurpose Redeemer
txInfoRedeemers       :: Map ScriptPurpose Redeemer
    , TxInfo -> Map DatumHash Datum
txInfoData            :: Map DatumHash Datum
    , TxInfo -> TxId
txInfoId              :: TxId
    -- ^ Hash of the pending transaction (excluding witnesses)
    } deriving stock (forall x. Rep TxInfo x -> TxInfo
forall x. TxInfo -> Rep TxInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxInfo x -> TxInfo
$cfrom :: forall x. TxInfo -> Rep TxInfo x
Generic, Int -> TxInfo -> ShowS
[TxInfo] -> ShowS
TxInfo -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxInfo] -> ShowS
$cshowList :: [TxInfo] -> ShowS
show :: TxInfo -> String
$cshow :: TxInfo -> String
showsPrec :: Int -> TxInfo -> ShowS
$cshowsPrec :: Int -> TxInfo -> ShowS
Haskell.Show, TxInfo -> TxInfo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxInfo -> TxInfo -> Bool
$c/= :: TxInfo -> TxInfo -> Bool
== :: TxInfo -> TxInfo -> Bool
$c== :: TxInfo -> TxInfo -> Bool
Haskell.Eq)

instance Eq TxInfo where
    {-# INLINABLE (==) #-}
    TxInfo [TxInInfo]
i [TxInInfo]
ri [TxOut]
o Value
f Value
m [DCert]
c Map StakingCredential Integer
w POSIXTimeRange
r [PubKeyHash]
s Map ScriptPurpose Redeemer
rs Map DatumHash Datum
d TxId
tid == :: TxInfo -> TxInfo -> Bool
== TxInfo [TxInInfo]
i' [TxInInfo]
ri' [TxOut]
o' Value
f' Value
m' [DCert]
c' Map StakingCredential Integer
w' POSIXTimeRange
r' [PubKeyHash]
s' Map ScriptPurpose Redeemer
rs' Map DatumHash Datum
d' TxId
tid' =
        [TxInInfo]
i forall a. Eq a => a -> a -> Bool
== [TxInInfo]
i' Bool -> Bool -> Bool
&& [TxInInfo]
ri forall a. Eq a => a -> a -> Bool
== [TxInInfo]
ri' Bool -> Bool -> Bool
&& [TxOut]
o forall a. Eq a => a -> a -> Bool
== [TxOut]
o' Bool -> Bool -> Bool
&& Value
f forall a. Eq a => a -> a -> Bool
== Value
f' Bool -> Bool -> Bool
&& Value
m forall a. Eq a => a -> a -> Bool
== Value
m' Bool -> Bool -> Bool
&& [DCert]
c forall a. Eq a => a -> a -> Bool
== [DCert]
c' Bool -> Bool -> Bool
&& Map StakingCredential Integer
w forall a. Eq a => a -> a -> Bool
== Map StakingCredential Integer
w' Bool -> Bool -> Bool
&& POSIXTimeRange
r forall a. Eq a => a -> a -> Bool
== POSIXTimeRange
r' Bool -> Bool -> Bool
&& [PubKeyHash]
s forall a. Eq a => a -> a -> Bool
== [PubKeyHash]
s' Bool -> Bool -> Bool
&& Map ScriptPurpose Redeemer
rs forall a. Eq a => a -> a -> Bool
== Map ScriptPurpose Redeemer
rs' Bool -> Bool -> Bool
&& Map DatumHash Datum
d forall a. Eq a => a -> a -> Bool
== Map DatumHash Datum
d' Bool -> Bool -> Bool
&& TxId
tid forall a. Eq a => a -> a -> Bool
== TxId
tid'

instance Pretty TxInfo where
    pretty :: forall ann. TxInfo -> Doc ann
pretty TxInfo{[TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs, [TxInInfo]
txInfoReferenceInputs :: [TxInInfo]
txInfoReferenceInputs :: TxInfo -> [TxInInfo]
txInfoReferenceInputs, [TxOut]
txInfoOutputs :: [TxOut]
txInfoOutputs :: TxInfo -> [TxOut]
txInfoOutputs, Value
txInfoFee :: Value
txInfoFee :: TxInfo -> Value
txInfoFee, Value
txInfoMint :: Value
txInfoMint :: TxInfo -> Value
txInfoMint, [DCert]
txInfoDCert :: [DCert]
txInfoDCert :: TxInfo -> [DCert]
txInfoDCert, Map StakingCredential Integer
txInfoWdrl :: Map StakingCredential Integer
txInfoWdrl :: TxInfo -> Map StakingCredential Integer
txInfoWdrl, POSIXTimeRange
txInfoValidRange :: POSIXTimeRange
txInfoValidRange :: TxInfo -> POSIXTimeRange
txInfoValidRange, [PubKeyHash]
txInfoSignatories :: [PubKeyHash]
txInfoSignatories :: TxInfo -> [PubKeyHash]
txInfoSignatories, Map ScriptPurpose Redeemer
txInfoRedeemers :: Map ScriptPurpose Redeemer
txInfoRedeemers :: TxInfo -> Map ScriptPurpose Redeemer
txInfoRedeemers, Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData, TxId
txInfoId :: TxId
txInfoId :: TxInfo -> TxId
txInfoId} =
        forall ann. [Doc ann] -> Doc ann
vsep
            [ Doc ann
"TxId:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty TxId
txInfoId
            , Doc ann
"Inputs:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty [TxInInfo]
txInfoInputs
            , Doc ann
"Reference inputs:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty [TxInInfo]
txInfoReferenceInputs
            , Doc ann
"Outputs:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty [TxOut]
txInfoOutputs
            , Doc ann
"Fee:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Value
txInfoFee
            , Doc ann
"Value minted:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Value
txInfoMint
            , Doc ann
"DCerts:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty [DCert]
txInfoDCert
            , Doc ann
"Wdrl:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Map StakingCredential Integer
txInfoWdrl
            , Doc ann
"Valid range:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty POSIXTimeRange
txInfoValidRange
            , Doc ann
"Signatories:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty [PubKeyHash]
txInfoSignatories
            , Doc ann
"Redeemers:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Map ScriptPurpose Redeemer
txInfoRedeemers
            , Doc ann
"Datums:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Map DatumHash Datum
txInfoData
            ]

data ScriptContext = ScriptContext{ScriptContext -> TxInfo
scriptContextTxInfo :: TxInfo, ScriptContext -> ScriptPurpose
scriptContextPurpose :: ScriptPurpose }
    deriving stock (forall x. Rep ScriptContext x -> ScriptContext
forall x. ScriptContext -> Rep ScriptContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ScriptContext x -> ScriptContext
$cfrom :: forall x. ScriptContext -> Rep ScriptContext x
Generic, ScriptContext -> ScriptContext -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ScriptContext -> ScriptContext -> Bool
$c/= :: ScriptContext -> ScriptContext -> Bool
== :: ScriptContext -> ScriptContext -> Bool
$c== :: ScriptContext -> ScriptContext -> Bool
Haskell.Eq, Int -> ScriptContext -> ShowS
[ScriptContext] -> ShowS
ScriptContext -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ScriptContext] -> ShowS
$cshowList :: [ScriptContext] -> ShowS
show :: ScriptContext -> String
$cshow :: ScriptContext -> String
showsPrec :: Int -> ScriptContext -> ShowS
$cshowsPrec :: Int -> ScriptContext -> ShowS
Haskell.Show)

instance Eq ScriptContext where
    {-# INLINABLE (==) #-}
    ScriptContext TxInfo
info ScriptPurpose
purpose == :: ScriptContext -> ScriptContext -> Bool
== ScriptContext TxInfo
info' ScriptPurpose
purpose' = TxInfo
info forall a. Eq a => a -> a -> Bool
== TxInfo
info' Bool -> Bool -> Bool
&& ScriptPurpose
purpose forall a. Eq a => a -> a -> Bool
== ScriptPurpose
purpose'

instance Pretty ScriptContext where
    pretty :: forall ann. ScriptContext -> Doc ann
pretty ScriptContext{TxInfo
scriptContextTxInfo :: TxInfo
scriptContextTxInfo :: ScriptContext -> TxInfo
scriptContextTxInfo, ScriptPurpose
scriptContextPurpose :: ScriptPurpose
scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose} =
        forall ann. [Doc ann] -> Doc ann
vsep
            [ Doc ann
"Purpose:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty ScriptPurpose
scriptContextPurpose
            , forall ann. Int -> Doc ann -> Doc ann
nest Int
2 forall a b. (a -> b) -> a -> b
$ forall ann. [Doc ann] -> Doc ann
vsep [Doc ann
"TxInfo:", forall a ann. Pretty a => a -> Doc ann
pretty TxInfo
scriptContextTxInfo]
            ]

{-# INLINABLE findOwnInput #-}
-- | Find the input currently being validated.
findOwnInput :: ScriptContext -> Maybe TxInInfo
findOwnInput :: ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext{scriptContextTxInfo :: ScriptContext -> TxInfo
scriptContextTxInfo=TxInfo{[TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs}, scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose=Spending TxOutRef
txOutRef} =
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\TxInInfo{TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef} -> TxOutRef
txInInfoOutRef forall a. Eq a => a -> a -> Bool
== TxOutRef
txOutRef) [TxInInfo]
txInfoInputs
findOwnInput ScriptContext
_ = forall a. Maybe a
Nothing

{-# INLINABLE findDatum #-}
-- | Find the data corresponding to a data hash, if there is one
findDatum :: DatumHash -> TxInfo -> Maybe Datum
findDatum :: DatumHash -> TxInfo -> Maybe Datum
findDatum DatumHash
dsh TxInfo{Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData} = forall k v. Eq k => k -> Map k v -> Maybe v
lookup DatumHash
dsh Map DatumHash Datum
txInfoData

{-# INLINABLE findDatumHash #-}
-- | Find the hash of a datum, if it is part of the pending transaction's
--   hashes
findDatumHash :: Datum -> TxInfo -> Maybe DatumHash
findDatumHash :: Datum -> TxInfo -> Maybe DatumHash
findDatumHash Datum
ds TxInfo{Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData} = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find forall {a}. (a, Datum) -> Bool
f (forall k v. Map k v -> [(k, v)]
toList Map DatumHash Datum
txInfoData)
    where
        f :: (a, Datum) -> Bool
f (a
_, Datum
ds') = Datum
ds' forall a. Eq a => a -> a -> Bool
== Datum
ds

{-# INLINABLE findTxInByTxOutRef #-}
findTxInByTxOutRef :: TxOutRef -> TxInfo -> Maybe TxInInfo
findTxInByTxOutRef :: TxOutRef -> TxInfo -> Maybe TxInInfo
findTxInByTxOutRef TxOutRef
outRef TxInfo{[TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs} =
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\TxInInfo{TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef} -> TxOutRef
txInInfoOutRef forall a. Eq a => a -> a -> Bool
== TxOutRef
outRef) [TxInInfo]
txInfoInputs

{-# INLINABLE findContinuingOutputs #-}
-- | Find the indices of all the outputs that pay to the same script address we are currently spending from, if any.
findContinuingOutputs :: ScriptContext -> [Integer]
findContinuingOutputs :: ScriptContext -> [Integer]
findContinuingOutputs ScriptContext
ctx | Just TxInInfo{txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved=TxOut{Address
txOutAddress :: TxOut -> Address
txOutAddress :: Address
txOutAddress}} <- ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext
ctx = forall a. (a -> Bool) -> [a] -> [Integer]
findIndices (Address -> TxOut -> Bool
f Address
txOutAddress) (TxInfo -> [TxOut]
txInfoOutputs forall a b. (a -> b) -> a -> b
$ ScriptContext -> TxInfo
scriptContextTxInfo ScriptContext
ctx)
    where
        f :: Address -> TxOut -> Bool
f Address
addr TxOut{txOutAddress :: TxOut -> Address
txOutAddress=Address
otherAddress} = Address
addr forall a. Eq a => a -> a -> Bool
== Address
otherAddress
findContinuingOutputs ScriptContext
_ = forall a. BuiltinString -> a
traceError BuiltinString
"Le" -- "Can't find any continuing outputs"

{-# INLINABLE getContinuingOutputs #-}
-- | Get all the outputs that pay to the same script address we are currently spending from, if any.
getContinuingOutputs :: ScriptContext -> [TxOut]
getContinuingOutputs :: ScriptContext -> [TxOut]
getContinuingOutputs ScriptContext
ctx | Just TxInInfo{txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved=TxOut{Address
txOutAddress :: Address
txOutAddress :: TxOut -> Address
txOutAddress}} <- ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext
ctx = forall a. (a -> Bool) -> [a] -> [a]
filter (Address -> TxOut -> Bool
f Address
txOutAddress) (TxInfo -> [TxOut]
txInfoOutputs forall a b. (a -> b) -> a -> b
$ ScriptContext -> TxInfo
scriptContextTxInfo ScriptContext
ctx)
    where
        f :: Address -> TxOut -> Bool
f Address
addr TxOut{txOutAddress :: TxOut -> Address
txOutAddress=Address
otherAddress} = Address
addr forall a. Eq a => a -> a -> Bool
== Address
otherAddress
getContinuingOutputs ScriptContext
_ = forall a. BuiltinString -> a
traceError BuiltinString
"Lf" -- "Can't get any continuing outputs"

{-# INLINABLE txSignedBy #-}
-- | Check if a transaction was signed by the given public key.
txSignedBy :: TxInfo -> PubKeyHash -> Bool
txSignedBy :: TxInfo -> PubKeyHash -> Bool
txSignedBy TxInfo{[PubKeyHash]
txInfoSignatories :: [PubKeyHash]
txInfoSignatories :: TxInfo -> [PubKeyHash]
txInfoSignatories} PubKeyHash
k = case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall a. Eq a => a -> a -> Bool
(==) PubKeyHash
k) [PubKeyHash]
txInfoSignatories of
    Just PubKeyHash
_  -> Bool
True
    Maybe PubKeyHash
Nothing -> Bool
False

{-# INLINABLE pubKeyOutputsAt #-}
-- | Get the values paid to a public key address by a pending transaction.
pubKeyOutputsAt :: PubKeyHash -> TxInfo -> [Value]
pubKeyOutputsAt :: PubKeyHash -> TxInfo -> [Value]
pubKeyOutputsAt PubKeyHash
pk TxInfo
p =
    let flt :: TxOut -> Maybe Value
flt TxOut{txOutAddress :: TxOut -> Address
txOutAddress = Address (PubKeyCredential PubKeyHash
pk') Maybe StakingCredential
_, Value
txOutValue :: TxOut -> Value
txOutValue :: Value
txOutValue} | PubKeyHash
pk forall a. Eq a => a -> a -> Bool
== PubKeyHash
pk' = forall a. a -> Maybe a
Just Value
txOutValue
        flt TxOut
_                             = forall a. Maybe a
Nothing
    in forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe TxOut -> Maybe Value
flt (TxInfo -> [TxOut]
txInfoOutputs TxInfo
p)

{-# INLINABLE valuePaidTo #-}
-- | Get the total value paid to a public key address by a pending transaction.
valuePaidTo :: TxInfo -> PubKeyHash -> Value
valuePaidTo :: TxInfo -> PubKeyHash -> Value
valuePaidTo TxInfo
ptx PubKeyHash
pkh = forall a. Monoid a => [a] -> a
mconcat (PubKeyHash -> TxInfo -> [Value]
pubKeyOutputsAt PubKeyHash
pkh TxInfo
ptx)

{-# INLINABLE valueSpent #-}
-- | Get the total value of inputs spent by this transaction.
valueSpent :: TxInfo -> Value
valueSpent :: TxInfo -> Value
valueSpent = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (TxOut -> Value
txOutValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInInfo -> TxOut
txInInfoResolved) forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInfo -> [TxInInfo]
txInfoInputs

{-# INLINABLE valueProduced #-}
-- | Get the total value of outputs produced by this transaction.
valueProduced :: TxInfo -> Value
valueProduced :: TxInfo -> Value
valueProduced = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxOut -> Value
txOutValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInfo -> [TxOut]
txInfoOutputs

{-# INLINABLE ownCurrencySymbol #-}
-- | The 'CurrencySymbol' of the current validator script.
ownCurrencySymbol :: ScriptContext -> CurrencySymbol
ownCurrencySymbol :: ScriptContext -> CurrencySymbol
ownCurrencySymbol ScriptContext{scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose=Minting CurrencySymbol
cs} = CurrencySymbol
cs
ownCurrencySymbol ScriptContext
_                                              = forall a. BuiltinString -> a
traceError BuiltinString
"Lh" -- "Can't get currency symbol of the current validator script"

{-# INLINABLE spendsOutput #-}
-- | Check if the pending transaction spends a specific transaction output
--   (identified by the hash of a transaction and an index into that
--   transactions' outputs)
spendsOutput :: TxInfo -> TxId -> Integer -> Bool
spendsOutput :: TxInfo -> TxId -> Integer -> Bool
spendsOutput TxInfo
p TxId
h Integer
i =
    let spendsOutRef :: TxInInfo -> Bool
spendsOutRef TxInInfo
inp =
            let outRef :: TxOutRef
outRef = TxInInfo -> TxOutRef
txInInfoOutRef TxInInfo
inp
            in TxId
h forall a. Eq a => a -> a -> Bool
== TxOutRef -> TxId
txOutRefId TxOutRef
outRef
                Bool -> Bool -> Bool
&& Integer
i forall a. Eq a => a -> a -> Bool
== TxOutRef -> Integer
txOutRefIdx TxOutRef
outRef

    in forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any TxInInfo -> Bool
spendsOutRef (TxInfo -> [TxInInfo]
txInfoInputs TxInfo
p)

makeLift ''TxInInfo
makeIsDataIndexed ''TxInInfo [('TxInInfo,0)]

makeLift ''TxInfo
makeIsDataIndexed ''TxInfo [('TxInfo,0)]

makeLift ''ScriptContext
makeIsDataIndexed ''ScriptContext [('ScriptContext,0)]