github.com/gagliardetto/solana-go@v1.11.0/programs/token/InitializeMultisig2.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 // Like InitializeMultisig, but does not require the Rent sysvar to be provided. 28 type InitializeMultisig2 struct { 29 // The number of signers (M) required to validate this multisignature account. 30 M *uint8 31 32 // [0] = [WRITE] account 33 // ··········· The multisignature account to initialize. 34 // 35 // [1] = [SIGNER] signers 36 // ··········· The signer accounts, must equal to N where 1 <= N <= 11. 37 Accounts ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 38 Signers ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 39 } 40 41 func (obj *InitializeMultisig2) SetAccounts(accounts []*ag_solanago.AccountMeta) error { 42 obj.Accounts, obj.Signers = ag_solanago.AccountMetaSlice(accounts).SplitFrom(1) 43 return nil 44 } 45 46 func (slice InitializeMultisig2) GetAccounts() (accounts []*ag_solanago.AccountMeta) { 47 accounts = append(accounts, slice.Accounts...) 48 accounts = append(accounts, slice.Signers...) 49 return 50 } 51 52 // NewInitializeMultisig2InstructionBuilder creates a new `InitializeMultisig2` instruction builder. 53 func NewInitializeMultisig2InstructionBuilder() *InitializeMultisig2 { 54 nd := &InitializeMultisig2{ 55 Accounts: make(ag_solanago.AccountMetaSlice, 1), 56 Signers: make(ag_solanago.AccountMetaSlice, 0), 57 } 58 return nd 59 } 60 61 // SetM sets the "m" parameter. 62 // The number of signers (M) required to validate this multisignature account. 63 func (inst *InitializeMultisig2) SetM(m uint8) *InitializeMultisig2 { 64 inst.M = &m 65 return inst 66 } 67 68 // SetAccount sets the "account" account. 69 // The multisignature account to initialize. 70 func (inst *InitializeMultisig2) SetAccount(account ag_solanago.PublicKey) *InitializeMultisig2 { 71 inst.Accounts[0] = ag_solanago.Meta(account).WRITE() 72 return inst 73 } 74 75 // GetAccount gets the "account" account. 76 // The multisignature account to initialize. 77 func (inst *InitializeMultisig2) GetAccount() *ag_solanago.AccountMeta { 78 return inst.Accounts[0] 79 } 80 81 // AddSigners adds the "signers" accounts. 82 // The signer accounts, must equal to N where 1 <= N <= 11. 83 func (inst *InitializeMultisig2) AddSigners(signers ...ag_solanago.PublicKey) *InitializeMultisig2 { 84 for _, signer := range signers { 85 inst.Signers = append(inst.Signers, ag_solanago.Meta(signer).SIGNER()) 86 } 87 return inst 88 } 89 90 func (inst InitializeMultisig2) Build() *Instruction { 91 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 92 Impl: inst, 93 TypeID: ag_binary.TypeIDFromUint8(Instruction_InitializeMultisig2), 94 }} 95 } 96 97 // ValidateAndBuild validates the instruction parameters and accounts; 98 // if there is a validation error, it returns the error. 99 // Otherwise, it builds and returns the instruction. 100 func (inst InitializeMultisig2) ValidateAndBuild() (*Instruction, error) { 101 if err := inst.Validate(); err != nil { 102 return nil, err 103 } 104 return inst.Build(), nil 105 } 106 107 func (inst *InitializeMultisig2) Validate() error { 108 // Check whether all (required) parameters are set: 109 { 110 if inst.M == nil { 111 return errors.New("M parameter is not set") 112 } 113 } 114 115 // Check whether all (required) accounts are set: 116 { 117 if inst.Accounts[0] == nil { 118 return errors.New("accounts.Account is not set") 119 } 120 if 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 *InitializeMultisig2) 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("InitializeMultisig2")). 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 paramsBranch.Child(ag_format.Param("M", *inst.M)) 141 }) 142 143 // Accounts of the instruction: 144 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 145 accountsBranch.Child(ag_format.Meta("account", inst.Accounts[0])) 146 147 signersBranch := accountsBranch.Child(fmt.Sprintf("signers[len=%v]", len(inst.Signers))) 148 for i, v := range inst.Signers { 149 if len(inst.Signers) > 9 && i < 10 { 150 signersBranch.Child(ag_format.Meta(fmt.Sprintf(" [%v]", i), v)) 151 } else { 152 signersBranch.Child(ag_format.Meta(fmt.Sprintf("[%v]", i), v)) 153 } 154 } 155 }) 156 }) 157 }) 158 } 159 160 func (obj InitializeMultisig2) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 161 // Serialize `M` param: 162 err = encoder.Encode(obj.M) 163 if err != nil { 164 return err 165 } 166 return nil 167 } 168 func (obj *InitializeMultisig2) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 169 // Deserialize `M`: 170 err = decoder.Decode(&obj.M) 171 if err != nil { 172 return err 173 } 174 return nil 175 } 176 177 // NewInitializeMultisig2Instruction declares a new InitializeMultisig2 instruction with the provided parameters and accounts. 178 func NewInitializeMultisig2Instruction( 179 // Parameters: 180 m uint8, 181 // Accounts: 182 account ag_solanago.PublicKey, 183 signers []ag_solanago.PublicKey, 184 ) *InitializeMultisig2 { 185 return NewInitializeMultisig2InstructionBuilder(). 186 SetM(m). 187 SetAccount(account). 188 AddSigners(signers...) 189 }