From 33f888b44935820362bc33bc0fe86969e9a93b01 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Sat, 8 Aug 2020 11:51:02 -0600 Subject: [PATCH] Omit delegate and delegatedAmount when none --- account-decoder/src/parse_token.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/account-decoder/src/parse_token.rs b/account-decoder/src/parse_token.rs index 9bd58fca757d26..025331fd2926c1 100644 --- a/account-decoder/src/parse_token.rs +++ b/account-decoder/src/parse_token.rs @@ -98,9 +98,11 @@ pub struct UiTokenAccount { pub mint: String, pub owner: String, pub token_amount: UiTokenAmount, + #[serde(skip_serializing_if = "Option::is_none")] pub delegate: Option, pub is_initialized: bool, pub is_native: bool, + #[serde(skip_serializing_if = "UiTokenAmount::is_none")] pub delegated_amount: UiTokenAmount, } @@ -112,6 +114,16 @@ pub struct UiTokenAmount { pub amount: StringAmount, } +impl UiTokenAmount { + fn is_none(&self) -> bool { + if let Ok(amount) = self.amount.parse::() { + 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;