diff --git a/account-decoder/src/parse_token.rs b/account-decoder/src/parse_token.rs index 92419b155ed268..6f56e931c9b279 100644 --- a/account-decoder/src/parse_token.rs +++ b/account-decoder/src/parse_token.rs @@ -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::() { let mint: Mint = *unpack(&mut data) @@ -102,8 +109,8 @@ pub struct UiTokenAccount { pub delegate: Option, 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, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] @@ -114,16 +121,6 @@ pub struct UiTokenAmount { pub amount: StringAmount, } -impl UiTokenAmount { - fn is_zero(&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;