github.com/gagliardetto/solana-go@v1.11.0/programs/system/AdvanceNonceAccount.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 system
    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  // Consumes a stored nonce, replacing it with a successor
    28  type AdvanceNonceAccount struct {
    29  
    30  	// [0] = [WRITE] NonceAccount
    31  	// ··········· Nonce account
    32  	//
    33  	// [1] = [] $(SysVarRecentBlockHashesPubkey)
    34  	// ··········· RecentBlockhashes sysvar
    35  	//
    36  	// [2] = [SIGNER] NonceAuthorityAccount
    37  	// ··········· Nonce authority
    38  	ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    39  }
    40  
    41  // NewAdvanceNonceAccountInstructionBuilder creates a new `AdvanceNonceAccount` instruction builder.
    42  func NewAdvanceNonceAccountInstructionBuilder() *AdvanceNonceAccount {
    43  	nd := &AdvanceNonceAccount{
    44  		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3),
    45  	}
    46  	nd.AccountMetaSlice[1] = ag_solanago.Meta(ag_solanago.SysVarRecentBlockHashesPubkey)
    47  	return nd
    48  }
    49  
    50  // Nonce account
    51  func (inst *AdvanceNonceAccount) SetNonceAccount(nonceAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
    52  	inst.AccountMetaSlice[0] = ag_solanago.Meta(nonceAccount).WRITE()
    53  	return inst
    54  }
    55  
    56  func (inst *AdvanceNonceAccount) GetNonceAccount() *ag_solanago.AccountMeta {
    57  	return inst.AccountMetaSlice[0]
    58  }
    59  
    60  // RecentBlockhashes sysvar
    61  func (inst *AdvanceNonceAccount) SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey ag_solanago.PublicKey) *AdvanceNonceAccount {
    62  	inst.AccountMetaSlice[1] = ag_solanago.Meta(SysVarRecentBlockHashesPubkey)
    63  	return inst
    64  }
    65  
    66  func (inst *AdvanceNonceAccount) GetSysVarRecentBlockHashesPubkeyAccount() *ag_solanago.AccountMeta {
    67  	return inst.AccountMetaSlice[1]
    68  }
    69  
    70  // Nonce authority
    71  func (inst *AdvanceNonceAccount) SetNonceAuthorityAccount(nonceAuthorityAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
    72  	inst.AccountMetaSlice[2] = ag_solanago.Meta(nonceAuthorityAccount).SIGNER()
    73  	return inst
    74  }
    75  
    76  func (inst *AdvanceNonceAccount) GetNonceAuthorityAccount() *ag_solanago.AccountMeta {
    77  	return inst.AccountMetaSlice[2]
    78  }
    79  
    80  func (inst AdvanceNonceAccount) Build() *Instruction {
    81  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
    82  		Impl:   inst,
    83  		TypeID: ag_binary.TypeIDFromUint32(Instruction_AdvanceNonceAccount, binary.LittleEndian),
    84  	}}
    85  }
    86  
    87  // ValidateAndBuild validates the instruction parameters and accounts;
    88  // if there is a validation error, it returns the error.
    89  // Otherwise, it builds and returns the instruction.
    90  func (inst AdvanceNonceAccount) ValidateAndBuild() (*Instruction, error) {
    91  	if err := inst.Validate(); err != nil {
    92  		return nil, err
    93  	}
    94  	return inst.Build(), nil
    95  }
    96  
    97  func (inst *AdvanceNonceAccount) Validate() error {
    98  	// Check whether all accounts are set:
    99  	for accIndex, acc := range inst.AccountMetaSlice {
   100  		if acc == nil {
   101  			return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  func (inst *AdvanceNonceAccount) EncodeToTree(parent ag_treeout.Branches) {
   108  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   109  		//
   110  		ParentFunc(func(programBranch ag_treeout.Branches) {
   111  			programBranch.Child(ag_format.Instruction("AdvanceNonceAccount")).
   112  				//
   113  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   114  
   115  					// Parameters of the instruction:
   116  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {})
   117  
   118  					// Accounts of the instruction:
   119  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   120  						accountsBranch.Child(ag_format.Meta("                  Nonce", inst.AccountMetaSlice[0]))
   121  						accountsBranch.Child(ag_format.Meta("SysVarRecentBlockHashes", inst.AccountMetaSlice[1]))
   122  						accountsBranch.Child(ag_format.Meta("         NonceAuthority", inst.AccountMetaSlice[2]))
   123  					})
   124  				})
   125  		})
   126  }
   127  
   128  func (inst AdvanceNonceAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) error {
   129  	return nil
   130  }
   131  
   132  func (inst *AdvanceNonceAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error {
   133  	return nil
   134  }
   135  
   136  // NewAdvanceNonceAccountInstruction declares a new AdvanceNonceAccount instruction with the provided parameters and accounts.
   137  func NewAdvanceNonceAccountInstruction(
   138  	// Accounts:
   139  	nonceAccount ag_solanago.PublicKey,
   140  	SysVarRecentBlockHashesPubkey ag_solanago.PublicKey,
   141  	nonceAuthorityAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
   142  	return NewAdvanceNonceAccountInstructionBuilder().
   143  		SetNonceAccount(nonceAccount).
   144  		SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey).
   145  		SetNonceAuthorityAccount(nonceAuthorityAccount)
   146  }