Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/services/api/contract_class_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ pub enum ContractClassError {
NotACasmContractClass,
#[error("Not a deprecated contract class")]
NotADeprecatedContractClass,
#[error("Parse error")]
ParseError,
#[error("Program error: {0}")]
ProgramError(String),
}
37 changes: 37 additions & 0 deletions src/services/api/contract_classes/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ impl ContractClass {
})
}

pub fn from_program_json_and_class_hash(
program_json: &str,
hinted_class_hash: Felt252,
) -> Result<Self, ContractClassError> {
let contract_class: starknet_api::deprecated_contract_class::ContractClass =
serde_json::from_str(program_json).map_err(|_| ContractClassError::ParseError)?;
let program = to_cairo_runner_program(contract_class.program)
.map_err(|e| ContractClassError::ProgramError(e.to_string()))?;
let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
Ok(ContractClass {
hinted_class_hash,
program,
entry_points_by_type,
abi: contract_class.abi,
})
}

/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON
/// at the given file path.
pub fn from_path<F>(path: F) -> Result<Self, ProgramError>
Expand Down Expand Up @@ -230,6 +247,8 @@ pub(crate) fn to_cairo_runner_program(

#[cfg(test)]
mod tests {
use std::fs;

use crate::core::contract_address::compute_deprecated_class_hash;

use super::*;
Expand Down Expand Up @@ -393,4 +412,22 @@ mod tests {

res.expect("should be able to read file");
}

#[test]
fn test_from_program_json_and_class_hash_should_equal_from_path() {
let path = "starknet_programs/fibonacci.json";
let contract_class_from_path =
ContractClass::from_path(path).expect("should be able to read file");
let program_json = fs::read_to_string(path).expect("should be able to read file");
let contract_class_from_program_json_and_class_hash =
ContractClass::from_program_json_and_class_hash(
&program_json,
contract_class_from_path.hinted_class_hash.clone(),
)
.expect("should be able to read file");
assert_eq!(
contract_class_from_path,
contract_class_from_program_json_and_class_hash
);
}
}