bankai_sdk/fetch/clients/
beacon_client.rs1use crate::errors::{SdkError, SdkResult};
2use alloy_rpc_types_beacon::header::HeaderResponse;
3
4pub struct BeaconFetcher {
5 pub beacon_rpc: String,
6}
7
8impl BeaconFetcher {
9 pub fn new(beacon_rpc: String) -> Self {
10 Self { beacon_rpc }
11 }
12
13 pub async fn fetch_header(&self, slot: u64) -> SdkResult<HeaderResponse> {
14 let url = format!("{}/eth/v1/beacon/headers/{}", self.beacon_rpc, slot);
15 let client = reqwest::Client::new();
16 let response = client
17 .get(&url)
18 .header("Accept", "application/json")
19 .send()
20 .await
21 .map_err(SdkError::from)?;
22 if response.status() == reqwest::StatusCode::NOT_FOUND {
23 return Err(SdkError::NotFound(format!(
24 "beacon header slot {slot} not found"
25 )));
26 }
27
28 let header_response = response
29 .json::<HeaderResponse>()
30 .await
31 .map_err(SdkError::from)?;
32
33 Ok(header_response)
34 }
35}