github.com/gagliardetto/solana-go@v1.11.0/programs/token/InitializeMint2.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 20 ag_binary "github.com/gagliardetto/binary" 21 ag_solanago "github.com/gagliardetto/solana-go" 22 ag_format "github.com/gagliardetto/solana-go/text/format" 23 ag_treeout "github.com/gagliardetto/treeout" 24 ) 25 26 // Like InitializeMint, but does not require the Rent sysvar to be provided. 27 type InitializeMint2 struct { 28 // Number of base 10 digits to the right of the decimal place. 29 Decimals *uint8 30 31 // The authority/multisignature to mint tokens. 32 MintAuthority *ag_solanago.PublicKey 33 34 // The freeze authority/multisignature of the mint. 35 FreezeAuthority *ag_solanago.PublicKey `bin:"optional"` 36 37 // [0] = [WRITE] mint 38 // ··········· The mint to initialize. 39 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 40 } 41 42 // NewInitializeMint2InstructionBuilder creates a new `InitializeMint2` instruction builder. 43 func NewInitializeMint2InstructionBuilder() *InitializeMint2 { 44 nd := &InitializeMint2{ 45 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 1), 46 } 47 return nd 48 } 49 50 // SetDecimals sets the "decimals" parameter. 51 // Number of base 10 digits to the right of the decimal place. 52 func (inst *InitializeMint2) SetDecimals(decimals uint8) *InitializeMint2 { 53 inst.Decimals = &decimals 54 return inst 55 } 56 57 // SetMintAuthority sets the "mint_authority" parameter. 58 // The authority/multisignature to mint tokens. 59 func (inst *InitializeMint2) SetMintAuthority(mint_authority ag_solanago.PublicKey) *InitializeMint2 { 60 inst.MintAuthority = &mint_authority 61 return inst 62 } 63 64 // SetFreezeAuthority sets the "freeze_authority" parameter. 65 // The freeze authority/multisignature of the mint. 66 func (inst *InitializeMint2) SetFreezeAuthority(freeze_authority ag_solanago.PublicKey) *InitializeMint2 { 67 inst.FreezeAuthority = &freeze_authority 68 return inst 69 } 70 71 // SetMintAccount sets the "mint" account. 72 // The mint to initialize. 73 func (inst *InitializeMint2) SetMintAccount(mint ag_solanago.PublicKey) *InitializeMint2 { 74 inst.AccountMetaSlice[0] = ag_solanago.Meta(mint).WRITE() 75 return inst 76 } 77 78 // GetMintAccount gets the "mint" account. 79 // The mint to initialize. 80 func (inst *InitializeMint2) GetMintAccount() *ag_solanago.AccountMeta { 81 return inst.AccountMetaSlice[0] 82 } 83 84 func (inst InitializeMint2) Build() *Instruction { 85 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 86 Impl: inst, 87 TypeID: ag_binary.TypeIDFromUint8(Instruction_InitializeMint2), 88 }} 89 } 90 91 // ValidateAndBuild validates the instruction parameters and accounts; 92 // if there is a validation error, it returns the error. 93 // Otherwise, it builds and returns the instruction. 94 func (inst InitializeMint2) ValidateAndBuild() (*Instruction, error) { 95 if err := inst.Validate(); err != nil { 96 return nil, err 97 } 98 return inst.Build(), nil 99 } 100 101 func (inst *InitializeMint2) Validate() error { 102 // Check whether all (required) parameters are set: 103 { 104 if inst.Decimals == nil { 105 return errors.New("Decimals parameter is not set") 106 } 107 if inst.MintAuthority == nil { 108 return errors.New("MintAuthority parameter is not set") 109 } 110 } 111 112 // Check whether all (required) accounts are set: 113 { 114 if inst.AccountMetaSlice[0] == nil { 115 return errors.New("accounts.Mint is not set") 116 } 117 } 118 return nil 119 } 120 121 func (inst *InitializeMint2) EncodeToTree(parent ag_treeout.Branches) { 122 parent.Child(ag_format.Program(ProgramName, ProgramID)). 123 // 124 ParentFunc(func(programBranch ag_treeout.Branches) { 125 programBranch.Child(ag_format.Instruction("InitializeMint2")). 126 // 127 ParentFunc(func(instructionBranch ag_treeout.Branches) { 128 129 // Parameters of the instruction: 130 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 131 paramsBranch.Child(ag_format.Param(" Decimals", *inst.Decimals)) 132 paramsBranch.Child(ag_format.Param(" MintAuthority", *inst.MintAuthority)) 133 paramsBranch.Child(ag_format.Param("FreezeAuthority (OPT)", inst.FreezeAuthority)) 134 }) 135 136 // Accounts of the instruction: 137 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 138 accountsBranch.Child(ag_format.Meta("mint", inst.AccountMetaSlice[0])) 139 }) 140 }) 141 }) 142 } 143 144 func (obj InitializeMint2) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { 145 // Serialize `Decimals` param: 146 err = encoder.Encode(obj.Decimals) 147 if err != nil { 148 return err 149 } 150 // Serialize `MintAuthority` param: 151 err = encoder.Encode(obj.MintAuthority) 152 if err != nil { 153 return err 154 } 155 // Serialize `FreezeAuthority` param (optional): 156 { 157 if obj.FreezeAuthority == nil { 158 err = encoder.WriteBool(false) 159 if err != nil { 160 return err 161 } 162 } else { 163 err = encoder.WriteBool(true) 164 if err != nil { 165 return err 166 } 167 err = encoder.Encode(obj.FreezeAuthority) 168 if err != nil { 169 return err 170 } 171 } 172 } 173 return nil 174 } 175 func (obj *InitializeMint2) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { 176 // Deserialize `Decimals`: 177 err = decoder.Decode(&obj.Decimals) 178 if err != nil { 179 return err 180 } 181 // Deserialize `MintAuthority`: 182 err = decoder.Decode(&obj.MintAuthority) 183 if err != nil { 184 return err 185 } 186 // Deserialize `FreezeAuthority` (optional): 187 { 188 ok, err := decoder.ReadBool() 189 if err != nil { 190 return err 191 } 192 if ok { 193 err = decoder.Decode(&obj.FreezeAuthority) 194 if err != nil { 195 return err 196 } 197 } 198 } 199 return nil 200 } 201 202 // NewInitializeMint2Instruction declares a new InitializeMint2 instruction with the provided parameters and accounts. 203 func NewInitializeMint2Instruction( 204 // Parameters: 205 decimals uint8, 206 mint_authority ag_solanago.PublicKey, 207 freeze_authority ag_solanago.PublicKey, 208 // Accounts: 209 mint ag_solanago.PublicKey) *InitializeMint2 { 210 return NewInitializeMint2InstructionBuilder(). 211 SetDecimals(decimals). 212 SetMintAuthority(mint_authority). 213 SetFreezeAuthority(freeze_authority). 214 SetMintAccount(mint) 215 }