-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6512656
commit 39104f4
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(()) | ||
} |