Skip to content

Commit

Permalink
Return converted (rebased) amount of tokens from `get_token_account_b…
Browse files Browse the repository at this point in the history
…alance` RPC
  • Loading branch information
vmarkushin committed Dec 2, 2024
1 parent 63f36e1 commit 592b8c8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
12 changes: 9 additions & 3 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ pub struct UiTokenAmount {
pub decimals: u8,
pub amount: StringAmount,
pub ui_amount_string: StringDecimals,
#[serde(skip_serializing_if = "Option::is_none")]
pub converted_ui_amount: Option<f64>,
}

impl UiTokenAmount {
Expand Down Expand Up @@ -273,6 +275,7 @@ pub fn token_amount_to_ui_amount(amount: u64, decimals: u8) -> UiTokenAmount {
decimals,
amount: amount.to_string(),
ui_amount_string: real_number_string_trimmed(amount, decimals),
converted_ui_amount: None,
}
}

Expand Down Expand Up @@ -340,7 +343,8 @@ mod test {
ui_amount: Some(0.42),
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string()
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
},
delegate: None,
state: UiAccountState::Initialized,
Expand Down Expand Up @@ -546,7 +550,8 @@ mod test {
ui_amount: Some(0.42),
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string()
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
},
delegate: None,
state: UiAccountState::Initialized,
Expand Down Expand Up @@ -582,7 +587,8 @@ mod test {
ui_amount: Some(0.42),
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string()
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
},
delegate: None,
state: UiAccountState::Initialized,
Expand Down
2 changes: 2 additions & 0 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10161,6 +10161,7 @@ pub mod tests {
decimals: 1,
amount: "11".to_string(),
ui_amount_string: "1.1".to_string(),
converted_ui_amount: None,
},
owner: Pubkey::new_unique().to_string(),
program_id: Pubkey::new_unique().to_string(),
Expand All @@ -10173,6 +10174,7 @@ pub mod tests {
decimals: 1,
amount: "11".to_string(),
ui_amount_string: "1.1".to_string(),
converted_ui_amount: None,
},
owner: Pubkey::new_unique().to_string(),
program_id: Pubkey::new_unique().to_string(),
Expand Down
2 changes: 2 additions & 0 deletions ledger/src/token_balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ mod test {
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
},
program_id: spl_token::id().to_string(),
})
Expand Down Expand Up @@ -479,6 +480,7 @@ mod test {
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
},
program_id: spl_token_2022::id().to_string(),
})
Expand Down
20 changes: 19 additions & 1 deletion rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The `rpc` module implements the Solana RPC interface.
use solana_account_decoder::parse_token::real_number_string_trimmed;
use spl_token_mantis::state::MintWithRebase;
use {
crate::{
Expand Down Expand Up @@ -2038,8 +2039,23 @@ impl JsonRpcRequestProcessor {
.map_err(|_| Error::invalid_params("Invalid param: not a Token account".to_string()))?;
let mint = &Pubkey::from_str(&token_account.base.mint.to_string())
.expect("Token account mint should be convertible to Pubkey");
let mint_account = bank
.get_account(mint)
.ok_or_else(|| Error::internal_error())?;
let mint_with_rebase = MintWithRebase::unpack_maybe_not_rebase(mint_account.data())
.map_err(|_| Error::internal_error())?;
let original_amount = token_account.base.amount;
let (_, decimals) = get_mint_owner_and_decimals(&bank, mint)?;
let balance = token_amount_to_ui_amount(token_account.base.amount, decimals);
let mut balance = token_amount_to_ui_amount(original_amount, decimals);

let converted_amount = mint_with_rebase
.unrebased_amount(original_amount)
.unwrap_or(original_amount);
let converted_amount_decimals = 10_usize
.checked_pow(decimals as u32)
.map(|dividend| converted_amount as f64 / dividend as f64);
balance.converted_ui_amount = converted_amount_decimals;

Ok(new_response(&bank, balance))
}

Expand Down Expand Up @@ -9052,6 +9068,7 @@ pub mod tests {
decimals: 2,
amount: "42".to_string(),
ui_amount_string: "0.42".to_string(),
converted_ui_amount: None,
}
},
RpcTokenAccountBalance {
Expand All @@ -9061,6 +9078,7 @@ pub mod tests {
decimals: 2,
amount: "10".to_string(),
ui_amount_string: "0.1".to_string(),
converted_ui_amount: None,
}
}
]
Expand Down
1 change: 1 addition & 0 deletions storage-proto/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ impl From<generated::TokenBalance> for TransactionTokenBalance {
ui_token_amount.decimals as u8,
)
},
converted_ui_amount: None,
},
owner: value.owner,
program_id: value.program_id,
Expand Down
1 change: 1 addition & 0 deletions storage-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl From<StoredTokenAmount> for UiTokenAmount {
decimals,
amount,
ui_amount_string,
converted_ui_amount: None,
}
}
}
Expand Down

0 comments on commit 592b8c8

Please sign in to comment.