github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_mint_transaction.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  )
    28  
    29  // TokenMintTransaction
    30  // Mints tokens from the Token's treasury Account. If no Supply Key is defined, the transaction
    31  // will resolve to TOKEN_HAS_NO_SUPPLY_KEY.
    32  // The operation decreases the Total Supply of the Token. Total supply cannot go below zero.
    33  // The amount provided must be in the lowest denomination possible. Example:
    34  // Token A has 2 decimals. In order to mint 100 tokens, one must provide amount of 10000. In order
    35  // to mint 100.55 tokens, one must provide amount of 10055.
    36  type TokenMintTransaction struct {
    37  	Transaction
    38  	tokenID *TokenID
    39  	amount  uint64
    40  	meta    [][]byte
    41  }
    42  
    43  // NewTokenMintTransaction creates TokenMintTransaction which
    44  // mints tokens from the Token's treasury Account. If no Supply Key is defined, the transaction
    45  // will resolve to TOKEN_HAS_NO_SUPPLY_KEY.
    46  // The operation decreases the Total Supply of the Token. Total supply cannot go below zero.
    47  // The amount provided must be in the lowest denomination possible. Example:
    48  // Token A has 2 decimals. In order to mint 100 tokens, one must provide amount of 10000. In order
    49  // to mint 100.55 tokens, one must provide amount of 10055.
    50  func NewTokenMintTransaction() *TokenMintTransaction {
    51  	tx := TokenMintTransaction{
    52  		Transaction: _NewTransaction(),
    53  	}
    54  
    55  	tx._SetDefaultMaxTransactionFee(NewHbar(30))
    56  
    57  	return &tx
    58  }
    59  
    60  func _TokenMintTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TokenMintTransaction {
    61  	return &TokenMintTransaction{
    62  		Transaction: tx,
    63  		tokenID:     _TokenIDFromProtobuf(pb.GetTokenMint().GetToken()),
    64  		amount:      pb.GetTokenMint().GetAmount(),
    65  		meta:        pb.GetTokenMint().GetMetadata(),
    66  	}
    67  }
    68  
    69  // SetTokenID Sets the token for which to mint tokens. If token does not exist, transaction results in
    70  // INVALID_TOKEN_ID
    71  func (tx *TokenMintTransaction) SetTokenID(tokenID TokenID) *TokenMintTransaction {
    72  	tx._RequireNotFrozen()
    73  	tx.tokenID = &tokenID
    74  	return tx
    75  }
    76  
    77  // GetTokenID returns the TokenID for this TokenMintTransaction
    78  func (tx *TokenMintTransaction) GetTokenID() TokenID {
    79  	if tx.tokenID == nil {
    80  		return TokenID{}
    81  	}
    82  
    83  	return *tx.tokenID
    84  }
    85  
    86  // SetAmount Sets the amount to mint from the Treasury Account. Amount must be a positive non-zero number, not
    87  // bigger than the token balance of the treasury account (0; balance], represented in the lowest
    88  // denomination.
    89  func (tx *TokenMintTransaction) SetAmount(amount uint64) *TokenMintTransaction {
    90  	tx._RequireNotFrozen()
    91  	tx.amount = amount
    92  	return tx
    93  }
    94  
    95  // GetAmount returns the amount to mint from the Treasury Account
    96  func (tx *TokenMintTransaction) GetAmount() uint64 {
    97  	return tx.amount
    98  }
    99  
   100  // SetMetadatas
   101  // Applicable to tokens of type NON_FUNGIBLE_UNIQUE. A list of metadata that are being created.
   102  // Maximum allowed size of each metadata is 100 bytes
   103  func (tx *TokenMintTransaction) SetMetadatas(meta [][]byte) *TokenMintTransaction {
   104  	tx._RequireNotFrozen()
   105  	tx.meta = meta
   106  	return tx
   107  }
   108  
   109  // SetMetadata
   110  // Applicable to tokens of type NON_FUNGIBLE_UNIQUE. A list of metadata that are being created.
   111  // Maximum allowed size of each metadata is 100 bytes
   112  func (tx *TokenMintTransaction) SetMetadata(meta []byte) *TokenMintTransaction {
   113  	tx._RequireNotFrozen()
   114  	if tx.meta == nil {
   115  		tx.meta = make([][]byte, 0)
   116  	}
   117  	tx.meta = append(tx.meta, [][]byte{meta}...)
   118  	return tx
   119  }
   120  
   121  // GetMetadatas returns the metadata that are being created.
   122  func (tx *TokenMintTransaction) GetMetadatas() [][]byte {
   123  	return tx.meta
   124  }
   125  
   126  // ---- Required Interfaces ---- //
   127  
   128  // Sign uses the provided privateKey to sign the transaction.
   129  func (tx *TokenMintTransaction) Sign(privateKey PrivateKey) *TokenMintTransaction {
   130  	tx.Transaction.Sign(privateKey)
   131  	return tx
   132  }
   133  
   134  // SignWithOperator signs the transaction with client's operator privateKey.
   135  func (tx *TokenMintTransaction) SignWithOperator(client *Client) (*TokenMintTransaction, error) {
   136  	_, err := tx.Transaction.signWithOperator(client, tx)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	return tx, nil
   141  }
   142  
   143  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   144  // with the publicKey as the map key.
   145  func (tx *TokenMintTransaction) SignWith(
   146  	publicKey PublicKey,
   147  	signer TransactionSigner,
   148  ) *TokenMintTransaction {
   149  	tx.Transaction.SignWith(publicKey, signer)
   150  	return tx
   151  }
   152  
   153  // AddSignature adds a signature to the transaction.
   154  func (tx *TokenMintTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenMintTransaction {
   155  	tx.Transaction.AddSignature(publicKey, signature)
   156  	return tx
   157  }
   158  
   159  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   160  func (tx *TokenMintTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenMintTransaction {
   161  	tx.Transaction.SetGrpcDeadline(deadline)
   162  	return tx
   163  }
   164  
   165  func (tx *TokenMintTransaction) Freeze() (*TokenMintTransaction, error) {
   166  	return tx.FreezeWith(nil)
   167  }
   168  
   169  func (tx *TokenMintTransaction) FreezeWith(client *Client) (*TokenMintTransaction, error) {
   170  	_, err := tx.Transaction.freezeWith(client, tx)
   171  	return tx, err
   172  }
   173  
   174  // SetMaxTransactionFee sets the max transaction fee for this TokenMintTransaction.
   175  func (tx *TokenMintTransaction) SetMaxTransactionFee(fee Hbar) *TokenMintTransaction {
   176  	tx.Transaction.SetMaxTransactionFee(fee)
   177  	return tx
   178  }
   179  
   180  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   181  func (tx *TokenMintTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenMintTransaction {
   182  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   183  	return tx
   184  }
   185  
   186  // SetTransactionMemo sets the memo for this TokenMintTransaction.
   187  func (tx *TokenMintTransaction) SetTransactionMemo(memo string) *TokenMintTransaction {
   188  	tx.Transaction.SetTransactionMemo(memo)
   189  	return tx
   190  }
   191  
   192  // SetTransactionValidDuration sets the valid duration for this TokenMintTransaction.
   193  func (tx *TokenMintTransaction) SetTransactionValidDuration(duration time.Duration) *TokenMintTransaction {
   194  	tx.Transaction.SetTransactionValidDuration(duration)
   195  	return tx
   196  }
   197  
   198  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   199  func (tx *TokenMintTransaction) ToBytes() ([]byte, error) {
   200  	bytes, err := tx.Transaction.toBytes(tx)
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  	return bytes, nil
   205  }
   206  
   207  // SetTransactionID sets the TransactionID for this TokenMintTransaction.
   208  func (tx *TokenMintTransaction) SetTransactionID(transactionID TransactionID) *TokenMintTransaction {
   209  	tx.Transaction.SetTransactionID(transactionID)
   210  	return tx
   211  }
   212  
   213  // SetNodeAccountIDs sets the _Node AccountID for this TokenMintTransaction.
   214  func (tx *TokenMintTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenMintTransaction {
   215  	tx.Transaction.SetNodeAccountIDs(nodeID)
   216  	return tx
   217  }
   218  
   219  // SetMaxRetry sets the max number of errors before execution will fail.
   220  func (tx *TokenMintTransaction) SetMaxRetry(count int) *TokenMintTransaction {
   221  	tx.Transaction.SetMaxRetry(count)
   222  	return tx
   223  }
   224  
   225  // SetMaxBackoff The maximum amount of time to wait between retries.
   226  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   227  func (tx *TokenMintTransaction) SetMaxBackoff(max time.Duration) *TokenMintTransaction {
   228  	tx.Transaction.SetMaxBackoff(max)
   229  	return tx
   230  }
   231  
   232  // SetMinBackoff sets the minimum amount of time to wait between retries.
   233  func (tx *TokenMintTransaction) SetMinBackoff(min time.Duration) *TokenMintTransaction {
   234  	tx.Transaction.SetMinBackoff(min)
   235  	return tx
   236  }
   237  
   238  func (tx *TokenMintTransaction) SetLogLevel(level LogLevel) *TokenMintTransaction {
   239  	tx.Transaction.SetLogLevel(level)
   240  	return tx
   241  }
   242  
   243  func (tx *TokenMintTransaction) Execute(client *Client) (TransactionResponse, error) {
   244  	return tx.Transaction.execute(client, tx)
   245  }
   246  
   247  func (tx *TokenMintTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   248  	return tx.Transaction.schedule(tx)
   249  }
   250  
   251  // ----------- Overridden functions ----------------
   252  
   253  func (tx *TokenMintTransaction) getName() string {
   254  	return "TokenMintTransaction"
   255  }
   256  
   257  func (tx *TokenMintTransaction) validateNetworkOnIDs(client *Client) error {
   258  	if client == nil || !client.autoValidateChecksums {
   259  		return nil
   260  	}
   261  
   262  	if tx.tokenID != nil {
   263  		if err := tx.tokenID.ValidateChecksum(client); err != nil {
   264  			return err
   265  		}
   266  	}
   267  
   268  	return nil
   269  }
   270  
   271  func (tx *TokenMintTransaction) build() *services.TransactionBody {
   272  	return &services.TransactionBody{
   273  		TransactionFee:           tx.transactionFee,
   274  		Memo:                     tx.Transaction.memo,
   275  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   276  		TransactionID:            tx.transactionID._ToProtobuf(),
   277  		Data: &services.TransactionBody_TokenMint{
   278  			TokenMint: tx.buildProtoBody(),
   279  		},
   280  	}
   281  }
   282  
   283  func (tx *TokenMintTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   284  	return &services.SchedulableTransactionBody{
   285  		TransactionFee: tx.transactionFee,
   286  		Memo:           tx.Transaction.memo,
   287  		Data: &services.SchedulableTransactionBody_TokenMint{
   288  			TokenMint: tx.buildProtoBody(),
   289  		},
   290  	}, nil
   291  }
   292  
   293  func (tx *TokenMintTransaction) buildProtoBody() *services.TokenMintTransactionBody {
   294  	body := &services.TokenMintTransactionBody{
   295  		Amount: tx.amount,
   296  	}
   297  
   298  	if tx.meta != nil {
   299  		body.Metadata = tx.meta
   300  	}
   301  
   302  	if tx.tokenID != nil {
   303  		body.Token = tx.tokenID._ToProtobuf()
   304  	}
   305  
   306  	return body
   307  }
   308  
   309  func (tx *TokenMintTransaction) getMethod(channel *_Channel) _Method {
   310  	return _Method{
   311  		transaction: channel._GetToken().MintToken,
   312  	}
   313  }
   314  func (tx *TokenMintTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   315  	return tx.buildScheduled()
   316  }