-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathfallback_layer.rs
More file actions
53 lines (44 loc) · 1.67 KB
/
Copy pathfallback_layer.rs
File metadata and controls
53 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Test the fallback layer provider.
use std::{num::NonZeroUsize, time::Duration};
use alloy::{
providers::{Provider, ProviderBuilder},
rpc::client::RpcClient,
transports::{
http::{reqwest::Url, Http},
layers::FallbackLayer,
},
};
use example_support::rpc_urls;
use eyre::Result;
use tower::ServiceBuilder;
#[tokio::main]
async fn main() -> Result<()> {
let _ = tracing_subscriber::fmt::try_init();
// Configure the fallback layer for the endpoints supplied in `RPC_URLS`.
let rpc_urls = rpc_urls()?;
let active_transport_count =
NonZeroUsize::new(rpc_urls.len()).expect("rpc_urls() rejects an empty list");
let fallback_layer =
FallbackLayer::default().with_active_transport_count(active_transport_count);
// Define one transport for each configured endpoint.
let transports =
rpc_urls.iter().map(|url| Ok(Http::new(Url::parse(url)?))).collect::<Result<Vec<_>>>()?;
// Apply the FallbackLayer to the transports
let transport = ServiceBuilder::new().layer(fallback_layer).service(transports);
let client = RpcClient::builder().transport(transport, false);
let provider = ProviderBuilder::new().connect_client(client);
// Get the latest block number using the provider with ranked transports.
// This will also print the rankings of the transports to the console.
let max = 10;
let mut count = 0;
loop {
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {latest_block}");
tokio::time::sleep(Duration::from_secs(1)).await;
count += 1;
if count >= max {
break;
}
}
Ok(())
}