github.com/gagliardetto/solana-go@v1.11.0/programs/vote/Withdraw.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  // Copyright 2023 github.com/jumpcrypto
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package vote
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  
    22  	bin "github.com/gagliardetto/binary"
    23  	"github.com/gagliardetto/solana-go"
    24  	"github.com/gagliardetto/solana-go/text/format"
    25  	"github.com/gagliardetto/treeout"
    26  )
    27  
    28  type Withdraw struct {
    29  	// Number of lamports to withdraw from the vote account
    30  	Lamports *uint64
    31  
    32  	// [0] = [WRITE] VoteAccount
    33  	// ··········· Vote account to withdraw from
    34  	//
    35  	// [1] = [WRITE] ToAccount
    36  	// ··········· Account to receive the funds
    37  	//
    38  	// [2] = [WRITE SIGNER] AuthorizedWithdrawerPubkey
    39  	// ··········· Account authorized to do the witdraw
    40  	//
    41  	solana.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    42  }
    43  
    44  func (v *Withdraw) UnmarshalWithDecoder(dec *bin.Decoder) error {
    45  	// Deserialize `Lamports` param:
    46  	{
    47  		err := dec.Decode(&v.Lamports)
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  func (inst *Withdraw) MarshalWithEncoder(encoder *bin.Encoder) error {
    56  	// Serialize `Lamports` param:
    57  	{
    58  		err := encoder.Encode(*inst.Lamports)
    59  		if err != nil {
    60  			return err
    61  		}
    62  	}
    63  	return nil
    64  }
    65  
    66  func (inst *Withdraw) Validate() error {
    67  	// Check whether all (required) parameters are set:
    68  	{
    69  		if inst.Lamports == nil {
    70  			return errors.New("lamports parameter is not set")
    71  		}
    72  	}
    73  
    74  	// Check whether all accounts are set:
    75  	for accIndex, acc := range inst.AccountMetaSlice {
    76  		if acc == nil {
    77  			return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  // Vote account
    84  func (inst *Withdraw) SetVoteAccount(voteAccount solana.PublicKey) *Withdraw {
    85  	inst.AccountMetaSlice[0] = solana.Meta(voteAccount).WRITE()
    86  	return inst
    87  }
    88  
    89  // Recipient account
    90  func (inst *Withdraw) SetRecipientAccount(recipientAccount solana.PublicKey) *Withdraw {
    91  	inst.AccountMetaSlice[1] = solana.Meta(recipientAccount).WRITE()
    92  	return inst
    93  }
    94  
    95  // Withdraw authority account
    96  func (inst *Withdraw) SetWithdrawAuthorityAccount(withdrawAccount solana.PublicKey) *Withdraw {
    97  	inst.AccountMetaSlice[2] = solana.Meta(withdrawAccount).WRITE().SIGNER()
    98  	return inst
    99  }
   100  
   101  // Number of lamports to transfer to the recipient account
   102  func (inst *Withdraw) SetLamports(lamports uint64) *Withdraw {
   103  	inst.Lamports = &lamports
   104  	return inst
   105  }
   106  
   107  func (inst *Withdraw) EncodeToTree(parent treeout.Branches) {
   108  	parent.Child(format.Program(ProgramName, ProgramID)).
   109  		//
   110  		ParentFunc(func(programBranch treeout.Branches) {
   111  			programBranch.Child(format.Instruction("Withdraw")).
   112  				//
   113  				ParentFunc(func(instructionBranch treeout.Branches) {
   114  
   115  					// Parameters of the instruction:
   116  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch treeout.Branches) {
   117  						paramsBranch.Child(format.Param("Lamports", inst.Lamports))
   118  					})
   119  
   120  					// Accounts of the instruction:
   121  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch treeout.Branches) {
   122  						accountsBranch.Child(format.Meta("                Vote", inst.AccountMetaSlice.Get(0)))
   123  						accountsBranch.Child(format.Meta("           Recipient", inst.AccountMetaSlice.Get(1)))
   124  						accountsBranch.Child(format.Meta("AuthorizedWithdrawer", inst.AccountMetaSlice.Get(2)))
   125  					})
   126  				})
   127  		})
   128  }
   129  
   130  // NewWithdrawInstructionBuilder creates a new `Withdraw` instruction builder.
   131  func NewWithdrawInstructionBuilder() *Withdraw {
   132  	nd := &Withdraw{
   133  		AccountMetaSlice: make(solana.AccountMetaSlice, 3),
   134  	}
   135  	return nd
   136  }
   137  
   138  // NewWithdrawInstruction declares a new Withdraw instruction with the provided parameters and accounts.
   139  func NewWithdrawInstruction(
   140  	// Parameters:
   141  	lamports uint64,
   142  	// Accounts:
   143  	voteAccount solana.PublicKey,
   144  	recipientAccount solana.PublicKey,
   145  	withdrawAuthAccount solana.PublicKey,
   146  ) *Withdraw {
   147  	return NewWithdrawInstructionBuilder().
   148  		SetLamports(lamports).
   149  		SetVoteAccount(voteAccount).
   150  		SetRecipientAccount(recipientAccount).
   151  		SetWithdrawAuthorityAccount(withdrawAuthAccount)
   152  }