💻
Solana Go
GithubMissing content?
  • 🏠Introduction
  • 🛠️Prepare
  • Tour
    • Intro
    • Create Account
    • Request Airdrop
    • Get Balance (SOL)
    • Transfer (SOL)
    • Create Mint (Token)
    • Create Account (Token)
      • Random Token Account
      • Associated Token Account
    • Mint To (Token)
    • Get Balance (Token)
    • Transfer (Token)
  • Advanced
    • Durable Nonce
      • Create Nonce Account
      • Get Nonce Account
      • Use Nonce
  • Metaplex (NFT)
    • Get Metadata
  • Misc
    • Need To Know
Powered by GitBook
On this page

Was this helpful?

  1. Tour

Get Balance (Token)

After receiving token, we can fetch its balance by token account.

package main

import (
	"context"
	"fmt"
	"log"

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

func main() {
	c := client.NewClient(rpc.LocalnetRPCEndpoint)
	balance, decimals, err := c.GetTokenAccountBalance(
		context.Background(),
		"AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ",
	)
	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
}

different mint need different token account to hold, so we don't need to pass a mint to fetch balance. The token account has already represent which mint you are fetching.

PreviousMint To (Token)NextTransfer (Token)

Last updated 3 years ago

Was this helpful?