github.com/gagliardetto/solana-go@v1.11.0/programs/token/SyncNative.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 token 16 17 import ( 18 "errors" 19 20 ag_binary "github.com/gagliardetto/binary" 21 ag_solanago "github.com/gagliardetto/solana-go" 22 ag_format "github.com/gagliardetto/solana-go/text/format" 23 ag_treeout "github.com/gagliardetto/treeout" 24 ) 25 26 // Given a wrapped / native token account (a token account containing SOL) 27 // updates its amount field based on the account's underlying `lamports`. 28 // This is useful if a non-wrapped SOL account uses `system_instruction::transfer` 29 // to move lamports to a wrapped token account, and needs to have its token 30 // `amount` field updated. 31 type SyncNative struct { 32 33 // [0] = [WRITE] tokenAccount 34 // ··········· The native token account to sync with its underlying lamports. 35 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 36 } 37 38 // NewSyncNativeInstructionBuilder creates a new `SyncNative` instruction builder. 39 func NewSyncNativeInstructionBuilder() *SyncNative { 40 nd := &SyncNative{ 41 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 1), 42 } 43 return nd 44 } 45 46 // SetTokenAccount sets the "tokenAccount" account. 47 // The native token account to sync with its underlying lamports. 48 func (inst *SyncNative) SetTokenAccount(tokenAccount ag_solanago.PublicKey) *SyncNative { 49 inst.AccountMetaSlice[0] = ag_solanago.Meta(tokenAccount).WRITE() 50 return inst 51 } 52 53 // GetTokenAccount gets the "tokenAccount" account. 54 // The native token account to sync with its underlying lamports. 55 func (inst *SyncNative) GetTokenAccount() *ag_solanago.AccountMeta { 56 return inst.AccountMetaSlice[0] 57 } 58 59 func (inst SyncNative) Build() *Instruction { 60 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 61 Impl: inst, 62 TypeID: ag_binary.TypeIDFromUint8(Instruction_SyncNative), 63 }} 64 } 65 66 // ValidateAndBuild validates the instruction parameters and accounts; 67 // if there is a validation error, it returns the error. 68 // Otherwise, it builds and returns the instruction. 69 func (inst SyncNative) ValidateAndBuild() (*Instruction, error) { 70 if err := inst.Validate(); err != nil { 71 return nil, err 72 } 73 return inst.Build(), nil 74 } 75 76 func (inst *SyncNative) Validate() error { 77 // Check whether all (required) accounts are set: 78 { 79 if inst.AccountMetaSlice[0] == nil { 80 return errors.New("accounts.TokenAccount is not set") 81 } 82 } 83 return nil 84 } 85 86 func (inst *SyncNative) EncodeToTree(parent ag_treeout.Branches) { 87 parent.Child(ag_format.Program(ProgramName, ProgramID)). 88 // 89 ParentFunc(func(programBranch ag_treeout.Branches) { 90 programBranch.Child(ag_format.Instruction("SyncNative")). 91 // 92 ParentFunc(func(instructionBranch ag_treeout.Branches) { 93 94 // Parameters of the instruction: 95 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {}) 96 97 // Accounts of the instruction: 98 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 99 accountsBranch.Child(ag_format.Meta("tokenAccount", inst.AccountMetaSlice[0])) 100 }) 101 }) 102 }) 103 } 104 105 func (obj SyncNative) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 106 return nil 107 } 108 func (obj *SyncNative) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 109 return nil 110 } 111 112 // NewSyncNativeInstruction declares a new SyncNative instruction with the provided parameters and accounts. 113 func NewSyncNativeInstruction( 114 // Accounts: 115 tokenAccount ag_solanago.PublicKey) *SyncNative { 116 return NewSyncNativeInstructionBuilder(). 117 SetTokenAccount(tokenAccount) 118 }