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