Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 483883c

Browse files
Add from_program_json_and_class_hash (#836)
* Add from_program_json_and_class_hash * Use include_str * Remove unused reference * Add docs * Update docs --------- Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent 6805c61 commit 483883c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/services/api/contract_class_errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ pub enum ContractClassError {
1313
NotACasmContractClass,
1414
#[error("Not a deprecated contract class")]
1515
NotADeprecatedContractClass,
16+
#[error("Parse error")]
17+
ParseError,
18+
#[error("Program error: {0}")]
19+
ProgramError(String),
1620
}

src/services/api/contract_classes/deprecated_contract_class.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,36 @@ impl ContractClass {
128128
})
129129
}
130130

131+
/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON and a class hash.
132+
///
133+
/// This constructor avoids the need to recompute the class hash from the program JSON,
134+
/// but it does not verify that the given class hash is correct.
135+
///
136+
/// This could return a wrong [`ContractClass`] if the given class hash is wrong.
137+
///
138+
/// # Parameters
139+
/// - `program_json`: The JSON of the compiled Cairo 0 program.
140+
/// - `hinted_class_hash`: The class hash of the program.
141+
///
142+
/// # Returns
143+
/// A [`ContractClass`] parsed from the given JSON and class hash.
144+
pub fn from_program_json_and_class_hash(
145+
program_json: &str,
146+
hinted_class_hash: Felt252,
147+
) -> Result<Self, ContractClassError> {
148+
let contract_class: starknet_api::deprecated_contract_class::ContractClass =
149+
serde_json::from_str(program_json).map_err(|_| ContractClassError::ParseError)?;
150+
let program = to_cairo_runner_program(contract_class.program)
151+
.map_err(|e| ContractClassError::ProgramError(e.to_string()))?;
152+
let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
153+
Ok(ContractClass {
154+
hinted_class_hash,
155+
program,
156+
entry_points_by_type,
157+
abi: contract_class.abi,
158+
})
159+
}
160+
131161
/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON
132162
/// at the given file path.
133163
pub fn from_path<F>(path: F) -> Result<Self, ProgramError>
@@ -393,4 +423,22 @@ mod tests {
393423

394424
res.expect("should be able to read file");
395425
}
426+
427+
#[test]
428+
fn test_from_program_json_and_class_hash_should_equal_from_path() {
429+
let program_json = include_str!("../../../../starknet_programs/fibonacci.json");
430+
let contract_class_from_path = ContractClass::from_path("starknet_programs/fibonacci.json")
431+
.expect("should be able to read file");
432+
433+
let contract_class_from_program_json_and_class_hash =
434+
ContractClass::from_program_json_and_class_hash(
435+
program_json,
436+
contract_class_from_path.hinted_class_hash.clone(),
437+
)
438+
.expect("should be able to read file");
439+
assert_eq!(
440+
contract_class_from_path,
441+
contract_class_from_program_json_and_class_hash
442+
);
443+
}
396444
}

0 commit comments

Comments
 (0)