|
1 | 1 | use crate::core::contract_address::compute_hinted_class_hash; |
2 | 2 | use crate::services::api::contract_class_errors::ContractClassError; |
3 | | -use cairo_vm::felt::{Felt252, PRIME_STR}; |
4 | | -use cairo_vm::serde::deserialize_program::{ |
5 | | - deserialize_array_of_bigint_hex, deserialize_felt_hex, Attribute, BuiltinName, HintParams, |
6 | | - Identifier, ReferenceManager, |
7 | | -}; |
| 3 | +use cairo_vm::felt::Felt252; |
| 4 | +use cairo_vm::serde::deserialize_program::deserialize_felt_hex; |
8 | 5 | use cairo_vm::types::relocatable::MaybeRelocatable; |
9 | 6 | use cairo_vm::types::{errors::program_errors::ProgramError, program::Program}; |
10 | 7 | use core::str::FromStr; |
11 | 8 | use getset::{CopyGetters, Getters}; |
12 | 9 | use serde; |
13 | 10 | use serde::{Deserialize, Serialize}; |
14 | 11 | use serde_json::Value; |
15 | | -use starknet_api::deprecated_contract_class::{ |
16 | | - number_or_string, ContractClassAbiEntry, EntryPoint, |
17 | | -}; |
| 12 | +use starknet_api::deprecated_contract_class::{number_or_string, ContractClassAbiEntry}; |
18 | 13 | use std::collections::HashMap; |
19 | 14 | use std::path::Path; |
20 | 15 |
|
@@ -104,12 +99,6 @@ pub struct ContractClass { |
104 | 99 | pub(crate) abi: Option<AbiType>, |
105 | 100 | } |
106 | 101 |
|
107 | | -fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> { |
108 | | - use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson}; |
109 | | - let json = ProgramJson::deserialize(d)?; |
110 | | - Ok(parse_program_json(json, None).unwrap()) |
111 | | -} |
112 | | - |
113 | 102 | impl ContractClass { |
114 | 103 | pub fn new( |
115 | 104 | program_json: Value, |
@@ -207,72 +196,20 @@ impl FromStr for ContractClass { |
207 | 196 |
|
208 | 197 | /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON. |
209 | 198 | fn from_str(program_json: &str) -> Result<Self, ProgramError> { |
210 | | - let contract_class: starknet_api::deprecated_contract_class::ContractClass = |
211 | | - serde_json::from_str(program_json)?; |
212 | | - let program = to_cairo_runner_program(contract_class.program)?; |
213 | | - let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type); |
214 | 199 | let hinted_class_hash = |
215 | 200 | compute_hinted_class_hash(&serde_json::from_str(program_json)?).unwrap(); |
216 | | - Ok(ContractClass { |
217 | | - hinted_class_hash, |
218 | | - program, |
219 | | - entry_points_by_type, |
220 | | - abi: contract_class.abi, |
221 | | - }) |
| 201 | + Self::from_program_json_and_class_hash(program_json, hinted_class_hash) |
222 | 202 | } |
223 | 203 | } |
224 | 204 |
|
225 | 205 | // ------------------- |
226 | 206 | // Helper Functions |
227 | 207 | // ------------------- |
228 | 208 |
|
229 | | -pub(crate) fn convert_entry_points( |
230 | | - entry_points: HashMap<starknet_api::deprecated_contract_class::EntryPointType, Vec<EntryPoint>>, |
231 | | -) -> HashMap<EntryPointType, Vec<ContractEntryPoint>> { |
232 | | - let mut converted_entries: HashMap<EntryPointType, Vec<ContractEntryPoint>> = HashMap::new(); |
233 | | - for (entry_type, entry_points) in entry_points { |
234 | | - let en_type = entry_type.into(); |
235 | | - |
236 | | - let contracts_entry_points = entry_points |
237 | | - .into_iter() |
238 | | - .map(|e| { |
239 | | - let selector = Felt252::from_bytes_be(e.selector.0.bytes()); |
240 | | - let offset = e.offset.0; |
241 | | - ContractEntryPoint::new(selector, offset) |
242 | | - }) |
243 | | - .collect::<Vec<ContractEntryPoint>>(); |
244 | | - |
245 | | - converted_entries.insert(en_type, contracts_entry_points); |
246 | | - } |
247 | | - |
248 | | - converted_entries |
249 | | -} |
250 | | - |
251 | | -pub(crate) fn to_cairo_runner_program( |
252 | | - program: starknet_api::deprecated_contract_class::Program, |
253 | | -) -> Result<Program, ProgramError> { |
254 | | - let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?; |
255 | | - |
256 | | - if program.prime != *PRIME_STR { |
257 | | - return Err(ProgramError::PrimeDiffers(program.prime.to_string())); |
258 | | - }; |
259 | | - |
260 | | - let mut error_message_attributes = |
261 | | - serde_json::from_value::<Vec<Attribute>>(program.attributes).unwrap_or_default(); |
262 | | - error_message_attributes.retain(|attr| attr.name == "error_message"); |
263 | | - |
264 | | - let program = Program::new( |
265 | | - serde_json::from_value::<Vec<BuiltinName>>(program.builtins)?, |
266 | | - deserialize_array_of_bigint_hex(program.data)?, |
267 | | - None, |
268 | | - serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?, |
269 | | - serde_json::from_value::<ReferenceManager>(program.reference_manager)?, |
270 | | - identifiers, |
271 | | - error_message_attributes, |
272 | | - None, |
273 | | - )?; |
274 | | - |
275 | | - Ok(program) |
| 209 | +fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> { |
| 210 | + use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson}; |
| 211 | + let json = ProgramJson::deserialize(d)?; |
| 212 | + Ok(parse_program_json(json, None).unwrap()) |
276 | 213 | } |
277 | 214 |
|
278 | 215 | #[cfg(test)] |
|
0 commit comments