diff --git a/core/node/node_framework/src/implementations/layers/main_node_client.rs b/core/node/node_framework/src/implementations/layers/main_node_client.rs new file mode 100644 index 000000000000..80e5d44c350f --- /dev/null +++ b/core/node/node_framework/src/implementations/layers/main_node_client.rs @@ -0,0 +1,48 @@ +use std::num::NonZeroUsize; + +use anyhow::Context; +use zksync_types::{url::SensitiveUrl, L2ChainId}; +use zksync_web3_decl::client::{Client, DynClient, L2}; + +use crate::{ + implementations::resources::main_node_client::MainNodeClientResource, + service::ServiceContext, + wiring_layer::{WiringError, WiringLayer}, +}; + +#[derive(Debug)] +pub struct MainNodeClientLayer { + url: SensitiveUrl, + rate_limit_rps: NonZeroUsize, + l2_chain_id: L2ChainId, +} + +impl MainNodeClientLayer { + pub fn new(url: SensitiveUrl, rate_limit_rps: NonZeroUsize, l2_chain_id: L2ChainId) -> Self { + Self { + url, + rate_limit_rps, + l2_chain_id, + } + } +} + +#[async_trait::async_trait] +impl WiringLayer for MainNodeClientLayer { + fn layer_name(&self) -> &'static str { + "main_node_client_layer" + } + + async fn wire(self: Box, mut context: ServiceContext<'_>) -> Result<(), WiringError> { + let main_node_client = Client::http(self.url) + .context("failed creating JSON-RPC client for main node")? + .for_network(self.l2_chain_id.into()) + .with_allowed_requests_per_second(self.rate_limit_rps) + .build(); + + context.insert_resource(MainNodeClientResource( + Box::new(main_node_client) as Box> + ))?; + Ok(()) + } +} diff --git a/core/node/node_framework/src/implementations/layers/mod.rs b/core/node/node_framework/src/implementations/layers/mod.rs index da6e76377d1f..43b1f77e88c8 100644 --- a/core/node/node_framework/src/implementations/layers/mod.rs +++ b/core/node/node_framework/src/implementations/layers/mod.rs @@ -8,6 +8,7 @@ pub mod eth_watch; pub mod healtcheck_server; pub mod house_keeper; pub mod l1_gas; +pub mod main_node_client; pub mod metadata_calculator; pub mod object_store; pub mod pk_signing_eth_client;