Skip to content

Commit

Permalink
get_token_accounts_by_owner now returns UiTokenAccounts (#11367)
Browse files Browse the repository at this point in the history
(cherry picked from commit b5e03d6)

Co-authored-by: Michael Vines <[email protected]>
  • Loading branch information
mergify[bot] and mvines authored Aug 5, 2020
1 parent 5871462 commit b95c493
Showing 1 changed file with 109 additions and 8 deletions.
117 changes: 109 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, UiMint, UiMultisig, UiTokenAccount},
UiAccount,
};
use solana_sdk::{
account::Account,
clock::{
Expand Down Expand Up @@ -658,6 +661,87 @@ 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_mint(&self, pubkey: &Pubkey) -> ClientResult<Option<UiMint>> {
Ok(self
.get_token_mint_with_commitment(pubkey, CommitmentConfig::default())?
.value)
}

pub fn get_token_mint_with_commitment(
&self,
pubkey: &Pubkey,
commitment_config: CommitmentConfig,
) -> RpcResult<Option<UiMint>> {
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::Mint(ui_token_mint)) => Some(ui_token_mint),
_ => None,
})
.flatten(),
})
}

pub fn get_token_multisig(&self, pubkey: &Pubkey) -> ClientResult<Option<UiMultisig>> {
Ok(self
.get_token_multisig_with_commitment(pubkey, CommitmentConfig::default())?
.value)
}

pub fn get_token_multisig_with_commitment(
&self,
pubkey: &Pubkey,
commitment_config: CommitmentConfig,
) -> RpcResult<Option<UiMultisig>> {
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::Multisig(ui_token_multisig)) => Some(ui_token_multisig),
_ => 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 @@ -679,7 +763,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 @@ -694,7 +778,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 @@ -712,8 +796,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 @@ -724,7 +810,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 @@ -739,7 +825,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 @@ -753,7 +839,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 @@ -1143,6 +1232,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 b95c493

Please sign in to comment.