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