github.com/gagliardetto/solana-go@v1.11.0/programs/associated-token-account/instructions.go (about) 1 // Copyright 2021 github.com/gagliardetto 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package associatedtokenaccount 16 17 import ( 18 "fmt" 19 20 spew "github.com/davecgh/go-spew/spew" 21 bin "github.com/gagliardetto/binary" 22 solana "github.com/gagliardetto/solana-go" 23 text "github.com/gagliardetto/solana-go/text" 24 treeout "github.com/gagliardetto/treeout" 25 ) 26 27 var ProgramID solana.PublicKey = solana.SPLAssociatedTokenAccountProgramID 28 29 func SetProgramID(pubkey solana.PublicKey) { 30 ProgramID = pubkey 31 solana.RegisterInstructionDecoder(ProgramID, registryDecodeInstruction) 32 } 33 34 const ProgramName = "AssociatedTokenAccount" 35 36 func init() { 37 solana.RegisterInstructionDecoder(ProgramID, registryDecodeInstruction) 38 } 39 40 type Instruction struct { 41 bin.BaseVariant 42 } 43 44 func (inst *Instruction) EncodeToTree(parent treeout.Branches) { 45 if enToTree, ok := inst.Impl.(text.EncodableToTree); ok { 46 enToTree.EncodeToTree(parent) 47 } else { 48 parent.Child(spew.Sdump(inst)) 49 } 50 } 51 52 var InstructionImplDef = bin.NewVariantDefinition( 53 bin.NoTypeIDEncoding, // NOTE: the associated-token-account program has no ID encoding. 54 []bin.VariantType{ 55 { 56 "Create", (*Create)(nil), 57 }, 58 }, 59 ) 60 61 func (inst *Instruction) ProgramID() solana.PublicKey { 62 return ProgramID 63 } 64 65 func (inst *Instruction) Accounts() (out []*solana.AccountMeta) { 66 return inst.Impl.(solana.AccountsGettable).GetAccounts() 67 } 68 69 func (inst *Instruction) Data() ([]byte, error) { 70 return []byte{}, nil 71 } 72 73 func (inst *Instruction) TextEncode(encoder *text.Encoder, option *text.Option) error { 74 return encoder.Encode(inst.Impl, option) 75 } 76 77 func (inst *Instruction) UnmarshalWithDecoder(decoder *bin.Decoder) error { 78 return inst.BaseVariant.UnmarshalBinaryVariant(decoder, InstructionImplDef) 79 } 80 81 func (inst Instruction) MarshalWithEncoder(encoder *bin.Encoder) error { 82 return encoder.Encode(inst.Impl) 83 } 84 85 func registryDecodeInstruction(accounts []*solana.AccountMeta, data []byte) (interface{}, error) { 86 inst, err := DecodeInstruction(accounts, data) 87 if err != nil { 88 return nil, err 89 } 90 return inst, nil 91 } 92 93 func DecodeInstruction(accounts []*solana.AccountMeta, data []byte) (*Instruction, error) { 94 inst := new(Instruction) 95 if err := bin.NewBinDecoder(data).Decode(inst); err != nil { 96 return nil, fmt.Errorf("unable to decode instruction: %w", err) 97 } 98 if v, ok := inst.Impl.(solana.AccountsSettable); ok { 99 err := v.SetAccounts(accounts) 100 if err != nil { 101 return nil, fmt.Errorf("unable to set accounts for instruction: %w", err) 102 } 103 } 104 return inst, nil 105 }