Skip to content

Commit

Permalink
Return token amounts as floats (bp #11370) (#11378)
Browse files Browse the repository at this point in the history
* Return token amounts as floats (#11370)

* Return token amounts as floats

* Floating-point equality

* Return float and raw token amounts

* Fix decimals and token rpcs for native-mint tokens

* Fixup docs and review comments

(cherry picked from commit 86e3f96)

# Conflicts:
#	core/src/rpc.rs

* Fix conflicts

Co-authored-by: Tyera Eulberg <[email protected]>
Co-authored-by: Tyera Eulberg <[email protected]>
  • Loading branch information
3 people authored Aug 5, 2020
1 parent c14f98c commit 80525ac
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 61 deletions.
9 changes: 8 additions & 1 deletion account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ use spl_token_v1_0::{
};
use std::{mem::size_of, str::FromStr};

// A helper function to convert spl_token_v1_0::id() as spl_sdk::pubkey::Pubkey to solana_sdk::pubkey::Pubkey
// A helper function to convert spl_token_v1_0::id() as spl_sdk::pubkey::Pubkey to
// solana_sdk::pubkey::Pubkey
pub fn spl_token_id_v1_0() -> Pubkey {
Pubkey::from_str(&spl_token_v1_0::id().to_string()).unwrap()
}

// A helper function to convert spl_token_v1_0::native_mint::id() as spl_sdk::pubkey::Pubkey to
// solana_sdk::pubkey::Pubkey
pub fn spl_token_v1_0_native_mint() -> Pubkey {
Pubkey::from_str(&spl_token_v1_0::native_mint::id().to_string()).unwrap()
}

pub fn parse_token(data: &[u8]) -> Result<TokenAccountType, ParseAccountError> {
let mut data = data.to_vec();
if data.len() == size_of::<Account>() {
Expand Down
8 changes: 4 additions & 4 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ impl RpcClient {
})
}

pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<u64> {
pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<RpcTokenAmount> {
Ok(self
.get_token_account_balance_with_commitment(pubkey, CommitmentConfig::default())?
.value)
Expand All @@ -752,7 +752,7 @@ impl RpcClient {
&self,
pubkey: &Pubkey,
commitment_config: CommitmentConfig,
) -> RpcResult<u64> {
) -> RpcResult<RpcTokenAmount> {
self.send(
RpcRequest::GetTokenAccountBalance,
json!([pubkey.to_string(), commitment_config]),
Expand Down Expand Up @@ -849,7 +849,7 @@ impl RpcClient {
})
}

pub fn get_token_supply(&self, mint: &Pubkey) -> ClientResult<u64> {
pub fn get_token_supply(&self, mint: &Pubkey) -> ClientResult<RpcTokenAmount> {
Ok(self
.get_token_supply_with_commitment(mint, CommitmentConfig::default())?
.value)
Expand All @@ -859,7 +859,7 @@ impl RpcClient {
&self,
mint: &Pubkey,
commitment_config: CommitmentConfig,
) -> RpcResult<u64> {
) -> RpcResult<RpcTokenAmount> {
self.send(
RpcRequest::GetTokenSupply,
json!([mint.to_string(), commitment_config]),
Expand Down
12 changes: 11 additions & 1 deletion client/src/rpc_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_sdk::{
use std::{collections::HashMap, net::SocketAddr};

pub type RpcResult<T> = client_error::Result<Response<T>>;
pub type RpcAmount = String;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RpcResponseContext {
Expand Down Expand Up @@ -220,9 +221,18 @@ pub struct RpcStakeActivation {
pub inactive: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RpcTokenAmount {
pub ui_amount: f64,
pub decimals: u8,
pub amount: RpcAmount,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RpcTokenAccountBalance {
pub address: String,
pub amount: u64,
#[serde(flatten)]
pub amount: RpcTokenAmount,
}
Loading

0 comments on commit 80525ac

Please sign in to comment.