Skip to content

feat: added Etherlink #983

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SUPPORTED_CHAINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ Chain name with associated `chainId` query param to use.
| Ethereum Holesky | eip155:17000 |
| Arbitrum | eip155:42161 |
| Celo | eip155:42220 |
| Etherlink | eip155:42793 |
| Avalanche Fuji Testnet <sup>[1](#footnote1)</sup> | eip155:43113 |
| Avalanche C-Chain | eip155:43114 |
| Linea <sup>[1](#footnote1)</sup> | eip155:59144 |
| Polygon Amoy <sup>[1](#footnote1)</sup> | eip155:80002 |
| Berachain bArtio <sup>[1](#footnote1)</sup> | eip155:80084 |
| Base Sepolia | eip155:84532 |
| Etherlink Testnet | eip155:128123 |
| Arbitrum Sepolia | eip155:421614 |
| Scroll Mainnet <sup>[1](#footnote1)</sup> | eip155:534352 |
| Scroll Sepolia Testnet <sup>[1](#footnote1)</sup> | eip155:534351 |
Expand Down
55 changes: 55 additions & 0 deletions src/env/etherlink.rs
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 EtherlinkConfig {
pub supported_chains: HashMap<String, (String, Weight)>,
}

impl Default for EtherlinkConfig {
fn default() -> Self {
Self {
supported_chains: default_supported_chains(),
}
}
}

impl ProviderConfig for EtherlinkConfig {
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::Etherlink
}
}

fn default_supported_chains() -> HashMap<String, (String, Weight)> {
// Keep in-sync with SUPPORTED_CHAINS.md

HashMap::from([
// Etherlink Mainnet
(
"eip155:42793".into(),
(
"https://node.mainnet.etherlink.com".into(),
Weight::new(Priority::Normal).unwrap(),
),
),
// Etherlink Testnet
(
"eip155:128123".into(),
(
"https://node.ghostnet.etherlink.com".into(),
Weight::new(Priority::Normal).unwrap(),
),
),
])
}
3 changes: 2 additions & 1 deletion src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use {
};
pub use {
allnodes::*, arbitrum::*, aurora::*, base::*, berachain::*, binance::*, drpc::*, dune::*,
getblock::*, infura::*, lava::*, mantle::*, morph::*, near::*, odyssey::*, pokt::*,
etherlink::*, getblock::*, infura::*, lava::*, mantle::*, morph::*, near::*, odyssey::*, pokt::*,
publicnode::*, quicknode::*, server::*, solscan::*, syndica::*, unichain::*, wemix::*,
zerion::*, zksync::*, zora::*,
};
Expand All @@ -28,6 +28,7 @@ mod berachain;
mod binance;
mod drpc;
mod dune;
mod etherlink;
mod getblock;
mod infura;
mod lava;
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use {
},
env::{
AllnodesConfig, ArbitrumConfig, AuroraConfig, BaseConfig, BerachainConfig, BinanceConfig,
DrpcConfig, DuneConfig, GetBlockConfig, InfuraConfig, LavaConfig, MantleConfig,
DrpcConfig, DuneConfig, EtherlinkConfig, GetBlockConfig, InfuraConfig, LavaConfig, MantleConfig,
MorphConfig, NearConfig, OdysseyConfig, PoktConfig, PublicnodeConfig, QuicknodeConfig,
SolScanConfig, SyndicaConfig, UnichainConfig, WemixConfig, ZKSyncConfig, ZerionConfig,
ZoraConfig,
Expand All @@ -33,7 +33,7 @@ use {
hyper::{header::HeaderName, http, server::conn::AddrIncoming, Body, Server},
providers::{
AllnodesProvider, ArbitrumProvider, AuroraProvider, BaseProvider, BerachainProvider,
BinanceProvider, DrpcProvider, DuneProvider, GetBlockProvider, InfuraProvider,
BinanceProvider, DrpcProvider, DuneProvider, EtherlinkProvider, GetBlockProvider, InfuraProvider,
InfuraWsProvider, LavaProvider, MantleProvider, MorphProvider, NearProvider,
OdysseyProvider, PoktProvider, ProviderRepository, PublicnodeProvider, QuicknodeProvider,
SolScanProvider, SyndicaProvider, UnichainProvider, WemixProvider, ZKSyncProvider,
Expand Down Expand Up @@ -526,6 +526,7 @@ fn init_providers(config: &ProvidersConfig) -> ProviderRepository {

providers.add_rpc_provider::<BaseProvider, BaseConfig>(BaseConfig::default());
providers.add_rpc_provider::<BinanceProvider, BinanceConfig>(BinanceConfig::default());
providers.add_rpc_provider::<EtherlinkProvider, EtherlinkConfig>(EtherlinkConfig::default());
providers.add_rpc_provider::<ZKSyncProvider, ZKSyncConfig>(ZKSyncConfig::default());
providers.add_rpc_provider::<PublicnodeProvider, PublicnodeConfig>(PublicnodeConfig::default());
providers.add_rpc_provider::<QuicknodeProvider, QuicknodeConfig>(QuicknodeConfig::new(
Expand Down
74 changes: 74 additions & 0 deletions src/providers/etherlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use {
super::{Provider, ProviderKind, RateLimited, RpcProvider, RpcProviderFactory},
crate::{
env::EtherlinkConfig,
error::{RpcError, RpcResult},
},
async_trait::async_trait,
axum::response::{IntoResponse, Response},
hyper::{client::HttpConnector, http, Client, Method},
hyper_tls::HttpsConnector,
std::collections::HashMap,
tracing::debug,
};

#[derive(Debug)]
pub struct EtherlinkProvider {
pub client: Client<HttpsConnector<HttpConnector>>,
pub supported_chains: HashMap<String, String>,
}

impl Provider for EtherlinkProvider {
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::Etherlink
}
}

#[async_trait]
impl RateLimited for EtherlinkProvider {
async fn is_rate_limited(&self, response: &mut Response) -> bool {
response.status() == http::StatusCode::TOO_MANY_REQUESTS
}
}

#[async_trait]
impl RpcProvider for EtherlinkProvider {
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?.into_response();
}
}

impl RpcProviderFactory<EtherlinkConfig> for EtherlinkProvider {
fn new(provider_config: &EtherlinkConfig) -> 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();

EtherlinkProvider {
client: forward_proxy_client,
supported_chains,
}
}
}
5 changes: 5 additions & 0 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod bungee;
mod coinbase;
mod drpc;
mod dune;
mod etherlink;
mod getblock;
mod infura;
mod lava;
Expand Down Expand Up @@ -105,6 +106,7 @@ pub use {
bungee::BungeeProvider,
drpc::DrpcProvider,
dune::DuneProvider,
etherlink::EtherlinkProvider,
getblock::GetBlockProvider,
infura::{InfuraProvider, InfuraWsProvider},
lava::LavaProvider,
Expand Down Expand Up @@ -660,6 +662,7 @@ pub enum ProviderKind {
Binance,
Berachain,
Bungee,
Etherlink,
ZKSync,
Publicnode,
Base,
Expand Down Expand Up @@ -699,6 +702,7 @@ impl Display for ProviderKind {
ProviderKind::Berachain => "Berachain",
ProviderKind::Wemix => "Wemix",
ProviderKind::Bungee => "Bungee",
ProviderKind::Etherlink => "Etherlink",
ProviderKind::ZKSync => "zkSync",
ProviderKind::Publicnode => "Publicnode",
ProviderKind::Base => "Base",
Expand Down Expand Up @@ -737,6 +741,7 @@ impl ProviderKind {
"Binance" => Some(Self::Binance),
"Berachain" => Some(Self::Berachain),
"Bungee" => Some(Self::Bungee),
"Etherlink" => Some(Self::Etherlink),
"zkSync" => Some(Self::ZKSync),
"Publicnode" => Some(Self::Publicnode),
"Base" => Some(Self::Base),
Expand Down
28 changes: 28 additions & 0 deletions tests/functional/http/etherlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use {
super::check_if_rpc_is_responding_correctly_for_supported_chain,
rpc_proxy::providers::ProviderKind,
crate::context::ServerContext,
test_context::test_context,
};

#[test_context(ServerContext)]
#[tokio::test]
#[ignore]
async fn etherlink_provider_eip155_42793(ctx: &mut ServerContext) {
// Etherlink Mainnet
check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Etherlink,
"eip155:42793",
"0xa729",
)
.await;

check_if_rpc_is_responding_correctly_for_supported_chain(
ctx,
&ProviderKind::Etherlink,
"eip155:128123",
"0x1f47b",
)
.await;
}
1 change: 1 addition & 0 deletions tests/functional/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) mod aurora;
pub(crate) mod base;
pub(crate) mod berachain;
pub(crate) mod binance;
pub(crate) mod etherlink;
pub(crate) mod drpc;
pub(crate) mod getblock;
pub(crate) mod infura;
Expand Down