{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-strictness #-}
module Hydra.ScriptContext where
import PlutusLedgerApi.V2.Contexts hiding (
ScriptContext,
TxInfo (..),
scriptContextPurpose,
scriptContextTxInfo,
)
import PlutusTx.Prelude
import PlutusLedgerApi.V2 (
Address (..),
Credential (..),
CurrencySymbol,
Datum,
DatumHash,
Map,
OutputDatum,
PubKeyHash,
ScriptHash,
Value,
)
import PlutusTx (makeIsDataIndexed)
import PlutusTx.AssocMap (lookup)
data TxInfo = TxInfo
{ TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
, TxInfo -> BuiltinData
txInfoReferenceInputs :: BuiltinData
, TxInfo -> [TxOut]
txInfoOutputs :: [TxOut]
, TxInfo -> Value
txInfoFee :: Value
, TxInfo -> Value
txInfoMint :: Value
, TxInfo -> BuiltinData
txInfoDCert :: BuiltinData
, TxInfo -> BuiltinData
txInfoWdrl :: BuiltinData
,
TxInfo -> BuiltinData
txInfoValidRange :: BuiltinData
, TxInfo -> [PubKeyHash]
txInfoSignatories :: [PubKeyHash]
, TxInfo -> BuiltinData
txInfoRedeemers :: BuiltinData
, TxInfo -> Map DatumHash Datum
txInfoData :: Map DatumHash Datum
, TxInfo -> BuiltinData
txInfoId :: BuiltinData
}
makeIsDataIndexed ''TxInfo [('TxInfo, 0)]
data ScriptContext = ScriptContext
{ ScriptContext -> TxInfo
scriptContextTxInfo :: TxInfo
, ScriptContext -> ScriptPurpose
scriptContextPurpose :: ScriptPurpose
}
makeIsDataIndexed ''ScriptContext [('ScriptContext, 0)]
scriptOutputsAt :: ScriptHash -> TxInfo -> [(OutputDatum, Value)]
scriptOutputsAt :: ScriptHash -> TxInfo -> [(OutputDatum, Value)]
scriptOutputsAt ScriptHash
h TxInfo
p =
let flt :: TxOut -> Maybe (OutputDatum, Value)
flt TxOut{txOutDatum :: TxOut -> OutputDatum
txOutDatum = OutputDatum
d, txOutAddress :: TxOut -> Address
txOutAddress = Address (ScriptCredential ScriptHash
s) Maybe StakingCredential
_, Value
txOutValue :: Value
txOutValue :: TxOut -> Value
txOutValue} | ScriptHash
s ScriptHash -> ScriptHash -> Bool
forall a. Eq a => a -> a -> Bool
== ScriptHash
h = (OutputDatum, Value) -> Maybe (OutputDatum, Value)
forall a. a -> Maybe a
Just (OutputDatum
d, Value
txOutValue)
flt TxOut
_ = Maybe (OutputDatum, Value)
forall a. Maybe a
Nothing
in (TxOut -> Maybe (OutputDatum, Value))
-> [TxOut] -> [(OutputDatum, Value)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe TxOut -> Maybe (OutputDatum, Value)
flt (TxInfo -> [TxOut]
txInfoOutputs TxInfo
p)
{-# INLINEABLE scriptOutputsAt #-}
valueLockedBy :: TxInfo -> ScriptHash -> Value
valueLockedBy :: TxInfo -> ScriptHash -> Value
valueLockedBy TxInfo
ptx ScriptHash
h =
let outputs :: [Value]
outputs = ((OutputDatum, Value) -> Value)
-> [(OutputDatum, Value)] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map (OutputDatum, Value) -> Value
forall a b. (a, b) -> b
snd (ScriptHash -> TxInfo -> [(OutputDatum, Value)]
scriptOutputsAt ScriptHash
h TxInfo
ptx)
in [Value] -> Value
forall a. Monoid a => [a] -> a
mconcat [Value]
outputs
{-# INLINEABLE valueLockedBy #-}
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 :: TxOutRef
txInInfoOutRef :: TxInInfo -> 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
{-# INLINEABLE findOwnInput #-}
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
{-# INLINEABLE findDatum #-}
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
{-# INLINEABLE findTxInByTxOutRef #-}
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"
{-# INLINEABLE ownCurrencySymbol #-}