Skip to content

Commit

Permalink
Omit delegate and delegatedAmount when none
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Aug 8, 2020
1 parent df43967 commit 33f888b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub is_initialized: bool,
pub is_native: bool,
#[serde(skip_serializing_if = "UiTokenAmount::is_none")]
pub delegated_amount: UiTokenAmount,
}

Expand All @@ -112,6 +114,16 @@ pub struct UiTokenAmount {
pub amount: StringAmount,
}

impl UiTokenAmount {
fn is_none(&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

0 comments on commit 33f888b

Please sign in to comment.