web3-proxy/examples/subscribe_blocks.rs

30 lines
732 B
Rust
Raw Normal View History

2022-04-25 23:26:54 +03:00
/// subscribe to a websocket rpc
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 23:26:54 +03:00
// erigon
let url = "ws://10.11.12.16:8545";
// geth
// let url = "ws://10.11.12.16:8946";
println!("Subscribing to blocks from {}", url);
let provider = Ws::connect(url).await?;
let provider = Provider::new(provider).interval(Duration::from_secs(1));
let mut stream = provider.subscribe_blocks().await?.take(3);
2022-04-25 22:46:17 +03:00
while let Some(block) = stream.next().await {
2022-04-25 22:42:51 +03:00
println!(
2022-04-25 23:26:54 +03:00
"{:?} = Ts: {:?}, block number: {}",
block.hash.unwrap(),
2022-04-25 22:42:51 +03:00
block.timestamp,
block.number.unwrap(),
);
}
Ok(())
}