-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): adding Berachain provider and Berachain bArtio support
- Loading branch information
1 parent
cd5a0de
commit f8d9483
Showing
7 changed files
with
171 additions
and
2 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,47 @@ | ||
use { | ||
super::ProviderConfig, | ||
crate::providers::{Priority, Weight}, | ||
std::collections::HashMap, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct BerachainConfig { | ||
pub supported_chains: HashMap<String, (String, Weight)>, | ||
} | ||
|
||
impl Default for BerachainConfig { | ||
fn default() -> Self { | ||
Self { | ||
supported_chains: default_supported_chains(), | ||
} | ||
} | ||
} | ||
|
||
impl ProviderConfig for BerachainConfig { | ||
fn supported_chains(self) -> HashMap<String, (String, Weight)> { | ||
self.supported_chains | ||
} | ||
|
||
fn supported_ws_chains(self) -> HashMap<String, (String, Weight)> { | ||
HashMap::new() | ||
} | ||
|
||
fn provider_kind(&self) -> crate::providers::ProviderKind { | ||
crate::providers::ProviderKind::Berachain | ||
} | ||
} | ||
|
||
fn default_supported_chains() -> HashMap<String, (String, Weight)> { | ||
// Keep in-sync with SUPPORTED_CHAINS.md | ||
|
||
HashMap::from([ | ||
// Berachain bArtio | ||
( | ||
"eip155:80084".into(), | ||
( | ||
"https://bartio.rpc.berachain.com/".into(), | ||
Weight::new(Priority::Normal).unwrap(), | ||
), | ||
), | ||
]) | ||
} |
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,96 @@ | ||
use { | ||
super::{Provider, ProviderKind, RateLimited, RpcProvider, RpcProviderFactory}, | ||
crate::{ | ||
env::BerachainConfig, | ||
error::{RpcError, RpcResult}, | ||
}, | ||
async_trait::async_trait, | ||
axum::{ | ||
http::HeaderValue, | ||
response::{IntoResponse, Response}, | ||
}, | ||
hyper::{client::HttpConnector, http, Client, Method}, | ||
hyper_tls::HttpsConnector, | ||
std::collections::HashMap, | ||
tracing::debug, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct BerachainProvider { | ||
pub client: Client<HttpsConnector<HttpConnector>>, | ||
pub supported_chains: HashMap<String, String>, | ||
} | ||
|
||
impl Provider for BerachainProvider { | ||
fn supports_caip_chainid(&self, chain_id: &str) -> bool { | ||
self.supported_chains.contains_key(chain_id) | ||
} | ||
|
||
fn supported_caip_chains(&self) -> Vec<String> { | ||
self.supported_chains.keys().cloned().collect() | ||
} | ||
|
||
fn provider_kind(&self) -> ProviderKind { | ||
ProviderKind::Berachain | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl RateLimited for BerachainProvider { | ||
async fn is_rate_limited(&self, response: &mut Response) -> bool { | ||
response.status() == http::StatusCode::TOO_MANY_REQUESTS | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl RpcProvider for BerachainProvider { | ||
#[tracing::instrument(skip(self, body), fields(provider = %self.provider_kind()), level = "debug")] | ||
async fn proxy(&self, chain_id: &str, body: hyper::body::Bytes) -> RpcResult<Response> { | ||
let uri = self | ||
.supported_chains | ||
.get(chain_id) | ||
.ok_or(RpcError::ChainNotFound)?; | ||
|
||
let hyper_request = hyper::http::Request::builder() | ||
.method(Method::POST) | ||
.uri(uri) | ||
.header("Content-Type", "application/json") | ||
.body(hyper::body::Body::from(body))?; | ||
|
||
let response = self.client.request(hyper_request).await?; | ||
let status = response.status(); | ||
let body = hyper::body::to_bytes(response.into_body()).await?; | ||
|
||
if let Ok(response) = serde_json::from_slice::<jsonrpc::Response>(&body) { | ||
if response.error.is_some() && status.is_success() { | ||
debug!( | ||
"Strange: provider returned JSON RPC error, but status {status} is success: \ | ||
Berachain: {response:?}" | ||
); | ||
} | ||
} | ||
|
||
let mut response = (status, body).into_response(); | ||
response | ||
.headers_mut() | ||
.insert("Content-Type", HeaderValue::from_static("application/json")); | ||
Ok(response) | ||
} | ||
} | ||
|
||
impl RpcProviderFactory<BerachainConfig> for BerachainProvider { | ||
#[tracing::instrument(level = "debug")] | ||
fn new(provider_config: &BerachainConfig) -> Self { | ||
let forward_proxy_client = Client::builder().build::<_, hyper::Body>(HttpsConnector::new()); | ||
let supported_chains: HashMap<String, String> = provider_config | ||
.supported_chains | ||
.iter() | ||
.map(|(k, v)| (k.clone(), v.0.clone())) | ||
.collect(); | ||
|
||
BerachainProvider { | ||
client: forward_proxy_client, | ||
supported_chains, | ||
} | ||
} | ||
} |
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,18 @@ | ||
use { | ||
super::check_if_rpc_is_responding_correctly_for_supported_chain, crate::context::ServerContext, | ||
rpc_proxy::providers::ProviderKind, test_context::test_context, | ||
}; | ||
|
||
#[test_context(ServerContext)] | ||
#[tokio::test] | ||
#[ignore] | ||
async fn berachain_provider_eip155_80084(ctx: &mut ServerContext) { | ||
// Berachain bArtio | ||
check_if_rpc_is_responding_correctly_for_supported_chain( | ||
ctx, | ||
&ProviderKind::Berachain, | ||
"eip155:80084", | ||
"0x138d4", | ||
) | ||
.await; | ||
} |
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