github.com/gagliardetto/solana-go@v1.11.0/programs/system/WithdrawNonceAccount.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  	"errors"
    20  	"fmt"
    21  
    22  	ag_binary "github.com/gagliardetto/binary"
    23  	ag_solanago "github.com/gagliardetto/solana-go"
    24  	ag_format "github.com/gagliardetto/solana-go/text/format"
    25  	ag_treeout "github.com/gagliardetto/treeout"
    26  )
    27  
    28  // Withdraw funds from a nonce account
    29  type WithdrawNonceAccount struct {
    30  	// The u64 parameter is the lamports to withdraw, which must leave the account balance above the rent exempt reserve or at zero.
    31  	Lamports *uint64
    32  
    33  	// [0] = [WRITE] NonceAccount
    34  	// ··········· Nonce account
    35  	//
    36  	// [1] = [WRITE] RecipientAccount
    37  	// ··········· Recipient account
    38  	//
    39  	// [2] = [] $(SysVarRecentBlockHashesPubkey)
    40  	// ··········· RecentBlockhashes sysvar
    41  	//
    42  	// [3] = [] $(SysVarRentPubkey)
    43  	// ··········· Rent sysvar
    44  	//
    45  	// [4] = [SIGNER] NonceAuthorityAccount
    46  	// ··········· Nonce authority
    47  	ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    48  }
    49  
    50  // NewWithdrawNonceAccountInstructionBuilder creates a new `WithdrawNonceAccount` instruction builder.
    51  func NewWithdrawNonceAccountInstructionBuilder() *WithdrawNonceAccount {
    52  	nd := &WithdrawNonceAccount{
    53  		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 5),
    54  	}
    55  	nd.AccountMetaSlice[2] = ag_solanago.Meta(ag_solanago.SysVarRecentBlockHashesPubkey)
    56  	nd.AccountMetaSlice[3] = ag_solanago.Meta(ag_solanago.SysVarRentPubkey)
    57  	return nd
    58  }
    59  
    60  // The u64 parameter is the lamports to withdraw, which must leave the account balance above the rent exempt reserve or at zero.
    61  func (inst *WithdrawNonceAccount) SetLamports(lamports uint64) *WithdrawNonceAccount {
    62  	inst.Lamports = &lamports
    63  	return inst
    64  }
    65  
    66  // Nonce account
    67  func (inst *WithdrawNonceAccount) SetNonceAccount(nonceAccount ag_solanago.PublicKey) *WithdrawNonceAccount {
    68  	inst.AccountMetaSlice[0] = ag_solanago.Meta(nonceAccount).WRITE()
    69  	return inst
    70  }
    71  
    72  func (inst *WithdrawNonceAccount) GetNonceAccount() *ag_solanago.AccountMeta {
    73  	return inst.AccountMetaSlice[0]
    74  }
    75  
    76  // Recipient account
    77  func (inst *WithdrawNonceAccount) SetRecipientAccount(recipientAccount ag_solanago.PublicKey) *WithdrawNonceAccount {
    78  	inst.AccountMetaSlice[1] = ag_solanago.Meta(recipientAccount).WRITE()
    79  	return inst
    80  }
    81  
    82  func (inst *WithdrawNonceAccount) GetRecipientAccount() *ag_solanago.AccountMeta {
    83  	return inst.AccountMetaSlice[1]
    84  }
    85  
    86  // RecentBlockhashes sysvar
    87  func (inst *WithdrawNonceAccount) SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey ag_solanago.PublicKey) *WithdrawNonceAccount {
    88  	inst.AccountMetaSlice[2] = ag_solanago.Meta(SysVarRecentBlockHashesPubkey)
    89  	return inst
    90  }
    91  
    92  func (inst *WithdrawNonceAccount) GetSysVarRecentBlockHashesPubkeyAccount() *ag_solanago.AccountMeta {
    93  	return inst.AccountMetaSlice[2]
    94  }
    95  
    96  // Rent sysvar
    97  func (inst *WithdrawNonceAccount) SetSysVarRentPubkeyAccount(SysVarRentPubkey ag_solanago.PublicKey) *WithdrawNonceAccount {
    98  	inst.AccountMetaSlice[3] = ag_solanago.Meta(SysVarRentPubkey)
    99  	return inst
   100  }
   101  
   102  func (inst *WithdrawNonceAccount) GetSysVarRentPubkeyAccount() *ag_solanago.AccountMeta {
   103  	return inst.AccountMetaSlice[3]
   104  }
   105  
   106  // Nonce authority
   107  func (inst *WithdrawNonceAccount) SetNonceAuthorityAccount(nonceAuthorityAccount ag_solanago.PublicKey) *WithdrawNonceAccount {
   108  	inst.AccountMetaSlice[4] = ag_solanago.Meta(nonceAuthorityAccount).SIGNER()
   109  	return inst
   110  }
   111  
   112  func (inst *WithdrawNonceAccount) GetNonceAuthorityAccount() *ag_solanago.AccountMeta {
   113  	return inst.AccountMetaSlice[4]
   114  }
   115  
   116  func (inst WithdrawNonceAccount) Build() *Instruction {
   117  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
   118  		Impl:   inst,
   119  		TypeID: ag_binary.TypeIDFromUint32(Instruction_WithdrawNonceAccount, binary.LittleEndian),
   120  	}}
   121  }
   122  
   123  // ValidateAndBuild validates the instruction parameters and accounts;
   124  // if there is a validation error, it returns the error.
   125  // Otherwise, it builds and returns the instruction.
   126  func (inst WithdrawNonceAccount) ValidateAndBuild() (*Instruction, error) {
   127  	if err := inst.Validate(); err != nil {
   128  		return nil, err
   129  	}
   130  	return inst.Build(), nil
   131  }
   132  
   133  func (inst *WithdrawNonceAccount) Validate() error {
   134  	// Check whether all (required) parameters are set:
   135  	{
   136  		if inst.Lamports == nil {
   137  			return errors.New("Lamports parameter is not set")
   138  		}
   139  	}
   140  
   141  	// Check whether all accounts are set:
   142  	for accIndex, acc := range inst.AccountMetaSlice {
   143  		if acc == nil {
   144  			return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
   145  		}
   146  	}
   147  	return nil
   148  }
   149  
   150  func (inst *WithdrawNonceAccount) EncodeToTree(parent ag_treeout.Branches) {
   151  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   152  		//
   153  		ParentFunc(func(programBranch ag_treeout.Branches) {
   154  			programBranch.Child(ag_format.Instruction("WithdrawNonceAccount")).
   155  				//
   156  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   157  
   158  					// Parameters of the instruction:
   159  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   160  						paramsBranch.Child(ag_format.Param("Lamports", *inst.Lamports))
   161  					})
   162  
   163  					// Accounts of the instruction:
   164  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   165  						accountsBranch.Child(ag_format.Meta("                  Nonce", inst.AccountMetaSlice[0]))
   166  						accountsBranch.Child(ag_format.Meta("              Recipient", inst.AccountMetaSlice[1]))
   167  						accountsBranch.Child(ag_format.Meta("SysVarRecentBlockHashes", inst.AccountMetaSlice[2]))
   168  						accountsBranch.Child(ag_format.Meta("             SysVarRent", inst.AccountMetaSlice[3]))
   169  						accountsBranch.Child(ag_format.Meta("         NonceAuthority", inst.AccountMetaSlice[4]))
   170  					})
   171  				})
   172  		})
   173  }
   174  
   175  func (inst WithdrawNonceAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) error {
   176  	// Serialize `Lamports` param:
   177  	{
   178  		err := encoder.Encode(*inst.Lamports)
   179  		if err != nil {
   180  			return err
   181  		}
   182  	}
   183  	return nil
   184  }
   185  
   186  func (inst *WithdrawNonceAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error {
   187  	// Deserialize `Lamports` param:
   188  	{
   189  		err := decoder.Decode(&inst.Lamports)
   190  		if err != nil {
   191  			return err
   192  		}
   193  	}
   194  	return nil
   195  }
   196  
   197  // NewWithdrawNonceAccountInstruction declares a new WithdrawNonceAccount instruction with the provided parameters and accounts.
   198  func NewWithdrawNonceAccountInstruction(
   199  	// Parameters:
   200  	lamports uint64,
   201  	// Accounts:
   202  	nonceAccount ag_solanago.PublicKey,
   203  	recipientAccount ag_solanago.PublicKey,
   204  	SysVarRecentBlockHashesPubkey ag_solanago.PublicKey,
   205  	SysVarRentPubkey ag_solanago.PublicKey,
   206  	nonceAuthorityAccount ag_solanago.PublicKey) *WithdrawNonceAccount {
   207  	return NewWithdrawNonceAccountInstructionBuilder().
   208  		SetLamports(lamports).
   209  		SetNonceAccount(nonceAccount).
   210  		SetRecipientAccount(recipientAccount).
   211  		SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey).
   212  		SetSysVarRentPubkeyAccount(SysVarRentPubkey).
   213  		SetNonceAuthorityAccount(nonceAuthorityAccount)
   214  }