Skip to content

Commit

Permalink
feat: add json output
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnordelo committed Jan 30, 2024
1 parent 353813d commit 933f1ca
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 37 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ doc = false
anyhow = "1.0.79"
camino = "1.1.6"
clap = { version = "4.4.18", features = ["derive"] }
num = "0.4.1"
prettytable = "0.10.0"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "c974e5cb42e8d8344cee910b76005ec46b4dd3ed" }
serde = "1.0.196"
serde_json = { version = "1.0.99", features = ["preserve_order"] }
num = "0.4.1"
111 changes: 79 additions & 32 deletions src/bin/class_hash/commands/get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use anyhow::{Ok, Result};
use clap::Parser;
use num::bigint::BigInt;
Expand All @@ -7,60 +9,105 @@ use crate::commands::CliCommand;
use class_hash::artifacts::get_artifacts;
use class_hash::get_class_hashes;
use class_hash::scarb::{clean, compile, get_scarb_manifest, print_compiler_versions};
use class_hash::types::{Contract, Contracts};

#[derive(Parser, Debug)]
pub struct Get {
#[clap(
short,
long,
default_value_t = true,
help = "Compile the project before computing the hashes"
action,
help = "Avoid compiling the project before computing the hashes"
)]
pub compile: bool,
pub no_compile: bool,

#[clap(long, action, help = "Outputs the class hashes in JSON format")]
pub json: bool,
}

impl CliCommand for Get {
/// Get the class hashes from the project contracts.
fn run(&self) -> Result<()> {
let _ = get_scarb_manifest()?;

if self.compile {
if !self.no_compile {
clean()?;
compile()?;
}

let artifacts = get_artifacts()?;
let class_hashes = get_class_hashes(artifacts)?;

println!();
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
table.set_titles(row![bFg->"Contract:", bFg->"Class hashes:"]);

for (i, artifact) in class_hashes.iter().enumerate() {
if i > 0 {
table.add_empty_row();
}
let name = artifact.0;
let hashes = artifact.1;
let sierra = if hashes.0.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.0.parse::<BigInt>()?)
};
let casm = if hashes.1.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.1.parse::<BigInt>()?)
};
table.add_row(row![bFy->name, format!("Sierra: {}", sierra)]);
table.add_row(row!["", format!("Casm: {}", casm)]);
if self.json {
print_json_output(class_hashes)?;
} else {
print_table_output(class_hashes)?;
}
table.printstd();

println!();
print_compiler_versions()?;

Ok(())
}
}

/// Print the class hashes in JSON format.
fn print_json_output(class_hashes: HashMap<String, (String, String)>) -> Result<()> {
println!();

let mut contracts = Contracts {
contracts: Vec::new(),
};

for artifact in class_hashes.iter() {
let name = artifact.0;
let hashes = artifact.1;
let sierra = if hashes.0.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.0.parse::<BigInt>()?)
};
let casm = if hashes.1.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.1.parse::<BigInt>()?)
};
contracts
.contracts
.push(Contract::new(name.to_string(), sierra, casm));
}

println!("{}", serde_json::to_string_pretty(&contracts)?);

Ok(())
}

/// Print the class hashes in table format.
fn print_table_output(class_hashes: HashMap<String, (String, String)>) -> Result<()> {
println!();
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
table.set_titles(row![bFg->"Contract:", bFg->"Class hashes:"]);

for (i, artifact) in class_hashes.iter().enumerate() {
if i > 0 {
table.add_empty_row();
}
let name = artifact.0;
let hashes = artifact.1;
let sierra = if hashes.0.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.0.parse::<BigInt>()?)
};
let casm = if hashes.1.is_empty() {
"_".to_string()
} else {
format!("0x{:x}", hashes.1.parse::<BigInt>()?)
};
table.add_row(row![bFy->name, format!("Sierra: {}", sierra)]);
table.add_row(row!["", format!("Casm: {}", casm)]);
}
table.printstd();

println!();
print_compiler_versions()?;

Ok(())
}
1 change: 1 addition & 0 deletions src/class_hash/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod artifacts;
pub mod scarb;
pub mod types;

use anyhow::{Ok, Result};
use artifacts::Artifacts;
Expand Down
19 changes: 19 additions & 0 deletions src/class_hash/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::Serialize;

#[derive(Serialize)]
pub struct Contract {
name: String,
sierra: String,
casm: String,
}

#[derive(Serialize)]
pub struct Contracts {
pub contracts: Vec<Contract>,
}

impl Contract {
pub fn new(name: String, sierra: String, casm: String) -> Self {
Self { name, sierra, casm }
}
}

0 comments on commit 933f1ca

Please sign in to comment.