github.com/gagliardetto/solana-go@v1.11.0/programs/system/AllocateWithSeed.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  // Allocate space for and assign an account at an address derived from a base public key and a seed
    29  type AllocateWithSeed struct {
    30  	// Base public key
    31  	Base *ag_solanago.PublicKey
    32  
    33  	// String of ASCII chars, no longer than pubkey::MAX_SEED_LEN
    34  	Seed *string
    35  
    36  	// Number of bytes of memory to allocate
    37  	Space *uint64
    38  
    39  	// Owner program account address
    40  	Owner *ag_solanago.PublicKey
    41  
    42  	// [0] = [WRITE] AllocatedAccount
    43  	// ··········· Allocated account
    44  	//
    45  	// [1] = [SIGNER] BaseAccount
    46  	// ··········· Base account
    47  	ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    48  }
    49  
    50  // NewAllocateWithSeedInstructionBuilder creates a new `AllocateWithSeed` instruction builder.
    51  func NewAllocateWithSeedInstructionBuilder() *AllocateWithSeed {
    52  	nd := &AllocateWithSeed{
    53  		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 2),
    54  	}
    55  	return nd
    56  }
    57  
    58  // Base public key
    59  func (inst *AllocateWithSeed) SetBase(base ag_solanago.PublicKey) *AllocateWithSeed {
    60  	inst.Base = &base
    61  	return inst
    62  }
    63  
    64  // String of ASCII chars, no longer than pubkey::MAX_SEED_LEN
    65  func (inst *AllocateWithSeed) SetSeed(seed string) *AllocateWithSeed {
    66  	inst.Seed = &seed
    67  	return inst
    68  }
    69  
    70  // Number of bytes of memory to allocate
    71  func (inst *AllocateWithSeed) SetSpace(space uint64) *AllocateWithSeed {
    72  	inst.Space = &space
    73  	return inst
    74  }
    75  
    76  // Owner program account address
    77  func (inst *AllocateWithSeed) SetOwner(owner ag_solanago.PublicKey) *AllocateWithSeed {
    78  	inst.Owner = &owner
    79  	return inst
    80  }
    81  
    82  // Allocated account
    83  func (inst *AllocateWithSeed) SetAllocatedAccount(allocatedAccount ag_solanago.PublicKey) *AllocateWithSeed {
    84  	inst.AccountMetaSlice[0] = ag_solanago.Meta(allocatedAccount).WRITE()
    85  	return inst
    86  }
    87  
    88  func (inst *AllocateWithSeed) GetAllocatedAccount() *ag_solanago.AccountMeta {
    89  	return inst.AccountMetaSlice[0]
    90  }
    91  
    92  // Base account
    93  func (inst *AllocateWithSeed) SetBaseAccount(baseAccount ag_solanago.PublicKey) *AllocateWithSeed {
    94  	inst.AccountMetaSlice[1] = ag_solanago.Meta(baseAccount).SIGNER()
    95  	return inst
    96  }
    97  
    98  func (inst *AllocateWithSeed) GetBaseAccount() *ag_solanago.AccountMeta {
    99  	return inst.AccountMetaSlice[1]
   100  }
   101  
   102  func (inst AllocateWithSeed) Build() *Instruction {
   103  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
   104  		Impl:   inst,
   105  		TypeID: ag_binary.TypeIDFromUint32(Instruction_AllocateWithSeed, binary.LittleEndian),
   106  	}}
   107  }
   108  
   109  // ValidateAndBuild validates the instruction parameters and accounts;
   110  // if there is a validation error, it returns the error.
   111  // Otherwise, it builds and returns the instruction.
   112  func (inst AllocateWithSeed) ValidateAndBuild() (*Instruction, error) {
   113  	if err := inst.Validate(); err != nil {
   114  		return nil, err
   115  	}
   116  	return inst.Build(), nil
   117  }
   118  
   119  func (inst *AllocateWithSeed) Validate() error {
   120  	// Check whether all (required) parameters are set:
   121  	{
   122  		if inst.Base == nil {
   123  			return errors.New("Base parameter is not set")
   124  		}
   125  		if inst.Seed == nil {
   126  			return errors.New("Seed parameter is not set")
   127  		}
   128  		if inst.Space == nil {
   129  			return errors.New("Space parameter is not set")
   130  		}
   131  		if inst.Owner == nil {
   132  			return errors.New("Owner parameter is not set")
   133  		}
   134  	}
   135  
   136  	// Check whether all accounts are set:
   137  	for accIndex, acc := range inst.AccountMetaSlice {
   138  		if acc == nil {
   139  			return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
   140  		}
   141  	}
   142  	return nil
   143  }
   144  
   145  func (inst *AllocateWithSeed) EncodeToTree(parent ag_treeout.Branches) {
   146  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   147  		//
   148  		ParentFunc(func(programBranch ag_treeout.Branches) {
   149  			programBranch.Child(ag_format.Instruction("AllocateWithSeed")).
   150  				//
   151  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   152  
   153  					// Parameters of the instruction:
   154  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   155  						paramsBranch.Child(ag_format.Param(" Base", *inst.Base))
   156  						paramsBranch.Child(ag_format.Param(" Seed", *inst.Seed))
   157  						paramsBranch.Child(ag_format.Param("Space", *inst.Space))
   158  						paramsBranch.Child(ag_format.Param("Owner", *inst.Owner))
   159  					})
   160  
   161  					// Accounts of the instruction:
   162  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   163  						accountsBranch.Child(ag_format.Meta("Allocated", inst.AccountMetaSlice[0]))
   164  						accountsBranch.Child(ag_format.Meta("     Base", inst.AccountMetaSlice[1]))
   165  					})
   166  				})
   167  		})
   168  }
   169  
   170  func (inst AllocateWithSeed) MarshalWithEncoder(encoder *ag_binary.Encoder) error {
   171  	// Serialize `Base` param:
   172  	{
   173  		err := encoder.Encode(*inst.Base)
   174  		if err != nil {
   175  			return err
   176  		}
   177  	}
   178  	// Serialize `Seed` param:
   179  	{
   180  		err := encoder.WriteRustString(*inst.Seed)
   181  		if err != nil {
   182  			return err
   183  		}
   184  	}
   185  	// Serialize `Space` param:
   186  	{
   187  		err := encoder.Encode(*inst.Space)
   188  		if err != nil {
   189  			return err
   190  		}
   191  	}
   192  	// Serialize `Owner` param:
   193  	{
   194  		err := encoder.Encode(*inst.Owner)
   195  		if err != nil {
   196  			return err
   197  		}
   198  	}
   199  	return nil
   200  }
   201  
   202  func (inst *AllocateWithSeed) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error {
   203  	// Deserialize `Base` param:
   204  	{
   205  		err := decoder.Decode(&inst.Base)
   206  		if err != nil {
   207  			return err
   208  		}
   209  	}
   210  	// Deserialize `Seed` param:
   211  	{
   212  		value, err := decoder.ReadRustString()
   213  		if err != nil {
   214  			return err
   215  		}
   216  		inst.Seed = &value
   217  	}
   218  	// Deserialize `Space` param:
   219  	{
   220  		err := decoder.Decode(&inst.Space)
   221  		if err != nil {
   222  			return err
   223  		}
   224  	}
   225  	// Deserialize `Owner` param:
   226  	{
   227  		err := decoder.Decode(&inst.Owner)
   228  		if err != nil {
   229  			return err
   230  		}
   231  	}
   232  	return nil
   233  }
   234  
   235  // NewAllocateWithSeedInstruction declares a new AllocateWithSeed instruction with the provided parameters and accounts.
   236  func NewAllocateWithSeedInstruction(
   237  	// Parameters:
   238  	base ag_solanago.PublicKey,
   239  	seed string,
   240  	space uint64,
   241  	owner ag_solanago.PublicKey,
   242  	// Accounts:
   243  	allocatedAccount ag_solanago.PublicKey,
   244  	baseAccount ag_solanago.PublicKey) *AllocateWithSeed {
   245  	return NewAllocateWithSeedInstructionBuilder().
   246  		SetBase(base).
   247  		SetSeed(seed).
   248  		SetSpace(space).
   249  		SetOwner(owner).
   250  		SetAllocatedAccount(allocatedAccount).
   251  		SetBaseAccount(baseAccount)
   252  }