@@ -2,8 +2,8 @@ use crate::{
2
2
client_error:: { ClientError , ClientErrorKind , Result as ClientResult } ,
3
3
http_sender:: HttpSender ,
4
4
mock_sender:: { MockSender , Mocks } ,
5
- rpc_config:: { RpcLargestAccountsConfig , RpcSendTransactionConfig } ,
6
- rpc_request:: { RpcError , RpcRequest } ,
5
+ rpc_config:: { RpcLargestAccountsConfig , RpcSendTransactionConfig , RpcTokenAccountsFilter } ,
6
+ rpc_request:: { RpcError , RpcRequest , TokenAccountsFilter } ,
7
7
rpc_response:: * ,
8
8
rpc_sender:: RpcSender ,
9
9
} ;
@@ -503,17 +503,7 @@ impl RpcClient {
503
503
pub fn get_program_accounts ( & self , pubkey : & Pubkey ) -> ClientResult < Vec < ( Pubkey , Account ) > > {
504
504
let accounts: Vec < RpcKeyedAccount > =
505
505
self . send ( RpcRequest :: GetProgramAccounts , json ! ( [ pubkey. to_string( ) ] ) ) ?;
506
- let mut pubkey_accounts: Vec < ( Pubkey , Account ) > = Vec :: new ( ) ;
507
- for RpcKeyedAccount { pubkey, account } in accounts. into_iter ( ) {
508
- let pubkey = pubkey. parse ( ) . map_err ( |_| {
509
- ClientError :: new_with_request (
510
- RpcError :: ParseError ( "Pubkey" . to_string ( ) ) . into ( ) ,
511
- RpcRequest :: GetProgramAccounts ,
512
- )
513
- } ) ?;
514
- pubkey_accounts. push ( ( pubkey, account. decode ( ) . unwrap ( ) ) ) ;
515
- }
516
- Ok ( pubkey_accounts)
506
+ parse_keyed_accounts ( accounts, RpcRequest :: GetProgramAccounts )
517
507
}
518
508
519
509
/// Request the transaction count.
@@ -660,6 +650,125 @@ impl RpcClient {
660
650
Ok ( hash)
661
651
}
662
652
653
+ pub fn get_token_account_balance ( & self , pubkey : & Pubkey ) -> ClientResult < u64 > {
654
+ Ok ( self
655
+ . get_token_account_balance_with_commitment ( pubkey, CommitmentConfig :: default ( ) ) ?
656
+ . value )
657
+ }
658
+
659
+ pub fn get_token_account_balance_with_commitment (
660
+ & self ,
661
+ pubkey : & Pubkey ,
662
+ commitment_config : CommitmentConfig ,
663
+ ) -> RpcResult < u64 > {
664
+ self . send (
665
+ RpcRequest :: GetTokenAccountBalance ,
666
+ json ! ( [ pubkey. to_string( ) , commitment_config] ) ,
667
+ )
668
+ }
669
+
670
+ pub fn get_token_accounts_by_delegate (
671
+ & self ,
672
+ delegate : & Pubkey ,
673
+ token_account_filter : TokenAccountsFilter ,
674
+ ) -> ClientResult < Vec < ( Pubkey , Account ) > > {
675
+ Ok ( self
676
+ . get_token_accounts_by_delegate_with_commitment (
677
+ delegate,
678
+ token_account_filter,
679
+ CommitmentConfig :: default ( ) ,
680
+ ) ?
681
+ . value )
682
+ }
683
+
684
+ pub fn get_token_accounts_by_delegate_with_commitment (
685
+ & self ,
686
+ delegate : & Pubkey ,
687
+ token_account_filter : TokenAccountsFilter ,
688
+ commitment_config : CommitmentConfig ,
689
+ ) -> RpcResult < Vec < ( Pubkey , Account ) > > {
690
+ let token_account_filter = match token_account_filter {
691
+ TokenAccountsFilter :: Mint ( mint) => RpcTokenAccountsFilter :: Mint ( mint. to_string ( ) ) ,
692
+ TokenAccountsFilter :: ProgramId ( program_id) => {
693
+ RpcTokenAccountsFilter :: ProgramId ( program_id. to_string ( ) )
694
+ }
695
+ } ;
696
+ let Response {
697
+ context,
698
+ value : accounts,
699
+ } = self . send (
700
+ RpcRequest :: GetTokenAccountsByDelegate ,
701
+ json ! ( [
702
+ delegate. to_string( ) ,
703
+ token_account_filter,
704
+ commitment_config
705
+ ] ) ,
706
+ ) ?;
707
+ let pubkey_accounts =
708
+ parse_keyed_accounts ( accounts, RpcRequest :: GetTokenAccountsByDelegate ) ?;
709
+ Ok ( Response {
710
+ context,
711
+ value : pubkey_accounts,
712
+ } )
713
+ }
714
+
715
+ pub fn get_token_accounts_by_owner (
716
+ & self ,
717
+ owner : & Pubkey ,
718
+ token_account_filter : TokenAccountsFilter ,
719
+ ) -> ClientResult < Vec < ( Pubkey , Account ) > > {
720
+ Ok ( self
721
+ . get_token_accounts_by_owner_with_commitment (
722
+ owner,
723
+ token_account_filter,
724
+ CommitmentConfig :: default ( ) ,
725
+ ) ?
726
+ . value )
727
+ }
728
+
729
+ pub fn get_token_accounts_by_owner_with_commitment (
730
+ & self ,
731
+ owner : & Pubkey ,
732
+ token_account_filter : TokenAccountsFilter ,
733
+ commitment_config : CommitmentConfig ,
734
+ ) -> RpcResult < Vec < ( Pubkey , Account ) > > {
735
+ let token_account_filter = match token_account_filter {
736
+ TokenAccountsFilter :: Mint ( mint) => RpcTokenAccountsFilter :: Mint ( mint. to_string ( ) ) ,
737
+ TokenAccountsFilter :: ProgramId ( program_id) => {
738
+ RpcTokenAccountsFilter :: ProgramId ( program_id. to_string ( ) )
739
+ }
740
+ } ;
741
+ let Response {
742
+ context,
743
+ value : accounts,
744
+ } = self . send (
745
+ RpcRequest :: GetTokenAccountsByOwner ,
746
+ json ! ( [ owner. to_string( ) , token_account_filter, commitment_config] ) ,
747
+ ) ?;
748
+ let pubkey_accounts = parse_keyed_accounts ( accounts, RpcRequest :: GetTokenAccountsByOwner ) ?;
749
+ Ok ( Response {
750
+ context,
751
+ value : pubkey_accounts,
752
+ } )
753
+ }
754
+
755
+ pub fn get_token_supply ( & self , mint : & Pubkey ) -> ClientResult < u64 > {
756
+ Ok ( self
757
+ . get_token_supply_with_commitment ( mint, CommitmentConfig :: default ( ) ) ?
758
+ . value )
759
+ }
760
+
761
+ pub fn get_token_supply_with_commitment (
762
+ & self ,
763
+ mint : & Pubkey ,
764
+ commitment_config : CommitmentConfig ,
765
+ ) -> RpcResult < u64 > {
766
+ self . send (
767
+ RpcRequest :: GetTokenSupply ,
768
+ json ! ( [ mint. to_string( ) , commitment_config] ) ,
769
+ )
770
+ }
771
+
663
772
fn poll_balance_with_timeout_and_commitment (
664
773
& self ,
665
774
pubkey : & Pubkey ,
@@ -999,6 +1108,23 @@ pub fn get_rpc_request_str(rpc_addr: SocketAddr, tls: bool) -> String {
999
1108
}
1000
1109
}
1001
1110
1111
+ fn parse_keyed_accounts (
1112
+ accounts : Vec < RpcKeyedAccount > ,
1113
+ request : RpcRequest ,
1114
+ ) -> ClientResult < Vec < ( Pubkey , Account ) > > {
1115
+ let mut pubkey_accounts: Vec < ( Pubkey , Account ) > = Vec :: new ( ) ;
1116
+ for RpcKeyedAccount { pubkey, account } in accounts. into_iter ( ) {
1117
+ let pubkey = pubkey. parse ( ) . map_err ( |_| {
1118
+ ClientError :: new_with_request (
1119
+ RpcError :: ParseError ( "Pubkey" . to_string ( ) ) . into ( ) ,
1120
+ request,
1121
+ )
1122
+ } ) ?;
1123
+ pubkey_accounts. push ( ( pubkey, account. decode ( ) . unwrap ( ) ) ) ;
1124
+ }
1125
+ Ok ( pubkey_accounts)
1126
+ }
1127
+
1002
1128
#[ cfg( test) ]
1003
1129
mod tests {
1004
1130
use super :: * ;
0 commit comments