{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-missing-import-lists #-}
module Plutus.Script.Utils.V1.Contexts
    ( module Contexts
    , outputsAt
    , valuePaidTo
    ) where

import Plutus.V1.Ledger.Api (Address, Value)
import Plutus.V1.Ledger.Contexts as Contexts hiding (valuePaidTo)
import PlutusTx.Prelude (Maybe (Just, Nothing), mapMaybe, mconcat, (==))

{-# INLINABLE outputsAt #-}
-- | Get the values paid to a public key address by a pending transaction.
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 #-}
-- | Get the total value paid to a public key address by a pending transaction.
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)