Skip to main content

Check Balances

In this section let’s set up the wallet to enable checking SOL and Token balances.

Program Implementation

We can check the balances for SOL and tokens by opening Phantom. Let’s perform the same check using a program.

Checking SOL Balance

Let’s start with checking the SOL balance from a program.

Code

Open your tour_de_whirlpool folder in Visual Studio Code, or your preferred development environment. Create a file called 011_get_sol_balance.ts under the src folder with the following contents.

Execution Result

Run the code, and then verify that the public key and SOL balance match the Phantom UI.
balance01

Key Points

  • You can interact with Solana’s Devnet using the RPC endpoint, “https://api.devnet.solana.com
  • Method for reading in a private key (read in wallet.json and create an instance of the Keypair class)
  • Use the toBase58() method to display the public key in base58 encoding
  • Use the Connection class’s getBalance method to obtain the SOL balance
  • SOL is denominated in lamports internally
  • 1 SOL = 1,000,000,000 lamports

API Used

Checking Token Balances

Next, let’s check token balances from a program.

Code

Create a file called 012_get_token_balance.ts under the src folder with the following contents.

Execution Result

Run the code, and then verify that the Phantom UI displays the same token balances. The address displayed after “TokenAccount” will differ for different wallet addresses.
balance02

Key Points

  • Use the getTokenAccountsByOwner() method of the Connection class to obtain token balances from a specified account.
  • The data in the account you obtain is TokenAccount data and needs to be deserialized.
  • You can deserialize the data using the deserializeTokenAccount method from TokenUtil.
  • You can determine which TokenAccount is for which Token using the “mint” field.
  • Just like SOL balances are denominated in lamports, token account balances are converted into integer values by moving the decimal point.
  • The number of usable digits after the decimal point is defined for each token and can be referred to as “decimal” or “scale”.
  • Use the fromU64 method (convert from U64 format) in DecimalUtil for conversions that include moving the position of the decimal point, including lamports to SOL.

getTokenAccountsByOwner in depth

In Solana, all information is handled by accounts. Programs manage what will be stored inside accounts. The TokenProgram, which manages tokens, defines two types of accounts for storing data, TokenAccount and Mint. Because the token balance is stored in an account that holds TokenAccount information, getTokenAccountsByOwner will search for accounts that meet the criteria below.
  • The account is owned by the TokenProgram.
  • The data stored in the account is TokenAccount data.
  • The token owner address, stored in the TokenAccount owner field, is the address of the specified wallet.
Since one TokenAccount can only store one token balance, the account structure looks like the following diagram. token-account-structure

APIs Used

This completes the Basic Wallet Functionality (Checking Balances) section!