github.com/gagliardetto/solana-go@v1.11.0/programs/system/InitializeNonceAccount.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 // Drive state of Uninitalized nonce account to Initialized, setting the nonce value 29 type InitializeNonceAccount struct { 30 // The Pubkey parameter specifies the entity authorized to execute nonce instruction on the account. 31 // No signatures are required to execute this instruction, enabling derived nonce account addresses. 32 Authorized *ag_solanago.PublicKey 33 34 // [0] = [WRITE] NonceAccount 35 // ··········· Nonce account 36 // 37 // [1] = [] $(SysVarRecentBlockHashesPubkey) 38 // ··········· RecentBlockhashes sysvar 39 // 40 // [2] = [] $(SysVarRentPubkey) 41 // ··········· Rent sysvar 42 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 43 } 44 45 // NewInitializeNonceAccountInstructionBuilder creates a new `InitializeNonceAccount` instruction builder. 46 func NewInitializeNonceAccountInstructionBuilder() *InitializeNonceAccount { 47 nd := &InitializeNonceAccount{ 48 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3), 49 } 50 nd.AccountMetaSlice[1] = ag_solanago.Meta(ag_solanago.SysVarRecentBlockHashesPubkey) 51 nd.AccountMetaSlice[2] = ag_solanago.Meta(ag_solanago.SysVarRentPubkey) 52 return nd 53 } 54 55 // The Pubkey parameter specifies the entity authorized to execute nonce instruction on the account. 56 // No signatures are required to execute this instruction, enabling derived nonce account addresses. 57 func (inst *InitializeNonceAccount) SetAuthorized(authorized ag_solanago.PublicKey) *InitializeNonceAccount { 58 inst.Authorized = &authorized 59 return inst 60 } 61 62 // Nonce account 63 func (inst *InitializeNonceAccount) SetNonceAccount(nonceAccount ag_solanago.PublicKey) *InitializeNonceAccount { 64 inst.AccountMetaSlice[0] = ag_solanago.Meta(nonceAccount).WRITE() 65 return inst 66 } 67 68 func (inst *InitializeNonceAccount) GetNonceAccount() *ag_solanago.AccountMeta { 69 return inst.AccountMetaSlice[0] 70 } 71 72 // RecentBlockhashes sysvar 73 func (inst *InitializeNonceAccount) SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey ag_solanago.PublicKey) *InitializeNonceAccount { 74 inst.AccountMetaSlice[1] = ag_solanago.Meta(SysVarRecentBlockHashesPubkey) 75 return inst 76 } 77 78 func (inst *InitializeNonceAccount) GetSysVarRecentBlockHashesPubkeyAccount() *ag_solanago.AccountMeta { 79 return inst.AccountMetaSlice[1] 80 } 81 82 // Rent sysvar 83 func (inst *InitializeNonceAccount) SetSysVarRentPubkeyAccount(SysVarRentPubkey ag_solanago.PublicKey) *InitializeNonceAccount { 84 inst.AccountMetaSlice[2] = ag_solanago.Meta(SysVarRentPubkey) 85 return inst 86 } 87 88 func (inst *InitializeNonceAccount) GetSysVarRentPubkeyAccount() *ag_solanago.AccountMeta { 89 return inst.AccountMetaSlice[2] 90 } 91 92 func (inst InitializeNonceAccount) Build() *Instruction { 93 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 94 Impl: inst, 95 TypeID: ag_binary.TypeIDFromUint32(Instruction_InitializeNonceAccount, binary.LittleEndian), 96 }} 97 } 98 99 // ValidateAndBuild validates the instruction parameters and accounts; 100 // if there is a validation error, it returns the error. 101 // Otherwise, it builds and returns the instruction. 102 func (inst InitializeNonceAccount) ValidateAndBuild() (*Instruction, error) { 103 if err := inst.Validate(); err != nil { 104 return nil, err 105 } 106 return inst.Build(), nil 107 } 108 109 func (inst *InitializeNonceAccount) Validate() error { 110 // Check whether all (required) parameters are set: 111 { 112 if inst.Authorized == nil { 113 return errors.New("Authorized parameter is not set") 114 } 115 } 116 117 // Check whether all accounts are set: 118 for accIndex, acc := range inst.AccountMetaSlice { 119 if acc == nil { 120 return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex) 121 } 122 } 123 return nil 124 } 125 126 func (inst *InitializeNonceAccount) EncodeToTree(parent ag_treeout.Branches) { 127 parent.Child(ag_format.Program(ProgramName, ProgramID)). 128 // 129 ParentFunc(func(programBranch ag_treeout.Branches) { 130 programBranch.Child(ag_format.Instruction("InitializeNonceAccount")). 131 // 132 ParentFunc(func(instructionBranch ag_treeout.Branches) { 133 134 // Parameters of the instruction: 135 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 136 paramsBranch.Child(ag_format.Param("Authorized", *inst.Authorized)) 137 }) 138 139 // Accounts of the instruction: 140 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 141 accountsBranch.Child(ag_format.Meta(" Nonce", inst.AccountMetaSlice[0])) 142 accountsBranch.Child(ag_format.Meta("SysVarRecentBlockHashes", inst.AccountMetaSlice[1])) 143 accountsBranch.Child(ag_format.Meta(" SysVarRent", inst.AccountMetaSlice[2])) 144 }) 145 }) 146 }) 147 } 148 149 func (inst InitializeNonceAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) error { 150 // Serialize `Authorized` param: 151 { 152 err := encoder.Encode(*inst.Authorized) 153 if err != nil { 154 return err 155 } 156 } 157 return nil 158 } 159 160 func (inst *InitializeNonceAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error { 161 // Deserialize `Authorized` param: 162 { 163 err := decoder.Decode(&inst.Authorized) 164 if err != nil { 165 return err 166 } 167 } 168 return nil 169 } 170 171 // NewInitializeNonceAccountInstruction declares a new InitializeNonceAccount instruction with the provided parameters and accounts. 172 func NewInitializeNonceAccountInstruction( 173 // Parameters: 174 authorized ag_solanago.PublicKey, 175 // Accounts: 176 nonceAccount ag_solanago.PublicKey, 177 SysVarRecentBlockHashesPubkey ag_solanago.PublicKey, 178 SysVarRentPubkey ag_solanago.PublicKey) *InitializeNonceAccount { 179 return NewInitializeNonceAccountInstructionBuilder(). 180 SetAuthorized(authorized). 181 SetNonceAccount(nonceAccount). 182 SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey). 183 SetSysVarRentPubkeyAccount(SysVarRentPubkey) 184 }