Skip to content

Commit

Permalink
get_token_accounts_by_owner now returns UiTokenAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Aug 4, 2020
1 parent 8c873e2 commit 9e9872c
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use bincode::serialize;
use indicatif::{ProgressBar, ProgressStyle};
use log::*;
use serde_json::{json, Value};
use solana_account_decoder::UiAccount;
use solana_account_decoder::{
parse_token::{parse_token, TokenAccountType, UiTokenAccount},
UiAccount,
};
use solana_sdk::{
account::Account,
clock::{
Expand Down Expand Up @@ -650,6 +653,33 @@ impl RpcClient {
Ok(hash)
}

pub fn get_token_account(&self, pubkey: &Pubkey) -> ClientResult<Option<UiTokenAccount>> {
Ok(self
.get_token_account_with_commitment(pubkey, CommitmentConfig::default())?
.value)
}

pub fn get_token_account_with_commitment(
&self,
pubkey: &Pubkey,
commitment_config: CommitmentConfig,
) -> RpcResult<Option<UiTokenAccount>> {
let Response {
context,
value: account,
} = self.get_account_with_commitment(pubkey, commitment_config)?;

Ok(Response {
context,
value: account
.map(|account| match parse_token(&account.data) {
Ok(TokenAccountType::Account(ui_token_account)) => Some(ui_token_account),
_ => None,
})
.flatten(),
})
}

pub fn get_token_account_balance(&self, pubkey: &Pubkey) -> ClientResult<u64> {
Ok(self
.get_token_account_balance_with_commitment(pubkey, CommitmentConfig::default())?
Expand All @@ -671,7 +701,7 @@ impl RpcClient {
&self,
delegate: &Pubkey,
token_account_filter: TokenAccountsFilter,
) -> ClientResult<Vec<(Pubkey, Account)>> {
) -> ClientResult<Vec<(Pubkey, UiTokenAccount)>> {
Ok(self
.get_token_accounts_by_delegate_with_commitment(
delegate,
Expand All @@ -686,7 +716,7 @@ impl RpcClient {
delegate: &Pubkey,
token_account_filter: TokenAccountsFilter,
commitment_config: CommitmentConfig,
) -> RpcResult<Vec<(Pubkey, Account)>> {
) -> RpcResult<Vec<(Pubkey, UiTokenAccount)>> {
let token_account_filter = match token_account_filter {
TokenAccountsFilter::Mint(mint) => RpcTokenAccountsFilter::Mint(mint.to_string()),
TokenAccountsFilter::ProgramId(program_id) => {
Expand All @@ -704,8 +734,10 @@ impl RpcClient {
commitment_config
]),
)?;
let pubkey_accounts =
parse_keyed_accounts(accounts, RpcRequest::GetTokenAccountsByDelegate)?;
let pubkey_accounts = accounts_to_token_accounts(parse_keyed_accounts(
accounts,
RpcRequest::GetTokenAccountsByDelegate,
)?);
Ok(Response {
context,
value: pubkey_accounts,
Expand All @@ -716,7 +748,7 @@ impl RpcClient {
&self,
owner: &Pubkey,
token_account_filter: TokenAccountsFilter,
) -> ClientResult<Vec<(Pubkey, Account)>> {
) -> ClientResult<Vec<(Pubkey, UiTokenAccount)>> {
Ok(self
.get_token_accounts_by_owner_with_commitment(
owner,
Expand All @@ -731,7 +763,7 @@ impl RpcClient {
owner: &Pubkey,
token_account_filter: TokenAccountsFilter,
commitment_config: CommitmentConfig,
) -> RpcResult<Vec<(Pubkey, Account)>> {
) -> RpcResult<Vec<(Pubkey, UiTokenAccount)>> {
let token_account_filter = match token_account_filter {
TokenAccountsFilter::Mint(mint) => RpcTokenAccountsFilter::Mint(mint.to_string()),
TokenAccountsFilter::ProgramId(program_id) => {
Expand All @@ -745,7 +777,10 @@ impl RpcClient {
RpcRequest::GetTokenAccountsByOwner,
json!([owner.to_string(), token_account_filter, commitment_config]),
)?;
let pubkey_accounts = parse_keyed_accounts(accounts, RpcRequest::GetTokenAccountsByOwner)?;
let pubkey_accounts = accounts_to_token_accounts(parse_keyed_accounts(
accounts,
RpcRequest::GetTokenAccountsByDelegate,
)?);
Ok(Response {
context,
value: pubkey_accounts,
Expand Down Expand Up @@ -1133,6 +1168,18 @@ fn parse_keyed_accounts(
Ok(pubkey_accounts)
}

fn accounts_to_token_accounts(
pubkey_accounts: Vec<(Pubkey, Account)>,
) -> Vec<(Pubkey, UiTokenAccount)> {
pubkey_accounts
.into_iter()
.filter_map(|(pubkey, account)| match parse_token(&account.data) {
Ok(TokenAccountType::Account(ui_token_account)) => Some((pubkey, ui_token_account)),
_ => None,
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 9e9872c

Please sign in to comment.