This example reads a token account that exists only on mainnet. Run it
against a Surfpool local network that forks mainnet
(
surfpool start --network mainnet fetches the account on demand), or point
the RPC URL at a mainnet endpoint.- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
import { address, createSolanaRpc } from "@solana/kit";
const rpc = createSolanaRpc("http://localhost:8899");
const tokenAccountAddress = address(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ",
);
const balance = await rpc.getTokenAccountBalance(tokenAccountAddress).send();
console.log(balance);
Output from a local Surfpool network
Output
{
context: { slot: 438827231n, apiVersion: '4.1.2' },
value: { uiAmount: 0, decimals: 6, amount: '0', uiAmountString: '0' }
}
Web3.js v3
import { Connection, PublicKey } from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const tokenAccount = new PublicKey(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ",
);
const tokenAmount = await connection.getTokenAccountBalance(tokenAccount);
console.log(`amount: ${tokenAmount.value.amount}`);
console.log(`decimals: ${tokenAmount.value.decimals}`);
Output from a local Surfpool network
Output
amount: 0
decimals: 6
Rust
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::pubkey;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let token_account_address = pubkey!("GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ");
let balance = client
.get_token_account_balance(&token_account_address)
.await?;
println!("{:#?}", balance);
Ok(())
}
Output from a local Surfpool network
Output
UiTokenAmount {
ui_amount: Some(
0.0,
),
decimals: 6,
amount: "0",
ui_amount_string: "0",
}
Python
import asyncio
from solana.rpc.async_api import AsyncClient
from solders.pubkey import Pubkey
async def main():
async with AsyncClient("http://localhost:8899") as client:
token_account_address = Pubkey.from_string(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ"
)
balance = await client.get_token_account_balance(token_account_address)
print(balance)
asyncio.run(main())
Output from a local Surfpool network
Output
GetTokenAccountBalanceResp { context: RpcResponseContext { slot: 461545421, api_version: Some("4.2.1") }, value: UiTokenAmount(UiTokenAmount { ui_amount: Some(0.0), decimals: 6, amount: "0", ui_amount_string: "0" }) }
Go
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/solana-foundation/solana-go/v2"
"github.com/solana-foundation/solana-go/v2/rpc"
)
func main() {
client := rpc.New("http://localhost:8899")
tokenAccountAddress := solana.MustPublicKeyFromBase58("GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ")
balance, err := client.GetTokenAccountBalance(context.Background(), tokenAccountAddress, rpc.CommitmentConfirmed)
if err != nil {
panic(err)
}
out, _ := json.MarshalIndent(balance, "", " ")
fmt.Println(string(out))
}
Output from a local Surfpool network
Output
{
"context": {
"slot": 461555398,
"apiVersion": "4.2.1"
},
"value": {
"amount": "0",
"decimals": 6,
"uiAmount": 0,
"uiAmountString": "0"
}
}
Web3.js (legacy)
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.import { Connection, PublicKey } from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const tokenAccount = new PublicKey(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ",
);
const tokenAmount = await connection.getTokenAccountBalance(tokenAccount);
console.log(`amount: ${tokenAmount.value.amount}`);
console.log(`decimals: ${tokenAmount.value.decimals}`);
Output from a local Surfpool network
Output
amount: 0
decimals: 6
A token account can only hold one kind of mint. When you specify a token
account, you also specify its mint.