github.com/gagliardetto/solana-go@v1.11.0/programs/system/CreateAccountWithSeed.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 system 16 17 import ( 18 "encoding/binary" 19 "errors" 20 "fmt" 21 22 ag_binary "github.com/gagliardetto/binary" 23 ag_solanago "github.com/gagliardetto/solana-go" 24 ag_format "github.com/gagliardetto/solana-go/text/format" 25 ag_treeout "github.com/gagliardetto/treeout" 26 ) 27 28 // Create a new account at an address derived from a base pubkey and a seed 29 type CreateAccountWithSeed struct { 30 // Base public key 31 Base *ag_solanago.PublicKey 32 33 // String of ASCII chars, no longer than Pubkey::MAX_SEED_LEN 34 Seed *string 35 36 // Number of lamports to transfer to the new account 37 Lamports *uint64 38 39 // Number of bytes of memory to allocate 40 Space *uint64 41 42 // Owner program account address 43 Owner *ag_solanago.PublicKey 44 45 // [0] = [WRITE, SIGNER] FundingAccount 46 // ··········· Funding account 47 // 48 // [1] = [WRITE] CreatedAccount 49 // ··········· Created account 50 // 51 // [2] = [SIGNER] BaseAccount 52 // ··········· Base account 53 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 54 } 55 56 // NewCreateAccountWithSeedInstructionBuilder creates a new `CreateAccountWithSeed` instruction builder. 57 func NewCreateAccountWithSeedInstructionBuilder() *CreateAccountWithSeed { 58 nd := &CreateAccountWithSeed{ 59 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3), 60 } 61 return nd 62 } 63 64 // Base public key 65 func (inst *CreateAccountWithSeed) SetBase(base ag_solanago.PublicKey) *CreateAccountWithSeed { 66 inst.Base = &base 67 return inst 68 } 69 70 // String of ASCII chars, no longer than Pubkey::MAX_SEED_LEN 71 func (inst *CreateAccountWithSeed) SetSeed(seed string) *CreateAccountWithSeed { 72 inst.Seed = &seed 73 return inst 74 } 75 76 // Number of lamports to transfer to the new account 77 func (inst *CreateAccountWithSeed) SetLamports(lamports uint64) *CreateAccountWithSeed { 78 inst.Lamports = &lamports 79 return inst 80 } 81 82 // Number of bytes of memory to allocate 83 func (inst *CreateAccountWithSeed) SetSpace(space uint64) *CreateAccountWithSeed { 84 inst.Space = &space 85 return inst 86 } 87 88 // Owner program account address 89 func (inst *CreateAccountWithSeed) SetOwner(owner ag_solanago.PublicKey) *CreateAccountWithSeed { 90 inst.Owner = &owner 91 return inst 92 } 93 94 // Funding account 95 func (inst *CreateAccountWithSeed) SetFundingAccount(fundingAccount ag_solanago.PublicKey) *CreateAccountWithSeed { 96 inst.AccountMetaSlice[0] = ag_solanago.Meta(fundingAccount).WRITE().SIGNER() 97 return inst 98 } 99 100 func (inst *CreateAccountWithSeed) GetFundingAccount() *ag_solanago.AccountMeta { 101 return inst.AccountMetaSlice[0] 102 } 103 104 // Created account 105 func (inst *CreateAccountWithSeed) SetCreatedAccount(createdAccount ag_solanago.PublicKey) *CreateAccountWithSeed { 106 inst.AccountMetaSlice[1] = ag_solanago.Meta(createdAccount).WRITE() 107 return inst 108 } 109 110 func (inst *CreateAccountWithSeed) GetCreatedAccount() *ag_solanago.AccountMeta { 111 return inst.AccountMetaSlice[1] 112 } 113 114 // Base account 115 func (inst *CreateAccountWithSeed) SetBaseAccount(baseAccount ag_solanago.PublicKey) *CreateAccountWithSeed { 116 inst.AccountMetaSlice[2] = ag_solanago.Meta(baseAccount).SIGNER() 117 return inst 118 } 119 120 func (inst *CreateAccountWithSeed) GetBaseAccount() *ag_solanago.AccountMeta { 121 return inst.AccountMetaSlice[2] 122 } 123 124 func (inst CreateAccountWithSeed) Build() *Instruction { 125 { 126 if !inst.Base.Equals(inst.GetFundingAccount().PublicKey) { 127 inst.AccountMetaSlice[2] = ag_solanago.Meta(*inst.Base).SIGNER() 128 } 129 } 130 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 131 Impl: inst, 132 TypeID: ag_binary.TypeIDFromUint32(Instruction_CreateAccountWithSeed, binary.LittleEndian), 133 }} 134 } 135 136 // ValidateAndBuild validates the instruction parameters and accounts; 137 // if there is a validation error, it returns the error. 138 // Otherwise, it builds and returns the instruction. 139 func (inst CreateAccountWithSeed) ValidateAndBuild() (*Instruction, error) { 140 if err := inst.Validate(); err != nil { 141 return nil, err 142 } 143 return inst.Build(), nil 144 } 145 146 func (inst *CreateAccountWithSeed) Validate() error { 147 // Check whether all (required) parameters are set: 148 { 149 if inst.Base == nil { 150 return errors.New("Base parameter is not set") 151 } 152 if inst.Seed == nil { 153 return errors.New("Seed parameter is not set") 154 } 155 if inst.Lamports == nil { 156 return errors.New("Lamports parameter is not set") 157 } 158 if inst.Space == nil { 159 return errors.New("Space parameter is not set") 160 } 161 if inst.Owner == nil { 162 return errors.New("Owner parameter is not set") 163 } 164 } 165 166 // Check whether all accounts are set: 167 { 168 if inst.AccountMetaSlice[0] == nil { 169 return fmt.Errorf("FundingAccount is not set") 170 } 171 if inst.AccountMetaSlice[1] == nil { 172 return fmt.Errorf("CreatedAccount is not set") 173 } 174 } 175 return nil 176 } 177 178 func (inst *CreateAccountWithSeed) EncodeToTree(parent ag_treeout.Branches) { 179 parent.Child(ag_format.Program(ProgramName, ProgramID)). 180 // 181 ParentFunc(func(programBranch ag_treeout.Branches) { 182 programBranch.Child(ag_format.Instruction("CreateAccountWithSeed")). 183 // 184 ParentFunc(func(instructionBranch ag_treeout.Branches) { 185 186 // Parameters of the instruction: 187 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 188 paramsBranch.Child(ag_format.Param(" Base", *inst.Base)) 189 paramsBranch.Child(ag_format.Param(" Seed", *inst.Seed)) 190 paramsBranch.Child(ag_format.Param("Lamports", *inst.Lamports)) 191 paramsBranch.Child(ag_format.Param(" Space", *inst.Space)) 192 paramsBranch.Child(ag_format.Param(" Owner", *inst.Owner)) 193 }) 194 195 // Accounts of the instruction: 196 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 197 accountsBranch.Child(ag_format.Meta("Funding", inst.AccountMetaSlice[0])) 198 accountsBranch.Child(ag_format.Meta("Created", inst.AccountMetaSlice[1])) 199 accountsBranch.Child(ag_format.Meta(" Base", inst.AccountMetaSlice[2])) 200 }) 201 }) 202 }) 203 } 204 205 func (inst CreateAccountWithSeed) MarshalWithEncoder(encoder *ag_binary.Encoder) error { 206 // Serialize `Base` param: 207 { 208 err := encoder.Encode(*inst.Base) 209 if err != nil { 210 return err 211 } 212 } 213 // Serialize `Seed` param: 214 { 215 err := encoder.WriteRustString(*inst.Seed) 216 if err != nil { 217 return err 218 } 219 } 220 // Serialize `Lamports` param: 221 { 222 err := encoder.Encode(*inst.Lamports) 223 if err != nil { 224 return err 225 } 226 } 227 // Serialize `Space` param: 228 { 229 err := encoder.Encode(*inst.Space) 230 if err != nil { 231 return err 232 } 233 } 234 // Serialize `Owner` param: 235 { 236 err := encoder.Encode(*inst.Owner) 237 if err != nil { 238 return err 239 } 240 } 241 return nil 242 } 243 244 func (inst *CreateAccountWithSeed) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error { 245 // Deserialize `Base` param: 246 { 247 err := decoder.Decode(&inst.Base) 248 if err != nil { 249 return err 250 } 251 } 252 // Deserialize `Seed` param: 253 { 254 value, err := decoder.ReadRustString() 255 if err != nil { 256 return err 257 } 258 inst.Seed = &value 259 } 260 // Deserialize `Lamports` param: 261 { 262 err := decoder.Decode(&inst.Lamports) 263 if err != nil { 264 return err 265 } 266 } 267 // Deserialize `Space` param: 268 { 269 err := decoder.Decode(&inst.Space) 270 if err != nil { 271 return err 272 } 273 } 274 // Deserialize `Owner` param: 275 { 276 err := decoder.Decode(&inst.Owner) 277 if err != nil { 278 return err 279 } 280 } 281 return nil 282 } 283 284 // NewCreateAccountWithSeedInstruction declares a new CreateAccountWithSeed instruction with the provided parameters and accounts. 285 func NewCreateAccountWithSeedInstruction( 286 // Parameters: 287 base ag_solanago.PublicKey, 288 seed string, 289 lamports uint64, 290 space uint64, 291 owner ag_solanago.PublicKey, 292 // Accounts: 293 fundingAccount ag_solanago.PublicKey, 294 createdAccount ag_solanago.PublicKey, 295 baseAccount ag_solanago.PublicKey) *CreateAccountWithSeed { 296 return NewCreateAccountWithSeedInstructionBuilder(). 297 SetBase(base). 298 SetSeed(seed). 299 SetLamports(lamports). 300 SetSpace(space). 301 SetOwner(owner). 302 SetFundingAccount(fundingAccount). 303 SetCreatedAccount(createdAccount). 304 SetBaseAccount(baseAccount) 305 }