-
Notifications
You must be signed in to change notification settings - Fork 56
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 the Aurora native mainnet and testnet RPC (#469)
- Loading branch information
1 parent
9733f4b
commit 48b18c7
Showing
6 changed files
with
183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use { | ||
super::ProviderConfig, | ||
crate::providers::{Priority, Weight}, | ||
std::collections::HashMap, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct AuroraConfig { | ||
pub supported_chains: HashMap<String, (String, Weight)>, | ||
} | ||
|
||
impl Default for AuroraConfig { | ||
fn default() -> Self { | ||
Self { | ||
supported_chains: default_supported_chains(), | ||
} | ||
} | ||
} | ||
|
||
impl ProviderConfig for AuroraConfig { | ||
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::Aurora | ||
} | ||
} | ||
|
||
fn default_supported_chains() -> HashMap<String, (String, Weight)> { | ||
// Keep in-sync with SUPPORTED_CHAINS.md | ||
|
||
HashMap::from([ | ||
// Aurora Mainnet | ||
( | ||
"eip155:1313161554".into(), | ||
( | ||
"https://mainnet.aurora.dev".into(), | ||
Weight::new(Priority::High).unwrap(), | ||
), | ||
), | ||
// Aurora Testnet | ||
( | ||
"eip155:1313161555".into(), | ||
( | ||
"https://testnet.aurora.dev".into(), | ||
Weight::new(Priority::High).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,99 @@ | ||
use { | ||
super::{Provider, ProviderKind, RateLimited, RpcProvider, RpcProviderFactory}, | ||
crate::{ | ||
env::AuroraConfig, | ||
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::info, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct AuroraProvider { | ||
pub client: Client<HttpsConnector<HttpConnector>>, | ||
pub supported_chains: HashMap<String, String>, | ||
} | ||
|
||
impl Provider for AuroraProvider { | ||
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::Aurora | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl RateLimited for AuroraProvider { | ||
async fn is_rate_limited(&self, response: &mut Response) -> bool | ||
where | ||
Self: Sized, | ||
{ | ||
response.status() == http::StatusCode::TOO_MANY_REQUESTS | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl RpcProvider for AuroraProvider { | ||
#[tracing::instrument(skip(self, body), fields(provider = %self.provider_kind()))] | ||
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() { | ||
info!( | ||
"Strange: provider returned JSON RPC error, but status {status} is success: \ | ||
Aurora: {response:?}" | ||
); | ||
} | ||
} | ||
|
||
let mut response = (status, body).into_response(); | ||
response | ||
.headers_mut() | ||
.insert("Content-Type", HeaderValue::from_static("application/json")); | ||
Ok(response) | ||
} | ||
} | ||
|
||
impl RpcProviderFactory<AuroraConfig> for AuroraProvider { | ||
#[tracing::instrument] | ||
fn new(provider_config: &AuroraConfig) -> 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(); | ||
|
||
AuroraProvider { | ||
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,22 @@ | ||
use { | ||
super::check_if_rpc_is_responding_correctly_for_supported_chain, | ||
crate::context::ServerContext, | ||
test_context::test_context, | ||
}; | ||
|
||
#[test_context(ServerContext)] | ||
#[tokio::test] | ||
#[ignore] | ||
async fn aurora_provider(ctx: &mut ServerContext) { | ||
// Aurora Mainnet | ||
check_if_rpc_is_responding_correctly_for_supported_chain( | ||
ctx, | ||
"eip155:1313161554", | ||
"0x4e454152", | ||
) | ||
.await; | ||
|
||
// Aurora Testnet | ||
check_if_rpc_is_responding_correctly_for_supported_chain(ctx, "eip155:1313161555", "0x4e454153") | ||
.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