github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/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  	"github.com/hashgraph/hedera-protobufs-go/services"
    25  
    26  	"time"
    27  )
    28  
    29  type FreezeTransaction struct {
    30  	Transaction
    31  	startTime  time.Time
    32  	endTime    time.Time
    33  	fileID     *FileID
    34  	fileHash   []byte
    35  	freezeType FreezeType
    36  }
    37  
    38  func NewFreezeTransaction() *FreezeTransaction {
    39  	tx := FreezeTransaction{
    40  		Transaction: _NewTransaction(),
    41  	}
    42  
    43  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    44  
    45  	return &tx
    46  }
    47  
    48  func _FreezeTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *FreezeTransaction {
    49  	startTime := time.Date(
    50  		time.Now().Year(), time.Now().Month(), time.Now().Day(),
    51  		int(pb.GetFreeze().GetStartHour()), int(pb.GetFreeze().GetStartMin()), // nolint
    52  		0, time.Now().Nanosecond(), time.Now().Location(),
    53  	)
    54  
    55  	endTime := time.Date(
    56  		time.Now().Year(), time.Now().Month(), time.Now().Day(),
    57  		int(pb.GetFreeze().GetEndHour()), int(pb.GetFreeze().GetEndMin()), // nolint
    58  		0, time.Now().Nanosecond(), time.Now().Location(),
    59  	)
    60  
    61  	return &FreezeTransaction{
    62  		Transaction: tx,
    63  		startTime:   startTime,
    64  		endTime:     endTime,
    65  		fileID:      _FileIDFromProtobuf(pb.GetFreeze().GetUpdateFile()),
    66  		fileHash:    pb.GetFreeze().FileHash,
    67  	}
    68  }
    69  
    70  func (tx *FreezeTransaction) SetStartTime(startTime time.Time) *FreezeTransaction {
    71  	tx._RequireNotFrozen()
    72  	tx.startTime = startTime
    73  	return tx
    74  }
    75  
    76  func (tx *FreezeTransaction) GetStartTime() time.Time {
    77  	return tx.startTime
    78  }
    79  
    80  // Deprecated
    81  func (tx *FreezeTransaction) SetEndTime(endTime time.Time) *FreezeTransaction {
    82  	tx._RequireNotFrozen()
    83  	tx.endTime = endTime
    84  	return tx
    85  }
    86  
    87  // Deprecated
    88  func (tx *FreezeTransaction) GetEndTime() time.Time {
    89  	return tx.endTime
    90  }
    91  
    92  func (tx *FreezeTransaction) SetFileID(id FileID) *FreezeTransaction {
    93  	tx._RequireNotFrozen()
    94  	tx.fileID = &id
    95  	return tx
    96  }
    97  
    98  func (tx *FreezeTransaction) GetFileID() *FileID {
    99  	return tx.fileID
   100  }
   101  
   102  func (tx *FreezeTransaction) SetFreezeType(freezeType FreezeType) *FreezeTransaction {
   103  	tx._RequireNotFrozen()
   104  	tx.freezeType = freezeType
   105  	return tx
   106  }
   107  
   108  func (tx *FreezeTransaction) GetFreezeType() FreezeType {
   109  	return tx.freezeType
   110  }
   111  
   112  func (tx *FreezeTransaction) SetFileHash(hash []byte) *FreezeTransaction {
   113  	tx._RequireNotFrozen()
   114  	tx.fileHash = hash
   115  	return tx
   116  }
   117  
   118  func (tx *FreezeTransaction) GetFileHash() []byte {
   119  	return tx.fileHash
   120  }
   121  
   122  // ---- Required Interfaces ---- //
   123  
   124  // Sign uses the provided privateKey to sign the transaction.
   125  func (tx *FreezeTransaction) Sign(
   126  	privateKey PrivateKey,
   127  ) *FreezeTransaction {
   128  	tx.Transaction.Sign(privateKey)
   129  	return tx
   130  }
   131  
   132  // SignWithOperator signs the transaction with client's operator privateKey.
   133  func (tx *FreezeTransaction) SignWithOperator(
   134  	client *Client,
   135  ) (*FreezeTransaction, error) {
   136  	// If the transaction is not signed by the _Operator, we need
   137  	// to sign the transaction with the _Operator
   138  	_, err := tx.Transaction.signWithOperator(client, tx)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	return tx, nil
   143  }
   144  
   145  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   146  // with the publicKey as the map key.
   147  func (tx *FreezeTransaction) SignWith(
   148  	publicKey PublicKey,
   149  	signer TransactionSigner,
   150  ) *FreezeTransaction {
   151  	tx.Transaction.SignWith(publicKey, signer)
   152  	return tx
   153  }
   154  
   155  // AddSignature adds a signature to the transaction.
   156  func (tx *FreezeTransaction) AddSignature(publicKey PublicKey, signature []byte) *FreezeTransaction {
   157  	tx.Transaction.AddSignature(publicKey, signature)
   158  	return tx
   159  }
   160  
   161  func (tx *FreezeTransaction) Freeze() (*FreezeTransaction, error) {
   162  	return tx.FreezeWith(nil)
   163  }
   164  
   165  func (tx *FreezeTransaction) FreezeWith(client *Client) (*FreezeTransaction, error) {
   166  	_, err := tx.Transaction.freezeWith(client, tx)
   167  	return tx, err
   168  }
   169  
   170  // GetMaxTransactionFee returns the maximum transaction fee the operator (paying account) is willing to pay.
   171  func (tx *FreezeTransaction) GetMaxTransactionFee() Hbar {
   172  	return tx.Transaction.GetMaxTransactionFee()
   173  }
   174  
   175  // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay.
   176  func (tx *FreezeTransaction) SetMaxTransactionFee(fee Hbar) *FreezeTransaction {
   177  	tx._RequireNotFrozen()
   178  	tx.Transaction.SetMaxTransactionFee(fee)
   179  	return tx
   180  }
   181  
   182  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   183  func (tx *FreezeTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *FreezeTransaction {
   184  	tx._RequireNotFrozen()
   185  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   186  	return tx
   187  }
   188  
   189  // SetTransactionMemo sets the memo for this FreezeTransaction.
   190  func (tx *FreezeTransaction) SetTransactionMemo(memo string) *FreezeTransaction {
   191  	tx._RequireNotFrozen()
   192  	tx.Transaction.SetTransactionMemo(memo)
   193  	return tx
   194  }
   195  
   196  // SetTransactionValidDuration sets the valid duration for this FreezeTransaction.
   197  func (tx *FreezeTransaction) SetTransactionValidDuration(duration time.Duration) *FreezeTransaction {
   198  	tx._RequireNotFrozen()
   199  	tx.Transaction.SetTransactionValidDuration(duration)
   200  	return tx
   201  }
   202  
   203  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   204  func (tx *FreezeTransaction) ToBytes() ([]byte, error) {
   205  	bytes, err := tx.Transaction.toBytes(tx)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	return bytes, nil
   210  }
   211  
   212  // SetTransactionID sets the TransactionID for this FreezeTransaction.
   213  func (tx *FreezeTransaction) SetTransactionID(transactionID TransactionID) *FreezeTransaction {
   214  	tx._RequireNotFrozen()
   215  
   216  	tx.Transaction.SetTransactionID(transactionID)
   217  	return tx
   218  }
   219  
   220  // SetNodeAccountID sets the _Node AccountID for this FreezeTransaction.
   221  func (tx *FreezeTransaction) SetNodeAccountIDs(nodeID []AccountID) *FreezeTransaction {
   222  	tx._RequireNotFrozen()
   223  	tx.Transaction.SetNodeAccountIDs(nodeID)
   224  	return tx
   225  }
   226  
   227  // SetMaxRetry sets the max number of errors before execution will fail.
   228  func (tx *FreezeTransaction) SetMaxRetry(count int) *FreezeTransaction {
   229  	tx.Transaction.SetMaxRetry(count)
   230  	return tx
   231  }
   232  
   233  // SetMaxBackoff The maximum amount of time to wait between retries.
   234  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   235  func (tx *FreezeTransaction) SetMaxBackoff(max time.Duration) *FreezeTransaction {
   236  	tx.Transaction.SetMaxBackoff(max)
   237  	return tx
   238  }
   239  
   240  // SetMinBackoff sets the minimum amount of time to wait between retries.
   241  func (tx *FreezeTransaction) SetMinBackoff(min time.Duration) *FreezeTransaction {
   242  	tx.Transaction.SetMinBackoff(min)
   243  	return tx
   244  }
   245  
   246  func (tx *FreezeTransaction) SetLogLevel(level LogLevel) *FreezeTransaction {
   247  	tx.Transaction.SetLogLevel(level)
   248  	return tx
   249  }
   250  
   251  func (tx *FreezeTransaction) Execute(client *Client) (TransactionResponse, error) {
   252  	return tx.Transaction.execute(client, tx)
   253  }
   254  
   255  func (tx *FreezeTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   256  	return tx.Transaction.schedule(tx)
   257  }
   258  
   259  // ----------- Overridden functions ----------------
   260  
   261  func (tx *FreezeTransaction) getName() string {
   262  	return "FreezeTransaction"
   263  }
   264  func (tx *FreezeTransaction) build() *services.TransactionBody {
   265  	return &services.TransactionBody{
   266  		TransactionFee:           tx.transactionFee,
   267  		Memo:                     tx.Transaction.memo,
   268  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   269  		TransactionID:            tx.transactionID._ToProtobuf(),
   270  		Data: &services.TransactionBody_Freeze{
   271  			Freeze: tx.buildProtoBody(),
   272  		},
   273  	}
   274  }
   275  func (tx *FreezeTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   276  	return &services.SchedulableTransactionBody{
   277  		TransactionFee: tx.transactionFee,
   278  		Memo:           tx.Transaction.memo,
   279  		Data: &services.SchedulableTransactionBody_Freeze{
   280  			Freeze: tx.buildProtoBody(),
   281  		},
   282  	}, nil
   283  }
   284  func (tx *FreezeTransaction) buildProtoBody() *services.FreezeTransactionBody {
   285  	body := &services.FreezeTransactionBody{
   286  		FileHash:   tx.fileHash,
   287  		StartTime:  _TimeToProtobuf(tx.startTime),
   288  		FreezeType: services.FreezeType(tx.freezeType),
   289  	}
   290  
   291  	if tx.fileID != nil {
   292  		body.UpdateFile = tx.fileID._ToProtobuf()
   293  	}
   294  
   295  	return body
   296  }
   297  func (tx *FreezeTransaction) getMethod(channel *_Channel) _Method {
   298  	return _Method{
   299  		transaction: channel._GetFreeze().Freeze,
   300  	}
   301  }
   302  func (tx *FreezeTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   303  	return tx.buildScheduled()
   304  }