github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_burn_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  // TokenBurnTransaction Burns tokens from the Token's treasury Account.
    30  // 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
    33  // zero.
    34  // The amount provided must be in the lowest denomination possible. Example:
    35  // Token A has 2 decimals. In order to burn 100 tokens, one must provide amount of 10000. In order
    36  // to burn 100.55 tokens, one must provide amount of 10055.
    37  type TokenBurnTransaction struct {
    38  	Transaction
    39  	tokenID *TokenID
    40  	amount  uint64
    41  	serial  []int64
    42  }
    43  
    44  // NewTokenBurnTransaction creates TokenBurnTransaction which burns tokens from the Token's treasury Account.
    45  // If no Supply Key is defined, the transaction
    46  // will resolve to TOKEN_HAS_NO_SUPPLY_KEY.
    47  // The operation decreases the Total Supply of the Token. Total supply cannot go below
    48  // zero.
    49  // The amount provided must be in the lowest denomination possible. Example:
    50  // Token A has 2 decimals. In order to burn 100 tokens, one must provide amount of 10000. In order
    51  // to burn 100.55 tokens, one must provide amount of 10055.
    52  func NewTokenBurnTransaction() *TokenBurnTransaction {
    53  	tx := TokenBurnTransaction{
    54  		Transaction: _NewTransaction(),
    55  	}
    56  
    57  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    58  
    59  	return &tx
    60  }
    61  
    62  func _TokenBurnTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TokenBurnTransaction {
    63  	return &TokenBurnTransaction{
    64  		Transaction: tx,
    65  		tokenID:     _TokenIDFromProtobuf(pb.GetTokenBurn().Token),
    66  		amount:      pb.GetTokenBurn().GetAmount(),
    67  		serial:      pb.GetTokenBurn().GetSerialNumbers(),
    68  	}
    69  }
    70  
    71  // SetTokenID Sets the token for which to burn tokens. If token does not exist, transaction results in
    72  // INVALID_TOKEN_ID
    73  func (tx *TokenBurnTransaction) SetTokenID(tokenID TokenID) *TokenBurnTransaction {
    74  	tx._RequireNotFrozen()
    75  	tx.tokenID = &tokenID
    76  	return tx
    77  }
    78  
    79  // GetTokenID returns the TokenID for the token which will be burned.
    80  func (tx *TokenBurnTransaction) GetTokenID() TokenID {
    81  	if tx.tokenID == nil {
    82  		return TokenID{}
    83  	}
    84  
    85  	return *tx.tokenID
    86  }
    87  
    88  // SetAmount Sets the amount to burn from the Treasury Account. Amount must be a positive non-zero number, not
    89  // bigger than the token balance of the treasury account (0; balance], represented in the lowest
    90  // denomination.
    91  func (tx *TokenBurnTransaction) SetAmount(amount uint64) *TokenBurnTransaction {
    92  	tx._RequireNotFrozen()
    93  	tx.amount = amount
    94  	return tx
    95  }
    96  
    97  // Deprecated: Use TokenBurnTransaction.GetAmount() instead.
    98  func (tx *TokenBurnTransaction) GetAmmount() uint64 {
    99  	return tx.amount
   100  }
   101  
   102  func (tx *TokenBurnTransaction) GetAmount() uint64 {
   103  	return tx.amount
   104  }
   105  
   106  // SetSerialNumber
   107  // Applicable to tokens of type NON_FUNGIBLE_UNIQUE.
   108  // The list of serial numbers to be burned.
   109  func (tx *TokenBurnTransaction) SetSerialNumber(serial int64) *TokenBurnTransaction {
   110  	tx._RequireNotFrozen()
   111  	if tx.serial == nil {
   112  		tx.serial = make([]int64, 0)
   113  	}
   114  	tx.serial = append(tx.serial, serial)
   115  	return tx
   116  }
   117  
   118  // SetSerialNumbers sets the list of serial numbers to be burned.
   119  func (tx *TokenBurnTransaction) SetSerialNumbers(serial []int64) *TokenBurnTransaction {
   120  	tx._RequireNotFrozen()
   121  	tx.serial = serial
   122  	return tx
   123  }
   124  
   125  // GetSerialNumbers returns the list of serial numbers to be burned.
   126  func (tx *TokenBurnTransaction) GetSerialNumbers() []int64 {
   127  	return tx.serial
   128  }
   129  
   130  // ---- Required Interfaces ---- //
   131  
   132  // Sign uses the provided privateKey to sign the transaction.
   133  func (tx *TokenBurnTransaction) Sign(privateKey PrivateKey) *TokenBurnTransaction {
   134  	tx.Transaction.Sign(privateKey)
   135  	return tx
   136  }
   137  
   138  // SignWithOperator signs the transaction with client's operator privateKey.
   139  func (tx *TokenBurnTransaction) SignWithOperator(client *Client) (*TokenBurnTransaction, error) {
   140  	_, err := tx.Transaction.signWithOperator(client, tx)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	return tx, nil
   145  }
   146  
   147  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   148  // with the publicKey as the map key.
   149  func (tx *TokenBurnTransaction) SignWith(
   150  	publicKey PublicKey,
   151  	signer TransactionSigner,
   152  ) *TokenBurnTransaction {
   153  	tx.Transaction.SignWith(publicKey, signer)
   154  	return tx
   155  }
   156  
   157  // AddSignature adds a signature to the transaction.
   158  func (tx *TokenBurnTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenBurnTransaction {
   159  	tx.Transaction.AddSignature(publicKey, signature)
   160  	return tx
   161  }
   162  
   163  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   164  func (tx *TokenBurnTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenBurnTransaction {
   165  	tx.Transaction.SetGrpcDeadline(deadline)
   166  	return tx
   167  }
   168  
   169  func (tx *TokenBurnTransaction) Freeze() (*TokenBurnTransaction, error) {
   170  	return tx.FreezeWith(nil)
   171  }
   172  
   173  func (tx *TokenBurnTransaction) FreezeWith(client *Client) (*TokenBurnTransaction, error) {
   174  	_, err := tx.Transaction.freezeWith(client, tx)
   175  	return tx, err
   176  }
   177  
   178  // SetMaxTransactionFee sets the max transaction fee for this TokenBurnTransaction.
   179  func (tx *TokenBurnTransaction) SetMaxTransactionFee(fee Hbar) *TokenBurnTransaction {
   180  	tx.Transaction.SetMaxTransactionFee(fee)
   181  	return tx
   182  }
   183  
   184  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   185  func (tx *TokenBurnTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenBurnTransaction {
   186  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   187  	return tx
   188  }
   189  
   190  // SetTransactionMemo sets the memo for this TokenBurnTransaction.
   191  func (tx *TokenBurnTransaction) SetTransactionMemo(memo string) *TokenBurnTransaction {
   192  	tx.Transaction.SetTransactionMemo(memo)
   193  	return tx
   194  }
   195  
   196  // SetTransactionValidDuration sets the valid duration for this TokenBurnTransaction.
   197  func (tx *TokenBurnTransaction) SetTransactionValidDuration(duration time.Duration) *TokenBurnTransaction {
   198  	tx.Transaction.SetTransactionValidDuration(duration)
   199  	return tx
   200  }
   201  
   202  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   203  func (tx *TokenBurnTransaction) ToBytes() ([]byte, error) {
   204  	bytes, err := tx.Transaction.toBytes(tx)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	return bytes, nil
   209  }
   210  
   211  // SetTransactionID sets the TransactionID for this TokenBurnTransaction.
   212  func (tx *TokenBurnTransaction) SetTransactionID(transactionID TransactionID) *TokenBurnTransaction {
   213  	tx.Transaction.SetTransactionID(transactionID)
   214  	return tx
   215  }
   216  
   217  // SetNodeAccountIDs sets the _Node AccountID for this TokenBurnTransaction.
   218  func (tx *TokenBurnTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenBurnTransaction {
   219  	tx.Transaction.SetNodeAccountIDs(nodeID)
   220  	return tx
   221  }
   222  
   223  // SetMaxRetry sets the max number of errors before execution will fail.
   224  func (tx *TokenBurnTransaction) SetMaxRetry(count int) *TokenBurnTransaction {
   225  	tx.Transaction.SetMaxRetry(count)
   226  	return tx
   227  }
   228  
   229  // SetMaxBackoff The maximum amount of time to wait between retries.
   230  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   231  func (tx *TokenBurnTransaction) SetMaxBackoff(max time.Duration) *TokenBurnTransaction {
   232  	tx.Transaction.SetMaxBackoff(max)
   233  	return tx
   234  }
   235  
   236  // SetMinBackoff sets the minimum amount of time to wait between retries.
   237  func (tx *TokenBurnTransaction) SetMinBackoff(min time.Duration) *TokenBurnTransaction {
   238  	tx.Transaction.SetMinBackoff(min)
   239  	return tx
   240  }
   241  
   242  func (tx *TokenBurnTransaction) SetLogLevel(level LogLevel) *TokenBurnTransaction {
   243  	tx.Transaction.SetLogLevel(level)
   244  	return tx
   245  }
   246  
   247  func (tx *TokenBurnTransaction) Execute(client *Client) (TransactionResponse, error) {
   248  	return tx.Transaction.execute(client, tx)
   249  }
   250  
   251  func (tx *TokenBurnTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   252  	return tx.Transaction.schedule(tx)
   253  }
   254  
   255  // ----------- Overridden functions ----------------
   256  
   257  func (tx *TokenBurnTransaction) getName() string {
   258  	return "TokenBurnTransaction"
   259  }
   260  
   261  func (tx *TokenBurnTransaction) validateNetworkOnIDs(client *Client) error {
   262  	if client == nil || !client.autoValidateChecksums {
   263  		return nil
   264  	}
   265  
   266  	if tx.tokenID != nil {
   267  		if err := tx.tokenID.ValidateChecksum(client); err != nil {
   268  			return err
   269  		}
   270  	}
   271  
   272  	return nil
   273  }
   274  
   275  func (tx *TokenBurnTransaction) build() *services.TransactionBody {
   276  	return &services.TransactionBody{
   277  		TransactionFee:           tx.transactionFee,
   278  		Memo:                     tx.Transaction.memo,
   279  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   280  		TransactionID:            tx.transactionID._ToProtobuf(),
   281  		Data: &services.TransactionBody_TokenBurn{
   282  			TokenBurn: tx.buildProtoBody(),
   283  		},
   284  	}
   285  }
   286  
   287  func (tx *TokenBurnTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   288  	return &services.SchedulableTransactionBody{
   289  		TransactionFee: tx.transactionFee,
   290  		Memo:           tx.Transaction.memo,
   291  		Data: &services.SchedulableTransactionBody_TokenBurn{
   292  			TokenBurn: tx.buildProtoBody(),
   293  		},
   294  	}, nil
   295  }
   296  
   297  func (tx *TokenBurnTransaction) buildProtoBody() *services.TokenBurnTransactionBody {
   298  	body := &services.TokenBurnTransactionBody{
   299  		Amount: tx.amount,
   300  	}
   301  
   302  	if tx.tokenID != nil {
   303  		body.Token = tx.tokenID._ToProtobuf()
   304  	}
   305  
   306  	if tx.serial != nil {
   307  		body.SerialNumbers = tx.serial
   308  	}
   309  
   310  	return body
   311  }
   312  
   313  func (tx *TokenBurnTransaction) getMethod(channel *_Channel) _Method {
   314  	return _Method{
   315  		transaction: channel._GetToken().BurnToken,
   316  	}
   317  }
   318  func (tx *TokenBurnTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   319  	return tx.buildScheduled()
   320  }