Skip to content

Commit

Permalink
feat: add abi command
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Jul 17, 2024
1 parent 0f3493f commit 5334056
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ regex = "1.8.4"
rpassword = "7.2.0"
serde = { version = "1.0.164", features = ["derive"] }
serde_json = { version = "1.0.99", features = ["preserve_order"] }
serde_json_pythonic = { version = "0.1.2", default-features = false, features = ["alloc", "raw_value"] }
serde_with = "2.3.3"
sha2 = "0.10.8"
shellexpand = "3.1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ enum Subcommands {
Selector(Selector),
#[clap(about = "Calculate class hash from any contract artifacts (Sierra, casm, legacy)")]
ClassHash(ClassHash),
#[clap(about = "Extract contract ABI from a class artifact (Sierra or legacy)")]
Abi(Abi),
#[clap(about = "Encode string into felt with the Cairo short string representation")]
ToCairoString(ToCairoString),
#[clap(about = "Decode string from felt with the Cairo short string representation")]
Expand Down Expand Up @@ -179,6 +181,7 @@ async fn run_command(cli: Cli) -> Result<()> {
(false, Some(command)) => match command {
Subcommands::Selector(cmd) => cmd.run(),
Subcommands::ClassHash(cmd) => cmd.run(),
Subcommands::Abi(cmd) => cmd.run(),
Subcommands::ToCairoString(cmd) => cmd.run(),
Subcommands::ParseCairoString(cmd) => cmd.run(),
Subcommands::Mont(cmd) => cmd.run(),
Expand Down
74 changes: 74 additions & 0 deletions src/subcommands/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;
use serde_json_pythonic::to_string_pythonic;
use starknet::core::types::contract::{legacy::LegacyContractClass, CompiledClass, SierraClass};

use crate::{path::ExpandedPathbufParser, utils::print_colored_json};

#[derive(Debug, Parser)]
pub struct Abi {
#[clap(
long,
help = "Present the ABI as a flattened string in a Pythoic style"
)]
flatten: bool,
#[clap(
long,
help = "When --flatten is used, serialize the ABI in the Pythoic style instead of compact"
)]
pythonic: bool,
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to contract artifact file"
)]
file: PathBuf,
}

impl Abi {
pub fn run(self) -> Result<()> {
if self.pythonic && !self.flatten {
anyhow::bail!("--pythonic can only be used with --flatten");
}

// Working around a deserialization bug in `starknet-rs`:
// https://github.com/xJonathanLEI/starknet-rs/issues/392

if let Ok(class) =
serde_json::from_reader::<_, SierraClass>(std::fs::File::open(&self.file)?)
{
let abi = class.abi;
if self.flatten {
if self.pythonic {
println!("{}", to_string_pythonic(&abi)?);
} else {
println!("{}", serde_json::to_string(&abi)?);
}
} else {
print_colored_json(&abi)?;
}
} else if let Ok(class) =
serde_json::from_reader::<_, LegacyContractClass>(std::fs::File::open(&self.file)?)
{
let abi = class.abi;
if self.flatten {
if self.pythonic {
println!("{}", to_string_pythonic(&abi)?);
} else {
println!("{}", serde_json::to_string(&abi)?);
}
} else {
print_colored_json(&abi)?;
}
} else if serde_json::from_reader::<_, CompiledClass>(std::fs::File::open(self.file)?)
.is_ok()
{
anyhow::bail!("cannot extract ABI from casm");
} else {
anyhow::bail!("failed to parse contract artifact");
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ pub use block_traces::BlockTraces;

mod transaction_status;
pub use transaction_status::TransactionStatus;

mod abi;
pub use abi::Abi;

0 comments on commit 5334056

Please sign in to comment.