2021-08-30 11:00:30 +00:00
|
|
|
use ethers::{prelude::*, utils::Ganache};
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::time::Duration;
|
2020-06-17 18:01:20 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
2022-02-03 20:36:05 +00:00
|
|
|
async fn main() -> eyre::Result<()> {
|
2021-08-30 11:00:30 +00:00
|
|
|
let ganache = Ganache::new().block_time(1u64).spawn();
|
|
|
|
let ws = Ws::connect(ganache.ws_endpoint()).await?;
|
2020-06-22 08:44:08 +00:00
|
|
|
let provider = Provider::new(ws).interval(Duration::from_millis(2000));
|
2021-08-30 11:00:30 +00:00
|
|
|
let mut stream = provider.watch_blocks().await?.take(5);
|
2020-06-17 18:01:20 +00:00
|
|
|
while let Some(block) = stream.next().await {
|
2022-03-18 05:08:15 +00:00
|
|
|
let block = provider.get_block(block).await?.unwrap();
|
2022-03-18 05:09:21 +00:00
|
|
|
println!(
|
|
|
|
"Ts: {:?}, block number: {} -> {:?}",
|
|
|
|
block.timestamp,
|
|
|
|
block.number.unwrap(),
|
|
|
|
block.hash.unwrap()
|
|
|
|
);
|
2020-06-17 18:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|