token

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package token provides typed instruction builders for the SPL Token program. The builders cover the common hot-path operations: transfer, mint, burn, close, initialize. Every builder returns a solana.Instruction ready for solana.NewMessage.

This package binds the classic SPL Token program at TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA. For Token-2022 support (the SPL Token program extension at TokenzQd...), see the sibling programs/token2022 package (to land in a follow-up PR).

Index

Constants

This section is empty.

Variables

View Source
var ProgramID = solana.MustPublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")

ProgramID is the canonical address of the SPL Token program.

Functions

func NewApprove

func NewApprove(source, delegate, owner solana.PublicKey, amount uint64) solana.Instruction

NewApprove builds an SPL Token Approve instruction that authorizes delegate to transfer up to amount tokens from source on behalf of owner.

func NewApproveChecked

func NewApproveChecked(source, mint, delegate, owner solana.PublicKey, amount uint64, decimals uint8) solana.Instruction

NewApproveChecked builds an SPL Token ApproveChecked instruction (tag 13). Like NewApprove, it authorizes delegate to transfer up to amount tokens from source, but it also requires the mint pubkey and decimals — the runtime verifies both match the on-chain token account, guarding against mint-confusion attacks.

func NewBurn

func NewBurn(account, mint, authority solana.PublicKey, amount uint64) solana.Instruction

NewBurn builds an SPL Token Burn instruction that destroys amount tokens from account. authority must be the account's owner and must sign the transaction.

func NewBurnChecked

func NewBurnChecked(account, mint, authority solana.PublicKey, amount uint64, decimals uint8) solana.Instruction

NewBurnChecked builds an SPL Token BurnChecked instruction. Like Burn but validates decimals matches the mint on chain.

func NewCloseAccount

func NewCloseAccount(account, destination, authority solana.PublicKey) solana.Instruction

NewCloseAccount builds an SPL Token CloseAccount instruction that returns the token account's rent lamports to destination. The token account must have a zero balance and authority must sign the transaction.

func NewFreezeAccount

func NewFreezeAccount(account, mint, freezeAuthority solana.PublicKey) solana.Instruction

NewFreezeAccount builds an SPL Token FreezeAccount instruction. The mint's freeze authority must sign the transaction.

func NewInitializeAccount

func NewInitializeAccount(account, mint, owner solana.PublicKey) solana.Instruction

NewInitializeAccount builds the legacy SPL Token InitializeAccount instruction (tag 1). This form requires the rent sysvar; prefer NewInitializeAccount3 for new code.

func NewInitializeAccount3

func NewInitializeAccount3(account, mint, owner solana.PublicKey) solana.Instruction

NewInitializeAccount3 builds an SPL Token InitializeAccount3 instruction. The newer form (as opposed to InitializeAccount / InitializeAccount2) takes the owner as a parameter instead of requiring the rent sysvar as an account input, and is the recommended form for all new code.

func NewInitializeMint

func NewInitializeMint(mint solana.PublicKey, decimals uint8, mintAuthority solana.PublicKey, freezeAuthority *solana.PublicKey) solana.Instruction

NewInitializeMint builds the legacy SPL Token InitializeMint instruction (tag 0). This form requires the rent sysvar as an account; prefer NewInitializeMint2 for new code.

freezeAuthority may be nil to indicate no freeze authority.

func NewInitializeMint2

func NewInitializeMint2(mint solana.PublicKey, decimals uint8, mintAuthority solana.PublicKey, freezeAuthority solana.PublicKey) solana.Instruction

NewInitializeMint2 builds an SPL Token InitializeMint2 instruction. Unlike the legacy InitializeMint (tag 0), this form does not require the rent sysvar as an account input, so it is the recommended way to create new mints.

freezeAuthority may be the zero PublicKey to indicate no freeze authority; the encoded option byte is set accordingly.

func NewInitializeMultisig

func NewInitializeMultisig(multisig solana.PublicKey, m uint8, signers []solana.PublicKey) solana.Instruction

NewInitializeMultisig builds an SPL Token InitializeMultisig instruction that initialises a multisig authority account requiring m of len(signers) signatures. signers are the constituent signer public keys.

func NewMintTo

func NewMintTo(mint, destination, authority solana.PublicKey, amount uint64) solana.Instruction

NewMintTo builds an SPL Token MintTo instruction that creates amount new tokens in destination. authority must be the mint's mint authority and must sign the transaction.

func NewMintToChecked

func NewMintToChecked(mint, destination, authority solana.PublicKey, amount uint64, decimals uint8) solana.Instruction

NewMintToChecked builds an SPL Token MintToChecked instruction. Like MintTo but validates decimals matches the mint on chain.

func NewRevoke

func NewRevoke(source, owner solana.PublicKey) solana.Instruction

NewRevoke builds an SPL Token Revoke instruction that removes any previously approved delegate from source. owner must sign.

func NewSetAuthority

func NewSetAuthority(account solana.PublicKey, currentAuthority solana.PublicKey, newAuthority *solana.PublicKey, authType AuthorityType) solana.Instruction

NewSetAuthority builds an SPL Token SetAuthority instruction that changes one of the authority fields on account. newAuthority is optional: pass nil to clear the authority (only valid for some authority types). currentAuthority must sign the transaction.

func NewSyncNative

func NewSyncNative(account solana.PublicKey) solana.Instruction

NewSyncNative builds an SPL Token SyncNative instruction that syncs the lamport balance of a wrapped-SOL token account with its actual lamport balance. Call this after transferring SOL directly to a wrapped-SOL account.

func NewThawAccount

func NewThawAccount(account, mint, freezeAuthority solana.PublicKey) solana.Instruction

NewThawAccount builds an SPL Token ThawAccount instruction that unfreezes a frozen token account. The mint's freeze authority must sign.

func NewTransfer

func NewTransfer(source, destination, authority solana.PublicKey, amount uint64) solana.Instruction

NewTransfer builds an SPL Token Transfer instruction.

source and destination are the token account addresses (not the wallet addresses). authority is the token account's owner and must sign the transaction.

Prefer NewTransferChecked over this instruction: the checked variant validates the token's decimals on chain, protecting against mint confusion attacks.

func NewTransferChecked

func NewTransferChecked(source, mint, destination, authority solana.PublicKey, amount uint64, decimals uint8) solana.Instruction

NewTransferChecked builds an SPL Token TransferChecked instruction. Unlike NewTransfer, it requires the mint pubkey and the mint's decimals to be passed in, and the runtime verifies both match the token account on chain.

Types

type AccountState

type AccountState struct {
	// Mint is the address of the mint this account holds tokens of.
	Mint solana.PublicKey
	// Owner is the wallet address that controls this token account.
	Owner solana.PublicKey
	// Amount is the number of tokens held (base units).
	Amount uint64
	// Delegate is the optional delegate authority.
	Delegate *solana.PublicKey
	// State is the account state: 0=Uninitialized, 1=Initialized, 2=Frozen.
	State uint8
	// IsNative is set if the token account holds wrapped SOL. When
	// non-nil, it records the rent-exempt reserve.
	IsNative *uint64
	// DelegatedAmount is the amount approved to the delegate.
	DelegatedAmount uint64
	// CloseAuthority is the optional close authority.
	CloseAuthority *solana.PublicKey
}

AccountState is the on-chain state of an SPL Token account (token wallet).

func DecodeAccountState

func DecodeAccountState(data []byte) (*AccountState, error)

DecodeAccountState decodes a raw SPL Token account data buffer. The buffer must be at least 165 bytes long.

type AuthorityType

type AuthorityType uint8

AuthorityType identifies which authority field SetAuthority changes.

const (
	AuthorityMintTokens    AuthorityType = 0 // Mint authority
	AuthorityFreezeAccount AuthorityType = 1 // Freeze authority on a mint
	AuthorityAccountOwner  AuthorityType = 2 // Owner of a token account
	AuthorityCloseAccount  AuthorityType = 3 // Close authority on a token account
)

type MintState

type MintState struct {
	// MintAuthority is the optional authority that can mint new tokens.
	// Nil if the mint authority has been permanently disabled.
	MintAuthority *solana.PublicKey
	// Supply is the total number of tokens in circulation (base units).
	Supply uint64
	// Decimals is the number of base-10 digits to the right of the decimal.
	Decimals uint8
	// IsInitialized is true after the mint has been initialized.
	IsInitialized bool
	// FreezeAuthority is the optional authority that can freeze token accounts.
	FreezeAuthority *solana.PublicKey
}

MintState is the on-chain state of an SPL Token mint account. The binary layout follows the SPL Token program's Mint struct (82 bytes for a standard non-extended mint).

func DecodeMintState

func DecodeMintState(data []byte) (*MintState, error)

DecodeMintState decodes a raw SPL Token Mint account data buffer. The buffer must be at least 82 bytes long.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL