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

feat: add events function to set multiple event filter #1607

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Changes from all commits
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
16 changes: 15 additions & 1 deletion ethers-core/src/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,21 @@ impl Filter {
self
}

/// given the event in string form, it hashes it and adds it to the topics to monitor
/// Given the event signature in string form, it hashes it and adds it to the topics to monitor
#[must_use]
pub fn event(self, event_name: &str) -> Self {
let hash = H256::from(keccak256(event_name.as_bytes()));
self.topic0(hash)
}

/// Hashes all event signatures and sets them as array to topic0
#[must_use]
pub fn events(self, events: impl IntoIterator<Item = impl AsRef<[u8]>>) -> Self {
let events =
events.into_iter().map(|e| H256::from(keccak256(e.as_ref()))).collect::<Vec<_>>();
self.topic0(events)
}

/// Sets topic0 (the event name for non-anonymous events)
#[must_use]
pub fn topic0<T: Into<Topic>>(mut self, topic: T) -> Self {
Expand Down Expand Up @@ -544,6 +552,12 @@ impl From<H256> for Topic {
}
}

impl From<Vec<H256>> for ValueOrArray<H256> {
fn from(src: Vec<H256>) -> Self {
ValueOrArray::Array(src)
}
}

impl From<ValueOrArray<H256>> for Topic {
fn from(src: ValueOrArray<H256>) -> Self {
match src {
Expand Down