github.com/gagliardetto/solana-go@v1.11.0/programs/compute-budget/RequestHeapFrame.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 computebudget 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 const MAX_HEAP_FRAME_BYTES uint32 = 256 * 1024 27 28 type RequestHeapFrame struct { 29 HeapSize uint32 30 } 31 32 func (obj *RequestHeapFrame) SetAccounts(accounts []*ag_solanago.AccountMeta) error { 33 return nil 34 } 35 36 func (slice RequestHeapFrame) GetAccounts() (accounts []*ag_solanago.AccountMeta) { 37 return 38 } 39 40 // NewRequestHeapFrameInstructionBuilder creates a new `RequestHeapFrame` instruction builder. 41 func NewRequestHeapFrameInstructionBuilder() *RequestHeapFrame { 42 nd := &RequestHeapFrame{} 43 return nd 44 } 45 46 // Request heap frame in bytes 47 func (inst *RequestHeapFrame) SetHeapSize(heapSize uint32) *RequestHeapFrame { 48 inst.HeapSize = heapSize 49 return inst 50 } 51 52 func (inst RequestHeapFrame) Build() *Instruction { 53 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 54 Impl: inst, 55 TypeID: ag_binary.TypeIDFromUint8(Instruction_RequestHeapFrame), 56 }} 57 } 58 59 // ValidateAndBuild validates the instruction parameters and accounts; 60 // if there is a validation error, it returns the error. 61 // Otherwise, it builds and returns the instruction. 62 func (inst RequestHeapFrame) ValidateAndBuild() (*Instruction, error) { 63 if err := inst.Validate(); err != nil { 64 return nil, err 65 } 66 return inst.Build(), nil 67 } 68 69 func (inst *RequestHeapFrame) Validate() error { 70 // Check whether all (required) parameters are set: 71 { 72 if inst.HeapSize == 0 { 73 return errors.New("HeapSize parameter is not set") 74 } 75 if inst.HeapSize > MAX_HEAP_FRAME_BYTES { 76 return errors.New("HeapSize parameter exceeds the maximum heap frame bytes") 77 } 78 } 79 return nil 80 } 81 82 func (inst *RequestHeapFrame) EncodeToTree(parent ag_treeout.Branches) { 83 parent.Child(ag_format.Program(ProgramName, ProgramID)). 84 // 85 ParentFunc(func(programBranch ag_treeout.Branches) { 86 programBranch.Child(ag_format.Instruction("RequestHeapFrame")). 87 // 88 ParentFunc(func(instructionBranch ag_treeout.Branches) { 89 90 // Parameters of the instruction: 91 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 92 paramsBranch.Child(ag_format.Param("HeapSize", inst.HeapSize)) 93 }) 94 }) 95 }) 96 } 97 98 func (obj RequestHeapFrame) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 99 // Serialize `HeapSize` param: 100 err = encoder.Encode(obj.HeapSize) 101 if err != nil { 102 return err 103 } 104 return nil 105 } 106 func (obj *RequestHeapFrame) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 107 // Deserialize `HeapSize`: 108 err = decoder.Decode(&obj.HeapSize) 109 if err != nil { 110 return err 111 } 112 return nil 113 } 114 115 // NewRequestHeapFrameInstruction declares a new RequestHeapFrame instruction with the provided parameters and accounts. 116 func NewRequestHeapFrameInstruction( 117 // Parameters: 118 heapSize uint32, 119 ) *RequestHeapFrame { 120 return NewRequestHeapFrameInstructionBuilder().SetHeapSize(heapSize) 121 }