Skip to content

Commit

Permalink
feat: add filter utility function (#316)
Browse files Browse the repository at this point in the history
* feat: add filter utility function

* chore: typo

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>

* chore: typo

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
  • Loading branch information
mattsse and gakonst committed Jun 13, 2021
1 parent 34d38ca commit 1dda336
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions ethers-core/src/types/log.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -71,6 +72,65 @@ pub enum FilterBlockOption {
AtBlockHash(H256),
}

impl From<BlockNumber> for FilterBlockOption {
fn from(block: BlockNumber) -> Self {
let block = Some(block);
FilterBlockOption::Range {
from_block: block,
to_block: block,
}
}
}

impl From<U64> for FilterBlockOption {
fn from(block: U64) -> Self {
BlockNumber::from(block).into()
}
}

impl From<u64> for FilterBlockOption {
fn from(block: u64) -> Self {
BlockNumber::from(block).into()
}
}

impl<T: Into<BlockNumber>> From<Range<T>> for FilterBlockOption {
fn from(r: Range<T>) -> Self {
let from_block = Some(r.start.into());
let to_block = Some(r.end.into());
FilterBlockOption::Range {
from_block,
to_block,
}
}
}

impl<T: Into<BlockNumber>> From<RangeTo<T>> for FilterBlockOption {
fn from(r: RangeTo<T>) -> Self {
let to_block = Some(r.end.into());
FilterBlockOption::Range {
from_block: Some(BlockNumber::Earliest),
to_block,
}
}
}

impl<T: Into<BlockNumber>> From<RangeFrom<T>> for FilterBlockOption {
fn from(r: RangeFrom<T>) -> Self {
let from_block = Some(r.start.into());
FilterBlockOption::Range {
from_block,
to_block: Some(BlockNumber::Latest),
}
}
}

impl From<H256> for FilterBlockOption {
fn from(hash: H256) -> Self {
FilterBlockOption::AtBlockHash(hash)
}
}

impl Default for FilterBlockOption {
fn default() -> Self {
FilterBlockOption::Range {
Expand Down Expand Up @@ -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<FilterBlockOption>) -> Self {
self.block_option = filter.into();
self
}

#[allow(clippy::wrong_self_convention)]
pub fn from_block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
self.block_option = self.block_option.set_from_block(block.into());
Expand Down

0 comments on commit 1dda336

Please sign in to comment.