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