Skip to content

Commit

Permalink
Better topic0 (paradigmxyz#4950)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
supernovahs and mattsse authored Oct 9, 2023
1 parent 9561f28 commit 670d459
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions crates/rpc/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,26 @@ impl Filter {
#[must_use]
pub fn event(self, event_name: &str) -> Self {
let hash = keccak256(event_name.as_bytes());
self.topic0(hash)
self.event_signature(hash)
}

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

/// Sets event_signature(topic0) (the event name for non-anonymous events)
#[must_use]
pub fn event_signature<T: Into<Topic>>(mut self, topic: T) -> Self {
self.topics[0] = topic.into();
self
}

/// Sets topic0 (the event name for non-anonymous events)
#[must_use]
#[deprecated(note = "use `event_signature` instead")]
pub fn topic0<T: Into<Topic>>(mut self, topic: T) -> Self {
self.topics[0] = topic.into();
self
Expand Down
6 changes: 3 additions & 3 deletions examples/db-access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ fn receipts_provider_example<T: ReceiptProvider + TransactionsProvider + HeaderP
let addr = Address::random();
let topic = B256::random();

// TODO: Make it clearer how to choose between topic0 (event name) and the other 3 indexed
// topics. This API is a bit clunky and not obvious to use at the moemnt.
let filter = Filter::new().address(addr).topic0(topic);
// TODO: Make it clearer how to choose between event_signature(topic0) (event name) and the
// other 3 indexed topics. This API is a bit clunky and not obvious to use at the moemnt.
let filter = Filter::new().address(addr).event_signature(topic);
let filter_params = FilteredParams::new(Some(filter));
let address_filter = FilteredParams::address_filter(&addr.into());
let topics_filter = FilteredParams::topics_filter(&[topic.into()]);
Expand Down

0 comments on commit 670d459

Please sign in to comment.