github.com/gagliardetto/solana-go@v1.11.0/programs/compute-budget/SetComputeUnitLimit.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_COMPUTE_UNIT_LIMIT = 1400000 27 28 type SetComputeUnitLimit struct { 29 Units uint32 30 } 31 32 func (obj *SetComputeUnitLimit) SetAccounts(accounts []*ag_solanago.AccountMeta) error { 33 return nil 34 } 35 36 func (slice SetComputeUnitLimit) GetAccounts() (accounts []*ag_solanago.AccountMeta) { 37 return 38 } 39 40 // NewSetComputeUnitLimitInstructionBuilder creates a new `SetComputeUnitLimit` instruction builder. 41 func NewSetComputeUnitLimitInstructionBuilder() *SetComputeUnitLimit { 42 nd := &SetComputeUnitLimit{} 43 return nd 44 } 45 46 // Unit limit 47 func (inst *SetComputeUnitLimit) SetUnits(units uint32) *SetComputeUnitLimit { 48 inst.Units = units 49 return inst 50 } 51 52 func (inst SetComputeUnitLimit) Build() *Instruction { 53 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 54 Impl: inst, 55 TypeID: ag_binary.TypeIDFromUint8(Instruction_SetComputeUnitLimit), 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 SetComputeUnitLimit) 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 *SetComputeUnitLimit) Validate() error { 70 // Check whether all (required) parameters are set: 71 { 72 if inst.Units == 0 { 73 return errors.New("Units parameter is not set") 74 } 75 if inst.Units > MAX_COMPUTE_UNIT_LIMIT { 76 return errors.New("Units parameter exceeds the maximum compute unit") 77 } 78 } 79 return nil 80 } 81 82 func (inst *SetComputeUnitLimit) 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("SetComputeUnitLimit")). 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("Units", inst.Units)) 93 }) 94 }) 95 }) 96 } 97 98 func (obj SetComputeUnitLimit) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 99 // Serialize `Units` param: 100 err = encoder.Encode(obj.Units) 101 if err != nil { 102 return err 103 } 104 return nil 105 } 106 func (obj *SetComputeUnitLimit) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 107 // Deserialize `Units`: 108 err = decoder.Decode(&obj.Units) 109 if err != nil { 110 return err 111 } 112 return nil 113 } 114 115 // NewSetComputeUnitLimitInstruction declares a new SetComputeUnitLimit instruction with the provided parameters and accounts. 116 func NewSetComputeUnitLimitInstruction( 117 // Parameters: 118 units uint32, 119 ) *SetComputeUnitLimit { 120 return NewSetComputeUnitLimitInstructionBuilder().SetUnits(units) 121 }