github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_freeze_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  // TokenFreezeTransaction
    30  // Freezes transfers of the specified token for the account. Must be signed by the Token's freezeKey.
    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 no Freeze Key is defined, the transaction will resolve to TOKEN_HAS_NO_FREEZE_KEY.
    38  // Once executed the Account is marked as Frozen and will not be able to receive or send tokens
    39  // unless unfrozen. The operation is idempotent.
    40  type TokenFreezeTransaction struct {
    41  	Transaction
    42  	tokenID   *TokenID
    43  	accountID *AccountID
    44  }
    45  
    46  // NewTokenFreezeTransaction creates TokenFreezeTransaction which
    47  // freezes transfers of the specified token for the account. Must be signed by the Token's freezeKey.
    48  // If the provided account is not found, the transaction will resolve to INVALID_ACCOUNT_ID.
    49  // If the provided account has been deleted, the transaction will resolve to ACCOUNT_DELETED.
    50  // If the provided token is not found, the transaction will resolve to INVALID_TOKEN_ID.
    51  // If the provided token has been deleted, the transaction will resolve to TOKEN_WAS_DELETED.
    52  // If an Association between the provided token and account is not found, the transaction will
    53  // resolve to TOKEN_NOT_ASSOCIATED_TO_ACCOUNT.
    54  // If no Freeze Key is defined, the transaction will resolve to TOKEN_HAS_NO_FREEZE_KEY.
    55  // Once executed the Account is marked as Frozen and will not be able to receive or send tokens
    56  // unless unfrozen. The operation is idempotent.
    57  func NewTokenFreezeTransaction() *TokenFreezeTransaction {
    58  	tx := TokenFreezeTransaction{
    59  		Transaction: _NewTransaction(),
    60  	}
    61  
    62  	tx._SetDefaultMaxTransactionFee(NewHbar(30))
    63  
    64  	return &tx
    65  }
    66  
    67  func _TokenFreezeTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *TokenFreezeTransaction {
    68  	return &TokenFreezeTransaction{
    69  		Transaction: tx,
    70  		tokenID:     _TokenIDFromProtobuf(pb.GetTokenFreeze().GetToken()),
    71  		accountID:   _AccountIDFromProtobuf(pb.GetTokenFreeze().GetAccount()),
    72  	}
    73  }
    74  
    75  // SetTokenID Sets the token for which this account will be frozen. If token does not exist, transaction results
    76  // in INVALID_TOKEN_ID
    77  func (tx *TokenFreezeTransaction) SetTokenID(tokenID TokenID) *TokenFreezeTransaction {
    78  	tx._RequireNotFrozen()
    79  	tx.tokenID = &tokenID
    80  	return tx
    81  }
    82  
    83  // GetTokenID returns the token for which this account will be frozen.
    84  func (tx *TokenFreezeTransaction) GetTokenID() TokenID {
    85  	if tx.tokenID == nil {
    86  		return TokenID{}
    87  	}
    88  
    89  	return *tx.tokenID
    90  }
    91  
    92  // SetAccountID Sets the account to be frozen
    93  func (tx *TokenFreezeTransaction) SetAccountID(accountID AccountID) *TokenFreezeTransaction {
    94  	tx._RequireNotFrozen()
    95  	tx.accountID = &accountID
    96  	return tx
    97  }
    98  
    99  // GetAccountID returns the account to be frozen
   100  func (tx *TokenFreezeTransaction) GetAccountID() AccountID {
   101  	if tx.accountID == nil {
   102  		return AccountID{}
   103  	}
   104  
   105  	return *tx.accountID
   106  }
   107  
   108  // ---- Required Interfaces ---- //
   109  
   110  // Sign uses the provided privateKey to sign the transaction.
   111  func (tx *TokenFreezeTransaction) Sign(privateKey PrivateKey) *TokenFreezeTransaction {
   112  	tx.Transaction.Sign(privateKey)
   113  	return tx
   114  }
   115  
   116  // SignWithOperator signs the transaction with client's operator privateKey.
   117  func (tx *TokenFreezeTransaction) SignWithOperator(client *Client) (*TokenFreezeTransaction, error) {
   118  	_, err := tx.Transaction.signWithOperator(client, tx)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	return tx, nil
   123  }
   124  
   125  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   126  // with the publicKey as the map key.
   127  func (tx *TokenFreezeTransaction) SignWith(
   128  	publicKey PublicKey,
   129  	signer TransactionSigner,
   130  ) *TokenFreezeTransaction {
   131  	tx.Transaction.SignWith(publicKey, signer)
   132  	return tx
   133  }
   134  
   135  // AddSignature adds a signature to the transaction.
   136  func (tx *TokenFreezeTransaction) AddSignature(publicKey PublicKey, signature []byte) *TokenFreezeTransaction {
   137  	tx.Transaction.AddSignature(publicKey, signature)
   138  	return tx
   139  }
   140  
   141  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   142  func (tx *TokenFreezeTransaction) SetGrpcDeadline(deadline *time.Duration) *TokenFreezeTransaction {
   143  	tx.Transaction.SetGrpcDeadline(deadline)
   144  	return tx
   145  }
   146  
   147  func (tx *TokenFreezeTransaction) Freeze() (*TokenFreezeTransaction, error) {
   148  	return tx.FreezeWith(nil)
   149  }
   150  
   151  func (tx *TokenFreezeTransaction) FreezeWith(client *Client) (*TokenFreezeTransaction, error) {
   152  	_, err := tx.Transaction.freezeWith(client, tx)
   153  	return tx, err
   154  }
   155  
   156  // SetMaxTransactionFee sets the max transaction fee for this TokenFreezeTransaction.
   157  func (tx *TokenFreezeTransaction) SetMaxTransactionFee(fee Hbar) *TokenFreezeTransaction {
   158  	tx.Transaction.SetMaxTransactionFee(fee)
   159  	return tx
   160  }
   161  
   162  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   163  func (tx *TokenFreezeTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *TokenFreezeTransaction {
   164  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   165  	return tx
   166  }
   167  
   168  // SetTransactionMemo sets the memo for this TokenFreezeTransaction.
   169  func (tx *TokenFreezeTransaction) SetTransactionMemo(memo string) *TokenFreezeTransaction {
   170  	tx.Transaction.SetTransactionMemo(memo)
   171  	return tx
   172  }
   173  
   174  // SetTransactionValidDuration sets the valid duration for this TokenFreezeTransaction.
   175  func (tx *TokenFreezeTransaction) SetTransactionValidDuration(duration time.Duration) *TokenFreezeTransaction {
   176  	tx.Transaction.SetTransactionValidDuration(duration)
   177  	return tx
   178  }
   179  
   180  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   181  func (tx *TokenFreezeTransaction) ToBytes() ([]byte, error) {
   182  	bytes, err := tx.Transaction.toBytes(tx)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	return bytes, nil
   187  }
   188  
   189  // SetTransactionID sets the TransactionID for this TokenFreezeTransaction.
   190  func (tx *TokenFreezeTransaction) SetTransactionID(transactionID TransactionID) *TokenFreezeTransaction {
   191  	tx.Transaction.SetTransactionID(transactionID)
   192  	return tx
   193  }
   194  
   195  // SetNodeAccountIDs sets the _Node AccountID for this TokenFreezeTransaction.
   196  func (tx *TokenFreezeTransaction) SetNodeAccountIDs(nodeID []AccountID) *TokenFreezeTransaction {
   197  	tx.Transaction.SetNodeAccountIDs(nodeID)
   198  	return tx
   199  }
   200  
   201  // SetMaxRetry sets the max number of errors before execution will fail.
   202  func (tx *TokenFreezeTransaction) SetMaxRetry(count int) *TokenFreezeTransaction {
   203  	tx.Transaction.SetMaxRetry(count)
   204  	return tx
   205  }
   206  
   207  // SetMaxBackoff The maximum amount of time to wait between retries.
   208  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   209  func (tx *TokenFreezeTransaction) SetMaxBackoff(max time.Duration) *TokenFreezeTransaction {
   210  	tx.Transaction.SetMaxBackoff(max)
   211  	return tx
   212  }
   213  
   214  // SetMinBackoff sets the minimum amount of time to wait between retries.
   215  func (tx *TokenFreezeTransaction) SetMinBackoff(min time.Duration) *TokenFreezeTransaction {
   216  	tx.Transaction.SetMinBackoff(min)
   217  	return tx
   218  }
   219  
   220  func (tx *TokenFreezeTransaction) SetLogLevel(level LogLevel) *TokenFreezeTransaction {
   221  	tx.Transaction.SetLogLevel(level)
   222  	return tx
   223  }
   224  
   225  func (tx *TokenFreezeTransaction) Execute(client *Client) (TransactionResponse, error) {
   226  	return tx.Transaction.execute(client, tx)
   227  }
   228  
   229  func (tx *TokenFreezeTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   230  	return tx.Transaction.schedule(tx)
   231  }
   232  
   233  // ----------- Overridden functions ----------------
   234  
   235  func (tx *TokenFreezeTransaction) getName() string {
   236  	return "TokenFreezeTransaction"
   237  }
   238  
   239  func (tx *TokenFreezeTransaction) validateNetworkOnIDs(client *Client) error {
   240  	if client == nil || !client.autoValidateChecksums {
   241  		return nil
   242  	}
   243  
   244  	if tx.tokenID != nil {
   245  		if err := tx.tokenID.ValidateChecksum(client); err != nil {
   246  			return err
   247  		}
   248  	}
   249  
   250  	if tx.accountID != nil {
   251  		if err := tx.accountID.ValidateChecksum(client); err != nil {
   252  			return err
   253  		}
   254  	}
   255  
   256  	return nil
   257  }
   258  
   259  func (tx *TokenFreezeTransaction) build() *services.TransactionBody {
   260  	return &services.TransactionBody{
   261  		TransactionFee:           tx.transactionFee,
   262  		Memo:                     tx.Transaction.memo,
   263  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   264  		TransactionID:            tx.transactionID._ToProtobuf(),
   265  		Data: &services.TransactionBody_TokenFreeze{
   266  			TokenFreeze: tx.buildProtoBody(),
   267  		},
   268  	}
   269  }
   270  
   271  func (tx *TokenFreezeTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   272  	return &services.SchedulableTransactionBody{
   273  		TransactionFee: tx.transactionFee,
   274  		Memo:           tx.Transaction.memo,
   275  		Data: &services.SchedulableTransactionBody_TokenFreeze{
   276  			TokenFreeze: tx.buildProtoBody(),
   277  		},
   278  	}, nil
   279  }
   280  
   281  func (tx *TokenFreezeTransaction) buildProtoBody() *services.TokenFreezeAccountTransactionBody {
   282  	body := &services.TokenFreezeAccountTransactionBody{}
   283  	if tx.tokenID != nil {
   284  		body.Token = tx.tokenID._ToProtobuf()
   285  	}
   286  
   287  	if tx.accountID != nil {
   288  		body.Account = tx.accountID._ToProtobuf()
   289  	}
   290  
   291  	return body
   292  }
   293  
   294  func (tx *TokenFreezeTransaction) getMethod(channel *_Channel) _Method {
   295  	return _Method{
   296  		transaction: channel._GetToken().FreezeTokenAccount,
   297  	}
   298  }
   299  func (tx *TokenFreezeTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   300  	return tx.buildScheduled()
   301  }