2022-05-05 22:07:09 +03:00
|
|
|
/// poll an http rpc
|
|
|
|
use ethers::prelude::*;
|
|
|
|
use std::{str::FromStr, time::Duration};
|
|
|
|
|
|
|
|
#[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.
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
|
|
.compact()
|
|
|
|
.init();
|
2022-05-18 19:35:06 +03:00
|
|
|
|
|
|
|
fdlimit::raise_fd_limit();
|
|
|
|
|
2022-05-05 22:07:09 +03:00
|
|
|
// erigon does not support most filters
|
|
|
|
// let url = "http://10.11.12.16:8545";
|
|
|
|
// geth
|
2022-05-18 19:35:06 +03:00
|
|
|
let url = "http://10.11.12.16:8545";
|
2022-05-05 22:07:09 +03:00
|
|
|
|
|
|
|
println!("Watching blocks from {:?}", url);
|
|
|
|
|
|
|
|
let provider = Http::from_str(url)?;
|
|
|
|
|
|
|
|
let provider = Provider::new(provider).interval(Duration::from_secs(1));
|
|
|
|
|
2022-05-18 19:35:06 +03:00
|
|
|
let mut stream = provider.watch_blocks().await?;
|
2022-05-05 22:07:09 +03:00
|
|
|
while let Some(block_number) = stream.next().await {
|
|
|
|
let block = provider.get_block(block_number).await?.unwrap();
|
|
|
|
println!(
|
|
|
|
"{:?} = Ts: {:?}, block number: {}",
|
|
|
|
block.hash.unwrap(),
|
|
|
|
block.timestamp,
|
|
|
|
block.number.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|