From 1dda336a78ac201a7d1137c8ca066d8670801f1e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 13 Jun 2021 19:39:36 +0200 Subject: [PATCH] feat: add filter utility function (#316) * feat: add filter utility function * chore: typo Co-authored-by: Georgios Konstantopoulos * chore: typo Co-authored-by: Georgios Konstantopoulos Co-authored-by: Georgios Konstantopoulos --- ethers-core/src/types/log.rs | 126 +++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/ethers-core/src/types/log.rs b/ethers-core/src/types/log.rs index b2d05221..d30b7121 100644 --- a/ethers-core/src/types/log.rs +++ b/ethers-core/src/types/log.rs @@ -4,6 +4,7 @@ use crate::{ utils::keccak256, }; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; +use std::ops::{Range, RangeFrom, RangeTo}; /// A log produced by a transaction. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -71,6 +72,65 @@ pub enum FilterBlockOption { AtBlockHash(H256), } +impl From for FilterBlockOption { + fn from(block: BlockNumber) -> Self { + let block = Some(block); + FilterBlockOption::Range { + from_block: block, + to_block: block, + } + } +} + +impl From for FilterBlockOption { + fn from(block: U64) -> Self { + BlockNumber::from(block).into() + } +} + +impl From for FilterBlockOption { + fn from(block: u64) -> Self { + BlockNumber::from(block).into() + } +} + +impl> From> for FilterBlockOption { + fn from(r: Range) -> Self { + let from_block = Some(r.start.into()); + let to_block = Some(r.end.into()); + FilterBlockOption::Range { + from_block, + to_block, + } + } +} + +impl> From> for FilterBlockOption { + fn from(r: RangeTo) -> Self { + let to_block = Some(r.end.into()); + FilterBlockOption::Range { + from_block: Some(BlockNumber::Earliest), + to_block, + } + } +} + +impl> From> for FilterBlockOption { + fn from(r: RangeFrom) -> Self { + let from_block = Some(r.start.into()); + FilterBlockOption::Range { + from_block, + to_block: Some(BlockNumber::Latest), + } + } +} + +impl From for FilterBlockOption { + fn from(hash: H256) -> Self { + FilterBlockOption::AtBlockHash(hash) + } +} + impl Default for FilterBlockOption { fn default() -> Self { FilterBlockOption::Range { @@ -187,6 +247,72 @@ impl Filter { Self::default() } + /// Sets the inner filter object + /// + /// *NOTE:* ranges are always inclusive + /// + /// # Examples + /// + /// Match only a specific block + /// + /// ```rust + /// # use ethers::types::Filter; + /// # fn main() { + /// let filter = Filter::new().select(69u64); + /// # } + /// ``` + /// This is the same as `Filter::new().from_block(1337u64).to_block(1337u64)` + /// + /// Match the latest block only + /// + /// ```rust + /// # use ethers::types::{Filter, BlockNumber}; + /// # fn main() { + /// let filter = Filter::new().select(BlockNumber::Latest); + /// # } + /// ``` + /// + /// Match a block by its hash + /// + /// ```rust + /// # use ethers::types::{Filter, H256}; + /// # fn main() { + /// let filter = Filter::new().select(H256::zero()); + /// # } + /// ``` + /// This is the same as `at_block_hash` + /// + /// Match a range of blocks + /// + /// ```rust + /// # use ethers::types::{Filter, H256}; + /// # fn main() { + /// let filter = Filter::new().select(0u64..100u64); + /// # } + /// ``` + /// + /// Match all blocks in range `(1337..BlockNumber::Latest)` + /// + /// ```rust + /// # use ethers::types::{Filter, H256}; + /// # fn main() { + /// let filter = Filter::new().select(1337u64..); + /// # } + /// ``` + /// + /// Match all blocks in range `(BlockNumber::Earliest..1337)` + /// + /// ```rust + /// # use ethers::types::{Filter, H256}; + /// # fn main() { + /// let filter = Filter::new().select(..1337u64); + /// # } + /// ``` + pub fn select(mut self, filter: impl Into) -> Self { + self.block_option = filter.into(); + self + } + #[allow(clippy::wrong_self_convention)] pub fn from_block>(mut self, block: T) -> Self { self.block_option = self.block_option.set_from_block(block.into());