Skip to content

Commit

Permalink
Fixed the deserialization of get_account responses
Browse files Browse the repository at this point in the history
The deserialization of the `core_liquid_balance` wasn't taking into account that such field could be omitted.
  • Loading branch information
ptagl committed Sep 11, 2024
1 parent 7a4fc47 commit f483c55
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
16 changes: 10 additions & 6 deletions crates/antelope/src/api/v1/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<Asset>,
pub ram_quota: i64,
pub net_weight: i64,
pub cpu_weight: i64,
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -1037,8 +1041,8 @@ mod tests {
"#;

let res = serde_json::from_str::<AccountObject>(&simple_account_json).unwrap();
println!("{:#?}", res);
}
println!("{:#?}", res);
}

#[test]
fn deserialize_detailed_account() {
Expand Down
26 changes: 26 additions & 0 deletions crates/antelope/src/chain/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,32 @@ where
deserializer.deserialize_str(AssetVisitor)
}

pub(crate) fn deserialize_optional_asset<'de, D>(deserializer: D) -> Result<Option<Asset>, D::Error>
where
D: Deserializer<'de>,
{
struct OptionalAssetVisitor;

impl<'de> de::Visitor<'de> for OptionalAssetVisitor {
type Value = Option<Asset>;

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<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
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,
Expand Down
2 changes: 1 addition & 1 deletion crates/antelope/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit f483c55

Please sign in to comment.