Skip to content
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(katana): chain spec file management tools #2945

Merged
merged 4 commits into from
Jan 23, 2025
Merged
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
42 changes: 38 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bin/katana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ cainome.workspace = true
clap.workspace = true
clap_complete.workspace = true
comfy-table = "7.1.1"
dirs = "5.0.1"
dojo-utils.workspace = true
inquire = "0.7.5"
lazy_static.workspace = true
Expand Down
66 changes: 7 additions & 59 deletions bin/katana/src/cli/init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
mod deployment;

use std::fmt::Display;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{Context, Result};
use clap::Args;
use inquire::{Confirm, CustomType, Select};
use katana_chain_spec::{DEV_UNALLOCATED, SettlementLayer};
use katana_chain_spec::{SettlementLayer, DEV_UNALLOCATED};
use katana_primitives::chain::ChainId;
use katana_primitives::genesis::Genesis;
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::Genesis;
use katana_primitives::{ContractAddress, Felt};
use lazy_static::lazy_static;
use starknet::accounts::{ExecutionEncoding, SingleOwnerAccount};
Expand All @@ -26,11 +23,7 @@
const CARTRIDGE_SN_SEPOLIA_PROVIDER: &str = "https://api.cartridge.gg/x/starknet/sepolia";

#[derive(Debug, Args)]
pub struct InitArgs {
/// The path to where the config file will be written at.
#[arg(value_name = "PATH")]
pub output_path: Option<PathBuf>,
}
pub struct InitArgs;

impl InitArgs {
// TODO:
Expand All @@ -52,7 +45,9 @@
chain_spec.id = ChainId::parse(&input.id)?;
chain_spec.settlement = Some(settlement);

chain_spec.store(input.output_path)
katana_chain_spec::file::write(&chain_spec).context("failed to write chain spec file")?;

Check warning on line 48 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L48

Added line #L48 was not covered by tests

Ok(())

Check warning on line 50 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L50

Added line #L50 was not covered by tests
}

fn prompt(&self, rt: &Runtime) -> Result<PromptOutcome> {
Expand Down Expand Up @@ -86,7 +81,7 @@
SettlementChainOpt::Custom,
];

let network_type = Select::new("Select settlement chain", network_opts).prompt()?;
let network_type = Select::new("Settlement chain", network_opts).prompt()?;

Check warning on line 84 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L84

Added line #L84 was not covered by tests

let settlement_url = match network_type {
SettlementChainOpt::Sepolia => Url::parse(CARTRIDGE_SN_SEPOLIA_PROVIDER)?,
Expand Down Expand Up @@ -155,22 +150,12 @@
.prompt()?
};

let output_path = if let Some(path) = self.output_path.clone() {
path
} else {
CustomType::<Path>::new("Output path")
.with_default(config_path(&chain_id).map(Path)?)
.prompt()?
.0
};

Ok(PromptOutcome {
account: account_address,
settlement_contract,
settlement_id: parse_cairo_short_string(&l1_chain_id)?,
id: chain_id,
rpc_url: settlement_url,
output_path,
})
}
}
Expand All @@ -191,43 +176,6 @@
rpc_url: Url,

settlement_contract: ContractAddress,

// path at which the config file will be written at.
output_path: PathBuf,
}

// > CONFIG_DIR/$chain_id/config.json
fn config_path(id: &str) -> Result<PathBuf> {
Ok(config_dir(id)?.join("config").with_extension("json"))
}

fn config_dir(id: &str) -> Result<PathBuf> {
const KATANA_DIR: &str = "katana";

let _ = cairo_short_string_to_felt(id).context("Invalid id");
let path = dirs::config_local_dir().context("unsupported OS")?.join(KATANA_DIR).join(id);

if !path.exists() {
fs::create_dir_all(&path).expect("failed to create config directory");
}

Ok(path)
}

#[derive(Debug, Clone)]
struct Path(PathBuf);

impl FromStr for Path {
type Err = <PathBuf as FromStr>::Err;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
PathBuf::from_str(s).map(Self)
}
}

impl Display for Path {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.display())
}
}

lazy_static! {
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ lazy_static.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet.workspace = true
thiserror.workspace = true
url.workspace = true
dirs = "6.0.0"
toml.workspace = true

[dev-dependencies]
similar-asserts.workspace = true
Expand Down
Loading
Loading