@@ -7,12 +7,13 @@ pub mod query_types;
77
88use base64ct:: Encoding ;
99use query_types:: {
10- ChainIdentifierQuery , CheckpointArgs , CheckpointId , CheckpointQuery , CoinMetadata ,
11- CoinMetadataArgs , CoinMetadataQuery , EpochSummaryArgs , EpochSummaryQuery , EventFilter ,
12- EventsQuery , EventsQueryArgs , ObjectFilter , ObjectQuery , ObjectQueryArgs , ObjectsQuery ,
13- ObjectsQueryArgs , PageInfo , ProtocolConfigQuery , ProtocolConfigs , ProtocolVersionArgs ,
14- ServiceConfig , ServiceConfigQuery , TransactionBlockArgs , TransactionBlockQuery ,
15- TransactionBlocksQuery , TransactionBlocksQueryArgs , TransactionsFilter , Uint53 ,
10+ BalanceArgs , BalanceQuery , ChainIdentifierQuery , CheckpointArgs , CheckpointId , CheckpointQuery ,
11+ CoinMetadata , CoinMetadataArgs , CoinMetadataQuery , EpochSummaryArgs , EpochSummaryQuery ,
12+ EventFilter , EventsQuery , EventsQueryArgs , ObjectFilter , ObjectQuery , ObjectQueryArgs ,
13+ ObjectsQuery , ObjectsQueryArgs , PageInfo , ProtocolConfigQuery , ProtocolConfigs ,
14+ ProtocolVersionArgs , ServiceConfig , ServiceConfigQuery , TransactionBlockArgs ,
15+ TransactionBlockQuery , TransactionBlocksQuery , TransactionBlocksQueryArgs , TransactionsFilter ,
16+ Uint53 ,
1617} ;
1718use reqwest:: Url ;
1819use sui_types:: types:: {
@@ -194,6 +195,38 @@ impl Client {
194195 . ok_or_else ( || Error :: msg ( "No data in response" ) )
195196 }
196197
198+ // ===========================================================================
199+ // Balance API
200+ // ===========================================================================
201+
202+ /// Get the balance of all the coins owned by address for the provided coin type.
203+ /// Coin type will default to `0x2::coin::Coin<0x2::sui::SUI>` if not provided.
204+ pub async fn balance (
205+ & self ,
206+ address : Address ,
207+ coin_type : Option < & str > ,
208+ ) -> Result < Option < u128 > , Error > {
209+ let operation = BalanceQuery :: build ( BalanceArgs {
210+ address : address. into ( ) ,
211+ coin_type : coin_type. map ( |x| x. to_string ( ) ) ,
212+ } ) ;
213+ let response = self . run_query ( & operation) . await ?;
214+
215+ if let Some ( errors) = response. errors {
216+ return Err ( Error :: msg ( format ! ( "{:?}" , errors) ) ) ;
217+ }
218+
219+ let total_balance = response
220+ . data
221+ . map ( |b| b. owner . and_then ( |o| o. balance . map ( |b| b. total_balance ) ) )
222+ . ok_or_else ( || Error :: msg ( "No data in response" ) ) ?
223+ . flatten ( )
224+ . map ( |x| x. 0 . parse :: < u128 > ( ) )
225+ . transpose ( )
226+ . map_err ( |e| Error :: msg ( format ! ( "Cannot parse balance into u128: {e}" ) ) ) ?;
227+ Ok ( total_balance)
228+ }
229+
197230 // ===========================================================================
198231 // Coin API
199232 // ===========================================================================
@@ -576,6 +609,15 @@ mod tests {
576609 assert ! ( client. set_rpc_server( "9125/graphql" ) . is_err( ) ) ;
577610 }
578611
612+ #[ tokio:: test]
613+ async fn test_balance_query ( ) {
614+ for ( n, _) in NETWORKS . iter ( ) {
615+ let client = Client :: new ( n) . unwrap ( ) ;
616+ let balance = client. balance ( "0x1" . parse ( ) . unwrap ( ) , None ) . await ;
617+ assert ! ( balance. is_ok( ) , "Balance query failed for network: {n}" ) ;
618+ }
619+ }
620+
579621 #[ tokio:: test]
580622 async fn test_chain_id ( ) {
581623 for ( n, id) in NETWORKS . iter ( ) {
0 commit comments