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

Commit

Permalink
feature: allow event builder to stream with meta (#1483)
Browse files Browse the repository at this point in the history
* feature: allow event builder to stream with meta

* doc: improve rustdoc

* feature: subscribe_with_meta

* chore: fmt
  • Loading branch information
prestwich authored Jul 17, 2022
1 parent ba00f54 commit 13ce18f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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

0 comments on commit 13ce18f

Please sign in to comment.