pythonsolanacryptocurrency

Get Solana wallet balance with python


Is there any way to check the balance of a Solana wallet using Python?

I tried using Solscan, but I don't understand how it works and I can't find information about it on the internet.

If anyone knows any way, I would be grateful.


Solution

  • You could use solana-py. If you haven't already, install it through pip install solana. Then you can run the following Python code to check the balance of your wallet, replacing YOUR_WALLET_ADDRESS with your actual wallet address.

    from solana.rpc.api import Client
    from solana.publickey import PublicKey
    client = Client("https://api.mainnet-beta.solana.com")
    public_key = PublicKey(wallet_address)
    response = client.get_balance(public_key)
    balance_lamports = response["result"]["value"]
    balance_sol = balance_lamports / 1e9
    print(f"Wallet balance: {balance_lamports} lamports ({balance_sol} SOL)")