github.com/gagliardetto/solana-go@v1.11.0/programs/compute-budget/RequestUnitsDeprecated.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  type RequestUnitsDeprecated struct {
    27  	// Units to request
    28  	Units uint32
    29  
    30  	// Additional fee to add
    31  	AdditionalFee uint32
    32  }
    33  
    34  func (obj *RequestUnitsDeprecated) SetAccounts(accounts []*ag_solanago.AccountMeta) error {
    35  	return nil
    36  }
    37  
    38  func (slice RequestUnitsDeprecated) GetAccounts() (accounts []*ag_solanago.AccountMeta) {
    39  	return
    40  }
    41  
    42  // NewRequestUnitsDeprecatedInstructionBuilder creates a new `RequestUnitsDeprecated` instruction builder.
    43  func NewRequestUnitsDeprecatedInstructionBuilder() *RequestUnitsDeprecated {
    44  	nd := &RequestUnitsDeprecated{}
    45  	return nd
    46  }
    47  
    48  // Units to request
    49  func (inst *RequestUnitsDeprecated) SetUnits(units uint32) *RequestUnitsDeprecated {
    50  	inst.Units = units
    51  	return inst
    52  }
    53  
    54  // Additional fee to add
    55  func (inst *RequestUnitsDeprecated) SetAdditionalFee(additionalFee uint32) *RequestUnitsDeprecated {
    56  	inst.AdditionalFee = additionalFee
    57  	return inst
    58  }
    59  
    60  func (inst RequestUnitsDeprecated) Build() *Instruction {
    61  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
    62  		Impl:   inst,
    63  		TypeID: ag_binary.TypeIDFromUint8(Instruction_RequestUnitsDeprecated),
    64  	}}
    65  }
    66  
    67  // ValidateAndBuild validates the instruction parameters and accounts;
    68  // if there is a validation error, it returns the error.
    69  // Otherwise, it builds and returns the instruction.
    70  func (inst RequestUnitsDeprecated) 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 *RequestUnitsDeprecated) Validate() error {
    78  	// Check whether all (required) parameters are set:
    79  	{
    80  		if inst.Units == 0 {
    81  			return errors.New("Units parameter is not set")
    82  		}
    83  		if inst.Units > MAX_COMPUTE_UNIT_LIMIT {
    84  			return errors.New("Units parameter exceeds the maximum compute unit")
    85  		}
    86  		if inst.AdditionalFee == 0 {
    87  			return errors.New("AdditionalFee parameter is not set")
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  func (inst *RequestUnitsDeprecated) EncodeToTree(parent ag_treeout.Branches) {
    94  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
    95  		//
    96  		ParentFunc(func(programBranch ag_treeout.Branches) {
    97  			programBranch.Child(ag_format.Instruction("RequestUnitsDeprecated")).
    98  				//
    99  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   100  
   101  					// Parameters of the instruction:
   102  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   103  						paramsBranch.Child(ag_format.Param("        Units", inst.Units))
   104  						paramsBranch.Child(ag_format.Param("AdditionalFee", inst.AdditionalFee))
   105  					})
   106  				})
   107  		})
   108  }
   109  
   110  func (obj RequestUnitsDeprecated) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
   111  	// Serialize `Units` param:
   112  	err = encoder.Encode(obj.Units)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	// Serialize `AdditionalFee` param:
   118  	err = encoder.Encode(obj.AdditionalFee)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	return nil
   123  }
   124  func (obj *RequestUnitsDeprecated) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
   125  	// Deserialize `Units`:
   126  	err = decoder.Decode(&obj.Units)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	// Deserialize `AdditionalFee`:
   132  	err = decoder.Decode(&obj.AdditionalFee)
   133  	if err != nil {
   134  		return err
   135  	}
   136  	return nil
   137  }
   138  
   139  // NewRequestUnitsDeprecatedInstruction declares a new RequestUnitsDeprecated instruction with the provided parameters and accounts.
   140  func NewRequestUnitsDeprecatedInstruction(
   141  	// Parameters:
   142  	units uint32,
   143  	additionalFee uint32,
   144  ) *RequestUnitsDeprecated {
   145  	return NewRequestUnitsDeprecatedInstructionBuilder().
   146  		SetUnits(units).
   147  		SetAdditionalFee(additionalFee)
   148  }