token2022

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 7 Imported by: 0

README

Token2022

A Go package for Solana Token 2022 operations.

Installation

go get github.com/dwmfan/token2022

Usage

Finding Associated Token Address for Token 2022
package main

import (
    "fmt"
    "github.com/dwmfan/token2022"
    "github.com/gagliardetto/solana-go"
)

func main() {
    wallet := solana.MustPublicKeyFromBase58("nrw1b6stoyvm3QPsh78iWoJwsjM1b7KfcvxYT3LbFun")
    mint := solana.MustPublicKeyFromBase58("D8zFabAK4Jt2Wi1TZJvMnr6EeD9K4qpiGhya1NQpyrZn")
    
    address, _, err := token2022.FindAssociatedTokenAddress2022(wallet, mint)
    if err != nil {
        panic(err)
    }
    
    fmt.Println("Associated Token Address:", address.String())
}
Creating an Associated Token Account for Token 2022
package main

import (
    "context"
    "fmt"
    "github.com/dwmfan/token2022"
    "github.com/gagliardetto/solana-go"
    "github.com/gagliardetto/solana-go/rpc"
)

func main() {
    // Initialize client
    client := rpc.New("https://api.mainnet-beta.solana.com")
    
    // Define wallet, payer, and mint
    wallet := solana.MustPublicKeyFromBase58("nrw1b6stoyvm3QPsh78iWoJwsjM1b7KfcvxYT3LbFun")
    payer := solana.MustPublicKeyFromBase58("nrw1b6stoyvm3QPsh78iWoJwsjM1b7KfcvxYT3LbFun") // Using same as wallet for example
    mint := solana.MustPublicKeyFromBase58("D8zFabAK4Jt2Wi1TZJvMnr6EeD9K4qpiGhya1NQpyrZn")
    
    // Create instruction
    instruction := token2022.NewCreate2022Instruction(payer, wallet, mint)
    
    // Build and validate instruction
    built, err := instruction.ValidateAndBuild()
    if err != nil {
        panic(err)
    }
    
    // Create transaction
    recent, err := client.GetRecentBlockhash(context.Background(), rpc.CommitmentFinalized)
    if err != nil {
        panic(err)
    }
    
    tx, err := solana.NewTransaction(
        []solana.Instruction{built},
        recent.Value.Blockhash,
        solana.TransactionPayer(payer),
    )
    if err != nil {
        panic(err)
    }
    
    // Sign and send transaction (not shown)
    // ...
    
    fmt.Println("Transaction created successfully")
}

## License

MIT

Documentation

Index

Constants

View Source
const ProgramName = "Associated Token Account Program"

ProgramName is the name of the Associated Token Account program for Token 2022

Variables

ProgramID is the ID of the Associated Token Account program

Functions

func FindAssociatedTokenAddress2022

func FindAssociatedTokenAddress2022(
	wallet solana.PublicKey,
	mint solana.PublicKey,
) (solana.PublicKey, uint8, error)

Types

type AccountMetaGettable

type AccountMetaGettable interface {
	GetAccounts() []*solana.AccountMeta
}

AccountMetaGettable is an interface for getting account metas

type Create2022

type Create2022 struct {
	Payer  solana.PublicKey `bin:"-" borsh_skip:"true"`
	Wallet solana.PublicKey `bin:"-" borsh_skip:"true"`
	Mint   solana.PublicKey `bin:"-" borsh_skip:"true"`

	// [0] = [WRITE, SIGNER] Payer
	// ··········· Funding account
	//
	// [1] = [WRITE] AssociatedTokenAccount
	// ··········· Associated token account address to be created
	//
	// [2] = [] Wallet
	// ··········· Wallet address for the new associated token account
	//
	// [3] = [] TokenMint
	// ··········· The token mint for the new associated token account
	//
	// [4] = [] SystemProgram
	// ··········· System program ID
	//
	// [5] = [] TokenProgram
	// ··········· Token 2022 program ID
	//
	// [6] = [] SysVarRent
	// ··········· SysVarRentPubkey
	solana.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}

func NewCreate2022Instruction

func NewCreate2022Instruction(
	payer solana.PublicKey,
	walletAddress solana.PublicKey,
	splTokenMintAddress solana.PublicKey,
) *Create2022

NewCreate2022Instruction creates a new instruction for creating an associated token account for Token 2022

func NewCreate2022InstructionBuilder

func NewCreate2022InstructionBuilder() *Create2022

NewCreate2022InstructionBuilder creates a new `Create2022` instruction builder.

func (Create2022) Build

func (inst Create2022) Build() *Instruction

func (*Create2022) EncodeToTree

func (inst *Create2022) EncodeToTree(parent treeout.Branches)

func (Create2022) GetAccounts

func (inst Create2022) GetAccounts() []*solana.AccountMeta

GetAccounts implements the AccountMetaGettable interface

func (Create2022) MarshalWithEncoder

func (inst Create2022) MarshalWithEncoder(encoder *bin.Encoder) error

func (*Create2022) SetMint

func (inst *Create2022) SetMint(mint solana.PublicKey) *Create2022

func (*Create2022) SetPayer

func (inst *Create2022) SetPayer(payer solana.PublicKey) *Create2022

func (*Create2022) SetWallet

func (inst *Create2022) SetWallet(wallet solana.PublicKey) *Create2022

func (*Create2022) UnmarshalWithDecoder

func (inst *Create2022) UnmarshalWithDecoder(decoder *bin.Decoder) error

func (*Create2022) Validate

func (inst *Create2022) Validate() error

func (Create2022) ValidateAndBuild

func (inst Create2022) ValidateAndBuild() (*Instruction, error)

ValidateAndBuild validates the instruction accounts. If there is a validation error, return the error. Otherwise, build and return the instruction.

type Instruction

type Instruction struct {
	bin.BaseVariant
}

Instruction is a base type for all instructions.

func (*Instruction) Accounts

func (inst *Instruction) Accounts() []*solana.AccountMeta

Accounts returns the list of accounts that this instruction requires.

func (*Instruction) Data

func (inst *Instruction) Data() ([]byte, error)

Data serializes the instruction data.

func (*Instruction) MarshalWithEncoder

func (inst *Instruction) MarshalWithEncoder(encoder *bin.Encoder) error

MarshalWithEncoder implements the bin.EncoderDecoder interface

func (*Instruction) ProgramID

func (inst *Instruction) ProgramID() solana.PublicKey

ProgramID returns the program ID.

func (*Instruction) UnmarshalWithDecoder

func (inst *Instruction) UnmarshalWithDecoder(decoder *bin.Decoder) error

UnmarshalWithDecoder implements the bin.EncoderDecoder interface

type InstructionImpl

type InstructionImpl interface {
	bin.EncoderDecoder
	Validate() error
}

InstructionImpl is the interface that all instructions implement.

Jump to

Keyboard shortcuts

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