github.com/gagliardetto/solana-go@v1.11.0/programs/system/CreateAccount.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  // Create a new account
    29  type CreateAccount struct {
    30  	// Number of lamports to transfer to the new account
    31  	Lamports *uint64
    32  
    33  	// Number of bytes of memory to allocate
    34  	Space *uint64
    35  
    36  	// Address of program that will own the new account
    37  	Owner *ag_solanago.PublicKey
    38  
    39  	// [0] = [WRITE, SIGNER] FundingAccount
    40  	// ··········· Funding account
    41  	//
    42  	// [1] = [WRITE, SIGNER] NewAccount
    43  	// ··········· New account
    44  	ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    45  }
    46  
    47  // NewCreateAccountInstructionBuilder creates a new `CreateAccount` instruction builder.
    48  func NewCreateAccountInstructionBuilder() *CreateAccount {
    49  	nd := &CreateAccount{
    50  		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 2),
    51  	}
    52  	return nd
    53  }
    54  
    55  // Number of lamports to transfer to the new account
    56  func (inst *CreateAccount) SetLamports(lamports uint64) *CreateAccount {
    57  	inst.Lamports = &lamports
    58  	return inst
    59  }
    60  
    61  // Number of bytes of memory to allocate
    62  func (inst *CreateAccount) SetSpace(space uint64) *CreateAccount {
    63  	inst.Space = &space
    64  	return inst
    65  }
    66  
    67  // Address of program that will own the new account
    68  func (inst *CreateAccount) SetOwner(owner ag_solanago.PublicKey) *CreateAccount {
    69  	inst.Owner = &owner
    70  	return inst
    71  }
    72  
    73  // Funding account
    74  func (inst *CreateAccount) SetFundingAccount(fundingAccount ag_solanago.PublicKey) *CreateAccount {
    75  	inst.AccountMetaSlice[0] = ag_solanago.Meta(fundingAccount).WRITE().SIGNER()
    76  	return inst
    77  }
    78  
    79  func (inst *CreateAccount) GetFundingAccount() *ag_solanago.AccountMeta {
    80  	return inst.AccountMetaSlice[0]
    81  }
    82  
    83  // New account
    84  func (inst *CreateAccount) SetNewAccount(newAccount ag_solanago.PublicKey) *CreateAccount {
    85  	inst.AccountMetaSlice[1] = ag_solanago.Meta(newAccount).WRITE().SIGNER()
    86  	return inst
    87  }
    88  
    89  func (inst *CreateAccount) GetNewAccount() *ag_solanago.AccountMeta {
    90  	return inst.AccountMetaSlice[1]
    91  }
    92  
    93  func (inst CreateAccount) Build() *Instruction {
    94  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
    95  		Impl:   inst,
    96  		TypeID: ag_binary.TypeIDFromUint32(Instruction_CreateAccount, binary.LittleEndian),
    97  	}}
    98  }
    99  
   100  // ValidateAndBuild validates the instruction parameters and accounts;
   101  // if there is a validation error, it returns the error.
   102  // Otherwise, it builds and returns the instruction.
   103  func (inst CreateAccount) ValidateAndBuild() (*Instruction, error) {
   104  	if err := inst.Validate(); err != nil {
   105  		return nil, err
   106  	}
   107  	return inst.Build(), nil
   108  }
   109  
   110  func (inst *CreateAccount) Validate() error {
   111  	// Check whether all (required) parameters are set:
   112  	{
   113  		if inst.Lamports == nil {
   114  			return errors.New("Lamports parameter is not set")
   115  		}
   116  		if inst.Space == nil {
   117  			return errors.New("Space parameter is not set")
   118  		}
   119  		if inst.Owner == nil {
   120  			return errors.New("Owner parameter is not set")
   121  		}
   122  	}
   123  
   124  	// Check whether all accounts are set:
   125  	for accIndex, acc := range inst.AccountMetaSlice {
   126  		if acc == nil {
   127  			return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
   128  		}
   129  	}
   130  	return nil
   131  }
   132  
   133  func (inst *CreateAccount) EncodeToTree(parent ag_treeout.Branches) {
   134  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   135  		//
   136  		ParentFunc(func(programBranch ag_treeout.Branches) {
   137  			programBranch.Child(ag_format.Instruction("CreateAccount")).
   138  				//
   139  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   140  
   141  					// Parameters of the instruction:
   142  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   143  						paramsBranch.Child(ag_format.Param("Lamports", *inst.Lamports))
   144  						paramsBranch.Child(ag_format.Param("   Space", *inst.Space))
   145  						paramsBranch.Child(ag_format.Param("   Owner", *inst.Owner))
   146  					})
   147  
   148  					// Accounts of the instruction:
   149  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   150  						accountsBranch.Child(ag_format.Meta("Funding", inst.AccountMetaSlice[0]))
   151  						accountsBranch.Child(ag_format.Meta("    New", inst.AccountMetaSlice[1]))
   152  					})
   153  				})
   154  		})
   155  }
   156  
   157  func (inst CreateAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) error {
   158  	// Serialize `Lamports` param:
   159  	{
   160  		err := encoder.Encode(*inst.Lamports)
   161  		if err != nil {
   162  			return err
   163  		}
   164  	}
   165  	// Serialize `Space` param:
   166  	{
   167  		err := encoder.Encode(*inst.Space)
   168  		if err != nil {
   169  			return err
   170  		}
   171  	}
   172  	// Serialize `Owner` param:
   173  	{
   174  		err := encoder.Encode(*inst.Owner)
   175  		if err != nil {
   176  			return err
   177  		}
   178  	}
   179  	return nil
   180  }
   181  
   182  func (inst *CreateAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error {
   183  	// Deserialize `Lamports` param:
   184  	{
   185  		err := decoder.Decode(&inst.Lamports)
   186  		if err != nil {
   187  			return err
   188  		}
   189  	}
   190  	// Deserialize `Space` param:
   191  	{
   192  		err := decoder.Decode(&inst.Space)
   193  		if err != nil {
   194  			return err
   195  		}
   196  	}
   197  	// Deserialize `Owner` param:
   198  	{
   199  		err := decoder.Decode(&inst.Owner)
   200  		if err != nil {
   201  			return err
   202  		}
   203  	}
   204  	return nil
   205  }
   206  
   207  // NewCreateAccountInstruction declares a new CreateAccount instruction with the provided parameters and accounts.
   208  func NewCreateAccountInstruction(
   209  	// Parameters:
   210  	lamports uint64,
   211  	space uint64,
   212  	owner ag_solanago.PublicKey,
   213  	// Accounts:
   214  	fundingAccount ag_solanago.PublicKey,
   215  	newAccount ag_solanago.PublicKey) *CreateAccount {
   216  	return NewCreateAccountInstructionBuilder().
   217  		SetLamports(lamports).
   218  		SetSpace(space).
   219  		SetOwner(owner).
   220  		SetFundingAccount(fundingAccount).
   221  		SetNewAccount(newAccount)
   222  }