From 0618138987ce89a572a3cbe675714b29a59d96d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enol=20=C3=81lvarez?= Date: Thu, 27 Jul 2023 19:44:06 +0200 Subject: [PATCH] Fixes --- ethereum-explorer/.rustfmt.toml | 1 + ethereum-explorer/src/map_contract_events.rs | 25 ++++----- .../src/map_filter_transactions.rs | 51 +++++++++++++------ ethereum-explorer/substreams.yaml | 4 +- 4 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 ethereum-explorer/.rustfmt.toml diff --git a/ethereum-explorer/.rustfmt.toml b/ethereum-explorer/.rustfmt.toml new file mode 100644 index 0000000..866c756 --- /dev/null +++ b/ethereum-explorer/.rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 \ No newline at end of file diff --git a/ethereum-explorer/src/map_contract_events.rs b/ethereum-explorer/src/map_contract_events.rs index 16c07ed..00da71a 100644 --- a/ethereum-explorer/src/map_contract_events.rs +++ b/ethereum-explorer/src/map_contract_events.rs @@ -1,32 +1,33 @@ use crate::pb::eth::event::v1::Event; use crate::pb::eth::event::v1::Events; use crate::util; +use anyhow::anyhow; use anyhow::Ok; use substreams::errors::Error; use substreams::Hex; use substreams_ethereum::pb::eth::v2::Block; -use anyhow::anyhow; #[substreams::handlers::map] fn map_contract_events(contract_address: String, blk: Block) -> Result { verify_parameter(&contract_address)?; - - let events: Vec = blk.logs() - .filter(| log| log.address().to_vec() == Hex::decode(&contract_address).expect("already validated")) - .map(|log| Event { - address: Hex::encode(log.address()), - topics: log.topics().into_iter().map(| topic| Hex::encode(topic)).collect(), - tx_hash: Hex::encode(&log.receipt.transaction.hash) - }) - .collect(); + + let events: Vec = blk + .logs() + .filter(|log| log.address().to_vec() == Hex::decode(&contract_address).expect("already validated")) + .map(|log| Event { + address: Hex::encode(log.address()), + topics: log.topics().into_iter().map(Hex::encode).collect(), + tx_hash: Hex::encode(&log.receipt.transaction.hash), + }) + .collect(); Ok(Events { events }) } fn verify_parameter(address: &String) -> Result<(), Error> { if !util::is_address_valid(&address) { - return Err(anyhow!("Contract address is not valid")); + return Err(anyhow!("Contract address ({}) is not valid", address)); } Ok(()) -} \ No newline at end of file +} diff --git a/ethereum-explorer/src/map_filter_transactions.rs b/ethereum-explorer/src/map_filter_transactions.rs index ad6825d..aa2e6ad 100644 --- a/ethereum-explorer/src/map_filter_transactions.rs +++ b/ethereum-explorer/src/map_filter_transactions.rs @@ -1,9 +1,9 @@ use crate::pb::eth::transaction::v1::{Transaction, Transactions}; use crate::util; +use anyhow::anyhow; use serde::Deserialize; use substreams::Hex; -use substreams_ethereum::pb::eth::v2::{Block, TransactionTraceStatus, TransactionTrace}; -use anyhow::anyhow; +use substreams_ethereum::pb::eth::v2::{Block, TransactionTrace, TransactionTraceStatus}; #[derive(Deserialize)] struct TransactionFilterParams { @@ -13,36 +13,54 @@ struct TransactionFilterParams { #[substreams::handlers::map] fn map_filter_transactions(params: String, blk: Block) -> Result> { - let filters: TransactionFilterParams = serde_qs::from_str(¶ms).unwrap(); - verify_filter_params(&filters)?; + let filters = parse_filters_from_params(params)?; - let transactions: Vec = blk.transactions() + let transactions: Vec = blk + .transactions() .filter(|trans| apply_filter(&trans, &filters)) .map(|trans| Transaction { from: Hex::encode(&trans.from), to: Hex::encode(&trans.to), hash: Hex::encode(&trans.hash), - }).collect(); - + }) + .collect(); - Ok(Transactions { - transactions, - }) + Ok(Transactions { transactions }) } -fn verify_filter_params(params: &TransactionFilterParams) -> Result<(), Vec> { +fn parse_filters_from_params(params: String) -> Result> { + let parsed_result = serde_qs::from_str(¶ms); + if parsed_result.is_err() { + return Err(Vec::from([anyhow!("Unpexcted error while parsing parameters")])); + } + + let filters = parsed_result.unwrap(); + verify_filters(&filters)?; + + Ok(filters) +} + +fn verify_filters(params: &TransactionFilterParams) -> Result<(), Vec> { let mut errors: Vec = Vec::new(); if params.from.is_some() && !util::is_address_valid(¶ms.from.as_ref().unwrap()) { - errors.push(anyhow!("'from' address is not valid")); + let from = params.from.as_ref().unwrap(); + + if !util::is_address_valid(from) { + errors.push(anyhow!("'from' address ({}) is not valid", from)); + } } if params.to.is_some() && !util::is_address_valid(¶ms.to.as_ref().unwrap()) { - errors.push(anyhow!("'to' address is not valid")); + let to = params.to.as_ref().unwrap(); + + if !util::is_address_valid(to) { + errors.push(anyhow!("'to' address ({}) is not valid", to)); + } } if errors.len() > 0 { - return Err(errors) + return Err(errors); } Ok(()) @@ -51,7 +69,8 @@ fn verify_filter_params(params: &TransactionFilterParams) -> Result<(), Vec bool { if !filter_by_parameter(&filters.from, &transaction.from) || !filter_by_parameter(&filters.to, &transaction.to) - || transaction.status != (TransactionTraceStatus::Succeeded as i32) { + || transaction.status != (TransactionTraceStatus::Succeeded as i32) + { return false; } @@ -64,7 +83,7 @@ fn filter_by_parameter(parameter: &Option, transaction_field: &Vec) } let parameter_as_vec = &Hex::decode(parameter.as_ref().unwrap()).expect("already verified"); - if parameter_as_vec == transaction_field { + if transaction_field == parameter_as_vec { return true; } diff --git a/ethereum-explorer/substreams.yaml b/ethereum-explorer/substreams.yaml index 88ee0d8..e99db20 100644 --- a/ethereum-explorer/substreams.yaml +++ b/ethereum-explorer/substreams.yaml @@ -39,5 +39,7 @@ modules: type: proto:eth.event.v1.Events params: + # Filtering transactions with to = 0xdAC17F958D2ee523a2206206994597C13D831ec7 (USDT contract address) map_filter_transactions: "to=0xdAC17F958D2ee523a2206206994597C13D831ec7" - map_contract_events: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d" \ No newline at end of file + # Getting the logs of the BoredApeYachtClub smart contract + map_contract_events: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d" \ No newline at end of file