-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dapp-portal support to zk_inception (#2659)
## What❔ This PR introduces a new `portal` subcommand to the `zk_inception` CLI tool, enabling users to easily launch the [dapp-portal](https://github.com/matter-labs/dapp-portal) for their deployed chains. Usage: `zk_inception portal [--port 3030]` The ecosystem configurations are automatically converted to the [hyperchains](https://github.com/matter-labs/dapp-portal/tree/main/hyperchains#%EF%B8%8F-configure-manually) format, which is used to configure dapp-portal at runtime. Essentially, the following command is executed under the hood: `docker run -p PORT:3000 /path/to/portal.config.js:/usr/src/app/dist/config.js dapp-portal` ## Why ❔ Currently, running the dapp-portal requires users to manually pull the repository, install all dependencies, modify configurations, build the project, and then run it - a tedious and time-consuming process. This PR simplifies the process, allowing users to run the portal effortlessly with a single command. ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [x] Tests for the changes have been added / updated. - [] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`.
- Loading branch information
1 parent
16dff4f
commit 835d2d3
Showing
17 changed files
with
397 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
|
@@ -25,4 +25,5 @@ mod wallets; | |
|
||
pub mod external_node; | ||
pub mod forge_interface; | ||
pub mod portal; | ||
pub mod traits; |
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,124 @@ | ||
use std::path::{Path, PathBuf}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use types::TokenInfo; | ||
use xshell::Shell; | ||
|
||
use crate::{ | ||
consts::{LOCAL_CONFIGS_PATH, PORTAL_CONFIG_FILE}, | ||
traits::{FileConfigWithDefaultName, ReadConfig, SaveConfig}, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PortalRuntimeConfig { | ||
pub node_type: String, | ||
pub hyperchains_config: HyperchainsConfig, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct HyperchainsConfig(pub Vec<HyperchainConfig>); | ||
|
||
impl HyperchainsConfig { | ||
pub fn is_empty(&self) -> bool { | ||
self.0.is_empty() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct HyperchainConfig { | ||
pub network: NetworkConfig, | ||
pub tokens: Vec<TokenConfig>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct NetworkConfig { | ||
pub id: u64, // L2 Network ID | ||
pub key: String, // L2 Network key | ||
pub name: String, // L2 Network name | ||
pub rpc_url: String, // L2 RPC URL | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub block_explorer_url: Option<String>, // L2 Block Explorer URL | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub block_explorer_api: Option<String>, // L2 Block Explorer API | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub public_l1_network_id: Option<u64>, // Ethereum Mainnet or Ethereum Sepolia Testnet ID | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub l1_network: Option<L1NetworkConfig>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct L1NetworkConfig { | ||
pub id: u64, | ||
pub name: String, | ||
pub network: String, | ||
pub native_currency: TokenInfo, | ||
pub rpc_urls: RpcUrls, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct RpcUrls { | ||
pub default: RpcUrlConfig, | ||
pub public: RpcUrlConfig, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct RpcUrlConfig { | ||
pub http: Vec<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct TokenConfig { | ||
pub address: String, | ||
pub symbol: String, | ||
pub decimals: u8, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub l1_address: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub name: Option<String>, | ||
} | ||
|
||
impl PortalRuntimeConfig { | ||
pub fn get_config_path(ecosystem_base_path: &Path) -> PathBuf { | ||
ecosystem_base_path | ||
.join(LOCAL_CONFIGS_PATH) | ||
.join(PORTAL_CONFIG_FILE) | ||
} | ||
} | ||
|
||
impl FileConfigWithDefaultName for PortalRuntimeConfig { | ||
const FILE_NAME: &'static str = PORTAL_CONFIG_FILE; | ||
} | ||
|
||
impl SaveConfig for PortalRuntimeConfig { | ||
fn save(&self, shell: &Shell, path: impl AsRef<Path>) -> anyhow::Result<()> { | ||
// The dapp-portal is served as a pre-built static app in a Docker image. | ||
// It uses a JavaScript file (config.js) that injects the configuration at runtime | ||
// by overwriting the '##runtimeConfig' property of the window object. | ||
// Therefore, we generate a JavaScript file instead of a JSON file. | ||
// This file will be mounted to the Docker image when it runs. | ||
let json = serde_json::to_string_pretty(&self)?; | ||
let config_js_content = format!("window['##runtimeConfig'] = {};", json); | ||
Ok(shell.write_file(path, config_js_content.as_bytes())?) | ||
} | ||
} | ||
|
||
impl ReadConfig for PortalRuntimeConfig { | ||
fn read(shell: &Shell, path: impl AsRef<Path>) -> anyhow::Result<Self> { | ||
let config_js_content = shell.read_file(path)?; | ||
// Extract the JSON part from the JavaScript file | ||
let json_start = config_js_content | ||
.find('{') | ||
.ok_or_else(|| anyhow::anyhow!("Invalid config file format"))?; | ||
let json_end = config_js_content | ||
.rfind('}') | ||
.ok_or_else(|| anyhow::anyhow!("Invalid config file format"))?; | ||
let json_str = &config_js_content[json_start..=json_end]; | ||
// Parse the JSON into PortalRuntimeConfig | ||
let config: PortalRuntimeConfig = serde_json::from_str(json_str)?; | ||
Ok(config) | ||
} | ||
} |
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 serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] | ||
pub struct TokenInfo { | ||
pub name: String, | ||
pub symbol: String, | ||
pub decimals: u8, | ||
} | ||
|
||
impl TokenInfo { | ||
pub fn eth() -> Self { | ||
Self { | ||
name: "Ether".to_string(), | ||
symbol: "ETH".to_string(), | ||
decimals: 18, | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub use containers::*; | ||
pub use portal::*; | ||
pub use run_server::*; | ||
pub use update::*; | ||
|
||
mod containers; | ||
mod portal; | ||
mod run_server; | ||
mod update; |
12 changes: 12 additions & 0 deletions
12
zk_toolbox/crates/zk_inception/src/commands/args/portal.rs
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,12 @@ | ||
use clap::Parser; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Parser)] | ||
pub struct PortalArgs { | ||
#[clap( | ||
long, | ||
default_value = "3030", | ||
help = "The port number for the portal app" | ||
)] | ||
pub port: u16, | ||
} |
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
Oops, something went wrong.