Get Token Balance

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/blocto/solana-go-sdk/client"
	"github.com/blocto/solana-go-sdk/rpc"
)

func main() {
	c := client.NewClient(rpc.DevnetRPCEndpoint)

	// should pass a token account address
	balance, decimals, err := c.GetTokenAccountBalance(
		context.Background(),
		"HeCBh32JJ8DxcjTyc6q46tirHR8hd2xj3mGoAcQ7eduL",
	)
	if err != nil {
		log.Fatalln("get balance error", err)
	}
	// the smallest unit like lamports
	fmt.Println("balance", balance)
	// the decimals of mint which token account holds
	fmt.Println("decimals", decimals)

	// if you want use a normal unit, you can do
	// balance / 10^decimals
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Last Updated:
Contributors: yihau