55 lines
1.7 KiB
Rust
Raw Normal View History

2026-01-08 09:10:00 +02:00
use anyhow::Result;
pub use logos_blockchain_common_http_client::{BasicAuthCredentials, CommonHttpClient, Error};
use logos_blockchain_core::mantle::SignedMantleTx;
2026-01-21 11:47:28 -03:00
use reqwest::{Client, Url};
2026-01-07 16:29:37 +02:00
2026-01-08 09:10:00 +02:00
// Simple wrapper
// maybe extend in the future for our purposes
2026-01-21 11:47:28 -03:00
pub struct BedrockClient {
http_client: CommonHttpClient,
node_url: Url,
}
2026-01-07 16:29:37 +02:00
2026-01-08 09:10:00 +02:00
impl BedrockClient {
2026-01-21 11:47:28 -03:00
pub fn new(auth: Option<BasicAuthCredentials>, node_url: Url) -> Result<Self> {
2026-01-08 09:10:00 +02:00
let client = Client::builder()
2026-01-20 10:26:22 +02:00
//Add more fields if needed
2026-01-08 09:10:00 +02:00
.timeout(std::time::Duration::from_secs(60))
.build()?;
2026-01-07 16:29:37 +02:00
2026-01-21 11:47:28 -03:00
let http_client = CommonHttpClient::new_with_client(client, auth);
Ok(Self {
http_client,
node_url,
})
}
pub async fn post_transaction(&self, tx: SignedMantleTx) -> Result<(), Error> {
self.http_client
.post_transaction(self.node_url.clone(), tx)
.await
2026-01-07 16:29:37 +02:00
}
2026-01-21 14:50:29 +02:00
pub async fn get_lib_stream(&self, url: Url) -> Result<impl Stream<Item = BlockInfo>, Error> {
self.0.get_lib_stream(url).await
}
pub async fn get_block_by_id(
&self,
url: &Url,
header_id: HeaderId,
start_delay_millis: u64,
max_retries: usize,
) -> Result<Option<Block<SignedMantleTx>>, Error> {
let strategy = tokio_retry::strategy::FibonacciBackoff::from_millis(start_delay_millis)
.take(max_retries);
Retry::spawn(strategy, || {
self.0
.get_block_by_id(url.clone(), header_id)
.inspect_err(|err| warn!("Block fetching failed with err: {err:#?}"))
})
.await
}
2026-01-07 16:29:37 +02:00
}