Skip to content

Commit 266fcb1

Browse files
starknet_api: fix compiler->SNAPI contract class ABI string conversion for tests
1 parent e875064 commit 266fcb1

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

crates/starknet_api/src/state.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::core::{
2525
use crate::deprecated_contract_class::ContractClass as DeprecatedContractClass;
2626
use crate::hash::{PoseidonHash, StarkHash};
2727
use crate::rpc_transaction::EntryPointByType;
28+
#[cfg(any(test, feature = "testing"))]
29+
use crate::test_utils::py_json_dumps;
2830
use crate::{impl_from_through_intermediate, StarknetApiError, StarknetApiResult};
2931

3032
pub type DeclaredClasses = IndexMap<ClassHash, SierraContractClass>;
@@ -311,7 +313,10 @@ impl From<cairo_lang_starknet_classes::contract_class::ContractClass> for Sierra
311313
.collect(),
312314
contract_class_version: cairo_lang_contract_class.contract_class_version,
313315
entry_points_by_type: cairo_lang_contract_class.entry_points_by_type.into(),
314-
abi: cairo_lang_contract_class.abi.map(|abi| abi.json()).unwrap_or_default(),
316+
abi: cairo_lang_contract_class
317+
.abi
318+
.map(|abi| py_json_dumps(&abi).expect("ABI is valid JSON"))
319+
.unwrap_or_default(),
315320
}
316321
}
317322
}

crates/starknet_api/src/test_utils.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,54 @@ impl ContractClass {
212212
ContractClass::V1((default_casm, SierraVersion::default()))
213213
}
214214
}
215+
216+
/// Formats a json object in the same way that python's json.dumps() formats.
217+
pub(crate) struct PyJsonFormatter;
218+
219+
impl PyJsonFormatter {
220+
pub(crate) fn comma() -> &'static [u8; 2] {
221+
b", "
222+
}
223+
224+
pub(crate) fn colon() -> &'static [u8; 2] {
225+
b": "
226+
}
227+
}
228+
229+
impl serde_json::ser::Formatter for PyJsonFormatter {
230+
fn begin_array_value<W: ?Sized + std::io::Write>(
231+
&mut self,
232+
writer: &mut W,
233+
first: bool,
234+
) -> std::io::Result<()> {
235+
if !first {
236+
writer.write_all(Self::comma())?;
237+
}
238+
Ok(())
239+
}
240+
241+
fn begin_object_key<W: ?Sized + std::io::Write>(
242+
&mut self,
243+
writer: &mut W,
244+
first: bool,
245+
) -> std::io::Result<()> {
246+
if !first {
247+
writer.write_all(Self::comma())?;
248+
}
249+
Ok(())
250+
}
251+
252+
fn begin_object_value<W: ?Sized + std::io::Write>(
253+
&mut self,
254+
writer: &mut W,
255+
) -> std::io::Result<()> {
256+
writer.write_all(Self::colon())
257+
}
258+
}
259+
260+
pub(crate) fn py_json_dumps<T: ?Sized + Serialize>(value: &T) -> Result<String, serde_json::Error> {
261+
let mut string_buffer = vec![];
262+
let mut ser = serde_json::Serializer::with_formatter(&mut string_buffer, PyJsonFormatter);
263+
value.serialize(&mut ser)?;
264+
Ok(String::from_utf8(string_buffer).expect("serialized JSON should be valid UTF-8"))
265+
}

0 commit comments

Comments
 (0)