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

feature: allow event builder to stream with meta #1483

Merged
merged 4 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

- Make `ethers-solc` optional dependency of `ethers`, needs `ethers-solc` feature to activate
[#1463](https://github.com/gakonst/ethers-rs/pull/1463)
- Add `rawMetadata:String` field to configurable contract output
- Add `rawMetadata:String` field to configurable contract output
[#1365](https://github.com/gakonst/ethers-rs/pull/1365)
- Use relative source paths and `solc --base-path`
[#1317](https://github.com/gakonst/ethers-rs/pull/1317)
Expand Down Expand Up @@ -237,6 +237,8 @@

### Unreleased

- Add `Event::stream_with_meta` and `Event::subscribe_with_meta`
[#1483](https://github.com/gakonst/ethers-rs/pull/1483)
- Added tx builder methods to `ContractFactory`
[#1289](https://github.com/gakonst/ethers-rs/pull/1289)
- Relax Clone requirements when Arc<Middleware> is used
Expand Down
42 changes: 42 additions & 0 deletions ethers-contract/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ where
self.provider.watch(&self.filter).await.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(filter.id, filter, Box::new(move |log| self.parse_log(log))))
}

/// As [`Self::stream`], but does not discard [`Log`] metadata.
pub async fn stream_with_meta(
&'a self,
) -> Result<
// Wraps the FilterWatcher with a mapping to the event
EventStream<'a, FilterWatcher<'a, M::Provider, Log>, (D, LogMeta), ContractError<M>>,
ContractError<M>,
> {
let filter =
self.provider.watch(&self.filter).await.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(
filter.id,
filter,
Box::new(move |log| {
let meta = LogMeta::from(&log);
Ok((self.parse_log(log)?, meta))
}),
))
}
}

impl<'a, M, D> Event<'a, M, D>
Expand All @@ -189,6 +209,28 @@ where
.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(filter.id, filter, Box::new(move |log| self.parse_log(log))))
}

pub async fn subscribe_with_meta(
&'a self,
) -> Result<
// Wraps the SubscriptionStream with a mapping to the event
EventStream<'a, SubscriptionStream<'a, M::Provider, Log>, (D, LogMeta), ContractError<M>>,
ContractError<M>,
> {
let filter = self
.provider
.subscribe_logs(&self.filter)
.await
.map_err(ContractError::MiddlewareError)?;
Ok(EventStream::new(
filter.id,
filter,
Box::new(move |log| {
let meta = LogMeta::from(&log);
Ok((self.parse_log(log)?, meta))
}),
))
}
}

impl<M, D> Event<'_, M, D>
Expand Down