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