forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_with_auth.rs
More file actions
40 lines (32 loc) · 1.17 KB
/
Copy pathhttp_with_auth.rs
File metadata and controls
40 lines (32 loc) · 1.17 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
//! Example of using the reqwest HTTP client with an `Authorization` header to get the latest block
//! number.
use alloy::{
providers::{Provider, ProviderBuilder},
rpc::client::RpcClient,
transports::http::{
reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION},
Client,
},
Http,
},
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Set the Authorization header.
let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, HeaderValue::from_static("deadbeef"));
// Create the reqwest::Client with the AUTHORIZATION header.
let client_with_auth = Client::builder().default_headers(headers).build()?;
// Create the HTTP transport.
let rpc_url = "https://eth.merkle.io".parse()?;
let http = Http::with_client(client_with_auth, rpc_url);
let rpc_client = RpcClient::new(http, false);
// Create a provider with the HTTP transport.
let provider = ProviderBuilder::new().on_client(rpc_client);
// Get latest block number.
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {latest_block}");
Ok(())
}