Skip to content

Commit

Permalink
feat(katana): chain spec file management tools (#2945)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Jan 23, 2025
1 parent 1af1789 commit 01111f4
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 130 deletions.
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 @@ use tokio::runtime::Runtime;
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 @@ impl InitArgs {
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")?;

Ok(())
}

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

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

let settlement_url = match network_type {
SettlementChainOpt::Sepolia => Url::parse(CARTRIDGE_SN_SEPOLIA_PROVIDER)?,
Expand Down Expand Up @@ -155,22 +150,12 @@ impl InitArgs {
.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 @@ struct PromptOutcome {
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

0 comments on commit 01111f4

Please sign in to comment.