github.com/gagliardetto/solana-go@v1.11.0/programs/token/SetAuthority.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  // Sets a new authority of a mint or account.
    28  type SetAuthority struct {
    29  	// The type of authority to update.
    30  	AuthorityType *AuthorityType
    31  
    32  	// The new authority.
    33  	NewAuthority *ag_solanago.PublicKey `bin:"optional"`
    34  
    35  	// [0] = [WRITE] subject
    36  	// ··········· The mint or account to change the authority of.
    37  	//
    38  	// [1] = [] authority
    39  	// ··········· The current authority of the mint or account.
    40  	//
    41  	// [2...] = [SIGNER] signers
    42  	// ··········· M signer accounts.
    43  	Accounts ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    44  	Signers  ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
    45  }
    46  
    47  func (obj *SetAuthority) SetAccounts(accounts []*ag_solanago.AccountMeta) error {
    48  	obj.Accounts, obj.Signers = ag_solanago.AccountMetaSlice(accounts).SplitFrom(2)
    49  	return nil
    50  }
    51  
    52  func (slice SetAuthority) GetAccounts() (accounts []*ag_solanago.AccountMeta) {
    53  	accounts = append(accounts, slice.Accounts...)
    54  	accounts = append(accounts, slice.Signers...)
    55  	return
    56  }
    57  
    58  // NewSetAuthorityInstructionBuilder creates a new `SetAuthority` instruction builder.
    59  func NewSetAuthorityInstructionBuilder() *SetAuthority {
    60  	nd := &SetAuthority{
    61  		Accounts: make(ag_solanago.AccountMetaSlice, 2),
    62  		Signers:  make(ag_solanago.AccountMetaSlice, 0),
    63  	}
    64  	return nd
    65  }
    66  
    67  // SetAuthorityType sets the "authority_type" parameter.
    68  // The type of authority to update.
    69  func (inst *SetAuthority) SetAuthorityType(authority_type AuthorityType) *SetAuthority {
    70  	inst.AuthorityType = &authority_type
    71  	return inst
    72  }
    73  
    74  // SetNewAuthority sets the "new_authority" parameter.
    75  // The new authority.
    76  func (inst *SetAuthority) SetNewAuthority(new_authority ag_solanago.PublicKey) *SetAuthority {
    77  	inst.NewAuthority = &new_authority
    78  	return inst
    79  }
    80  
    81  // SetSubjectAccount sets the "subject" account.
    82  // The mint or account to change the authority of.
    83  func (inst *SetAuthority) SetSubjectAccount(subject ag_solanago.PublicKey) *SetAuthority {
    84  	inst.Accounts[0] = ag_solanago.Meta(subject).WRITE()
    85  	return inst
    86  }
    87  
    88  // GetSubjectAccount gets the "subject" account.
    89  // The mint or account to change the authority of.
    90  func (inst *SetAuthority) GetSubjectAccount() *ag_solanago.AccountMeta {
    91  	return inst.Accounts[0]
    92  }
    93  
    94  // SetAuthorityAccount sets the "authority" account.
    95  // The current authority of the mint or account.
    96  func (inst *SetAuthority) SetAuthorityAccount(authority ag_solanago.PublicKey, multisigSigners ...ag_solanago.PublicKey) *SetAuthority {
    97  	inst.Accounts[1] = ag_solanago.Meta(authority)
    98  	if len(multisigSigners) == 0 {
    99  		inst.Accounts[1].SIGNER()
   100  	}
   101  	for _, signer := range multisigSigners {
   102  		inst.Signers = append(inst.Signers, ag_solanago.Meta(signer).SIGNER())
   103  	}
   104  	return inst
   105  }
   106  
   107  // GetAuthorityAccount gets the "authority" account.
   108  // The current authority of the mint or account.
   109  func (inst *SetAuthority) GetAuthorityAccount() *ag_solanago.AccountMeta {
   110  	return inst.Accounts[1]
   111  }
   112  
   113  func (inst SetAuthority) Build() *Instruction {
   114  	return &Instruction{BaseVariant: ag_binary.BaseVariant{
   115  		Impl:   inst,
   116  		TypeID: ag_binary.TypeIDFromUint8(Instruction_SetAuthority),
   117  	}}
   118  }
   119  
   120  // ValidateAndBuild validates the instruction parameters and accounts;
   121  // if there is a validation error, it returns the error.
   122  // Otherwise, it builds and returns the instruction.
   123  func (inst SetAuthority) ValidateAndBuild() (*Instruction, error) {
   124  	if err := inst.Validate(); err != nil {
   125  		return nil, err
   126  	}
   127  	return inst.Build(), nil
   128  }
   129  
   130  func (inst *SetAuthority) Validate() error {
   131  	// Check whether all (required) parameters are set:
   132  	{
   133  		if inst.AuthorityType == nil {
   134  			return errors.New("AuthorityType parameter is not set")
   135  		}
   136  	}
   137  
   138  	// Check whether all (required) accounts are set:
   139  	{
   140  		if inst.Accounts[0] == nil {
   141  			return errors.New("accounts.Subject is not set")
   142  		}
   143  		if inst.Accounts[1] == nil {
   144  			return errors.New("accounts.Authority is not set")
   145  		}
   146  		if !inst.Accounts[1].IsSigner && len(inst.Signers) == 0 {
   147  			return fmt.Errorf("accounts.Signers is not set")
   148  		}
   149  		if len(inst.Signers) > MAX_SIGNERS {
   150  			return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
   151  		}
   152  	}
   153  	return nil
   154  }
   155  
   156  func (inst *SetAuthority) EncodeToTree(parent ag_treeout.Branches) {
   157  	parent.Child(ag_format.Program(ProgramName, ProgramID)).
   158  		//
   159  		ParentFunc(func(programBranch ag_treeout.Branches) {
   160  			programBranch.Child(ag_format.Instruction("SetAuthority")).
   161  				//
   162  				ParentFunc(func(instructionBranch ag_treeout.Branches) {
   163  
   164  					// Parameters of the instruction:
   165  					instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
   166  						paramsBranch.Child(ag_format.Param("     AuthorityType", *inst.AuthorityType))
   167  						paramsBranch.Child(ag_format.Param("NewAuthority (OPT)", inst.NewAuthority))
   168  					})
   169  
   170  					// Accounts of the instruction:
   171  					instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
   172  						accountsBranch.Child(ag_format.Meta("  subject", inst.Accounts[0]))
   173  						accountsBranch.Child(ag_format.Meta("authority", inst.Accounts[1]))
   174  
   175  						signersBranch := accountsBranch.Child(fmt.Sprintf("signers[len=%v]", len(inst.Signers)))
   176  						for i, v := range inst.Signers {
   177  							if len(inst.Signers) > 9 && i < 10 {
   178  								signersBranch.Child(ag_format.Meta(fmt.Sprintf(" [%v]", i), v))
   179  							} else {
   180  								signersBranch.Child(ag_format.Meta(fmt.Sprintf("[%v]", i), v))
   181  							}
   182  						}
   183  					})
   184  				})
   185  		})
   186  }
   187  
   188  func (obj SetAuthority) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
   189  	// Serialize `AuthorityType` param:
   190  	err = encoder.Encode(obj.AuthorityType)
   191  	if err != nil {
   192  		return err
   193  	}
   194  	// Serialize `NewAuthority` param (optional):
   195  	{
   196  		if obj.NewAuthority == nil {
   197  			err = encoder.WriteBool(false)
   198  			if err != nil {
   199  				return err
   200  			}
   201  		} else {
   202  			err = encoder.WriteBool(true)
   203  			if err != nil {
   204  				return err
   205  			}
   206  			err = encoder.Encode(obj.NewAuthority)
   207  			if err != nil {
   208  				return err
   209  			}
   210  		}
   211  	}
   212  	return nil
   213  }
   214  func (obj *SetAuthority) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
   215  	// Deserialize `AuthorityType`:
   216  	err = decoder.Decode(&obj.AuthorityType)
   217  	if err != nil {
   218  		return err
   219  	}
   220  	// Deserialize `NewAuthority` (optional):
   221  	{
   222  		ok, err := decoder.ReadBool()
   223  		if err != nil {
   224  			return err
   225  		}
   226  		if ok {
   227  			err = decoder.Decode(&obj.NewAuthority)
   228  			if err != nil {
   229  				return err
   230  			}
   231  		}
   232  	}
   233  	return nil
   234  }
   235  
   236  // NewSetAuthorityInstruction declares a new SetAuthority instruction with the provided parameters and accounts.
   237  func NewSetAuthorityInstruction(
   238  	// Parameters:
   239  	authority_type AuthorityType,
   240  	new_authority ag_solanago.PublicKey,
   241  	// Accounts:
   242  	subject ag_solanago.PublicKey,
   243  	authority ag_solanago.PublicKey,
   244  	multisigSigners []ag_solanago.PublicKey,
   245  ) *SetAuthority {
   246  	return NewSetAuthorityInstructionBuilder().
   247  		SetAuthorityType(authority_type).
   248  		SetNewAuthority(new_authority).
   249  		SetSubjectAccount(subject).
   250  		SetAuthorityAccount(authority, multisigSigners...)
   251  }