1

I have 100s of solana private keys for a service I'm building. How do I check each one for a balance without having to import them to the solana-cli each time? Is there a simple command to check a private key's balance?

3 Answers 3

1

You'll need to read them into your CLI somehow. If they're stored in files, you can use solana_sdk::signer::keypair::read_keypair_file, get the pubkey, and then get that balance, so some pseudo-code could be:

let keypair_path = Path::new("./path/to/keypair.json");
let rpc_client = RpcClient::new("http://localhost:8899");
let kp = read_keypair_file(&keypair_path);
let balance = rpc_client.get_balance(&kp.pubkey());

More information at https://github.com/solana-labs/solana/blob/1db7da5c32231b2ae72be29173c9ded986544f62/sdk/src/signer/keypair.rs#L124

1

I'd imagine you could do a shell script like this to iterate over different wallets in a file, list.txt:

list.txt:

WALLET_ID_1
WALLET_ID_2
WALLET_ID_3

balances.sh:

output_file="balances.txt"

while read line; do
    balance=$(solana balance "$line")
    echo "$line: $balance" >> "$output_file"
done < list.txt

This should output a file, balances.txt that looks something like this:

WALLET_ID_1: 1.181062312 SOL
WALLET_ID_2: 2.046516532 SOL
WALLET_ID_3: 3.84642558 SOL

if you wanted to do this from private keys saved on your machine (as opposed to public keys, you'd modify your list to include file paths to each private key (or have your shell script loop through files in a folder). and add -k to your solana balance query, eg:

balance=$(solana balance -k "$path")

something like:

output_file="balances.txt"
directory="/keys/"

for file in in "$directory"/*.json; do
    # Extract public key from private key
    key=$(solana address -k "$file")

    # Get balance from private key
    balance=$(solana balance -k "$file")
    echo "$key: $balance" >> "$output_file"
done
0
use anchor_lang::{
    prelude::*,
    solana_program::account_info::AccountInfo,


let balance = AccountInfo::lamports(&account.to_account_info())

Remember to pass the account as account info

1
  • This solution only works on-chain, and the question is about CLI usage
    – Jon C
    Commented Sep 17, 2022 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.