@@ -350,6 +350,16 @@ pub enum Command {
350350 #[ clap( value_enum) ]
351351 shell : clap_complete:: Shell ,
352352 } ,
353+ #[ clap( name = "balance" , alias = "balance" ) ]
354+ /// Get your balance
355+ Balance {
356+ /// Account balance to check (defaults to configured wallet)
357+ #[ clap( index = 1 , value_name = "ACCOUNT_ADDRESS" ) ]
358+ pubkey : Option < Pubkey > ,
359+ /// Display balance in lamports instead of SOL
360+ #[ clap( long) ]
361+ lamports : bool ,
362+ } ,
353363}
354364
355365#[ derive( Debug , Parser ) ]
@@ -915,6 +925,7 @@ fn process_command(opts: Opts) -> Result<()> {
915925 ) ;
916926 Ok ( ( ) )
917927 }
928+ Command :: Balance { pubkey, lamports } => balance ( & opts. cfg_override , pubkey, lamports) ,
918929 }
919930}
920931
@@ -4417,6 +4428,36 @@ fn create_client<U: ToString>(url: U) -> RpcClient {
44174428 RpcClient :: new_with_commitment ( url, CommitmentConfig :: confirmed ( ) )
44184429}
44194430
4431+ fn balance ( cfg_override : & ConfigOverride , pubkey : Option < Pubkey > , lamports : bool ) -> Result < ( ) > {
4432+ // Get the config
4433+ let cfg = Config :: discover ( cfg_override) ?. expect ( "Not in anchor workspace." ) ;
4434+
4435+ // Create RPC client from cluster URL
4436+ let client = RpcClient :: new ( cfg. provider . cluster . url ( ) ) ;
4437+
4438+ // Determine the actual pubkey to check
4439+ let actual_pubkey = if let Some ( pubkey) = pubkey {
4440+ pubkey
4441+ } else {
4442+ // Load keypair from wallet path and get pubkey
4443+ let keypair = Keypair :: read_from_file ( cfg. provider . wallet . to_string ( ) )
4444+ . map_err ( |e| anyhow:: anyhow!( "Failed to read keypair: {}" , e) ) ?;
4445+ keypair. pubkey ( )
4446+ } ;
4447+
4448+ // Get balance
4449+ let balance = client. get_balance ( & actual_pubkey) ?;
4450+
4451+ // Format and display output
4452+ if lamports {
4453+ println ! ( "{}" , balance) ;
4454+ } else {
4455+ println ! ( "{:.9} SOL" , balance as f64 / 1_000_000_000.0 ) ;
4456+ }
4457+
4458+ Ok ( ( ) )
4459+ }
4460+
44204461#[ cfg( test) ]
44214462mod tests {
44224463 use super :: * ;
0 commit comments