-
Notifications
You must be signed in to change notification settings - Fork 235
Feat(sncast): Add utility for calculating a contract's class hash #3651
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
Merged
cptartur
merged 39 commits into
foundry-rs:master
from
naijauser:3584-sncast-util-calculate-contract-class-hash
Aug 28, 2025
Merged
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
bd1f3a7
setup class hash command and response
a2cd991
complete logic to get class hash
7d5971d
start implementing ClassHash command logic
6b81895
add skip_compile to ClassHash
01374aa
complete class hash logic
f34a208
rename command
9e480f2
remove unused import
f0a6431
remove redundant lines
a60d9e0
create e2e file for class_hash
272ccc2
remove skip_compile command and load_artifacts separation
79af91d
remove unused import
c69f980
update response name
887573f
add e2e test test_happy_case_get_class_hash
388fd93
update changelog
1391500
Merge branch 'master' into 3584-sncast-util-calculate-contract-class-…
ed1b2af
add class_hash.md docs
ce9abb4
move class hash to utils
edd6847
update docs
006590a
remove unnecessary clone
2c40021
Update CHANGELOG.md
naijauser 7a5b8e3
move class_hash to utils
44398b9
move class_hash to utils
d59ff24
clean up success message reporting
cea602a
update success response
25764dc
fix test
6aea513
update class-hash docs
aad4c7a
Update crates/sncast/src/response/class_hash.rs
naijauser ba2a64b
Update crates/sncast/src/response/class_hash.rs
naijauser 9b7982d
Update crates/sncast/src/response/class_hash.rs
naijauser 6939ec2
clippy: borrow
7050672
resolve lint warnings
3b85143
update test assert
b2c5873
fix padded felt test
942501f
Update docs/src/appendix/sncast/utils/class_hash.md
naijauser a17efae
update doc contract name
9d8d2c6
update test assertion
aabdc5a
Merge branch 'master' into 3584-sncast-util-calculate-contract-class-…
naijauser 7d63af3
fmt
8fa30e1
add class-hash to SUMMARY.md
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,67 @@ | ||
use conversions::{padded_felt::PaddedFelt, serde::serialize::CairoSerialize, string::IntoHexStr}; | ||
use foundry_ui::{Message, styling}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
|
||
use crate::response::{cast_message::SncastMessage, command::CommandResponse}; | ||
|
||
#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)] | ||
pub struct ClassHashGeneratedResponse { | ||
pub class_hash: PaddedFelt, | ||
} | ||
|
||
impl CommandResponse for ClassHashGeneratedResponse {} | ||
|
||
impl Message for SncastMessage<ClassHashGeneratedResponse> { | ||
fn text(&self) -> String { | ||
styling::OutputBuilder::new() | ||
.success_message("Class hash generated") | ||
.blank_line() | ||
.field( | ||
"Class Hash", | ||
&self.command_response.class_hash.into_hex_string(), | ||
) | ||
.build() | ||
} | ||
|
||
fn json(&self) -> serde_json::Value { | ||
serde_json::to_value(&self.command_response).unwrap_or_else(|err| { | ||
json!({ | ||
"error": "Failed to serialize response", | ||
"command": self.command, | ||
"details": err.to_string() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)] | ||
#[serde(tag = "status")] | ||
pub enum ClassHashResponse { | ||
naijauser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[serde(untagged)] | ||
Success(ClassHashGeneratedResponse), | ||
} | ||
|
||
impl CommandResponse for ClassHashResponse {} | ||
|
||
impl Message for SncastMessage<ClassHashResponse> { | ||
fn text(&self) -> String { | ||
match &self.command_response { | ||
ClassHashResponse::Success(response) => styling::OutputBuilder::new() | ||
.success_message("Class hash generated") | ||
naijauser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.blank_line() | ||
.field("Class Hash", &response.class_hash.into_hex_string()) | ||
.build(), | ||
} | ||
} | ||
|
||
fn json(&self) -> serde_json::Value { | ||
serde_json::to_value(&self.command_response).unwrap_or_else(|err| { | ||
json!({ | ||
"error": "Failed to serialize response", | ||
"command": self.command, | ||
"details": err.to_string() | ||
}) | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,47 @@ | ||
use anyhow::Context; | ||
use clap::Args; | ||
use conversions::{IntoConv, byte_array::ByteArray}; | ||
use scarb_api::StarknetContractArtifacts; | ||
use sncast::{ | ||
ErrorData, | ||
response::{ | ||
class_hash::{ClassHashGeneratedResponse, ClassHashResponse}, | ||
errors::StarknetCommandError, | ||
}, | ||
}; | ||
use starknet::core::types::contract::SierraClass; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Args)] | ||
#[command(about = "Generate the class hash of a contract", long_about = None)] | ||
pub struct ClassHash { | ||
/// Contract name | ||
#[arg(short = 'c', long = "contract-name")] | ||
pub contract: String, | ||
|
||
/// Specifies scarb package to be used | ||
#[arg(long)] | ||
pub package: Option<String>, | ||
} | ||
|
||
pub fn get_class_hash( | ||
class_hash: ClassHash, | ||
artifacts: &HashMap<String, StarknetContractArtifacts>, | ||
) -> Result<ClassHashResponse, StarknetCommandError> { | ||
let contract_artifacts = artifacts.get(&class_hash.contract).ok_or( | ||
StarknetCommandError::ContractArtifactsNotFound(ErrorData { | ||
data: ByteArray::from(class_hash.contract.as_str()), | ||
}), | ||
)?; | ||
|
||
let contract_definition: SierraClass = serde_json::from_str(&contract_artifacts.sierra) | ||
.context("Failed to parse sierra artifact")?; | ||
|
||
let class_hash = contract_definition | ||
.class_hash() | ||
.map_err(anyhow::Error::from)?; | ||
|
||
Ok(ClassHashResponse::Success(ClassHashGeneratedResponse { | ||
class_hash: class_hash.into_(), | ||
})) | ||
} |
This file contains hidden or 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 hidden or 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,29 @@ | ||
use crate::helpers::{ | ||
constants::CONTRACTS_DIR, fixtures::duplicate_contract_directory_with_salt, runner::runner, | ||
}; | ||
use indoc::indoc; | ||
use shared::test_utils::output_assert::assert_stdout_contains; | ||
|
||
#[test] | ||
fn test_happy_case_get_class_hash() { | ||
let contract_path = duplicate_contract_directory_with_salt( | ||
CONTRACTS_DIR.to_string() + "/map", | ||
"put", | ||
"human_readable", | ||
); | ||
|
||
let args = vec!["class-hash", "--contract-name", "Map"]; | ||
|
||
let snapbox = runner(&args).current_dir(contract_path.path()); | ||
|
||
let output = snapbox.assert().success(); | ||
|
||
assert_stdout_contains( | ||
output, | ||
indoc! {r" | ||
Success: Class hash generated | ||
|
||
Class Hash: 0x6ee50dd7f45bd73b5e0e6f3c32013529f31477af0103c4c593915b6c1d6eefe | ||
naijauser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"}, | ||
); | ||
} |
This file contains hidden or 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,5 +1,6 @@ | ||
mod account; | ||
mod call; | ||
mod class_hash; | ||
mod completions; | ||
mod declare; | ||
mod deploy; | ||
|
This file contains hidden or 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,25 @@ | ||
# Calculate contract class hash | ||
|
||
## Overview | ||
Use `sncast`'s `class-hash` command to calculate the class hash of a contract. | ||
|
||
## Examples | ||
|
||
### General Example | ||
|
||
```shell | ||
$ sncast \ | ||
class-hash \ | ||
--contract-name CheatcodeChecker | ||
``` | ||
|
||
<details> | ||
<summary>Output:</summary> | ||
|
||
```shell | ||
Success: Class Hash generated | ||
|
||
Class Hash: 0x0[..] | ||
``` | ||
</details> | ||
<br> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.