-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of subscribing to logs from the Ethereum network using an ext…
…ernal provider. (#59) * mod: added subscribe_to_log_external_provider * mod: rpc filtering update * fmt
- Loading branch information
1 parent
3b4f36c
commit ec2fc48
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
examples/subscriptions/examples/subscribe_to_log_external_provider.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Example of subscribing to logs from the Ethereum network using an external provider. | ||
use alloy::{ | ||
primitives::{address, b256}, | ||
providers::{Provider, ProviderBuilder}, | ||
rpc::{ | ||
client::WsConnect, | ||
types::eth::{BlockNumberOrTag, Filter}, | ||
}, | ||
}; | ||
use eyre::Result; | ||
use futures_util::stream::StreamExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let uniswap_token_address = address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984"); | ||
let tranfer_event_signature = | ||
b256!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"); | ||
let filter = Filter::new() | ||
.address(uniswap_token_address) | ||
.event_signature(tranfer_event_signature) | ||
.from_block(BlockNumberOrTag::Latest); | ||
|
||
let rpc_url = "wss://eth.merkle.io"; // DON'T use wss://eth.merkle.io _> this filters wrongly, tested alchemy.io's to be working fine | ||
|
||
// Create the provider. | ||
let ws = WsConnect::new(rpc_url); | ||
let provider = ProviderBuilder::new().on_ws(ws).await.unwrap(); | ||
|
||
let sub = provider.subscribe_logs(&filter).await.expect("Failed to subscribe to logs"); | ||
let mut stream = sub.into_stream(); | ||
|
||
while let Some(log) = stream.next().await { | ||
println!("Uniswap token logs: {log:?}"); | ||
} | ||
|
||
Ok(()) | ||
} |