Three Ways to Check the Balance of a Solana Wallet Address
5 min read
Overview​
If you are just starting your Solana journey, you will probably need to check an account's balance. In this guide, we will walk through three easy ways to check the balance of a Solana Wallet:
- Using Solana's Command Line Interface (Solana CLI) (Jump to Solana CLI)
- Using Solana's JavaScript API (Jump to Solana-Web3.js)
- Using a cURL script (Jump to cURL)
What You Will Need​
- Solana CLI latest version installed
- Nodejs (version 16.15 or higher) installed
- Basic JavaScript experience
- cURL stable version installed
- A wallet to query (e.g.,
E645TckHQnDcavVv92Etc6xSWQaq8zzPtPRGBheviRAk
)
Before querying our wallet, let's do a quick recap on Solana's clusters, as we will need to specify a specific cluster when checking a wallet's balance. "A Solana cluster is a set of validators working together to serve client transactions and maintain the integrity of the ledger. Many clusters may coexist." (Source: docs.solana.com/cluster/overview). In fact, Solana maintains three clusters that each serve different purposes:
- Mainnet Beta: production, permissionless environment where real tokens are used
- Devnet: a playground for application developers testing Solana applications (tokens on Devnet are not real and have no financial value). Devnet usually runs the same software release as Mainnet Beta
- Testnet: an environment where Solana core contributors and validators stress test new updates and features, with a focus on testing network performance (tokens on Testnet are not real and have no financial value)
Source: docs.solana.com/clusters
For the purposes of this guide, we will use Mainnet-Beta
, but you should know that a wallet can have a balance on each cluster at the same time.
Method 1 - Solana CLI​
Our first method is to check the balance of a wallet using Solana CLI. If you do not already have it installed, follow the instructions for your operating environment at docs.solana.com/cli/install-solana-cli-tools. To make sure your installation was successful, open a new terminal and type:
solana --version
You should see something like this:
You're ready to go! All you need to do is get your wallet address handy--you can copy it directly from Phantom or any other wallet:
In your terminal, fetch your wallet balance by entering:
solana balance YOUR_WALLET_ADDRESS -u mainnet-beta
You can modify your search to get the balance of that wallet on different clusters by modifying your -u
(URL for Solana's JSON RPC) option. To get a devnet or testnet balance, try:
solana balance YOUR_WALLET_ADDRESS -u devnet # for Devnet
# or
solana balance YOUR_WALLET_ADDRESS -u testnet # for Testnet
These default clusters (mainnet-beta, testnet, devnet) are public JSON RPC endpoints. If you are planning to do a lot of queries or building on Solana, you will probably want an endpoint of your own. See why over 50% of projects on Solana choose QuickNode and sign up for a free account here. You will want a mainnet node to query a wallet's true balance:
You can now modify your query to use your endpoint:
solana balance YOUR_WALLET_ADDRESS -u https://example.solana.quiknode.pro/000000/
Great job!
Method 2 - Solana-Web3.js​
Create a new project directory and file, balance.js, in your terminal with the following:
mkdir sol_balance
cd sol_balance
echo > balance.js
Install Solana Web3 dependencies:
yarn init -y
yarn add @solana/web3.js
or
npm init -y
npm install --save @solana/web3.js
Open balance.js in a code editor of choice, and on line 1, require @solana/web3.js and store it in a constant, SOLANA. On line 2, destructure SOLANA to get a few necessary classes, methods, and constants.
const SOLANA = require('@solana/web3.js');
const { Connection, PublicKey, LAMPORTS_PER_SOL, clusterApiUrl } = SOLANA;
On line 4, establish a connection to the cluster using your QuickNode endpoint (or public cluster by using clusterApiUrl('CLUSTER_NAME')
), and define your wallet address as WALLET_ADDRESS
.
const QUICKNODE_RPC = 'https://example.solana.quiknode.pro/000000/'; // 👈 Replace with your QuickNode Endpoint OR clusterApiUrl('mainnet-beta')
const SOLANA_CONNECTION = new Connection(QUICKNODE_RPC);
const WALLET_ADDRESS = 'YOUR_WALLET_ADDRESS'; //👈 Replace with your wallet address
If you are going to be doing much with Solana-Web3.js, you will become very familiar with the Connection class. This is effectively how you establish a connection to a JSON RPC endpoint.
Finally, fetch your balance by calling the getBalance()
method on your instance of Connection:
(async () => {
let balance = await SOLANA_CONNECTION.getBalance(new PublicKey(WALLET_ADDRESS));
console.log(`Wallet Balance: ${balance/LAMPORTS_PER_SOL}`)
})();
A few notes about our script:
- We use an async function to allow for our
getBalance
promise to return a response from the network - Notice that instead of just passing our
WALLET_ADDRESS
we first create a new instance of PublicKey. PublicKeys are vital to fetching and sending information to a Solana cluster. You can define a PublicKey in your code by passing the string address of a wallet into thenew PublicKey()
constructor. - We divide our
balance
byLAMPORTS_PER_SOL
because Solana's native unit for tracking SOL is actually lamports (1 / 1_000_000_000 of 1 SOL).
Run your code. In your terminal type,
node balance
You should see something like this:
Nice job!
Method 3 - cURL​
cURL is a command line tool and library for transferring data with URLs. Most *nix-based systems have cURL support out of the box. Check if you have it by running the following:
curl -h
If you don't have it installed, head to curl.se to set it up.
Once you are ready to go, all you need to do is drop this HTTP request in your terminal (make sure to replace your endpoint and wallet address). In your terminal, enter:
curl https://example.solana.quiknode.pro/000000/ -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0", "id": 1,
"method": "getBalance",
"params": [
"YOUR_WALLET_ADDRESS"
]
}
'
You should see something like this:
Notice our balance is returned in the result.value
field in lamports.
Wrap Up​
Nice work! You now have three handy tools for fetching the balance of a Solana Wallet. If you are just getting started with your Solana journey, here are some resources that may be helpful:
We would love to hear more about what you are building. Drop us a line in Discord, or give us a follow on Twitter to stay up to date on all the latest information!
We <3 Feedback!​
If you have any feedback on this guide, let us know. We’d love to hear from you.