Skip to content

Commit

Permalink
Merge pull request #12 from blocktorch-xyz/block-1494-support-multipl…
Browse files Browse the repository at this point in the history
…e-addresses-in-params

Allow multiple batch inbox addresses
  • Loading branch information
catalyst17 committed Feb 14, 2024
2 parents e8077b3 + 12f3933 commit 15cfd85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
39 changes: 25 additions & 14 deletions src/map_filter_op_batch_inbox_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@ use substreams::Hex;
use substreams_ethereum::pb::eth::v2::Block;

#[derive(Debug, Deserialize)]
struct Params {
op_batch_inbox: String
struct FilterParams {
op_batch_inbox_addresses: Vec<String>
}

#[substreams::handlers::map]
fn map_filter_op_batch_inbox_transactions(params: String, blk: Block) -> Result<ListOfOpBatchInboxCallData, Vec<substreams::errors::Error>> {
// Parse the query string parameters safely
let query: Params = match serde_qs::from_str(&params) {
Ok(query) => query,
Err(_) => return Err(vec![substreams::errors::Error::msg("Failed to parse query parameters")]),
};

// Decode the op_batch_inbox address
let op_batch_inbox_address = match Hex::decode(&query.op_batch_inbox) {
Ok(addr) => addr,
Err(_) => return Err(vec![substreams::errors::Error::msg("Failed to decode op_batch_inbox address")]),
};
let decoded_addresses = filters_from_params(params)?;

let data: Vec<OpBatchInboxCallData> = blk
.transaction_traces.iter()
.filter(|tx| tx.to == op_batch_inbox_address)
.filter(|tx| decoded_addresses.iter().any(|addr| &tx.to == addr))
.map(|tx| OpBatchInboxCallData {
tx_hash: Hex::encode(&tx.hash),
batcher_address: Hex::encode(&tx.from),
Expand All @@ -36,3 +26,24 @@ fn map_filter_op_batch_inbox_transactions(params: String, blk: Block) -> Result<

Ok(ListOfOpBatchInboxCallData { data })
}

fn filters_from_params(params: String) -> Result<Vec<Vec<u8>>, Vec<substreams::errors::Error>> {
// Parse the query string parameters safely
let query: FilterParams = match serde_qs::from_str(&params) {
Ok(query) => query,
Err(_) => return Err(vec![substreams::errors::Error::msg("Failed to parse input parameters")]),
};

// Decode the op_batch_inbox address
let mut decoded_addresses = Vec::new();

for address in &query.op_batch_inbox_addresses {
let decoded_address = match Hex::decode(address) {
Ok(addr) => addr,
Err(_) => return Err(vec![substreams::errors::Error::msg("Failed to decode op_batch_inbox_addresses")]),
};
decoded_addresses.push(decoded_address);
}

Ok(decoded_addresses)
}
2 changes: 1 addition & 1 deletion substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ network: mainnet
networks:
mainnet:
params:
map_filter_op_batch_inbox_transactions: "op_batch_inbox=ff00000000000000000000000000000000000010"
map_filter_op_batch_inbox_transactions: "op_batch_inbox_addresses[]=ff00000000000000000000000000000000000010&"

0 comments on commit 15cfd85

Please sign in to comment.