github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_delete_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  // ContractDeleteTransaction marks a contract as deleted and transfers its remaining hBars, if any, to a
    30  // designated receiver. After a contract is deleted, it can no longer be called.
    31  type ContractDeleteTransaction struct {
    32  	Transaction
    33  	contractID        *ContractID
    34  	transferContactID *ContractID
    35  	transferAccountID *AccountID
    36  	permanentRemoval  bool
    37  }
    38  
    39  // NewContractDeleteTransaction creates ContractDeleteTransaction which marks a contract as deleted and transfers its remaining hBars, if any, to a
    40  // designated receiver. After a contract is deleted, it can no longer be called.
    41  func NewContractDeleteTransaction() *ContractDeleteTransaction {
    42  	tx := ContractDeleteTransaction{
    43  		Transaction: _NewTransaction(),
    44  	}
    45  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    46  
    47  	return &tx
    48  }
    49  
    50  func _ContractDeleteTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *ContractDeleteTransaction {
    51  	return &ContractDeleteTransaction{
    52  		Transaction:       tx,
    53  		contractID:        _ContractIDFromProtobuf(pb.GetContractDeleteInstance().GetContractID()),
    54  		transferContactID: _ContractIDFromProtobuf(pb.GetContractDeleteInstance().GetTransferContractID()),
    55  		transferAccountID: _AccountIDFromProtobuf(pb.GetContractDeleteInstance().GetTransferAccountID()),
    56  		permanentRemoval:  pb.GetContractDeleteInstance().GetPermanentRemoval(),
    57  	}
    58  }
    59  
    60  // Sets the contract ID which will be deleted.
    61  func (tx *ContractDeleteTransaction) SetContractID(contractID ContractID) *ContractDeleteTransaction {
    62  	tx._RequireNotFrozen()
    63  	tx.contractID = &contractID
    64  	return tx
    65  }
    66  
    67  // Returns the contract ID which will be deleted.
    68  func (tx *ContractDeleteTransaction) GetContractID() ContractID {
    69  	if tx.contractID == nil {
    70  		return ContractID{}
    71  	}
    72  
    73  	return *tx.contractID
    74  }
    75  
    76  // Sets the contract ID which will receive all remaining hbars.
    77  func (tx *ContractDeleteTransaction) SetTransferContractID(transferContactID ContractID) *ContractDeleteTransaction {
    78  	tx._RequireNotFrozen()
    79  	tx.transferContactID = &transferContactID
    80  	return tx
    81  }
    82  
    83  // Returns the contract ID which will receive all remaining hbars.
    84  func (tx *ContractDeleteTransaction) GetTransferContractID() ContractID {
    85  	if tx.transferContactID == nil {
    86  		return ContractID{}
    87  	}
    88  
    89  	return *tx.transferContactID
    90  }
    91  
    92  // Sets the account ID which will receive all remaining hbars.
    93  func (tx *ContractDeleteTransaction) SetTransferAccountID(accountID AccountID) *ContractDeleteTransaction {
    94  	tx._RequireNotFrozen()
    95  	tx.transferAccountID = &accountID
    96  
    97  	return tx
    98  }
    99  
   100  // Returns the account ID which will receive all remaining hbars.
   101  func (tx *ContractDeleteTransaction) GetTransferAccountID() AccountID {
   102  	if tx.transferAccountID == nil {
   103  		return AccountID{}
   104  	}
   105  
   106  	return *tx.transferAccountID
   107  }
   108  
   109  // SetPermanentRemoval
   110  // If set to true, means this is a "synthetic" system transaction being used to
   111  // alert mirror nodes that the contract is being permanently removed from the ledger.
   112  // IMPORTANT: User transactions cannot set this field to true, as permanent
   113  // removal is always managed by the ledger itself. Any ContractDeleteTransaction
   114  // submitted to HAPI with permanent_removal=true will be rejected with precheck status
   115  // PERMANENT_REMOVAL_REQUIRES_SYSTEM_INITIATION.
   116  func (tx *ContractDeleteTransaction) SetPermanentRemoval(remove bool) *ContractDeleteTransaction {
   117  	tx._RequireNotFrozen()
   118  	tx.permanentRemoval = remove
   119  
   120  	return tx
   121  }
   122  
   123  // GetPermanentRemoval returns true if this is a "synthetic" system transaction.
   124  func (tx *ContractDeleteTransaction) GetPermanentRemoval() bool {
   125  	return tx.permanentRemoval
   126  }
   127  
   128  // ---- Required Interfaces ---- //
   129  
   130  // Sign uses the provided privateKey to sign the transaction.
   131  func (tx *ContractDeleteTransaction) Sign(
   132  	privateKey PrivateKey,
   133  ) *ContractDeleteTransaction {
   134  	tx.Transaction.Sign(privateKey)
   135  	return tx
   136  }
   137  
   138  // SignWithOperator signs the transaction with client's operator privateKey.
   139  func (tx *ContractDeleteTransaction) SignWithOperator(
   140  	client *Client,
   141  ) (*ContractDeleteTransaction, error) {
   142  	_, err := tx.Transaction.signWithOperator(client, tx)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return tx, nil
   147  }
   148  
   149  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   150  // with the publicKey as the map key.
   151  func (tx *ContractDeleteTransaction) SignWith(
   152  	publicKey PublicKey,
   153  	signer TransactionSigner,
   154  ) *ContractDeleteTransaction {
   155  	tx.Transaction.SignWith(publicKey, signer)
   156  	return tx
   157  }
   158  
   159  func (tx *ContractDeleteTransaction) AddSignature(publicKey PublicKey, signature []byte) *ContractDeleteTransaction {
   160  	tx.Transaction.AddSignature(publicKey, signature)
   161  	return tx
   162  }
   163  
   164  // When execution is attempted, a single attempt will timeout when tx deadline is reached. (The SDK may subsequently retry the execution.)
   165  func (tx *ContractDeleteTransaction) SetGrpcDeadline(deadline *time.Duration) *ContractDeleteTransaction {
   166  	tx.Transaction.SetGrpcDeadline(deadline)
   167  	return tx
   168  }
   169  
   170  func (tx *ContractDeleteTransaction) Freeze() (*ContractDeleteTransaction, error) {
   171  	return tx.FreezeWith(nil)
   172  }
   173  
   174  func (tx *ContractDeleteTransaction) FreezeWith(client *Client) (*ContractDeleteTransaction, error) {
   175  	_, err := tx.Transaction.freezeWith(client, tx)
   176  	return tx, err
   177  }
   178  
   179  // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay.
   180  func (tx *ContractDeleteTransaction) SetMaxTransactionFee(fee Hbar) *ContractDeleteTransaction {
   181  	tx._RequireNotFrozen()
   182  	tx.Transaction.SetMaxTransactionFee(fee)
   183  	return tx
   184  }
   185  
   186  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   187  func (tx *ContractDeleteTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *ContractDeleteTransaction {
   188  	tx._RequireNotFrozen()
   189  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   190  	return tx
   191  }
   192  
   193  // SetTransactionMemo sets the memo for this ContractDeleteTransaction.
   194  func (tx *ContractDeleteTransaction) SetTransactionMemo(memo string) *ContractDeleteTransaction {
   195  	tx._RequireNotFrozen()
   196  	tx.Transaction.SetTransactionMemo(memo)
   197  	return tx
   198  }
   199  
   200  // SetTransactionValidDuration sets the valid duration for this ContractDeleteTransaction.
   201  func (tx *ContractDeleteTransaction) SetTransactionValidDuration(duration time.Duration) *ContractDeleteTransaction {
   202  	tx._RequireNotFrozen()
   203  	tx.Transaction.SetTransactionValidDuration(duration)
   204  	return tx
   205  }
   206  
   207  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   208  func (tx *ContractDeleteTransaction) ToBytes() ([]byte, error) {
   209  	bytes, err := tx.Transaction.toBytes(tx)
   210  	if err != nil {
   211  		return nil, err
   212  	}
   213  	return bytes, nil
   214  }
   215  
   216  // SetTransactionID sets the TransactionID for this ContractDeleteTransaction.
   217  func (tx *ContractDeleteTransaction) SetTransactionID(transactionID TransactionID) *ContractDeleteTransaction {
   218  	tx._RequireNotFrozen()
   219  
   220  	tx.Transaction.SetTransactionID(transactionID)
   221  	return tx
   222  }
   223  
   224  // SetNodeAccountIDs sets the _Node AccountID for this ContractDeleteTransaction.
   225  func (tx *ContractDeleteTransaction) SetNodeAccountIDs(nodeID []AccountID) *ContractDeleteTransaction {
   226  	tx._RequireNotFrozen()
   227  	tx.Transaction.SetNodeAccountIDs(nodeID)
   228  	return tx
   229  }
   230  
   231  // SetMaxRetry sets the max number of errors before execution will fail.
   232  func (tx *ContractDeleteTransaction) SetMaxRetry(count int) *ContractDeleteTransaction {
   233  	tx.Transaction.SetMaxRetry(count)
   234  	return tx
   235  }
   236  
   237  // SetMaxBackoff The maximum amount of time to wait between retries.
   238  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   239  func (tx *ContractDeleteTransaction) SetMaxBackoff(max time.Duration) *ContractDeleteTransaction {
   240  	tx.Transaction.SetMaxBackoff(max)
   241  	return tx
   242  }
   243  
   244  // SetMinBackoff sets the minimum amount of time to wait between retries.
   245  func (tx *ContractDeleteTransaction) SetMinBackoff(min time.Duration) *ContractDeleteTransaction {
   246  	tx.Transaction.SetMinBackoff(min)
   247  	return tx
   248  }
   249  
   250  func (tx *ContractDeleteTransaction) SetLogLevel(level LogLevel) *ContractDeleteTransaction {
   251  	tx.Transaction.SetLogLevel(level)
   252  	return tx
   253  }
   254  
   255  func (tx *ContractDeleteTransaction) Execute(client *Client) (TransactionResponse, error) {
   256  	return tx.Transaction.execute(client, tx)
   257  }
   258  
   259  func (tx *ContractDeleteTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   260  	return tx.Transaction.schedule(tx)
   261  }
   262  
   263  // ----------- Overridden functions ----------------
   264  
   265  func (tx *ContractDeleteTransaction) getName() string {
   266  	return "ContractDeleteTransaction"
   267  }
   268  func (tx *ContractDeleteTransaction) validateNetworkOnIDs(client *Client) error {
   269  	if client == nil || !client.autoValidateChecksums {
   270  		return nil
   271  	}
   272  
   273  	if tx.contractID != nil {
   274  		if err := tx.contractID.ValidateChecksum(client); err != nil {
   275  			return err
   276  		}
   277  	}
   278  
   279  	if tx.transferContactID != nil {
   280  		if err := tx.transferContactID.ValidateChecksum(client); err != nil {
   281  			return err
   282  		}
   283  	}
   284  
   285  	if tx.transferAccountID != nil {
   286  		if err := tx.transferAccountID.ValidateChecksum(client); err != nil {
   287  			return err
   288  		}
   289  	}
   290  
   291  	return nil
   292  }
   293  
   294  func (tx *ContractDeleteTransaction) build() *services.TransactionBody {
   295  	pb := services.TransactionBody{
   296  		TransactionFee:           tx.transactionFee,
   297  		Memo:                     tx.Transaction.memo,
   298  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   299  		TransactionID:            tx.transactionID._ToProtobuf(),
   300  		Data: &services.TransactionBody_ContractDeleteInstance{
   301  			ContractDeleteInstance: tx.buildProtoBody(),
   302  		},
   303  	}
   304  
   305  	return &pb
   306  }
   307  
   308  func (tx *ContractDeleteTransaction) buildProtoBody() *services.ContractDeleteTransactionBody {
   309  	body := &services.ContractDeleteTransactionBody{
   310  		PermanentRemoval: tx.permanentRemoval,
   311  	}
   312  
   313  	if tx.contractID != nil {
   314  		body.ContractID = tx.contractID._ToProtobuf()
   315  	}
   316  
   317  	if tx.transferContactID != nil {
   318  		body.Obtainers = &services.ContractDeleteTransactionBody_TransferContractID{
   319  			TransferContractID: tx.transferContactID._ToProtobuf(),
   320  		}
   321  	}
   322  
   323  	if tx.transferAccountID != nil {
   324  		body.Obtainers = &services.ContractDeleteTransactionBody_TransferAccountID{
   325  			TransferAccountID: tx.transferAccountID._ToProtobuf(),
   326  		}
   327  	}
   328  
   329  	return body
   330  }
   331  
   332  func (tx *ContractDeleteTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   333  	return &services.SchedulableTransactionBody{
   334  		TransactionFee: tx.transactionFee,
   335  		Memo:           tx.Transaction.memo,
   336  		Data: &services.SchedulableTransactionBody_ContractDeleteInstance{
   337  			ContractDeleteInstance: tx.buildProtoBody(),
   338  		},
   339  	}, nil
   340  }
   341  
   342  func (tx *ContractDeleteTransaction) getMethod(channel *_Channel) _Method {
   343  	return _Method{
   344  		transaction: channel._GetContract().DeleteContract,
   345  	}
   346  }
   347  
   348  func (tx *ContractDeleteTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   349  	return tx.buildScheduled()
   350  }