|
| 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 loaderv2 |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/binary" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + ag_binary "github.com/gagliardetto/binary" |
| 22 | + ag_solanago "github.com/gagliardetto/solana-go" |
| 23 | + ag_format "github.com/gagliardetto/solana-go/text/format" |
| 24 | + ag_treeout "github.com/gagliardetto/treeout" |
| 25 | +) |
| 26 | + |
| 27 | +// Finalize an account loaded with program data for execution. |
| 28 | +// |
| 29 | +// Account references: |
| 30 | +// [0] = [WRITE, SIGNER] Account to prepare for execution |
| 31 | +// [1] = [] Rent sysvar |
| 32 | +type Finalize struct { |
| 33 | + ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` |
| 34 | +} |
| 35 | + |
| 36 | +func NewFinalizeInstructionBuilder() *Finalize { |
| 37 | + return &Finalize{ |
| 38 | + AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 2), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (inst *Finalize) SetAccount(account ag_solanago.PublicKey) *Finalize { |
| 43 | + inst.AccountMetaSlice[0] = ag_solanago.Meta(account).WRITE().SIGNER() |
| 44 | + return inst |
| 45 | +} |
| 46 | + |
| 47 | +func (inst *Finalize) GetAccount() *ag_solanago.AccountMeta { |
| 48 | + return inst.AccountMetaSlice[0] |
| 49 | +} |
| 50 | + |
| 51 | +// SetRentSysvar attaches the rent sysvar account. The helper NewFinalizeInstruction |
| 52 | +// uses the canonical sysvar ID; this setter is exposed for callers that wish |
| 53 | +// to override it. |
| 54 | +func (inst *Finalize) SetRentSysvar(rent ag_solanago.PublicKey) *Finalize { |
| 55 | + inst.AccountMetaSlice[1] = ag_solanago.Meta(rent) |
| 56 | + return inst |
| 57 | +} |
| 58 | + |
| 59 | +func (inst *Finalize) GetRentSysvar() *ag_solanago.AccountMeta { |
| 60 | + return inst.AccountMetaSlice[1] |
| 61 | +} |
| 62 | + |
| 63 | +func (inst Finalize) Build() *Instruction { |
| 64 | + return &Instruction{BaseVariant: ag_binary.BaseVariant{ |
| 65 | + Impl: inst, |
| 66 | + TypeID: ag_binary.TypeIDFromUint32(Instruction_Finalize, binary.LittleEndian), |
| 67 | + }} |
| 68 | +} |
| 69 | + |
| 70 | +func (inst Finalize) ValidateAndBuild() (*Instruction, error) { |
| 71 | + if err := inst.Validate(); err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + return inst.Build(), nil |
| 75 | +} |
| 76 | + |
| 77 | +func (inst *Finalize) Validate() error { |
| 78 | + for i, acc := range inst.AccountMetaSlice { |
| 79 | + if acc == nil { |
| 80 | + return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", i) |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (inst *Finalize) EncodeToTree(parent ag_treeout.Branches) { |
| 87 | + parent.Child(ag_format.Program(ProgramName, ProgramID)). |
| 88 | + ParentFunc(func(programBranch ag_treeout.Branches) { |
| 89 | + programBranch.Child(ag_format.Instruction("Finalize")). |
| 90 | + ParentFunc(func(instructionBranch ag_treeout.Branches) { |
| 91 | + instructionBranch.Child("Accounts").ParentFunc(func(a ag_treeout.Branches) { |
| 92 | + a.Child(ag_format.Meta("Account", inst.AccountMetaSlice[0])) |
| 93 | + a.Child(ag_format.Meta(" Rent", inst.AccountMetaSlice[1])) |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +// Finalize carries no payload: the discriminant alone is the data. |
| 100 | +func (inst Finalize) MarshalWithEncoder(_ *ag_binary.Encoder) error { return nil } |
| 101 | +func (inst *Finalize) UnmarshalWithDecoder(_ *ag_binary.Decoder) error { return nil } |
| 102 | + |
| 103 | +func NewFinalizeInstruction(account ag_solanago.PublicKey) *Finalize { |
| 104 | + return NewFinalizeInstructionBuilder(). |
| 105 | + SetAccount(account). |
| 106 | + SetRentSysvar(ag_solanago.SysVarRentPubkey) |
| 107 | +} |
0 commit comments