github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_delete_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitContractDeleteTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  	contractID, err := ContractIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  
    50  	contractDelete := NewContractDeleteTransaction().
    51  		SetTransferAccountID(accountID).
    52  		SetContractID(contractID).
    53  		SetTransferContractID(contractID)
    54  
    55  	err = contractDelete.validateNetworkOnIDs(client)
    56  	require.NoError(t, err)
    57  }
    58  
    59  func TestUnitContractDeleteTransactionValidateWrong(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	client, err := _NewMockClient()
    63  	client.SetLedgerID(*NewLedgerIDTestnet())
    64  	require.NoError(t, err)
    65  	client.SetAutoValidateChecksums(true)
    66  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    67  	require.NoError(t, err)
    68  	contractID, err := ContractIDFromString("0.0.123-rmkykd")
    69  	require.NoError(t, err)
    70  
    71  	contractDelete := NewContractDeleteTransaction().
    72  		SetTransferAccountID(accountID).
    73  		SetContractID(contractID).
    74  		SetTransferContractID(contractID)
    75  
    76  	err = contractDelete.validateNetworkOnIDs(client)
    77  	assert.Error(t, err)
    78  	if err != nil {
    79  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    80  	}
    81  }
    82  
    83  func TestUnitContractDeleteTransactionGet(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	spenderAccountID1 := AccountID{Account: 7}
    87  	contractID := ContractID{Contract: 7}
    88  
    89  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    90  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    91  
    92  	transaction, err := NewContractDeleteTransaction().
    93  		SetTransactionID(transactionID).
    94  		SetNodeAccountIDs(nodeAccountID).
    95  		SetContractID(contractID).
    96  		SetTransferAccountID(spenderAccountID1).
    97  		SetTransferContractID(contractID).
    98  		SetTransactionMemo("").
    99  		SetTransactionValidDuration(60 * time.Second).
   100  		SetRegenerateTransactionID(false).
   101  		Freeze()
   102  	require.NoError(t, err)
   103  
   104  	transaction.GetTransactionID()
   105  	transaction.GetNodeAccountIDs()
   106  
   107  	_, err = transaction.GetTransactionHash()
   108  	require.NoError(t, err)
   109  
   110  	transaction.GetContractID()
   111  	transaction.GetMaxTransactionFee()
   112  	transaction.GetTransactionMemo()
   113  	transaction.GetRegenerateTransactionID()
   114  	_, err = transaction.GetSignatures()
   115  	require.NoError(t, err)
   116  	transaction.GetRegenerateTransactionID()
   117  	transaction.GetMaxTransactionFee()
   118  	transaction.GetTransferAccountID()
   119  	transaction.GetTransferContractID()
   120  	transaction.GetRegenerateTransactionID()
   121  }
   122  
   123  func TestUnitContractDeleteTransactionSetNothing(t *testing.T) {
   124  	t.Parallel()
   125  
   126  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   127  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   128  
   129  	transaction, err := NewContractDeleteTransaction().
   130  		SetTransactionID(transactionID).
   131  		SetNodeAccountIDs(nodeAccountID).
   132  		Freeze()
   133  	require.NoError(t, err)
   134  
   135  	transaction.GetTransactionID()
   136  	transaction.GetNodeAccountIDs()
   137  
   138  	_, err = transaction.GetTransactionHash()
   139  	require.NoError(t, err)
   140  
   141  	transaction.GetContractID()
   142  	transaction.GetMaxTransactionFee()
   143  	transaction.GetTransactionMemo()
   144  	transaction.GetRegenerateTransactionID()
   145  	_, err = transaction.GetSignatures()
   146  	require.NoError(t, err)
   147  	transaction.GetRegenerateTransactionID()
   148  	transaction.GetMaxTransactionFee()
   149  	transaction.GetTransferAccountID()
   150  	transaction.GetTransferContractID()
   151  	transaction.GetRegenerateTransactionID()
   152  }
   153  
   154  func TestUnitContractDeleteTransactionProtoCheck(t *testing.T) {
   155  	t.Parallel()
   156  
   157  	spenderAccountID1 := AccountID{Account: 7}
   158  	contractID := ContractID{Contract: 7}
   159  
   160  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   161  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   162  
   163  	transaction, err := NewContractDeleteTransaction().
   164  		SetTransactionID(transactionID).
   165  		SetNodeAccountIDs(nodeAccountID).
   166  		SetContractID(contractID).
   167  		SetTransferAccountID(spenderAccountID1).
   168  		SetTransactionMemo("").
   169  		SetTransactionValidDuration(60 * time.Second).
   170  		SetRegenerateTransactionID(false).
   171  		SetPermanentRemoval(true).
   172  		Freeze()
   173  	require.NoError(t, err)
   174  
   175  	transaction.GetTransactionID()
   176  	transaction.GetNodeAccountIDs()
   177  
   178  	proto := transaction.build().GetContractDeleteInstance()
   179  	require.Equal(t, proto.ContractID.String(), contractID._ToProtobuf().String())
   180  	require.Equal(t, proto.Obtainers.(*services.ContractDeleteTransactionBody_TransferAccountID).TransferAccountID.String(),
   181  		spenderAccountID1._ToProtobuf().String())
   182  	require.Equal(t, proto.PermanentRemoval, true)
   183  }
   184  
   185  func TestUnitContractDeleteTransactionCoverage(t *testing.T) {
   186  	t.Parallel()
   187  
   188  	checksum := "dmqui"
   189  	grpc := time.Second * 30
   190  	contract := ContractID{Contract: 3, checksum: &checksum}
   191  	account := AccountID{Account: 3, checksum: &checksum}
   192  	nodeAccountID := []AccountID{{Account: 10}}
   193  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   194  
   195  	newKey, err := PrivateKeyGenerateEd25519()
   196  	require.NoError(t, err)
   197  
   198  	client, err := _NewMockClient()
   199  	client.SetLedgerID(*NewLedgerIDTestnet())
   200  	require.NoError(t, err)
   201  	client.SetAutoValidateChecksums(true)
   202  
   203  	transaction, err := NewContractDeleteTransaction().
   204  		SetTransactionID(transactionID).
   205  		SetNodeAccountIDs(nodeAccountID).
   206  		SetContractID(contract).
   207  		SetTransferContractID(contract).
   208  		SetPermanentRemoval(true).
   209  		SetTransferAccountID(account).
   210  		SetGrpcDeadline(&grpc).
   211  		SetMaxTransactionFee(NewHbar(3)).
   212  		SetMaxRetry(3).
   213  		SetMaxBackoff(time.Second * 30).
   214  		SetMinBackoff(time.Second * 10).
   215  		SetTransactionMemo("no").
   216  		SetTransactionValidDuration(time.Second * 30).
   217  		SetRegenerateTransactionID(false).
   218  		Freeze()
   219  	require.NoError(t, err)
   220  
   221  	transaction.validateNetworkOnIDs(client)
   222  
   223  	_, err = transaction.Schedule()
   224  	require.NoError(t, err)
   225  	transaction.GetTransactionID()
   226  	transaction.GetNodeAccountIDs()
   227  	transaction.GetMaxRetry()
   228  	transaction.GetMaxTransactionFee()
   229  	transaction.GetMaxBackoff()
   230  	transaction.GetMinBackoff()
   231  	transaction.GetRegenerateTransactionID()
   232  	byt, err := transaction.ToBytes()
   233  	require.NoError(t, err)
   234  	txFromBytes, err := TransactionFromBytes(byt)
   235  	require.NoError(t, err)
   236  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   237  	require.NoError(t, err)
   238  
   239  	_, err = transaction.GetTransactionHash()
   240  	require.NoError(t, err)
   241  	transaction.GetMaxTransactionFee()
   242  	transaction.GetTransactionMemo()
   243  	transaction.GetRegenerateTransactionID()
   244  	transaction.GetPermanentRemoval()
   245  	transaction.GetTransferAccountID()
   246  	_, err = transaction.GetSignatures()
   247  	require.NoError(t, err)
   248  	transaction.getName()
   249  	switch b := txFromBytes.(type) {
   250  	case ContractDeleteTransaction:
   251  		b.AddSignature(newKey.PublicKey(), sig)
   252  	}
   253  }
   254  
   255  func TestUnitContractDeleteTransactionMock(t *testing.T) {
   256  	t.Parallel()
   257  
   258  	nodeAccountID := []AccountID{{Account: 3}}
   259  	transactionID := TransactionIDGenerate(AccountID{Account: 3})
   260  
   261  	call := func(request *services.Transaction) *services.TransactionResponse {
   262  		require.NotEmpty(t, request.SignedTransactionBytes)
   263  		signedTransaction := services.SignedTransaction{}
   264  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   265  
   266  		require.NotEmpty(t, signedTransaction.BodyBytes)
   267  		transactionBody := services.TransactionBody{}
   268  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   269  
   270  		require.NotNil(t, transactionBody.TransactionID)
   271  		transactionId := transactionBody.TransactionID.String()
   272  		require.NotEqual(t, "", transactionId)
   273  
   274  		sigMap := signedTransaction.GetSigMap()
   275  		require.NotNil(t, sigMap)
   276  
   277  		return &services.TransactionResponse{
   278  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   279  		}
   280  	}
   281  	responses := [][]interface{}{{
   282  		call,
   283  	}}
   284  
   285  	client, server := NewMockClientAndServer(responses)
   286  	defer server.Close()
   287  
   288  	_, err := NewContractDeleteTransaction().
   289  		SetTransactionID(transactionID).
   290  		SetNodeAccountIDs(nodeAccountID).
   291  		SetContractID(ContractID{Contract: 3}).
   292  		Execute(client)
   293  	require.NoError(t, err)
   294  }