github.com/gagliardetto/solana-go@v1.11.0/programs/system/AssignWithSeed.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 // Assign account to a program based on a seed 29 type AssignWithSeed 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 // Owner program account 37 Owner *ag_solanago.PublicKey 38 39 // [0] = [WRITE] AssignedAccount 40 // ··········· Assigned account 41 // 42 // [1] = [SIGNER] BaseAccount 43 // ··········· Base account 44 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 45 } 46 47 // NewAssignWithSeedInstructionBuilder creates a new `AssignWithSeed` instruction builder. 48 func NewAssignWithSeedInstructionBuilder() *AssignWithSeed { 49 nd := &AssignWithSeed{ 50 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 2), 51 } 52 return nd 53 } 54 55 // Base public key 56 func (inst *AssignWithSeed) SetBase(base ag_solanago.PublicKey) *AssignWithSeed { 57 inst.Base = &base 58 return inst 59 } 60 61 // String of ASCII chars, no longer than pubkey::MAX_SEED_LEN 62 func (inst *AssignWithSeed) SetSeed(seed string) *AssignWithSeed { 63 inst.Seed = &seed 64 return inst 65 } 66 67 // Owner program account 68 func (inst *AssignWithSeed) SetOwner(owner ag_solanago.PublicKey) *AssignWithSeed { 69 inst.Owner = &owner 70 return inst 71 } 72 73 // Assigned account 74 func (inst *AssignWithSeed) SetAssignedAccount(assignedAccount ag_solanago.PublicKey) *AssignWithSeed { 75 inst.AccountMetaSlice[0] = ag_solanago.Meta(assignedAccount).WRITE() 76 return inst 77 } 78 79 func (inst *AssignWithSeed) GetAssignedAccount() *ag_solanago.AccountMeta { 80 return inst.AccountMetaSlice[0] 81 } 82 83 // Base account 84 func (inst *AssignWithSeed) SetBaseAccount(baseAccount ag_solanago.PublicKey) *AssignWithSeed { 85 inst.AccountMetaSlice[1] = ag_solanago.Meta(baseAccount).SIGNER() 86 return inst 87 } 88 89 func (inst *AssignWithSeed) GetBaseAccount() *ag_solanago.AccountMeta { 90 return inst.AccountMetaSlice[1] 91 } 92 93 func (inst AssignWithSeed) Build() *Instruction { 94 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 95 Impl: inst, 96 TypeID: ag_binary.TypeIDFromUint32(Instruction_AssignWithSeed, binary.LittleEndian), 97 }} 98 } 99 100 // ValidateAndBuild validates the instruction parameters and accounts; 101 // if there is a validation error, it returns the error. 102 // Otherwise, it builds and returns the instruction. 103 func (inst AssignWithSeed) ValidateAndBuild() (*Instruction, error) { 104 if err := inst.Validate(); err != nil { 105 return nil, err 106 } 107 return inst.Build(), nil 108 } 109 110 func (inst *AssignWithSeed) Validate() error { 111 // Check whether all (required) parameters are set: 112 { 113 if inst.Base == nil { 114 return errors.New("Base parameter is not set") 115 } 116 if inst.Seed == nil { 117 return errors.New("Seed parameter is not set") 118 } 119 if inst.Owner == nil { 120 return errors.New("Owner parameter is not set") 121 } 122 } 123 124 // Check whether all accounts are set: 125 for accIndex, acc := range inst.AccountMetaSlice { 126 if acc == nil { 127 return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex) 128 } 129 } 130 return nil 131 } 132 133 func (inst *AssignWithSeed) EncodeToTree(parent ag_treeout.Branches) { 134 parent.Child(ag_format.Program(ProgramName, ProgramID)). 135 // 136 ParentFunc(func(programBranch ag_treeout.Branches) { 137 programBranch.Child(ag_format.Instruction("AssignWithSeed")). 138 // 139 ParentFunc(func(instructionBranch ag_treeout.Branches) { 140 141 // Parameters of the instruction: 142 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 143 paramsBranch.Child(ag_format.Param(" Base", *inst.Base)) 144 paramsBranch.Child(ag_format.Param(" Seed", *inst.Seed)) 145 paramsBranch.Child(ag_format.Param("Owner", *inst.Owner)) 146 }) 147 148 // Accounts of the instruction: 149 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 150 accountsBranch.Child(ag_format.Meta("Assigned", inst.AccountMetaSlice[0])) 151 accountsBranch.Child(ag_format.Meta(" Base", inst.AccountMetaSlice[1])) 152 }) 153 }) 154 }) 155 } 156 157 func (inst AssignWithSeed) MarshalWithEncoder(encoder *ag_binary.Encoder) error { 158 // Serialize `Base` param: 159 { 160 err := encoder.Encode(*inst.Base) 161 if err != nil { 162 return err 163 } 164 } 165 // Serialize `Seed` param: 166 { 167 err := encoder.WriteRustString(*inst.Seed) 168 if err != nil { 169 return err 170 } 171 } 172 // Serialize `Owner` param: 173 { 174 err := encoder.Encode(*inst.Owner) 175 if err != nil { 176 return err 177 } 178 } 179 return nil 180 } 181 182 func (inst *AssignWithSeed) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error { 183 // Deserialize `Base` param: 184 { 185 err := decoder.Decode(&inst.Base) 186 if err != nil { 187 return err 188 } 189 } 190 // Deserialize `Seed` param: 191 { 192 value, err := decoder.ReadRustString() 193 if err != nil { 194 return err 195 } 196 inst.Seed = &value 197 } 198 // Deserialize `Owner` param: 199 { 200 err := decoder.Decode(&inst.Owner) 201 if err != nil { 202 return err 203 } 204 } 205 return nil 206 } 207 208 // NewAssignWithSeedInstruction declares a new AssignWithSeed instruction with the provided parameters and accounts. 209 func NewAssignWithSeedInstruction( 210 // Parameters: 211 base ag_solanago.PublicKey, 212 seed string, 213 owner ag_solanago.PublicKey, 214 // Accounts: 215 assignedAccount ag_solanago.PublicKey, 216 baseAccount ag_solanago.PublicKey) *AssignWithSeed { 217 return NewAssignWithSeedInstructionBuilder(). 218 SetBase(base). 219 SetSeed(seed). 220 SetOwner(owner). 221 SetAssignedAccount(assignedAccount). 222 SetBaseAccount(baseAccount) 223 }