{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
{-# OPTIONS_GHC -Wno-missing-import-lists #-}
{-# OPTIONS_GHC -fno-strictness #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module Plutus.Script.Utils.V2.Contexts
( module Contexts
, findTxInByTxOutRef
, findTxRefInByTxOutRef
, outputsAt
, valuePaidTo
) where
import Plutus.V1.Ledger.Api (Address, Value)
import Plutus.V2.Ledger.Api qualified as PV2
import Plutus.V2.Ledger.Contexts as Contexts hiding (findTxInByTxOutRef, valuePaidTo)
import PlutusTx.Prelude (Maybe (Just, Nothing), find, mapMaybe, mconcat, (==))
{-# INLINABLE findTxInByTxOutRef #-}
findTxInByTxOutRef :: TxOutRef -> PV2.TxInfo -> Maybe PV2.TxInInfo
findTxInByTxOutRef :: TxOutRef -> TxInfo -> Maybe TxInInfo
findTxInByTxOutRef TxOutRef
outRef PV2.TxInfo{[TxInInfo]
txInfoInputs :: TxInfo -> [TxInInfo]
txInfoInputs :: [TxInInfo]
PV2.txInfoInputs} =
(TxInInfo -> Bool) -> [TxInInfo] -> Maybe TxInInfo
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\PV2.TxInInfo{TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
txInInfoOutRef :: TxOutRef
PV2.txInInfoOutRef} -> TxOutRef
txInInfoOutRef TxOutRef -> TxOutRef -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef
outRef) [TxInInfo]
txInfoInputs
{-# INLINABLE findTxRefInByTxOutRef #-}
findTxRefInByTxOutRef :: TxOutRef -> PV2.TxInfo -> Maybe PV2.TxInInfo
findTxRefInByTxOutRef :: TxOutRef -> TxInfo -> Maybe TxInInfo
findTxRefInByTxOutRef TxOutRef
outRef PV2.TxInfo{[TxInInfo]
txInfoReferenceInputs :: TxInfo -> [TxInInfo]
txInfoReferenceInputs :: [TxInInfo]
PV2.txInfoReferenceInputs} =
(TxInInfo -> Bool) -> [TxInInfo] -> Maybe TxInInfo
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\PV2.TxInInfo{TxOutRef
txInInfoOutRef :: TxOutRef
txInInfoOutRef :: TxInInfo -> TxOutRef
PV2.txInInfoOutRef} -> TxOutRef
txInInfoOutRef TxOutRef -> TxOutRef -> Bool
forall a. Eq a => a -> a -> Bool
== TxOutRef
outRef) [TxInInfo]
txInfoReferenceInputs
{-# INLINABLE outputsAt #-}
outputsAt :: Address -> TxInfo -> [Value]
outputsAt :: Address -> TxInfo -> [Value]
outputsAt Address
addr TxInfo
p =
let flt :: TxOut -> Maybe Value
flt TxOut{Address
txOutAddress :: TxOut -> Address
txOutAddress :: Address
txOutAddress, Value
txOutValue :: TxOut -> Value
txOutValue :: Value
txOutValue} | Address
txOutAddress Address -> Address -> Bool
forall a. Eq a => a -> a -> Bool
== Address
addr = 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 -> Address -> Value
valuePaidTo :: TxInfo -> Address -> Value
valuePaidTo TxInfo
ptx Address
addr = [Value] -> Value
forall a. Monoid a => [a] -> a
mconcat (Address -> TxInfo -> [Value]
outputsAt Address
addr TxInfo
ptx)