solana-go/programs/token/Revoke.go

160 lines
4.7 KiB
Go
Raw Normal View History

2021-09-03 15:43:43 -07:00
package token
import (
"encoding/binary"
2021-09-04 07:51:19 -07:00
"errors"
2021-09-04 08:41:00 -07:00
"fmt"
2021-09-04 07:51:19 -07:00
ag_binary "github.com/gagliardetto/binary"
2021-09-03 15:43:43 -07:00
ag_solanago "github.com/gagliardetto/solana-go"
ag_format "github.com/gagliardetto/solana-go/text/format"
ag_treeout "github.com/gagliardetto/treeout"
)
// Revokes the delegate's authority.
type Revoke struct {
// [0] = [WRITE] source
// ··········· The source account.
//
// [1] = [] owner
// ··········· The source account's owner.
2021-09-04 08:41:00 -07:00
//
// [2...] = [SIGNER] signers
// ··········· M signer accounts.
Accounts ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
Signers ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}
func (obj *Revoke) SetAccounts(accounts []*ag_solanago.AccountMeta) error {
obj.Accounts, obj.Signers = ag_solanago.AccountMetaSlice(accounts).SplitFrom(2)
return nil
}
func (slice Revoke) GetAccounts() (accounts []*ag_solanago.AccountMeta) {
accounts = append(accounts, slice.Accounts...)
accounts = append(accounts, slice.Signers...)
return
2021-09-03 15:43:43 -07:00
}
// NewRevokeInstructionBuilder creates a new `Revoke` instruction builder.
func NewRevokeInstructionBuilder() *Revoke {
nd := &Revoke{
2021-09-04 08:41:00 -07:00
Accounts: make(ag_solanago.AccountMetaSlice, 2),
Signers: make(ag_solanago.AccountMetaSlice, 0),
2021-09-03 15:43:43 -07:00
}
return nd
}
2021-09-03 16:55:23 -07:00
// SetSourceAccount sets the "source" account.
2021-09-03 15:43:43 -07:00
// The source account.
func (inst *Revoke) SetSourceAccount(source ag_solanago.PublicKey) *Revoke {
2021-09-04 08:41:00 -07:00
inst.Accounts[0] = ag_solanago.Meta(source).WRITE()
2021-09-03 15:43:43 -07:00
return inst
}
2021-09-03 16:55:23 -07:00
// GetSourceAccount gets the "source" account.
// The source account.
2021-09-03 15:43:43 -07:00
func (inst *Revoke) GetSourceAccount() *ag_solanago.AccountMeta {
2021-09-04 08:41:00 -07:00
return inst.Accounts[0]
2021-09-03 15:43:43 -07:00
}
2021-09-03 16:55:23 -07:00
// SetOwnerAccount sets the "owner" account.
2021-09-03 15:43:43 -07:00
// The source account's owner.
2021-09-04 08:41:00 -07:00
func (inst *Revoke) SetOwnerAccount(owner ag_solanago.PublicKey, multisigSigners ...ag_solanago.PublicKey) *Revoke {
inst.Accounts[1] = ag_solanago.Meta(owner)
if len(multisigSigners) == 0 {
inst.Accounts[1].SIGNER()
}
for _, signer := range multisigSigners {
inst.Signers = append(inst.Signers, ag_solanago.Meta(signer).SIGNER())
}
2021-09-03 15:43:43 -07:00
return inst
}
2021-09-03 16:55:23 -07:00
// GetOwnerAccount gets the "owner" account.
// The source account's owner.
2021-09-03 15:43:43 -07:00
func (inst *Revoke) GetOwnerAccount() *ag_solanago.AccountMeta {
2021-09-04 08:41:00 -07:00
return inst.Accounts[1]
2021-09-03 15:43:43 -07:00
}
func (inst Revoke) Build() *Instruction {
return &Instruction{BaseVariant: ag_binary.BaseVariant{
Impl: inst,
TypeID: ag_binary.TypeIDFromUint32(Instruction_Revoke, binary.LittleEndian),
}}
}
// ValidateAndBuild validates the instruction parameters and accounts;
// if there is a validation error, it returns the error.
// Otherwise, it builds and returns the instruction.
func (inst Revoke) ValidateAndBuild() (*Instruction, error) {
if err := inst.Validate(); err != nil {
return nil, err
}
return inst.Build(), nil
}
func (inst *Revoke) Validate() error {
// Check whether all (required) accounts are set:
{
2021-09-04 08:41:00 -07:00
if inst.Accounts[0] == nil {
2021-09-04 07:51:19 -07:00
return errors.New("accounts.Source is not set")
2021-09-03 15:43:43 -07:00
}
2021-09-04 08:41:00 -07:00
if inst.Accounts[1] == nil {
2021-09-04 07:51:19 -07:00
return errors.New("accounts.Owner is not set")
2021-09-03 15:43:43 -07:00
}
2021-09-04 08:41:00 -07:00
if !inst.Accounts[1].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
}
2021-09-03 15:43:43 -07:00
}
return nil
}
func (inst *Revoke) EncodeToTree(parent ag_treeout.Branches) {
parent.Child(ag_format.Program(ProgramName, ProgramID)).
//
ParentFunc(func(programBranch ag_treeout.Branches) {
programBranch.Child(ag_format.Instruction("Revoke")).
//
ParentFunc(func(instructionBranch ag_treeout.Branches) {
// Parameters of the instruction:
instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {})
// Accounts of the instruction:
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
2021-09-04 08:41:00 -07:00
accountsBranch.Child(ag_format.Meta("source", inst.Accounts[0]))
accountsBranch.Child(ag_format.Meta("owner", inst.Accounts[1]))
signersBranch := accountsBranch.Child(fmt.Sprintf("signers[len=%v]", len(inst.Signers)))
for i, v := range inst.Signers {
signersBranch.Child(ag_format.Meta(fmt.Sprintf("signers[%v]", i), v))
}
2021-09-03 15:43:43 -07:00
})
})
})
}
func (obj Revoke) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
return nil
}
func (obj *Revoke) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
return nil
}
// NewRevokeInstruction declares a new Revoke instruction with the provided parameters and accounts.
func NewRevokeInstruction(
// Accounts:
source ag_solanago.PublicKey,
2021-09-04 08:41:00 -07:00
owner ag_solanago.PublicKey,
multisigSigners []ag_solanago.PublicKey,
) *Revoke {
2021-09-03 15:43:43 -07:00
return NewRevokeInstructionBuilder().
SetSourceAccount(source).
2021-09-04 08:41:00 -07:00
SetOwnerAccount(owner, multisigSigners...)
2021-09-03 15:43:43 -07:00
}