From 24d321a8e44e3ac2789e02242c353b81b3659d51 Mon Sep 17 00:00:00 2001 From: Daniel Bigos Date: Mon, 19 Feb 2024 17:14:57 +0100 Subject: [PATCH] feat: add SCALE Codec for DeprecatedContractClass Refers: https://github.com/keep-starknet-strange/madara/issues/1430 --- src/deprecated_contract_class.rs | 35 +++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/deprecated_contract_class.rs b/src/deprecated_contract_class.rs index b0af9695..fcfb5c04 100644 --- a/src/deprecated_contract_class.rs +++ b/src/deprecated_contract_class.rs @@ -10,18 +10,7 @@ use crate::serde_utils::deserialize_optional_contract_class_abi_entry_vector; use crate::StarknetApiError; /// A deprecated contract class. -#[derive( - Debug, - Clone, - Default, - Eq, - PartialEq, - Deserialize, - Serialize, - // TODO - // Encode, - // Decode -)] +#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)] pub struct ContractClass { // Starknet does not verify the abi. If we can't parse it, we set it to None. #[serde(default, deserialize_with = "deserialize_optional_contract_class_abi_entry_vector")] @@ -31,6 +20,28 @@ pub struct ContractClass { pub entry_points_by_type: IndexMap>, } +// TODO find a smarter way than using JSON +// Start refactoring with `Program` struct +impl Encode for ContractClass { + fn encode(&self) -> Vec { + let json_repr: String = json!(self).to_string(); + json_repr.encode() + } +} + +// TODO find a smarter way than using JSON +// Start refactoring with `Program` struct +impl Decode for ContractClass { + fn decode( + input: &mut I, + ) -> Result { + let json_repr = ::decode(input)?; + serde_json::from_str(&json_repr).map_err(|_e| { + parity_scale_codec::Error::from("serde_json deserialization error for ContractClass") + }) + } +} + /// A [ContractClass](`crate::deprecated_contract_class::ContractClass`) abi entry. #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Encode, Decode)] #[serde(deny_unknown_fields)]