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