{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Rpc.Server.Internal.UtxoRpc.Predicate
( matchesUtxoPredicate
, exactAddressPredicate
, extractAddressesFromPredicate
, matchesAddressPattern
, matchesAssetPattern
, matchesTxOutputPattern
, matchesAnyUtxoPattern
, serialisePaymentCredential
, serialiseStakeCredential
)
where
import Cardano.Api.Address
import Cardano.Api.Era
import Cardano.Api.Serialise.Raw
import Cardano.Api.Tx
import Cardano.Api.Value
import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
import RIO hiding (toList)
import Data.ByteString qualified as BS
import Data.ProtoLens (defMessage)
import Data.Set qualified as Set
import GHC.IsList
import Network.GRPC.Spec (Proto)
matchesUtxoPredicate
:: IsCardanoEra era
=> Proto UtxoRpc.UtxoPredicate
-> TxOut CtxUTxO era
-> Bool
matchesUtxoPredicate :: forall era.
IsCardanoEra era =>
Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
matchesUtxoPredicate Proto UtxoPredicate
p TxOut CtxUTxO era
txOut =
(Proto AnyUtxoPattern -> Bool)
-> Maybe (Proto AnyUtxoPattern) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Proto AnyUtxoPattern -> TxOut CtxUTxO era -> Bool
forall era.
IsCardanoEra era =>
Proto AnyUtxoPattern -> TxOut CtxUTxO era -> Bool
`matchesAnyUtxoPattern` TxOut CtxUTxO era
txOut) (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
(Maybe (Proto AnyUtxoPattern))
(Proto UtxoPredicate)
(Maybe (Proto AnyUtxoPattern))
-> Maybe (Proto AnyUtxoPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto AnyUtxoPattern))
(Proto UtxoPredicate)
(Maybe (Proto AnyUtxoPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'match" a) =>
LensLike' f s a
UtxoRpc.maybe'match)
Bool -> Bool -> Bool
&& Bool -> Bool
not ((Proto UtxoPredicate -> Bool) -> [Proto UtxoPredicate] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
forall era.
IsCardanoEra era =>
Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
`matchesUtxoPredicate` TxOut CtxUTxO era
txOut) (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "not" a) =>
LensLike' f s a
UtxoRpc.not))
Bool -> Bool -> Bool
&& (Proto UtxoPredicate -> Bool) -> [Proto UtxoPredicate] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
forall era.
IsCardanoEra era =>
Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
`matchesUtxoPredicate` TxOut CtxUTxO era
txOut) (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "allOf" a) =>
LensLike' f s a
UtxoRpc.allOf)
Bool -> Bool -> Bool
&& ([Proto UtxoPredicate] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "anyOf" a) =>
LensLike' f s a
UtxoRpc.anyOf) Bool -> Bool -> Bool
|| (Proto UtxoPredicate -> Bool) -> [Proto UtxoPredicate] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
forall era.
IsCardanoEra era =>
Proto UtxoPredicate -> TxOut CtxUTxO era -> Bool
`matchesUtxoPredicate` TxOut CtxUTxO era
txOut) (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "anyOf" a) =>
LensLike' f s a
UtxoRpc.anyOf))
matchesAnyUtxoPattern
:: IsCardanoEra era
=> Proto UtxoRpc.AnyUtxoPattern
-> TxOut CtxUTxO era
-> Bool
matchesAnyUtxoPattern :: forall era.
IsCardanoEra era =>
Proto AnyUtxoPattern -> TxOut CtxUTxO era -> Bool
matchesAnyUtxoPattern Proto AnyUtxoPattern
pat TxOut CtxUTxO era
txOut =
(Proto TxOutputPattern -> Bool)
-> Maybe (Proto TxOutputPattern) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Proto TxOutputPattern -> TxOut CtxUTxO era -> Bool
forall era.
IsCardanoEra era =>
Proto TxOutputPattern -> TxOut CtxUTxO era -> Bool
`matchesTxOutputPattern` TxOut CtxUTxO era
txOut) (Proto AnyUtxoPattern
pat Proto AnyUtxoPattern
-> Getting
(Maybe (Proto TxOutputPattern))
(Proto AnyUtxoPattern)
(Maybe (Proto TxOutputPattern))
-> Maybe (Proto TxOutputPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto TxOutputPattern))
(Proto AnyUtxoPattern)
(Maybe (Proto TxOutputPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'cardano" a) =>
LensLike' f s a
UtxoRpc.maybe'cardano)
matchesTxOutputPattern
:: IsCardanoEra era
=> Proto UtxoRpc.TxOutputPattern
-> TxOut CtxUTxO era
-> Bool
matchesTxOutputPattern :: forall era.
IsCardanoEra era =>
Proto TxOutputPattern -> TxOut CtxUTxO era -> Bool
matchesTxOutputPattern Proto TxOutputPattern
pat (TxOut AddressInEra era
addrInEra TxOutValue era
txOutValue TxOutDatum CtxUTxO era
_datum ReferenceScript era
_script) =
(Proto AddressPattern -> Bool)
-> Maybe (Proto AddressPattern) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Proto AddressPattern -> AddressInEra era -> Bool
forall era.
IsCardanoEra era =>
Proto AddressPattern -> AddressInEra era -> Bool
`matchesAddressPattern` AddressInEra era
addrInEra) (Proto TxOutputPattern
pat Proto TxOutputPattern
-> Getting
(Maybe (Proto AddressPattern))
(Proto TxOutputPattern)
(Maybe (Proto AddressPattern))
-> Maybe (Proto AddressPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto AddressPattern))
(Proto TxOutputPattern)
(Maybe (Proto AddressPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'address" a) =>
LensLike' f s a
UtxoRpc.maybe'address)
Bool -> Bool -> Bool
&& (Proto AssetPattern -> Bool) -> Maybe (Proto AssetPattern) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Proto AssetPattern -> Value -> Bool
`matchesAssetPattern` TxOutValue era -> Value
forall era. TxOutValue era -> Value
txOutValueToValue TxOutValue era
txOutValue) (Proto TxOutputPattern
pat Proto TxOutputPattern
-> Getting
(Maybe (Proto AssetPattern))
(Proto TxOutputPattern)
(Maybe (Proto AssetPattern))
-> Maybe (Proto AssetPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto AssetPattern))
(Proto TxOutputPattern)
(Maybe (Proto AssetPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'asset" a) =>
LensLike' f s a
UtxoRpc.maybe'asset)
matchesAddressPattern
:: IsCardanoEra era
=> Proto UtxoRpc.AddressPattern
-> AddressInEra era
-> Bool
matchesAddressPattern :: forall era.
IsCardanoEra era =>
Proto AddressPattern -> AddressInEra era -> Bool
matchesAddressPattern Proto AddressPattern
pat AddressInEra era
addr =
Bool
exactMatch Bool -> Bool -> Bool
&& Bool
paymentMatch Bool -> Bool -> Bool
&& Bool
delegationMatch
where
matchesRawField :: ByteString -> ByteString -> Bool
matchesRawField ByteString
field ByteString
actual = ByteString -> Bool
BS.null ByteString
field Bool -> Bool -> Bool
|| ByteString
field ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
actual
exactMatch :: Bool
exactMatch = ByteString -> ByteString -> Bool
matchesRawField (Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "exactAddress" a) =>
LensLike' f s a
UtxoRpc.exactAddress) (ByteString -> Bool) -> ByteString -> Bool
forall a b. (a -> b) -> a -> b
$ AddressInEra era -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes AddressInEra era
addr
paymentMatch :: Bool
paymentMatch = case AddressInEra era
addr of
AddressInEra ShelleyAddressInEra{} (ShelleyAddress Network
_ Credential Payment
payCred StakeReference
_) ->
ByteString -> ByteString -> Bool
matchesRawField (Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "paymentPart" a) =>
LensLike' f s a
UtxoRpc.paymentPart) (ByteString -> Bool)
-> (PaymentCredential -> ByteString) -> PaymentCredential -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PaymentCredential -> ByteString
serialisePaymentCredential (PaymentCredential -> Bool) -> PaymentCredential -> Bool
forall a b. (a -> b) -> a -> b
$
Credential Payment -> PaymentCredential
fromShelleyPaymentCredential Credential Payment
payCred
AddressInEra era
_ -> ByteString -> Bool
BS.null (ByteString -> Bool) -> ByteString -> Bool
forall a b. (a -> b) -> a -> b
$ Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "paymentPart" a) =>
LensLike' f s a
UtxoRpc.paymentPart
delegationMatch :: Bool
delegationMatch = case AddressInEra era
addr of
AddressInEra ShelleyAddressInEra{} (ShelleyAddress Network
_ Credential Payment
_ StakeReference
stakeRef) ->
case StakeReference -> StakeAddressReference
fromShelleyStakeReference StakeReference
stakeRef of
StakeAddressByValue StakeCredential
cred ->
ByteString -> ByteString -> Bool
matchesRawField (Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "delegationPart" a) =>
LensLike' f s a
UtxoRpc.delegationPart) (ByteString -> Bool) -> ByteString -> Bool
forall a b. (a -> b) -> a -> b
$ StakeCredential -> ByteString
serialiseStakeCredential StakeCredential
cred
StakeAddressReference
_ -> ByteString -> Bool
BS.null (ByteString -> Bool) -> ByteString -> Bool
forall a b. (a -> b) -> a -> b
$ Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "delegationPart" a) =>
LensLike' f s a
UtxoRpc.delegationPart
AddressInEra era
_ -> ByteString -> Bool
BS.null (ByteString -> Bool) -> ByteString -> Bool
forall a b. (a -> b) -> a -> b
$ Proto AddressPattern
pat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "delegationPart" a) =>
LensLike' f s a
UtxoRpc.delegationPart
exactAddressPredicate
:: IsCardanoEra era
=> AddressInEra era
-> Proto UtxoRpc.UtxoPredicate
exactAddressPredicate :: forall era.
IsCardanoEra era =>
AddressInEra era -> Proto UtxoPredicate
exactAddressPredicate AddressInEra era
address =
Proto UtxoPredicate
forall msg. Message msg => msg
defMessage
Proto UtxoPredicate
-> (Proto UtxoPredicate -> Proto UtxoPredicate)
-> Proto UtxoPredicate
forall a b. a -> (a -> b) -> b
& LensLike' Identity (Proto UtxoPredicate) (Proto AnyUtxoPattern)
forall (f :: * -> *) s a.
(Functor f, HasField s "match" a) =>
LensLike' f s a
UtxoRpc.match
LensLike' Identity (Proto UtxoPredicate) (Proto AnyUtxoPattern)
-> Proto AnyUtxoPattern
-> Proto UtxoPredicate
-> Proto UtxoPredicate
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ( Proto AnyUtxoPattern
forall msg. Message msg => msg
defMessage
Proto AnyUtxoPattern
-> (Proto AnyUtxoPattern -> Proto AnyUtxoPattern)
-> Proto AnyUtxoPattern
forall a b. a -> (a -> b) -> b
& LensLike' Identity (Proto AnyUtxoPattern) (Proto TxOutputPattern)
forall (f :: * -> *) s a.
(Functor f, HasField s "cardano" a) =>
LensLike' f s a
UtxoRpc.cardano
LensLike' Identity (Proto AnyUtxoPattern) (Proto TxOutputPattern)
-> Proto TxOutputPattern
-> Proto AnyUtxoPattern
-> Proto AnyUtxoPattern
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Proto TxOutputPattern
forall msg. Message msg => msg
defMessage Proto TxOutputPattern
-> (Proto TxOutputPattern -> Proto TxOutputPattern)
-> Proto TxOutputPattern
forall a b. a -> (a -> b) -> b
& LensLike' Identity (Proto TxOutputPattern) (Proto AddressPattern)
forall (f :: * -> *) s a.
(Functor f, HasField s "address" a) =>
LensLike' f s a
UtxoRpc.address LensLike' Identity (Proto TxOutputPattern) (Proto AddressPattern)
-> Proto AddressPattern
-> Proto TxOutputPattern
-> Proto TxOutputPattern
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Proto AddressPattern
forall msg. Message msg => msg
defMessage Proto AddressPattern
-> (Proto AddressPattern -> Proto AddressPattern)
-> Proto AddressPattern
forall a b. a -> (a -> b) -> b
& LensLike' Identity (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "exactAddress" a) =>
LensLike' f s a
UtxoRpc.exactAddress LensLike' Identity (Proto AddressPattern) ByteString
-> ByteString -> Proto AddressPattern -> Proto AddressPattern
forall s t a b. ASetter s t a b -> b -> s -> t
.~ AddressInEra era -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes AddressInEra era
address))
)
serialisePaymentCredential :: PaymentCredential -> ByteString
serialisePaymentCredential :: PaymentCredential -> ByteString
serialisePaymentCredential (PaymentCredentialByKey Hash PaymentKey
h) = Hash PaymentKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Hash PaymentKey
h
serialisePaymentCredential (PaymentCredentialByScript ScriptHash
h) = ScriptHash -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes ScriptHash
h
serialiseStakeCredential :: StakeCredential -> ByteString
serialiseStakeCredential :: StakeCredential -> ByteString
serialiseStakeCredential (StakeCredentialByKey Hash StakeKey
h) = Hash StakeKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Hash StakeKey
h
serialiseStakeCredential (StakeCredentialByScript ScriptHash
h) = ScriptHash -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes ScriptHash
h
matchesAssetPattern
:: Proto UtxoRpc.AssetPattern
-> Value
-> Bool
matchesAssetPattern :: Proto AssetPattern -> Value -> Bool
matchesAssetPattern Proto AssetPattern
pat Value
value =
((AssetId, Quantity) -> Bool) -> [(AssetId, Quantity)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (AssetId, Quantity) -> Bool
matchesEntry (Value -> [Item Value]
forall l. IsList l => l -> [Item l]
toList Value
value)
where
patternPolicy :: ByteString
patternPolicy = Proto AssetPattern
pat Proto AssetPattern
-> Getting ByteString (Proto AssetPattern) ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AssetPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "policyId" a) =>
LensLike' f s a
UtxoRpc.policyId
patternTokenName :: ByteString
patternTokenName = Proto AssetPattern
pat Proto AssetPattern
-> Getting ByteString (Proto AssetPattern) ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AssetPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "assetName" a) =>
LensLike' f s a
UtxoRpc.assetName
matchesEntry :: (AssetId, Quantity) -> Bool
matchesEntry (AssetId PolicyId
policy AssetName
tokenName, Quantity Integer
qty) =
(ByteString -> Bool
BS.null ByteString
patternPolicy Bool -> Bool -> Bool
|| PolicyId -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes PolicyId
policy ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
patternPolicy)
Bool -> Bool -> Bool
&& (ByteString -> Bool
BS.null ByteString
patternTokenName Bool -> Bool -> Bool
|| AssetName -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes AssetName
tokenName ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
patternTokenName)
Bool -> Bool -> Bool
&& Integer
qty Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0
matchesEntry (AssetId
AdaAssetId, Quantity
_) = Bool
False
extractAddressesFromPredicate :: Proto UtxoRpc.UtxoPredicate -> Maybe (Set AddressAny)
Proto UtxoPredicate
p =
case (Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
(Maybe (Proto AnyUtxoPattern))
(Proto UtxoPredicate)
(Maybe (Proto AnyUtxoPattern))
-> Maybe (Proto AnyUtxoPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto AnyUtxoPattern))
(Proto UtxoPredicate)
(Maybe (Proto AnyUtxoPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'match" a) =>
LensLike' f s a
UtxoRpc.maybe'match, Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "not" a) =>
LensLike' f s a
UtxoRpc.not, Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "allOf" a) =>
LensLike' f s a
UtxoRpc.allOf, Proto UtxoPredicate
p Proto UtxoPredicate
-> Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
-> [Proto UtxoPredicate]
forall s a. s -> Getting a s a -> a
^. Getting
[Proto UtxoPredicate] (Proto UtxoPredicate) [Proto UtxoPredicate]
forall (f :: * -> *) s a.
(Functor f, HasField s "anyOf" a) =>
LensLike' f s a
UtxoRpc.anyOf) of
(Just Proto AnyUtxoPattern
pat, [], [], []) -> Proto AnyUtxoPattern -> Maybe (Set AddressAny)
extractAddressFromPattern Proto AnyUtxoPattern
pat
(Maybe (Proto AnyUtxoPattern)
Nothing, [], [], anyPreds :: [Proto UtxoPredicate]
anyPreds@(Proto UtxoPredicate
_ : [Proto UtxoPredicate]
_)) ->
[Set AddressAny] -> Set AddressAny
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions ([Set AddressAny] -> Set AddressAny)
-> Maybe [Set AddressAny] -> Maybe (Set AddressAny)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Proto UtxoPredicate -> Maybe (Set AddressAny))
-> [Proto UtxoPredicate] -> Maybe [Set AddressAny]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Proto UtxoPredicate -> Maybe (Set AddressAny)
extractAddressesFromPredicate [Proto UtxoPredicate]
anyPreds
(Maybe (Proto AnyUtxoPattern), [Proto UtxoPredicate],
[Proto UtxoPredicate], [Proto UtxoPredicate])
_ -> Maybe (Set AddressAny)
forall a. Maybe a
Nothing
where
extractAddressFromPattern :: Proto UtxoRpc.AnyUtxoPattern -> Maybe (Set AddressAny)
extractAddressFromPattern :: Proto AnyUtxoPattern -> Maybe (Set AddressAny)
extractAddressFromPattern Proto AnyUtxoPattern
pat = do
txoPat <- Proto AnyUtxoPattern
pat Proto AnyUtxoPattern
-> Getting
(Maybe (Proto TxOutputPattern))
(Proto AnyUtxoPattern)
(Maybe (Proto TxOutputPattern))
-> Maybe (Proto TxOutputPattern)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto TxOutputPattern))
(Proto AnyUtxoPattern)
(Maybe (Proto TxOutputPattern))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'cardano" a) =>
LensLike' f s a
UtxoRpc.maybe'cardano
addrPat <- txoPat ^. UtxoRpc.maybe'address
let exact = Proto AddressPattern
addrPat Proto AddressPattern
-> Getting ByteString (Proto AddressPattern) ByteString
-> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto AddressPattern) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "exactAddress" a) =>
LensLike' f s a
UtxoRpc.exactAddress
guard $ not (BS.null exact)
addrAny <- either (const Nothing) Just $ deserialiseFromRawBytes AsAddressAny exact
pure $ Set.singleton addrAny