Skip to content

Commit

Permalink
feat: add SCALE Codec for DeprecatedContractClass
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Feb 20, 2024
1 parent ff92c50 commit 24d321a
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -31,6 +20,28 @@ pub struct ContractClass {
pub entry_points_by_type: IndexMap<EntryPointType, Vec<EntryPoint>>,
}

// TODO find a smarter way than using JSON
// Start refactoring with `Program` struct
impl Encode for ContractClass {
fn encode(&self) -> Vec<u8> {
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<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let json_repr = <String>::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)]
Expand Down

0 comments on commit 24d321a

Please sign in to comment.