github.com/gagliardetto/solana-go@v1.11.0/programs/token/InitializeAccount2.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 // Like InitializeAccount, but the owner pubkey is passed via instruction data 27 // rather than the accounts list. This variant may be preferable when using 28 // Cross Program Invocation from an instruction that does not need the owner's 29 // `AccountInfo` otherwise. 30 type InitializeAccount2 struct { 31 // The new account's owner/multisignature. 32 Owner *ag_solanago.PublicKey 33 34 // [0] = [WRITE] account 35 // ··········· The account to initialize. 36 // 37 // [1] = [] mint 38 // ··········· The mint this account will be associated with. 39 // 40 // [2] = [] $(SysVarRentPubkey) 41 // ··········· Rent sysvar. 42 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 43 } 44 45 // NewInitializeAccount2InstructionBuilder creates a new `InitializeAccount2` instruction builder. 46 func NewInitializeAccount2InstructionBuilder() *InitializeAccount2 { 47 nd := &InitializeAccount2{ 48 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3), 49 } 50 nd.AccountMetaSlice[2] = ag_solanago.Meta(ag_solanago.SysVarRentPubkey) 51 return nd 52 } 53 54 // SetOwner sets the "owner" parameter. 55 // The new account's owner/multisignature. 56 func (inst *InitializeAccount2) SetOwner(owner ag_solanago.PublicKey) *InitializeAccount2 { 57 inst.Owner = &owner 58 return inst 59 } 60 61 // SetAccount sets the "account" account. 62 // The account to initialize. 63 func (inst *InitializeAccount2) SetAccount(account ag_solanago.PublicKey) *InitializeAccount2 { 64 inst.AccountMetaSlice[0] = ag_solanago.Meta(account).WRITE() 65 return inst 66 } 67 68 // GetAccount gets the "account" account. 69 // The account to initialize. 70 func (inst *InitializeAccount2) GetAccount() *ag_solanago.AccountMeta { 71 return inst.AccountMetaSlice[0] 72 } 73 74 // SetMintAccount sets the "mint" account. 75 // The mint this account will be associated with. 76 func (inst *InitializeAccount2) SetMintAccount(mint ag_solanago.PublicKey) *InitializeAccount2 { 77 inst.AccountMetaSlice[1] = ag_solanago.Meta(mint) 78 return inst 79 } 80 81 // GetMintAccount gets the "mint" account. 82 // The mint this account will be associated with. 83 func (inst *InitializeAccount2) GetMintAccount() *ag_solanago.AccountMeta { 84 return inst.AccountMetaSlice[1] 85 } 86 87 // SetSysVarRentPubkeyAccount sets the "$(SysVarRentPubkey)" account. 88 // Rent sysvar. 89 func (inst *InitializeAccount2) SetSysVarRentPubkeyAccount(SysVarRentPubkey ag_solanago.PublicKey) *InitializeAccount2 { 90 inst.AccountMetaSlice[2] = ag_solanago.Meta(SysVarRentPubkey) 91 return inst 92 } 93 94 // GetSysVarRentPubkeyAccount gets the "$(SysVarRentPubkey)" account. 95 // Rent sysvar. 96 func (inst *InitializeAccount2) GetSysVarRentPubkeyAccount() *ag_solanago.AccountMeta { 97 return inst.AccountMetaSlice[2] 98 } 99 100 func (inst InitializeAccount2) Build() *Instruction { 101 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 102 Impl: inst, 103 TypeID: ag_binary.TypeIDFromUint8(Instruction_InitializeAccount2), 104 }} 105 } 106 107 // ValidateAndBuild validates the instruction parameters and accounts; 108 // if there is a validation error, it returns the error. 109 // Otherwise, it builds and returns the instruction. 110 func (inst InitializeAccount2) ValidateAndBuild() (*Instruction, error) { 111 if err := inst.Validate(); err != nil { 112 return nil, err 113 } 114 return inst.Build(), nil 115 } 116 117 func (inst *InitializeAccount2) Validate() error { 118 // Check whether all (required) parameters are set: 119 { 120 if inst.Owner == nil { 121 return errors.New("Owner parameter is not set") 122 } 123 } 124 125 // Check whether all (required) accounts are set: 126 { 127 if inst.AccountMetaSlice[0] == nil { 128 return errors.New("accounts.Account is not set") 129 } 130 if inst.AccountMetaSlice[1] == nil { 131 return errors.New("accounts.Mint is not set") 132 } 133 if inst.AccountMetaSlice[2] == nil { 134 return errors.New("accounts.SysVarRentPubkey is not set") 135 } 136 } 137 return nil 138 } 139 140 func (inst *InitializeAccount2) EncodeToTree(parent ag_treeout.Branches) { 141 parent.Child(ag_format.Program(ProgramName, ProgramID)). 142 // 143 ParentFunc(func(programBranch ag_treeout.Branches) { 144 programBranch.Child(ag_format.Instruction("InitializeAccount2")). 145 // 146 ParentFunc(func(instructionBranch ag_treeout.Branches) { 147 148 // Parameters of the instruction: 149 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 150 paramsBranch.Child(ag_format.Param("Owner", *inst.Owner)) 151 }) 152 153 // Accounts of the instruction: 154 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 155 accountsBranch.Child(ag_format.Meta(" account", inst.AccountMetaSlice[0])) 156 accountsBranch.Child(ag_format.Meta(" mint", inst.AccountMetaSlice[1])) 157 accountsBranch.Child(ag_format.Meta("SysVarRent", inst.AccountMetaSlice[2])) 158 }) 159 }) 160 }) 161 } 162 163 func (obj InitializeAccount2) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 164 // Serialize `Owner` param: 165 err = encoder.Encode(obj.Owner) 166 if err != nil { 167 return err 168 } 169 return nil 170 } 171 func (obj *InitializeAccount2) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 172 // Deserialize `Owner`: 173 err = decoder.Decode(&obj.Owner) 174 if err != nil { 175 return err 176 } 177 return nil 178 } 179 180 // NewInitializeAccount2Instruction declares a new InitializeAccount2 instruction with the provided parameters and accounts. 181 func NewInitializeAccount2Instruction( 182 // Parameters: 183 owner ag_solanago.PublicKey, 184 // Accounts: 185 account ag_solanago.PublicKey, 186 mint ag_solanago.PublicKey, 187 SysVarRentPubkey ag_solanago.PublicKey) *InitializeAccount2 { 188 return NewInitializeAccount2InstructionBuilder(). 189 SetOwner(owner). 190 SetAccount(account). 191 SetMintAccount(mint). 192 SetSysVarRentPubkeyAccount(SysVarRentPubkey) 193 }