Skip to content

Commit 35490f8

Browse files
committed
feat-add: anchor balance cli
1 parent 571190d commit 35490f8

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

cli/src/lib.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,19 +4429,36 @@ fn create_client<U: ToString>(url: U) -> RpcClient {
44294429
}
44304430

44314431
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.");
4432+
// Get config or use defaults when outside workspace
4433+
let (cluster_url, wallet_path) = match Config::discover(cfg_override) {
4434+
Ok(Some(cfg)) => (
4435+
cfg.provider.cluster.url().to_string(),
4436+
cfg.provider.wallet.to_string(),
4437+
),
4438+
_ => {
4439+
// Default to mainnet and standard Solana CLI keypair path
4440+
let default_cluster = "https://api.mainnet-beta.solana.com".to_string();
4441+
let default_keypair = dirs::home_dir()
4442+
.map(|home| {
4443+
home.join(".config/solana/id.json")
4444+
.to_string_lossy()
4445+
.to_string()
4446+
})
4447+
.unwrap_or_else(|| "~/.config/solana/id.json".to_string());
4448+
(default_cluster, default_keypair)
4449+
}
4450+
};
44344451

44354452
// Create RPC client from cluster URL
4436-
let client = RpcClient::new(cfg.provider.cluster.url());
4453+
let client = RpcClient::new(cluster_url);
44374454

44384455
// Determine the actual pubkey to check
44394456
let actual_pubkey = if let Some(pubkey) = pubkey {
44404457
pubkey
44414458
} else {
44424459
// 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))?;
4460+
let keypair = Keypair::read_from_file(wallet_path)
4461+
.map_err(|e| anyhow!("Failed to read keypair: {}", e))?;
44454462
keypair.pubkey()
44464463
};
44474464

0 commit comments

Comments
 (0)