Skip to content

Commit

Permalink
fix: serialization ReceiptBlock (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron authored Jun 18, 2024
1 parent 4dbc169 commit 36ec1d6
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions starknet-core/src/types/receipt_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

use crate::serde::unsigned_field_element::UfeHex;
use starknet_types_core::felt::Felt;

/// A more idiomatic way to access `execution_status` and `revert_reason`.
Expand Down Expand Up @@ -48,21 +49,22 @@ impl ReceiptBlock {
}
}

#[serde_as]
#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "no_unknown_fields", serde(deny_unknown_fields))]
struct Raw {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde_as(as = "Option<UfeHex>")]
block_hash: Option<Felt>,
#[serde(skip_serializing_if = "Option::is_none")]
block_number: Option<u64>,
}

impl Serialize for ReceiptBlock {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
#[serde_as]
struct Raw<'a> {
#[serde_as(as = "Option<UfeHex>")]
#[serde(skip_serializing_if = "Option::is_none")]
block_hash: Option<&'a Felt>,
#[serde(skip_serializing_if = "Option::is_none")]
block_number: Option<&'a u64>,
}

let raw = match self {
Self::Pending => Raw {
block_hash: None,
Expand All @@ -72,8 +74,8 @@ impl Serialize for ReceiptBlock {
block_hash,
block_number,
} => Raw {
block_hash: Some(block_hash),
block_number: Some(block_number),
block_hash: Some(*block_hash),
block_number: Some(*block_number),
},
};

Expand All @@ -86,17 +88,6 @@ impl<'de> Deserialize<'de> for ReceiptBlock {
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde_as]
#[cfg_attr(feature = "no_unknown_fields", serde(deny_unknown_fields))]
struct Raw {
#[serde_as(as = "Option<UfeHex>")]
#[serde(skip_serializing_if = "Option::is_none")]
block_hash: Option<Felt>,
#[serde(skip_serializing_if = "Option::is_none")]
block_number: Option<u64>,
}

let raw = Raw::deserialize(deserializer)?;

match (raw.block_hash, raw.block_number) {
Expand Down

0 comments on commit 36ec1d6

Please sign in to comment.