web3-proxy/examples/subscribe_blocks.rs

21 lines
605 B
Rust
Raw Normal View History

2022-04-25 22:42:51 +03:00
use ethers::prelude::*;
use std::time::Duration;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
2022-04-25 22:46:17 +03:00
let ws = Ws::connect("ws://10.11.12.16:8545").await?;
// let ws = Ws::connect("ws://10.11.12.16:8946").await?;
2022-04-25 22:42:51 +03:00
let provider = Provider::new(ws).interval(Duration::from_secs(1));
2022-04-25 22:46:17 +03:00
let mut stream = provider.subscribe_blocks().await?.take(5);
while let Some(block) = stream.next().await {
2022-04-25 22:42:51 +03:00
println!(
"Ts: {:?}, block number: {} -> {:?}",
block.timestamp,
block.number.unwrap(),
block.hash.unwrap()
);
}
Ok(())
}