github.com/gagliardetto/solana-go@v1.11.0/programs/token/InitializeAccount3.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 token
    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  // Like InitializeAccount2, but does not require the Rent sysvar to be provided.
    27  type InitializeAccount3 struct {
    28  	// The new account's owner/multisignature.
    29  	Owner *ag_solanago.PublicKey
    30  
    31  	// [0] = [WRITE] account
    32  	// ··········· The account to initialize.
    33  	//
    34  	// [1] = [] mint
    35  	// ··········· The mint this account will be associated with.
    36  	ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    37  }
    38  
    39  // NewInitializeAccount3InstructionBuilder creates a new `InitializeAccount3` instruction builder.
    40  func NewInitializeAccount3InstructionBuilder() *InitializeAccount3 {
    41  	nd := &InitializeAccount3{
    42  		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 2),
    43  	}
    44  	return nd
    45  }
    46  
    47  // SetOwner sets the "owner" parameter.
    48  // The new account's owner/multisignature.
    49  func (inst *InitializeAccount3) SetOwner(owner ag_solanago.PublicKey) *InitializeAccount3 {
    50  	inst.Owner = &owner
    51  	return inst
    52  }
    53  
    54  // SetAccount sets the "account" account.
    55  // The account to initialize.
    56  func (inst *InitializeAccount3) SetAccount(account ag_solanago.PublicKey) *InitializeAccount3 {
    57  	inst.AccountMetaSlice[0] = ag_solanago.Meta(account).WRITE()
    58  	return inst
    59  }
    60  
    61  // GetAccount gets the "account" account.
    62  // The account to initialize.
    63  func (inst *InitializeAccount3) GetAccount() *ag_solanago.AccountMeta {
    64  	return inst.AccountMetaSlice[0]
    65  }
    66  
    67  // SetMintAccount sets the "mint" account.
    68  // The mint this account will be associated with.
    69  func (inst *InitializeAccount3) SetMintAccount(mint ag_solanago.PublicKey) *InitializeAccount3 {
    70  	inst.AccountMetaSlice[1] = ag_solanago.Meta(mint)
    71  	return inst
    72  }
    73  
    74  // GetMintAccount gets the "mint" account.
    75  // The mint this account will be associated with.
    76  func (inst *InitializeAccount3) GetMintAccount() *ag_solanago.AccountMeta {
    77  	return inst.AccountMetaSlice[1]
    78  }
    79  
    80  func (inst InitializeAccount3) Build() *Instruction {
    81  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
    82  		Impl:   inst,
    83  		TypeID: ag_binary.TypeIDFromUint8(Instruction_InitializeAccount3),
    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 InitializeAccount3) 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 *InitializeAccount3) Validate() error {
    98  	// Check whether all (required) parameters are set:
    99  	{
   100  		if inst.Owner == nil {
   101  			return errors.New("Owner parameter is not set")
   102  		}
   103  	}
   104  
   105  	// Check whether all (required) accounts are set:
   106  	{
   107  		if inst.AccountMetaSlice[0] == nil {
   108  			return errors.New("accounts.Account is not set")
   109  		}
   110  		if inst.AccountMetaSlice[1] == nil {
   111  			return errors.New("accounts.Mint is not set")
   112  		}
   113  	}
   114  	return nil
   115  }
   116  
   117  func (inst *InitializeAccount3) EncodeToTree(parent ag_treeout.Branches) {
   118  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   119  		//
   120  		ParentFunc(func(programBranch ag_treeout.Branches) {
   121  			programBranch.Child(ag_format.Instruction("InitializeAccount3")).
   122  				//
   123  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   124  
   125  					// Parameters of the instruction:
   126  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   127  						paramsBranch.Child(ag_format.Param("Owner", *inst.Owner))
   128  					})
   129  
   130  					// Accounts of the instruction:
   131  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   132  						accountsBranch.Child(ag_format.Meta("account", inst.AccountMetaSlice[0]))
   133  						accountsBranch.Child(ag_format.Meta("   mint", inst.AccountMetaSlice[1]))
   134  					})
   135  				})
   136  		})
   137  }
   138  
   139  func (obj InitializeAccount3) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
   140  	// Serialize `Owner` param:
   141  	err = encoder.Encode(obj.Owner)
   142  	if err != nil {
   143  		return err
   144  	}
   145  	return nil
   146  }
   147  func (obj *InitializeAccount3) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
   148  	// Deserialize `Owner`:
   149  	err = decoder.Decode(&obj.Owner)
   150  	if err != nil {
   151  		return err
   152  	}
   153  	return nil
   154  }
   155  
   156  // NewInitializeAccount3Instruction declares a new InitializeAccount3 instruction with the provided parameters and accounts.
   157  func NewInitializeAccount3Instruction(
   158  	// Parameters:
   159  	owner ag_solanago.PublicKey,
   160  	// Accounts:
   161  	account ag_solanago.PublicKey,
   162  	mint ag_solanago.PublicKey) *InitializeAccount3 {
   163  	return NewInitializeAccount3InstructionBuilder().
   164  		SetOwner(owner).
   165  		SetAccount(account).
   166  		SetMintAccount(mint)
   167  }