chore: fix custom provider example

This commit is contained in:
Georgios Konstantopoulos 2023-02-02 11:38:29 -08:00
parent a544d4076c
commit 806af08f99
1 changed files with 6 additions and 3 deletions

View File

@ -1,7 +1,7 @@
//! Create a custom data transport to use with a Provider. //! Create a custom data transport to use with a Provider.
use async_trait::async_trait; use async_trait::async_trait;
use ethers::prelude::*; use ethers::{core::utils::Anvil, prelude::*};
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug; use std::fmt::Debug;
use thiserror::Error; use thiserror::Error;
@ -103,8 +103,11 @@ impl PubsubClient for WsOrIpc {
#[tokio::main] #[tokio::main]
async fn main() -> eyre::Result<()> { async fn main() -> eyre::Result<()> {
// Spawn Anvil
let anvil = Anvil::new().block_time(1u64).spawn();
// Connect to our transport // Connect to our transport
let transport = WsOrIpc::connect("ws://localhost:8546").await?; let transport = WsOrIpc::connect(&anvil.ws_endpoint()).await?;
// Wrap the transport in a provider // Wrap the transport in a provider
let provider = Provider::new(transport); let provider = Provider::new(transport);
@ -113,7 +116,7 @@ async fn main() -> eyre::Result<()> {
let block_number = provider.get_block_number().await?; let block_number = provider.get_block_number().await?;
println!("Current block: {block_number}"); println!("Current block: {block_number}");
let mut subscription = provider.subscribe_blocks().await?; let mut subscription = provider.subscribe_blocks().await?.take(3);
while let Some(block) = subscription.next().await { while let Some(block) = subscription.next().await {
println!("New block: {:?}", block.number); println!("New block: {:?}", block.number);
} }