tx_indexer/
filter.rs

1use oura::filters::selection::{Config, Predicate};
2use plutus_ledger_api::{
3    csl::{lib as csl, pla_to_csl::TryFromPLA},
4    v3::{script::MintingPolicyHash, value::CurrencySymbol},
5};
6
7/// Interesting transaction components to look for when filtering transactions
8/// relevant to the protocol.
9/// Set curr_symbols to empty vectors to handle any transaction event indiscriminately.
10pub struct Filter {
11    pub curr_symbols: Vec<CurrencySymbol>,
12}
13
14// We only obtain Transaction events that contain the policy in the output
15// NOTE: Must enable 'include_transaction_details' in oura Mapper config.
16impl Filter {
17    pub fn to_selection_config(self) -> Config {
18        Config {
19            check: Predicate::AnyOf(vec![
20                Predicate::VariantIn(vec!["RollBack".to_string(), "Block".to_string()]),
21                Predicate::AllOf(vec![
22                    Predicate::VariantIn(vec!["Transaction".to_string()]),
23                    if self.curr_symbols.is_empty() {
24                        ALWAYS_TRUE
25                    } else {
26                        Predicate::AnyOf(
27                            self.curr_symbols
28                                .into_iter()
29                                .map(serialize_cur_sym)
30                                .map(Predicate::PolicyEquals)
31                                .collect(),
32                        )
33                    },
34                ]),
35            ]),
36        }
37    }
38}
39
40// Filter predicate that always succeeds.
41const ALWAYS_TRUE: Predicate = Predicate::AllOf(vec![]);
42
43fn serialize_cur_sym(cur_sym: CurrencySymbol) -> String {
44    match cur_sym {
45        CurrencySymbol::Ada => String::new(),
46        CurrencySymbol::NativeToken(MintingPolicyHash(script_hash)) => {
47            csl::ScriptHash::try_from_pla(&script_hash)
48                .unwrap()
49                .to_hex()
50        }
51    }
52}