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