Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat: add filter utility function #316

Merged
merged 3 commits into from
Jun 13, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions ethers-core/src/types/log.rs
Original file line number Diff line number Diff line change
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 a all blocks in range `(1337..BlockNumber::Latest)`
mattsse marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```rust
/// # use ethers::types::{Filter, H256};
/// # fn main() {
/// let filter = Filter::new().select(1337u64..);
/// # }
/// ```
///
/// Match a all blocks in range `(BlockNumber::Earliest..1337)`
mattsse marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```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