Skip to content

Commit

Permalink
Avoid skip_serializing_if since that breaks deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Aug 11, 2020
1 parent bf82b3c commit 8f277ea
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ pub fn parse_token(
},
is_initialized: account.is_initialized,
is_native: account.is_native,
delegated_amount: token_amount_to_ui_amount(account.delegated_amount, decimals),
delegated_amount: if account.delegate.is_none() {
None
} else {
Some(token_amount_to_ui_amount(
account.delegated_amount,
decimals,
))
},
}))
} else if data.len() == size_of::<Mint>() {
let mint: Mint = *unpack(&mut data)
Expand Down Expand Up @@ -102,8 +109,8 @@ pub struct UiTokenAccount {
pub delegate: Option<String>,
pub is_initialized: bool,
pub is_native: bool,
#[serde(skip_serializing_if = "UiTokenAmount::is_zero")]
pub delegated_amount: UiTokenAmount,
#[serde(skip_serializing_if = "Option::is_none")]
pub delegated_amount: Option<UiTokenAmount>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand All @@ -114,16 +121,6 @@ pub struct UiTokenAmount {
pub amount: StringAmount,
}

impl UiTokenAmount {
fn is_zero(&self) -> bool {
if let Ok(amount) = self.amount.parse::<u64>() {
amount == 0
} else {
false
}
}
}

pub fn token_amount_to_ui_amount(amount: u64, decimals: u8) -> UiTokenAmount {
// Use `amount_to_ui_amount()` once spl_token is bumped to a version that supports it: https://github.com/solana-labs/solana-program-library/pull/211
let amount_decimals = amount as f64 / 10_usize.pow(decimals as u32) as f64;
Expand Down Expand Up @@ -188,11 +185,7 @@ mod test {
delegate: None,
is_initialized: true,
is_native: false,
delegated_amount: UiTokenAmount {
ui_amount: 0.0,
decimals: 2,
amount: "0".to_string()
},
delegated_amount: None,
}),
);

Expand Down

0 comments on commit 8f277ea

Please sign in to comment.