From f483c55618683b9a137d7a3427e41210dc73c14d Mon Sep 17 00:00:00 2001 From: Paolo Tagliaferri Date: Wed, 11 Sep 2024 17:58:29 +0200 Subject: [PATCH] Fixed the deserialization of `get_account` responses The deserialization of the `core_liquid_balance` wasn't taking into account that such field could be omitted. --- crates/antelope/src/api/v1/structs.rs | 16 ++++++++++------ crates/antelope/src/chain/asset.rs | 26 ++++++++++++++++++++++++++ crates/antelope/tests/client.rs | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/crates/antelope/src/api/v1/structs.rs b/crates/antelope/src/api/v1/structs.rs index b6ab57b..0f5ffb8 100644 --- a/crates/antelope/src/api/v1/structs.rs +++ b/crates/antelope/src/api/v1/structs.rs @@ -11,7 +11,7 @@ use crate::chain::public_key::PublicKey; use crate::chain::signature::Signature; use crate::chain::{ action::{Action, PermissionLevel}, - asset::{deserialize_asset, Asset}, + asset::{deserialize_asset, deserialize_optional_asset, Asset}, authority::Authority, block_id::{deserialize_block_id, deserialize_optional_block_id, BlockId}, checksum::{deserialize_checksum256, Checksum160, Checksum256}, @@ -459,8 +459,12 @@ pub struct AccountObject { pub last_code_update: TimePoint, #[serde(deserialize_with = "deserialize_timepoint")] pub created: TimePoint, - #[serde(deserialize_with = "deserialize_asset")] - pub core_liquid_balance: Asset, + #[serde( + deserialize_with = "deserialize_optional_asset", + default, + skip_serializing_if = "Option::is_none" + )] + pub core_liquid_balance: Option, pub ram_quota: i64, pub net_weight: i64, pub cpu_weight: i64, @@ -954,7 +958,7 @@ where mod tests { use crate::api::v1::structs::AccountObject; -#[test] + #[test] fn deserialize_simple_account() { // This simple account response doesn't contain details about `total_resources`, `self_delegated_bandwidth`, // `refund_request`, `voter_info`, and `rex_info`. @@ -1037,8 +1041,8 @@ mod tests { "#; let res = serde_json::from_str::(&simple_account_json).unwrap(); - println!("{:#?}", res); -} + println!("{:#?}", res); + } #[test] fn deserialize_detailed_account() { diff --git a/crates/antelope/src/chain/asset.rs b/crates/antelope/src/chain/asset.rs index 390b03b..3faaf6d 100644 --- a/crates/antelope/src/chain/asset.rs +++ b/crates/antelope/src/chain/asset.rs @@ -431,6 +431,32 @@ where deserializer.deserialize_str(AssetVisitor) } +pub(crate) fn deserialize_optional_asset<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + struct OptionalAssetVisitor; + + impl<'de> de::Visitor<'de> for OptionalAssetVisitor { + type Value = Option; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str( + "an optional string representing an asset in the format 'amount symbol_code'", + ) + } + + fn visit_some(self, deserializer: D) -> Result + where + D: Deserializer<'de>, + { + Ok(Some(deserialize_asset(deserializer)?)) + } + } + + deserializer.deserialize_option(OptionalAssetVisitor) +} + #[derive(Copy, Clone, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct ExtendedAsset { quantity: Asset, diff --git a/crates/antelope/tests/client.rs b/crates/antelope/tests/client.rs index 46f71cd..cbe2c5a 100644 --- a/crates/antelope/tests/client.rs +++ b/crates/antelope/tests/client.rs @@ -150,7 +150,7 @@ async fn chan_get_account() { assert_eq!( account.core_liquid_balance, - Asset::from_string("128559.5000 TLOS") + Some(Asset::from_string("128559.5000 TLOS")) ); } Err(e) => {