ethers-rs/examples/subscriptions/examples/subscribe_blocks.rs

21 lines
576 B
Rust

use ethers::providers::{Middleware, Provider, StreamExt, Ws};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
let provider =
Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
.await?;
let mut stream = provider.subscribe_blocks().await?.take(1);
while let Some(block) = stream.next().await {
println!(
"Ts: {:?}, block number: {} -> {:?}",
block.timestamp,
block.number.unwrap(),
block.hash.unwrap()
);
}
Ok(())
}