{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# 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
(
TxInfo(..)
, ScriptContext(..)
, ScriptPurpose(..)
, TxId (..)
, TxOut(..)
, TxOutRef(..)
, TxInInfo(..)
, findOwnInput
, findDatum
, findDatumHash
, findTxInByTxOutRef
, findContinuingOutputs
, getContinuingOutputs
, pubKeyOutputsAt
, valuePaidTo
, spendsOutput
, txSignedBy
, valueSpent
, valueProduced
, ownCurrencySymbol
) where
import PlutusTx.Prelude hiding (toList)
import Prelude qualified as Haskell
import GHC.Generics (Generic)
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 (Datum, DatumHash, Redeemer)
import PlutusLedgerApi.V1.Time (POSIXTimeRange)
import PlutusLedgerApi.V1.Value (CurrencySymbol, Value)
import PlutusLedgerApi.V2.Tx (TxId (..), TxOut (..), TxOutRef (..))
import PlutusTx.AssocMap (Map, lookup, toList)
import PlutusTx.Blueprint.Definition (HasBlueprintDefinition)
import PlutusTx.Blueprint.Definition.Derive (definitionRef)
import PlutusTx.Blueprint.TH (makeIsDataSchemaIndexed)
import PlutusTx.Lift (makeLift)
import Prettyprinter (Pretty (..), nest, vsep, (<+>))
data TxInInfo = TxInInfo
{ TxInInfo -> TxOutRef
txInInfoOutRef :: TxOutRef
, TxInInfo -> TxOut
txInInfoResolved :: TxOut
}
deriving stock ((forall x. TxInInfo -> Rep TxInInfo x)
-> (forall x. Rep TxInInfo x -> TxInInfo) -> Generic TxInInfo
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
$cfrom :: forall x. TxInInfo -> Rep TxInInfo x
from :: forall x. TxInInfo -> Rep TxInInfo x
$cto :: forall x. Rep TxInInfo x -> TxInInfo
to :: forall x. Rep TxInInfo x -> TxInInfo
Generic, Int -> TxInInfo -> ShowS
[TxInInfo] -> ShowS
TxInInfo -> String
(Int -> TxInInfo -> ShowS)
-> (TxInInfo -> String) -> ([TxInInfo] -> ShowS) -> Show TxInInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxInInfo -> ShowS
showsPrec :: Int -> TxInInfo -> ShowS
$cshow :: TxInInfo -> String
show :: TxInInfo -> String
$cshowList :: [TxInInfo] -> ShowS
showList :: [TxInInfo] -> ShowS
Haskell.Show, TxInInfo -> TxInInfo -> Bool
(TxInInfo -> TxInInfo -> Bool)
-> (TxInInfo -> TxInInfo -> Bool) -> Eq TxInInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxInInfo -> TxInInfo -> Bool
== :: TxInInfo -> TxInInfo -> Bool
$c/= :: TxInInfo -> TxInInfo -> Bool
/= :: TxInInfo -> TxInInfo -> Bool
Haskell.Eq)
deriving anyclass (DefinitionId
DefinitionId -> HasBlueprintDefinition TxInInfo
forall t. DefinitionId -> HasBlueprintDefinition t
$cdefinitionId :: DefinitionId
definitionId :: DefinitionId
HasBlueprintDefinition)
instance Eq TxInInfo where
TxInInfo TxOutRef
ref TxOut
res == :: TxInInfo -> TxInInfo -> Bool
== TxInInfo TxOutRef
ref' TxOut
res' = TxOutRef
ref TxOutRef -> TxOutRef -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef
ref' Bool -> Bool -> Bool
&& TxOut
res TxOut -> TxOut -> Bool
forall a. Eq a => a -> a -> Bool
== TxOut
res'
instance Pretty TxInInfo where
pretty :: forall ann. TxInInfo -> Doc ann
pretty TxInInfo{TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef, TxOut
txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved :: TxOut
txInInfoResolved} =
TxOutRef -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. TxOutRef -> Doc ann
pretty TxOutRef
txInInfoOutRef Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"->" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TxOut -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. TxOut -> Doc ann
pretty TxOut
txInInfoResolved
data TxInfo = TxInfo
{ TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
, TxInfo -> [TxInInfo]
txInfoReferenceInputs :: [TxInInfo]
, TxInfo -> [TxOut]
txInfoOutputs :: [TxOut]
, TxInfo -> Value
txInfoFee :: Value
, TxInfo -> Value
txInfoMint :: Value
, TxInfo -> [DCert]
txInfoDCert :: [DCert]
, TxInfo -> Map StakingCredential Integer
txInfoWdrl :: Map StakingCredential Integer
, TxInfo -> POSIXTimeRange
txInfoValidRange :: POSIXTimeRange
, TxInfo -> [PubKeyHash]
txInfoSignatories :: [PubKeyHash]
, TxInfo -> Map ScriptPurpose Redeemer
txInfoRedeemers :: Map ScriptPurpose Redeemer
, TxInfo -> Map DatumHash Datum
txInfoData :: Map DatumHash Datum
, TxInfo -> TxId
txInfoId :: TxId
}
deriving stock ((forall x. TxInfo -> Rep TxInfo x)
-> (forall x. Rep TxInfo x -> TxInfo) -> Generic TxInfo
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
$cfrom :: forall x. TxInfo -> Rep TxInfo x
from :: forall x. TxInfo -> Rep TxInfo x
$cto :: forall x. Rep TxInfo x -> TxInfo
to :: forall x. Rep TxInfo x -> TxInfo
Generic, Int -> TxInfo -> ShowS
[TxInfo] -> ShowS
TxInfo -> String
(Int -> TxInfo -> ShowS)
-> (TxInfo -> String) -> ([TxInfo] -> ShowS) -> Show TxInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxInfo -> ShowS
showsPrec :: Int -> TxInfo -> ShowS
$cshow :: TxInfo -> String
show :: TxInfo -> String
$cshowList :: [TxInfo] -> ShowS
showList :: [TxInfo] -> ShowS
Haskell.Show, TxInfo -> TxInfo -> Bool
(TxInfo -> TxInfo -> Bool)
-> (TxInfo -> TxInfo -> Bool) -> Eq TxInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxInfo -> TxInfo -> Bool
== :: TxInfo -> TxInfo -> Bool
$c/= :: TxInfo -> TxInfo -> Bool
/= :: TxInfo -> TxInfo -> Bool
Haskell.Eq)
deriving anyclass (DefinitionId
DefinitionId -> HasBlueprintDefinition TxInfo
forall t. DefinitionId -> HasBlueprintDefinition t
$cdefinitionId :: DefinitionId
definitionId :: DefinitionId
HasBlueprintDefinition)
instance Pretty TxInfo where
pretty :: forall ann. TxInfo -> Doc ann
pretty TxInfo{[TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs, [TxInInfo]
txInfoReferenceInputs :: TxInfo -> [TxInInfo]
txInfoReferenceInputs :: [TxInInfo]
txInfoReferenceInputs, [TxOut]
txInfoOutputs :: TxInfo -> [TxOut]
txInfoOutputs :: [TxOut]
txInfoOutputs, Value
txInfoFee :: TxInfo -> Value
txInfoFee :: Value
txInfoFee, Value
txInfoMint :: TxInfo -> Value
txInfoMint :: Value
txInfoMint, [DCert]
txInfoDCert :: TxInfo -> [DCert]
txInfoDCert :: [DCert]
txInfoDCert, Map StakingCredential Integer
txInfoWdrl :: TxInfo -> Map StakingCredential Integer
txInfoWdrl :: Map StakingCredential Integer
txInfoWdrl, POSIXTimeRange
txInfoValidRange :: TxInfo -> POSIXTimeRange
txInfoValidRange :: POSIXTimeRange
txInfoValidRange, [PubKeyHash]
txInfoSignatories :: TxInfo -> [PubKeyHash]
txInfoSignatories :: [PubKeyHash]
txInfoSignatories, Map ScriptPurpose Redeemer
txInfoRedeemers :: TxInfo -> Map ScriptPurpose Redeemer
txInfoRedeemers :: Map ScriptPurpose Redeemer
txInfoRedeemers, Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData, TxId
txInfoId :: TxInfo -> TxId
txInfoId :: TxId
txInfoId} =
[Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep
[ Doc ann
"TxId:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TxId -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. TxId -> Doc ann
pretty TxId
txInfoId
, Doc ann
"Inputs:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [TxInInfo] -> Doc ann
forall ann. [TxInInfo] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [TxInInfo]
txInfoInputs
, Doc ann
"Reference inputs:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [TxInInfo] -> Doc ann
forall ann. [TxInInfo] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [TxInInfo]
txInfoReferenceInputs
, Doc ann
"Outputs:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [TxOut] -> Doc ann
forall ann. [TxOut] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [TxOut]
txInfoOutputs
, Doc ann
"Fee:" 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
forall ann. Value -> Doc ann
pretty Value
txInfoFee
, Doc ann
"Value minted:" 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
forall ann. Value -> Doc ann
pretty Value
txInfoMint
, Doc ann
"DCerts:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [DCert] -> Doc ann
forall ann. [DCert] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [DCert]
txInfoDCert
, Doc ann
"Wdrl:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Map StakingCredential Integer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Map StakingCredential Integer -> Doc ann
pretty Map StakingCredential Integer
txInfoWdrl
, Doc ann
"Valid range:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> POSIXTimeRange -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. POSIXTimeRange -> Doc ann
pretty POSIXTimeRange
txInfoValidRange
, Doc ann
"Signatories:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [PubKeyHash] -> Doc ann
forall ann. [PubKeyHash] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [PubKeyHash]
txInfoSignatories
, Doc ann
"Redeemers:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Map ScriptPurpose Redeemer -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Map ScriptPurpose Redeemer -> Doc ann
pretty Map ScriptPurpose Redeemer
txInfoRedeemers
, Doc ann
"Datums:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Map DatumHash Datum -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Map DatumHash Datum -> Doc ann
pretty Map DatumHash Datum
txInfoData
]
data ScriptContext = ScriptContext
{ ScriptContext -> TxInfo
scriptContextTxInfo :: TxInfo
, ScriptContext -> ScriptPurpose
scriptContextPurpose :: ScriptPurpose
}
deriving stock ((forall x. ScriptContext -> Rep ScriptContext x)
-> (forall x. Rep ScriptContext x -> ScriptContext)
-> Generic ScriptContext
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
$cfrom :: forall x. ScriptContext -> Rep ScriptContext x
from :: forall x. ScriptContext -> Rep ScriptContext x
$cto :: forall x. Rep ScriptContext x -> ScriptContext
to :: forall x. Rep ScriptContext x -> ScriptContext
Generic, ScriptContext -> ScriptContext -> Bool
(ScriptContext -> ScriptContext -> Bool)
-> (ScriptContext -> ScriptContext -> Bool) -> Eq ScriptContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ScriptContext -> ScriptContext -> Bool
== :: ScriptContext -> ScriptContext -> Bool
$c/= :: ScriptContext -> ScriptContext -> Bool
/= :: ScriptContext -> ScriptContext -> Bool
Haskell.Eq, Int -> ScriptContext -> ShowS
[ScriptContext] -> ShowS
ScriptContext -> String
(Int -> ScriptContext -> ShowS)
-> (ScriptContext -> String)
-> ([ScriptContext] -> ShowS)
-> Show ScriptContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptContext -> ShowS
showsPrec :: Int -> ScriptContext -> ShowS
$cshow :: ScriptContext -> String
show :: ScriptContext -> String
$cshowList :: [ScriptContext] -> ShowS
showList :: [ScriptContext] -> ShowS
Haskell.Show)
instance Pretty ScriptContext where
pretty :: forall ann. ScriptContext -> Doc ann
pretty ScriptContext{TxInfo
scriptContextTxInfo :: ScriptContext -> TxInfo
scriptContextTxInfo :: TxInfo
scriptContextTxInfo, ScriptPurpose
scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose :: ScriptPurpose
scriptContextPurpose} =
[Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep
[ Doc ann
"Purpose:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> ScriptPurpose -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. ScriptPurpose -> Doc ann
pretty ScriptPurpose
scriptContextPurpose
, Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
nest 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
"TxInfo:", TxInfo -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. TxInfo -> Doc ann
pretty TxInfo
scriptContextTxInfo]
]
{-# INLINABLE findOwnInput #-}
findOwnInput :: ScriptContext -> Maybe TxInInfo
findOwnInput :: ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext{scriptContextTxInfo :: ScriptContext -> TxInfo
scriptContextTxInfo=TxInfo{[TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs}, scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose=Spending TxOutRef
txOutRef} =
(TxInInfo -> Bool) -> [TxInInfo] -> Maybe TxInInfo
forall a. (a -> Bool) -> [a] -> Maybe a
find (\TxInInfo{TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef} -> TxOutRef
txInInfoOutRef TxOutRef -> TxOutRef -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef
txOutRef) [TxInInfo]
txInfoInputs
findOwnInput ScriptContext
_ = Maybe TxInInfo
forall a. Maybe a
Nothing
{-# INLINABLE findDatum #-}
findDatum :: DatumHash -> TxInfo -> Maybe Datum
findDatum :: DatumHash -> TxInfo -> Maybe Datum
findDatum DatumHash
dsh TxInfo{Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData} = DatumHash -> Map DatumHash Datum -> Maybe Datum
forall k v. Eq k => k -> Map k v -> Maybe v
lookup DatumHash
dsh Map DatumHash Datum
txInfoData
{-# INLINABLE findDatumHash #-}
findDatumHash :: Datum -> TxInfo -> Maybe DatumHash
findDatumHash :: Datum -> TxInfo -> Maybe DatumHash
findDatumHash Datum
ds TxInfo{Map DatumHash Datum
txInfoData :: TxInfo -> Map DatumHash Datum
txInfoData :: Map DatumHash Datum
txInfoData} = (DatumHash, Datum) -> DatumHash
forall a b. (a, b) -> a
fst ((DatumHash, Datum) -> DatumHash)
-> Maybe (DatumHash, Datum) -> Maybe DatumHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((DatumHash, Datum) -> Bool)
-> [(DatumHash, Datum)] -> Maybe (DatumHash, Datum)
forall a. (a -> Bool) -> [a] -> Maybe a
find (DatumHash, Datum) -> Bool
forall {a}. (a, Datum) -> Bool
f (Map DatumHash Datum -> [(DatumHash, Datum)]
forall k v. Map k v -> [(k, v)]
toList Map DatumHash Datum
txInfoData)
where
f :: (a, Datum) -> Bool
f (a
_, Datum
ds') = Datum
ds' Datum -> Datum -> Bool
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 :: TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
txInfoInputs} =
(TxInInfo -> Bool) -> [TxInInfo] -> Maybe TxInInfo
forall a. (a -> Bool) -> [a] -> Maybe a
find (\TxInInfo{TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef} -> TxOutRef
txInInfoOutRef TxOutRef -> TxOutRef -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef
outRef) [TxInInfo]
txInfoInputs
{-# INLINABLE findContinuingOutputs #-}
findContinuingOutputs :: ScriptContext -> [Integer]
findContinuingOutputs :: ScriptContext -> [Integer]
findContinuingOutputs ScriptContext
ctx | Just TxInInfo{txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved=TxOut{Address
txOutAddress :: Address
txOutAddress :: TxOut -> Address
txOutAddress}} <- ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext
ctx = (TxOut -> Bool) -> [TxOut] -> [Integer]
forall a. (a -> Bool) -> [a] -> [Integer]
findIndices (Address -> TxOut -> Bool
f Address
txOutAddress) (TxInfo -> [TxOut]
txInfoOutputs (TxInfo -> [TxOut]) -> TxInfo -> [TxOut]
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 Address -> Address -> Bool
forall a. Eq a => a -> a -> Bool
== Address
otherAddress
findContinuingOutputs ScriptContext
_ = BuiltinString -> [Integer]
forall a. BuiltinString -> a
traceError BuiltinString
"Le"
{-# INLINABLE getContinuingOutputs #-}
getContinuingOutputs :: ScriptContext -> [TxOut]
getContinuingOutputs :: ScriptContext -> [TxOut]
getContinuingOutputs ScriptContext
ctx | Just TxInInfo{txInInfoResolved :: TxInInfo -> TxOut
txInInfoResolved=TxOut{Address
txOutAddress :: TxOut -> Address
txOutAddress :: Address
txOutAddress}} <- ScriptContext -> Maybe TxInInfo
findOwnInput ScriptContext
ctx = (TxOut -> Bool) -> [TxOut] -> [TxOut]
forall a. (a -> Bool) -> [a] -> [a]
filter (Address -> TxOut -> Bool
f Address
txOutAddress) (TxInfo -> [TxOut]
txInfoOutputs (TxInfo -> [TxOut]) -> TxInfo -> [TxOut]
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 Address -> Address -> Bool
forall a. Eq a => a -> a -> Bool
== Address
otherAddress
getContinuingOutputs ScriptContext
_ = BuiltinString -> [TxOut]
forall a. BuiltinString -> a
traceError BuiltinString
"Lf"
{-# INLINABLE txSignedBy #-}
txSignedBy :: TxInfo -> PubKeyHash -> Bool
txSignedBy :: TxInfo -> PubKeyHash -> Bool
txSignedBy TxInfo{[PubKeyHash]
txInfoSignatories :: TxInfo -> [PubKeyHash]
txInfoSignatories :: [PubKeyHash]
txInfoSignatories} PubKeyHash
k = case (PubKeyHash -> Bool) -> [PubKeyHash] -> Maybe PubKeyHash
forall a. (a -> Bool) -> [a] -> Maybe a
find (PubKeyHash -> PubKeyHash -> Bool
forall a. Eq a => a -> a -> Bool
(==) PubKeyHash
k) [PubKeyHash]
txInfoSignatories of
Just PubKeyHash
_ -> Bool
True
Maybe PubKeyHash
Nothing -> Bool
False
{-# INLINABLE pubKeyOutputsAt #-}
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 :: Value
txOutValue :: TxOut -> Value
txOutValue} | PubKeyHash
pk PubKeyHash -> PubKeyHash -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyHash
pk' = Value -> Maybe Value
forall a. a -> Maybe a
Just Value
txOutValue
flt TxOut
_ = Maybe Value
forall a. Maybe a
Nothing
in (TxOut -> Maybe Value) -> [TxOut] -> [Value]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe TxOut -> Maybe Value
flt (TxInfo -> [TxOut]
txInfoOutputs TxInfo
p)
{-# INLINABLE valuePaidTo #-}
valuePaidTo :: TxInfo -> PubKeyHash -> Value
valuePaidTo :: TxInfo -> PubKeyHash -> Value
valuePaidTo TxInfo
ptx PubKeyHash
pkh = [Value] -> Value
forall a. Monoid a => [a] -> a
mconcat (PubKeyHash -> TxInfo -> [Value]
pubKeyOutputsAt PubKeyHash
pkh TxInfo
ptx)
{-# INLINABLE valueSpent #-}
valueSpent :: TxInfo -> Value
valueSpent :: TxInfo -> Value
valueSpent = (TxInInfo -> Value) -> [TxInInfo] -> Value
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (TxOut -> Value
txOutValue (TxOut -> Value) -> (TxInInfo -> TxOut) -> TxInInfo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInInfo -> TxOut
txInInfoResolved) ([TxInInfo] -> Value) -> (TxInfo -> [TxInInfo]) -> TxInfo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInfo -> [TxInInfo]
txInfoInputs
{-# INLINABLE valueProduced #-}
valueProduced :: TxInfo -> Value
valueProduced :: TxInfo -> Value
valueProduced = (TxOut -> Value) -> [TxOut] -> Value
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxOut -> Value
txOutValue ([TxOut] -> Value) -> (TxInfo -> [TxOut]) -> TxInfo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxInfo -> [TxOut]
txInfoOutputs
{-# INLINABLE ownCurrencySymbol #-}
ownCurrencySymbol :: ScriptContext -> CurrencySymbol
ownCurrencySymbol :: ScriptContext -> CurrencySymbol
ownCurrencySymbol ScriptContext{scriptContextPurpose :: ScriptContext -> ScriptPurpose
scriptContextPurpose=Minting CurrencySymbol
cs} = CurrencySymbol
cs
ownCurrencySymbol ScriptContext
_ = BuiltinString -> CurrencySymbol
forall a. BuiltinString -> a
traceError BuiltinString
"Lh"
{-# INLINABLE spendsOutput #-}
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 TxId -> TxId -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef -> TxId
txOutRefId TxOutRef
outRef
Bool -> Bool -> Bool
&& Integer
i Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef -> Integer
txOutRefIdx TxOutRef
outRef
in (TxInInfo -> Bool) -> [TxInInfo] -> Bool
forall a. (a -> Bool) -> [a] -> Bool
any TxInInfo -> Bool
spendsOutRef (TxInfo -> [TxInInfo]
txInfoInputs TxInfo
p)
$(makeLift ''TxInInfo)
$(makeLift ''TxInfo)
$(makeLift ''ScriptContext)
$(makeIsDataSchemaIndexed ''TxInInfo [('TxInInfo, 0)])
$(makeIsDataSchemaIndexed ''TxInfo [('TxInfo, 0)])
$(makeIsDataSchemaIndexed ''ScriptContext [('ScriptContext, 0)])