Better topic0 (#4950)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Supernovahs.eth
2023-10-09 18:10:33 +05:30
committed by GitHub
parent 9561f2860a
commit 670d459147
2 changed files with 14 additions and 6 deletions

View File

@ -393,18 +393,26 @@ impl Filter {
#[must_use] #[must_use]
pub fn event(self, event_name: &str) -> Self { pub fn event(self, event_name: &str) -> Self {
let hash = keccak256(event_name.as_bytes()); 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] #[must_use]
pub fn events(self, events: impl IntoIterator<Item = impl AsRef<[u8]>>) -> Self { 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<_>>(); 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) /// Sets topic0 (the event name for non-anonymous events)
#[must_use] #[must_use]
#[deprecated(note = "use `event_signature` instead")]
pub fn topic0<T: Into<Topic>>(mut self, topic: T) -> Self { pub fn topic0<T: Into<Topic>>(mut self, topic: T) -> Self {
self.topics[0] = topic.into(); self.topics[0] = topic.into();
self self

View File

@ -182,9 +182,9 @@ fn receipts_provider_example<T: ReceiptProvider + TransactionsProvider + HeaderP
let addr = Address::random(); let addr = Address::random();
let topic = B256::random(); let topic = B256::random();
// TODO: Make it clearer how to choose between topic0 (event name) and the other 3 indexed // TODO: Make it clearer how to choose between event_signature(topic0) (event name) and the
// topics. This API is a bit clunky and not obvious to use at the moemnt. // 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); let filter = Filter::new().address(addr).event_signature(topic);
let filter_params = FilteredParams::new(Some(filter)); let filter_params = FilteredParams::new(Some(filter));
let address_filter = FilteredParams::address_filter(&addr.into()); let address_filter = FilteredParams::address_filter(&addr.into());
let topics_filter = FilteredParams::topics_filter(&[topic.into()]); let topics_filter = FilteredParams::topics_filter(&[topic.into()]);