Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::core::{
use crate::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use crate::hash::{PoseidonHash, StarkHash};
use crate::rpc_transaction::EntryPointByType;
#[cfg(any(test, feature = "testing"))]
use crate::test_utils::py_json_dumps;
use crate::{impl_from_through_intermediate, StarknetApiError, StarknetApiResult};

pub type DeclaredClasses = IndexMap<ClassHash, SierraContractClass>;
Expand Down Expand Up @@ -311,7 +313,10 @@ impl From<cairo_lang_starknet_classes::contract_class::ContractClass> for Sierra
.collect(),
contract_class_version: cairo_lang_contract_class.contract_class_version,
entry_points_by_type: cairo_lang_contract_class.entry_points_by_type.into(),
abi: cairo_lang_contract_class.abi.map(|abi| abi.json()).unwrap_or_default(),
abi: cairo_lang_contract_class
.abi
.map(|abi| py_json_dumps(&abi).expect("ABI is valid JSON"))
.unwrap_or_default(),
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,54 @@ impl ContractClass {
ContractClass::V1((default_casm, SierraVersion::default()))
}
}

/// Formats a json object in the same way that python's json.dumps() formats.
pub(crate) struct PyJsonFormatter;

impl PyJsonFormatter {
pub(crate) fn comma() -> &'static [u8; 2] {
b", "
}

pub(crate) fn colon() -> &'static [u8; 2] {
b": "
}
}

impl serde_json::ser::Formatter for PyJsonFormatter {
fn begin_array_value<W: ?Sized + std::io::Write>(
&mut self,
writer: &mut W,
first: bool,
) -> std::io::Result<()> {
if !first {
writer.write_all(Self::comma())?;
}
Ok(())
}

fn begin_object_key<W: ?Sized + std::io::Write>(
&mut self,
writer: &mut W,
first: bool,
) -> std::io::Result<()> {
if !first {
writer.write_all(Self::comma())?;
}
Ok(())
}

fn begin_object_value<W: ?Sized + std::io::Write>(
&mut self,
writer: &mut W,
) -> std::io::Result<()> {
writer.write_all(Self::colon())
}
}

pub(crate) fn py_json_dumps<T: ?Sized + Serialize>(value: &T) -> Result<String, serde_json::Error> {
let mut string_buffer = vec![];
let mut ser = serde_json::Serializer::with_formatter(&mut string_buffer, PyJsonFormatter);
value.serialize(&mut ser)?;
Ok(String::from_utf8(string_buffer).expect("serialized JSON should be valid UTF-8"))
}
Loading