From 592b8c835890a78abd255d104506badd05196797 Mon Sep 17 00:00:00 2001 From: Vladislav Markushin Date: Mon, 2 Dec 2024 12:49:06 -0300 Subject: [PATCH] Return converted (rebased) amount of tokens from `get_token_account_balance` RPC --- account-decoder/src/parse_token.rs | 12 +++++++++--- ledger/src/blockstore.rs | 2 ++ ledger/src/token_balances.rs | 2 ++ rpc/src/rpc.rs | 20 +++++++++++++++++++- storage-proto/src/convert.rs | 1 + storage-proto/src/lib.rs | 1 + 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/account-decoder/src/parse_token.rs b/account-decoder/src/parse_token.rs index 8f699b04dd..7c14809a86 100644 --- a/account-decoder/src/parse_token.rs +++ b/account-decoder/src/parse_token.rs @@ -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, } impl UiTokenAmount { @@ -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, } } @@ -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, @@ -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, @@ -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, diff --git a/ledger/src/blockstore.rs b/ledger/src/blockstore.rs index 9e10a4548f..1b755243e9 100644 --- a/ledger/src/blockstore.rs +++ b/ledger/src/blockstore.rs @@ -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(), @@ -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(), diff --git a/ledger/src/token_balances.rs b/ledger/src/token_balances.rs index 9f151f401e..f79a802b61 100644 --- a/ledger/src/token_balances.rs +++ b/ledger/src/token_balances.rs @@ -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(), }) @@ -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(), }) diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index 0f54909216..6074aa029f 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -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::{ @@ -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)) } @@ -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 { @@ -9061,6 +9078,7 @@ pub mod tests { decimals: 2, amount: "10".to_string(), ui_amount_string: "0.1".to_string(), + converted_ui_amount: None, } } ] diff --git a/storage-proto/src/convert.rs b/storage-proto/src/convert.rs index e907095194..493a269677 100644 --- a/storage-proto/src/convert.rs +++ b/storage-proto/src/convert.rs @@ -596,6 +596,7 @@ impl From for TransactionTokenBalance { ui_token_amount.decimals as u8, ) }, + converted_ui_amount: None, }, owner: value.owner, program_id: value.program_id, diff --git a/storage-proto/src/lib.rs b/storage-proto/src/lib.rs index 0832690f1e..d12d891738 100644 --- a/storage-proto/src/lib.rs +++ b/storage-proto/src/lib.rs @@ -89,6 +89,7 @@ impl From for UiTokenAmount { decimals, amount, ui_amount_string, + converted_ui_amount: None, } } }