web3-proxy/web3_proxy_cli/examples/subscribe_blocks.rs

36 lines
947 B
Rust
Raw Permalink Normal View History

2022-05-05 22:07:09 +03:00
/// subscribe to a websocket rpc
use std::time::Duration;
2023-08-03 10:16:54 +03:00
use web3_proxy::prelude::anyhow;
use web3_proxy::prelude::ethers::prelude::*;
use web3_proxy::prelude::fdlimit;
use web3_proxy::prelude::tokio;
2022-05-05 22:07:09 +03:00
#[tokio::main]
async fn main() -> anyhow::Result<()> {
2022-05-20 08:27:18 +03:00
// install global collector configured based on RUST_LOG env var.
2023-11-10 05:02:29 +03:00
fdlimit::raise_fd_limit()?;
2022-05-18 19:35:06 +03:00
2022-05-05 22:07:09 +03:00
// erigon
2022-09-30 07:18:18 +03:00
let url = "ws://10.11.12.16:8548";
2022-05-05 22:07:09 +03:00
// geth
2022-09-30 07:18:18 +03:00
// let url = "ws://10.11.12.16:8546";
2022-05-05 22:07:09 +03:00
println!("Subscribing to blocks from {}", url);
let provider = Ws::connect(url).await?;
let provider = Provider::new(provider).interval(Duration::from_secs(1));
2022-05-18 19:35:06 +03:00
let mut stream = provider.subscribe_blocks().await?;
2022-05-05 22:07:09 +03:00
while let Some(block) = stream.next().await {
println!(
"{:?} = Ts: {:?}, block number: {}",
block.hash.unwrap(),
block.timestamp,
block.number.unwrap(),
);
}
Ok(())
}