From ecd15e2eb9bf62f630b4573d6bc9c332ced8e355 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Tue, 13 Sep 2022 14:05:08 -0300 Subject: [PATCH] fix: Apply missing selection filters at Tx level --- src/filters/selection.rs | 54 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/filters/selection.rs b/src/filters/selection.rs index 3767c3a1..90c2ba1c 100644 --- a/src/filters/selection.rs +++ b/src/filters/selection.rs @@ -41,8 +41,29 @@ fn variant_in_matches(event: &Event, variants: &[String]) -> bool { } #[inline] -fn policy_matches(event: &Event, policy: &str) -> bool { +fn output_policy_matches(event: &Event, policy: &str) -> bool { match &event.data { + EventData::Transaction(TransactionRecord { + outputs: Some(outputs), + .. + }) => outputs + .iter() + .flat_map(|x| &x.assets) + .flatten() + .any(|x| relaxed_str_matches(&x.policy, policy)), + EventData::OutputAsset(OutputAssetRecord { policy: x, .. }) => { + relaxed_str_matches(x, policy) + } + _ => false, + } +} + +#[inline] +fn mint_policy_matches(event: &Event, policy: &str) -> bool { + match &event.data { + EventData::Transaction(TransactionRecord { + mint: Some(mint), .. + }) => mint.iter().any(|x| relaxed_str_matches(&x.policy, policy)), EventData::OutputAsset(OutputAssetRecord { policy: x, .. }) => { relaxed_str_matches(x, policy) } @@ -54,15 +75,36 @@ fn policy_matches(event: &Event, policy: &str) -> bool { #[inline] fn address_matches(event: &Event, address: &str) -> bool { match &event.data { + EventData::Transaction(TransactionRecord { + outputs: Some(o), .. + }) => o.iter().any(|x| relaxed_str_matches(&x.address, address)), EventData::TxOutput(TxOutputRecord { address: x, .. }) => relaxed_str_matches(x, address), _ => false, } } #[inline] -fn asset_matches(event: &Event, asset: &str) -> bool { +fn output_asset_matches(event: &Event, asset: &str) -> bool { match &event.data { + EventData::Transaction(TransactionRecord { + outputs: Some(outputs), + .. + }) => outputs + .iter() + .flat_map(|x| &x.assets) + .flatten() + .any(|x| relaxed_str_matches(&x.asset, asset)), EventData::OutputAsset(OutputAssetRecord { asset: x, .. }) => relaxed_str_matches(x, asset), + _ => false, + } +} + +#[inline] +fn mint_asset_matches(event: &Event, asset: &str) -> bool { + match &event.data { + EventData::Transaction(TransactionRecord { + mint: Some(mint), .. + }) => mint.iter().any(|x| relaxed_str_matches(&x.asset, asset)), EventData::Mint(MintRecord { asset: x, .. }) => relaxed_str_matches(x, asset), _ => false, } @@ -98,9 +140,13 @@ impl Predicate { match self { Predicate::VariantIn(x) => variant_in_matches(event, x), Predicate::VariantNotIn(x) => !variant_in_matches(event, x), - Predicate::PolicyEquals(x) => policy_matches(event, x), + Predicate::PolicyEquals(x) => { + output_policy_matches(event, x) || mint_policy_matches(event, x) + } Predicate::AddressEquals(x) => address_matches(event, x), - Predicate::AssetEquals(x) => asset_matches(event, x), + Predicate::AssetEquals(x) => { + output_asset_matches(event, x) || mint_asset_matches(event, x) + } Predicate::MetadataLabelEquals(x) => metadata_label_matches(event, x), Predicate::MetadataAnySubLabelEquals(x) => metadata_any_sub_label_matches(event, x), Predicate::Not(x) => !x.event_matches(event),