github.com/gagliardetto/solana-go@v1.11.0/programs/token/FreezeAccount.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  	"fmt"
    20  
    21  	ag_binary "github.com/gagliardetto/binary"
    22  	ag_solanago "github.com/gagliardetto/solana-go"
    23  	ag_format "github.com/gagliardetto/solana-go/text/format"
    24  	ag_treeout "github.com/gagliardetto/treeout"
    25  )
    26  
    27  // Freeze an Initialized account using the Mint's freeze_authority (if set).
    28  type FreezeAccount struct {
    29  
    30  	// [0] = [WRITE] account
    31  	// ··········· The account to freeze.
    32  	//
    33  	// [1] = [] mint
    34  	// ··········· The token mint.
    35  	//
    36  	// [2] = [] authority
    37  	// ··········· The mint freeze authority.
    38  	//
    39  	// [3...] = [SIGNER] signers
    40  	// ··········· M signer accounts.
    41  	Accounts ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    42  	Signers  ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    43  }
    44  
    45  func (obj *FreezeAccount) SetAccounts(accounts []*ag_solanago.AccountMeta) error {
    46  	obj.Accounts, obj.Signers = ag_solanago.AccountMetaSlice(accounts).SplitFrom(3)
    47  	return nil
    48  }
    49  
    50  func (slice FreezeAccount) GetAccounts() (accounts []*ag_solanago.AccountMeta) {
    51  	accounts = append(accounts, slice.Accounts...)
    52  	accounts = append(accounts, slice.Signers...)
    53  	return
    54  }
    55  
    56  // NewFreezeAccountInstructionBuilder creates a new `FreezeAccount` instruction builder.
    57  func NewFreezeAccountInstructionBuilder() *FreezeAccount {
    58  	nd := &FreezeAccount{
    59  		Accounts: make(ag_solanago.AccountMetaSlice, 3),
    60  		Signers:  make(ag_solanago.AccountMetaSlice, 0),
    61  	}
    62  	return nd
    63  }
    64  
    65  // SetAccount sets the "account" account.
    66  // The account to freeze.
    67  func (inst *FreezeAccount) SetAccount(account ag_solanago.PublicKey) *FreezeAccount {
    68  	inst.Accounts[0] = ag_solanago.Meta(account).WRITE()
    69  	return inst
    70  }
    71  
    72  // GetAccount gets the "account" account.
    73  // The account to freeze.
    74  func (inst *FreezeAccount) GetAccount() *ag_solanago.AccountMeta {
    75  	return inst.Accounts[0]
    76  }
    77  
    78  // SetMintAccount sets the "mint" account.
    79  // The token mint.
    80  func (inst *FreezeAccount) SetMintAccount(mint ag_solanago.PublicKey) *FreezeAccount {
    81  	inst.Accounts[1] = ag_solanago.Meta(mint)
    82  	return inst
    83  }
    84  
    85  // GetMintAccount gets the "mint" account.
    86  // The token mint.
    87  func (inst *FreezeAccount) GetMintAccount() *ag_solanago.AccountMeta {
    88  	return inst.Accounts[1]
    89  }
    90  
    91  // SetAuthorityAccount sets the "authority" account.
    92  // The mint freeze authority.
    93  func (inst *FreezeAccount) SetAuthorityAccount(authority ag_solanago.PublicKey, multisigSigners ...ag_solanago.PublicKey) *FreezeAccount {
    94  	inst.Accounts[2] = ag_solanago.Meta(authority)
    95  	if len(multisigSigners) == 0 {
    96  		inst.Accounts[2].SIGNER()
    97  	}
    98  	for _, signer := range multisigSigners {
    99  		inst.Signers = append(inst.Signers, ag_solanago.Meta(signer).SIGNER())
   100  	}
   101  	return inst
   102  }
   103  
   104  // GetAuthorityAccount gets the "authority" account.
   105  // The mint freeze authority.
   106  func (inst *FreezeAccount) GetAuthorityAccount() *ag_solanago.AccountMeta {
   107  	return inst.Accounts[2]
   108  }
   109  
   110  func (inst FreezeAccount) Build() *Instruction {
   111  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
   112  		Impl:   inst,
   113  		TypeID: ag_binary.TypeIDFromUint8(Instruction_FreezeAccount),
   114  	}}
   115  }
   116  
   117  // ValidateAndBuild validates the instruction parameters and accounts;
   118  // if there is a validation error, it returns the error.
   119  // Otherwise, it builds and returns the instruction.
   120  func (inst FreezeAccount) ValidateAndBuild() (*Instruction, error) {
   121  	if err := inst.Validate(); err != nil {
   122  		return nil, err
   123  	}
   124  	return inst.Build(), nil
   125  }
   126  
   127  func (inst *FreezeAccount) Validate() error {
   128  	// Check whether all (required) accounts are set:
   129  	{
   130  		if inst.Accounts[0] == nil {
   131  			return errors.New("accounts.Account is not set")
   132  		}
   133  		if inst.Accounts[1] == nil {
   134  			return errors.New("accounts.Mint is not set")
   135  		}
   136  		if inst.Accounts[2] == nil {
   137  			return errors.New("accounts.Authority is not set")
   138  		}
   139  		if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
   140  			return fmt.Errorf("accounts.Signers is not set")
   141  		}
   142  		if len(inst.Signers) > MAX_SIGNERS {
   143  			return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
   144  		}
   145  	}
   146  	return nil
   147  }
   148  
   149  func (inst *FreezeAccount) EncodeToTree(parent ag_treeout.Branches) {
   150  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   151  		//
   152  		ParentFunc(func(programBranch ag_treeout.Branches) {
   153  			programBranch.Child(ag_format.Instruction("FreezeAccount")).
   154  				//
   155  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   156  
   157  					// Parameters of the instruction:
   158  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {})
   159  
   160  					// Accounts of the instruction:
   161  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   162  						accountsBranch.Child(ag_format.Meta("  account", inst.Accounts[0]))
   163  						accountsBranch.Child(ag_format.Meta("     mint", inst.Accounts[1]))
   164  						accountsBranch.Child(ag_format.Meta("authority", inst.Accounts[2]))
   165  
   166  						signersBranch := accountsBranch.Child(fmt.Sprintf("signers[len=%v]", len(inst.Signers)))
   167  						for i, v := range inst.Signers {
   168  							if len(inst.Signers) > 9 && i < 10 {
   169  								signersBranch.Child(ag_format.Meta(fmt.Sprintf(" [%v]", i), v))
   170  							} else {
   171  								signersBranch.Child(ag_format.Meta(fmt.Sprintf("[%v]", i), v))
   172  							}
   173  						}
   174  					})
   175  				})
   176  		})
   177  }
   178  
   179  func (obj FreezeAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
   180  	return nil
   181  }
   182  func (obj *FreezeAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
   183  	return nil
   184  }
   185  
   186  // NewFreezeAccountInstruction declares a new FreezeAccount instruction with the provided parameters and accounts.
   187  func NewFreezeAccountInstruction(
   188  	// Accounts:
   189  	account ag_solanago.PublicKey,
   190  	mint ag_solanago.PublicKey,
   191  	authority ag_solanago.PublicKey,
   192  	multisigSigners []ag_solanago.PublicKey,
   193  ) *FreezeAccount {
   194  	return NewFreezeAccountInstructionBuilder().
   195  		SetAccount(account).
   196  		SetMintAccount(mint).
   197  		SetAuthorityAccount(authority, multisigSigners...)
   198  }