Adapt RpcClient to recent token method changes (#11519)
* Avoid skip_serializing_if since that breaks deserialization * Adapt RpcClient to recent token method changes
This commit is contained in:
@ -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)
|
||||
@ -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)]
|
||||
@ -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;
|
||||
@ -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,
|
||||
}),
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user