Skip to content

Commit

Permalink
Return float and raw token amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Aug 5, 2020
1 parent 8f51ed6 commit afc5269
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
10 changes: 9 additions & 1 deletion client/src/rpc_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,17 @@ pub struct RpcStakeActivation {
pub inactive: u64,
}

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

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RpcTokenAccountBalance {
pub address: String,
pub amount: f64,
#[serde(flatten)]
pub amount: RpcTokenAmount,
}
22 changes: 13 additions & 9 deletions core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ impl JsonRpcRequestProcessor {
&self,
pubkey: &Pubkey,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>> {
) -> Result<RpcResponse<RpcTokenAmount>> {
let bank = self.bank(commitment);
let account = bank.get_account(pubkey).ok_or_else(|| {
Error::invalid_params("Invalid param: could not find account".to_string())
Expand Down Expand Up @@ -871,7 +871,7 @@ impl JsonRpcRequestProcessor {
&self,
mint: &Pubkey,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>> {
) -> Result<RpcResponse<RpcTokenAmount>> {
let bank = self.bank(commitment);
let mint_account = bank.get_account(mint).ok_or_else(|| {
Error::invalid_params("Invalid param: could not find mint".to_string())
Expand Down Expand Up @@ -943,7 +943,7 @@ impl JsonRpcRequestProcessor {
}
})
.collect();
token_balances.sort_by(|a, b| a.amount.partial_cmp(&b.amount).unwrap().reverse());
token_balances.sort_by(|a, b| a.amount.raw_amount.cmp(&b.amount.raw_amount).reverse());
token_balances.truncate(NUM_LARGEST_ACCOUNTS);
Ok(new_response(&bank, token_balances))
}
Expand Down Expand Up @@ -1121,8 +1121,12 @@ fn get_mint_decimals(data: &[u8]) -> Result<u8> {
.map(|mint: &mut Mint| mint.decimals)
}

fn token_amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
amount as f64 / 10_usize.pow(decimals as u32) as f64
fn token_amount_to_ui_amount(amount: u64, decimals: u8) -> RpcTokenAmount {
let amount_decimals = amount as f64 / 10_usize.pow(decimals as u32) as f64;
RpcTokenAmount {
amount: amount_decimals,
raw_amount: amount,
}
}

#[rpc]
Expand Down Expand Up @@ -1408,15 +1412,15 @@ pub trait RpcSol {
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>>;
) -> Result<RpcResponse<RpcTokenAmount>>;

#[rpc(meta, name = "getTokenSupply")]
fn get_token_supply(
&self,
meta: Self::Metadata,
mint_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>>;
) -> Result<RpcResponse<RpcTokenAmount>>;

#[rpc(meta, name = "getTokenLargestAccounts")]
fn get_token_largest_accounts(
Expand Down Expand Up @@ -2041,7 +2045,7 @@ impl RpcSol for RpcSolImpl {
meta: Self::Metadata,
pubkey_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>> {
) -> Result<RpcResponse<RpcTokenAmount>> {
debug!(
"get_token_account_balance rpc request received: {:?}",
pubkey_str
Expand All @@ -2055,7 +2059,7 @@ impl RpcSol for RpcSolImpl {
meta: Self::Metadata,
mint_str: String,
commitment: Option<CommitmentConfig>,
) -> Result<RpcResponse<f64>> {
) -> Result<RpcResponse<RpcTokenAmount>> {
debug!("get_token_supply rpc request received: {:?}", mint_str);
let mint = verify_pubkey(mint_str)?;
meta.get_token_supply(&mint, commitment)
Expand Down
16 changes: 11 additions & 5 deletions docs/src/apps/jsonrpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,15 +1031,18 @@ Returns the token balance of an SPL Token account.
#### Results:
- `RpcResponse<f64>` - RpcResponse JSON object with `value` field set to the balance, using mint-prescribed decimals
The result will be an RpcResponse JSON object with `value` equal to a JSON object containing:
- `amount: <f64>` - the balance, using mint-prescribed decimals
- `rawAmount: <u64>` - the raw balance without decimals
#### Example:
```bash
// Request
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenAccountBalance", "params": ["7fUAJdStEuGbc3sM84cKRL6yYaaSstyLSU4ve5oovLS7"]}' http://localhost:8899
// Result
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":98.64,"id":1}
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":{"amount":98.64,"rawAmount":9864},"id":1}
```
### getTokenAccountsByDelegate
Expand Down Expand Up @@ -1125,16 +1128,19 @@ Returns the total supply of an SPL Token type.
#### Results:
- `RpcResponse<f64>` - RpcResponse JSON object with `value` field set to the total token supply, using mint-prescribed decimals
The result will be an RpcResponse JSON object with `value` equal to a JSON object containing:
- `amount: <f64>` - the total token supply, using mint-prescribed decimals
- `rawAmount: <u64>` - the raw total token supply without decimals
#### Example:
```bash
// Request
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getTokenSupply", "params": ["3wyAj7Rt1TWVPZVteFJPLa26JmLvdb1CAKEFZm3NY75E"]}' http://localhost:8899
// Result
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":1000.0,"id":1}
```
{"jsonrpc":"2.0","result":{"context":{"slot":1114},"value":{"amount":1000.0,"rawAmount":100000},"id":1}
```}
### getTransactionCount
Expand Down

0 comments on commit afc5269

Please sign in to comment.