-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(providers): event, polling and streaming methods #274
Changes from 1 commit
7d6f5f8
f2dac32
bc5fa21
4d3046b
b63d5f3
7111f00
14a797b
2ac273f
b8912bd
6edd8fe
188669f
1218bb2
7f9454e
025415d
6159a67
40b561a
069814d
48072de
1e1a7eb
bd0d19d
2c6f1d0
6c6932b
acc2544
87963a7
f1adb8e
6845bd8
e6a5587
8adaa41
5d8c267
240d506
168b0c3
3743e18
12a421f
ceb8725
eb0ee46
8ddf18c
98fd3f6
738a8ae
1e81c8c
512e15f
70dd850
19e43b3
2ed0440
0dacfb0
215d6e9
c76fb8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,23 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync | |
/// | ||
/// Returns a builder that is used to configure the poller. See [`PollerBuilder`] for more | ||
/// details. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Get the next 5 blocks: | ||
/// | ||
/// ```no_run | ||
/// # async fn example<N: alloy_network::Network>(provider: impl alloy_providers::Provider<N>) -> Result<(), Box<dyn std::error::Error>> { | ||
/// use futures::StreamExt; | ||
/// | ||
/// let poller = provider.watch_blocks().await?; | ||
/// let mut stream = poller.into_stream().flat_map(futures::stream::iter).take(5); | ||
/// while let Some(block_hash) = stream.next().await { | ||
/// println!("new block: {block_hash}"); | ||
/// } | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
async fn watch_blocks(&self) -> TransportResult<FilterPollerBuilder<T, B256>> { | ||
let id = self.new_block_filter().await?; | ||
Ok(PollerBuilder::new(self.weak_client(), "eth_getFilterChanges", (id,))) | ||
|
@@ -149,9 +166,20 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync | |
/// | ||
/// # Examples | ||
/// | ||
/// Get the next 5 pending transactions: | ||
/// | ||
/// ```no_run | ||
/// # async fn example<N: alloy_network::Network>(provider: impl alloy_providers::Provider<N>) -> Result<(), Box<dyn std::error::Error>> { | ||
/// use futures::StreamExt; | ||
/// | ||
/// let poller = provider.watch_pending_transactions().await?; | ||
/// let mut stream = poller.into_stream().flat_map(futures::stream::iter).take(5); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if it's worth making the watch_pending_transactions future always do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it returns a |
||
/// while let Some(tx_hash) = stream.next().await { | ||
/// println!("pending transaction: {tx_hash}"); | ||
/// } | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
async fn watch_pending_transactions(&self) -> TransportResult<FilterPollerBuilder<T, B256>> { | ||
let id = self.new_pending_transactions_filter().await?; | ||
Ok(PollerBuilder::new(self.weak_client(), "eth_getFilterChanges", (id,))) | ||
|
@@ -162,6 +190,29 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync | |
/// | ||
/// Returns a builder that is used to configure the poller. See [`PollerBuilder`] for more | ||
/// details. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Get the next 5 USDC transfer logs: | ||
/// | ||
/// ```no_run | ||
/// # async fn example<N: alloy_network::Network>(provider: impl alloy_providers::Provider<N>) -> Result<(), Box<dyn std::error::Error>> { | ||
/// use alloy_primitives::{address, b256}; | ||
/// use alloy_rpc_types::Filter; | ||
/// use futures::StreamExt; | ||
/// | ||
/// let address = address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"); | ||
/// let transfer_signature = b256!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); | ||
/// let filter = Filter::new().address(address).event_signature(transfer_signature); | ||
/// | ||
/// let poller = provider.watch_logs(&filter).await?; | ||
/// let mut stream = poller.into_stream().flat_map(futures::stream::iter).take(5); | ||
/// while let Some(log) = stream.next().await { | ||
/// println!("{log:#?}"); | ||
/// } | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
async fn watch_logs(&self, filter: &Filter) -> TransportResult<FilterPollerBuilder<T, Log>> { | ||
let id = self.new_filter(filter).await?; | ||
Ok(PollerBuilder::new(self.weak_client(), "eth_getFilterChanges", (id,))) | ||
|
@@ -186,7 +237,7 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync | |
self.client().prepare("eth_newPendingTransactionFilter", ()).await | ||
} | ||
|
||
/// Notify the provider that we are interested in new logs using the given filter. | ||
/// Notify the provider that we are interested in logs that match the given filter. | ||
/// | ||
/// Returns the ID to use with [`eth_getFilterChanges`](Self::get_filter_changes). | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this flat_map needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we poll
eth_getFilterChanges
returns aVec