-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(providers): adding the Aurora native mainnet and testnet RPC #469
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the priority
HIGH
by default for the native endpoint.