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

     1  //go:build all || e2e
     2  // +build all e2e
     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  	"fmt"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestIntegrationAccountDeleteTransactionCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	newKey, err := PrivateKeyGenerateEd25519()
    40  	require.NoError(t, err)
    41  
    42  	newBalance := NewHbar(2)
    43  
    44  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    45  
    46  	resp, err := NewAccountCreateTransaction().
    47  		SetKey(newKey.PublicKey()).
    48  		SetNodeAccountIDs(env.NodeAccountIDs).
    49  		SetInitialBalance(newBalance).
    50  		Execute(env.Client)
    51  	require.NoError(t, err)
    52  
    53  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    54  	require.NoError(t, err)
    55  
    56  	accountID := receipt.AccountID
    57  	require.NoError(t, err)
    58  
    59  	tx, err := NewAccountDeleteTransaction().
    60  		SetAccountID(*accountID).
    61  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    62  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    63  		SetTransactionID(TransactionIDGenerate(*accountID)).
    64  		FreezeWith(env.Client)
    65  	require.NoError(t, err)
    66  
    67  	tx = tx.Sign(newKey)
    68  
    69  	assert.True(t, newKey.PublicKey().VerifyTransaction(tx.Transaction))
    70  	assert.False(t, env.Client.GetOperatorPublicKey().VerifyTransaction(tx.Transaction))
    71  
    72  	resp, err = tx.Execute(env.Client)
    73  	require.NoError(t, err)
    74  
    75  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    76  	require.NoError(t, err)
    77  
    78  	err = CloseIntegrationTestEnv(env, nil)
    79  	require.NoError(t, err)
    80  }
    81  
    82  func TestIntegrationAccountDeleteTransactionNoTransferAccountID(t *testing.T) {
    83  	t.Parallel()
    84  	env := NewIntegrationTestEnv(t)
    85  
    86  	newKey, err := PrivateKeyGenerateEd25519()
    87  	require.NoError(t, err)
    88  
    89  	newBalance := NewHbar(2)
    90  
    91  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    92  
    93  	resp, err := NewAccountCreateTransaction().
    94  		SetKey(newKey.PublicKey()).
    95  		SetNodeAccountIDs(env.NodeAccountIDs).
    96  		SetInitialBalance(newBalance).
    97  		Execute(env.Client)
    98  	require.NoError(t, err)
    99  
   100  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   101  	require.NoError(t, err)
   102  
   103  	accountID := receipt.AccountID
   104  	require.NoError(t, err)
   105  
   106  	tx, err := NewAccountDeleteTransaction().
   107  		SetAccountID(*accountID).
   108  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   109  		FreezeWith(env.Client)
   110  	require.NoError(t, err)
   111  
   112  	resp, err = tx.Sign(newKey).Execute(env.Client)
   113  	assert.Error(t, err)
   114  	if err != nil {
   115  		assert.Equal(t, fmt.Sprintf("exceptional precheck status ACCOUNT_ID_DOES_NOT_EXIST received for transaction %s", resp.TransactionID), err.Error())
   116  	}
   117  
   118  	err = CloseIntegrationTestEnv(env, nil)
   119  	require.NoError(t, err)
   120  }
   121  
   122  func TestIntegrationAccountDeleteTransactionNoAccountID(t *testing.T) {
   123  	t.Parallel()
   124  	env := NewIntegrationTestEnv(t)
   125  
   126  	newKey, err := PrivateKeyGenerateEd25519()
   127  	require.NoError(t, err)
   128  
   129  	newBalance := NewHbar(2)
   130  
   131  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   132  
   133  	resp, err := NewAccountCreateTransaction().
   134  		SetKey(newKey.PublicKey()).
   135  		SetNodeAccountIDs(env.NodeAccountIDs).
   136  		SetInitialBalance(newBalance).
   137  		Execute(env.Client)
   138  	require.NoError(t, err)
   139  
   140  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   141  	require.NoError(t, err)
   142  
   143  	tx, err := NewAccountDeleteTransaction().
   144  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   145  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   146  		FreezeWith(env.Client)
   147  	require.NoError(t, err)
   148  
   149  	resp, err = tx.Sign(newKey).Execute(env.Client)
   150  	assert.Error(t, err)
   151  	if err != nil {
   152  		assert.Equal(t, fmt.Sprintf("exceptional precheck status ACCOUNT_ID_DOES_NOT_EXIST received for transaction %s", resp.TransactionID), err.Error())
   153  	}
   154  
   155  	err = CloseIntegrationTestEnv(env, nil)
   156  	require.NoError(t, err)
   157  }
   158  
   159  func TestIntegrationAccountDeleteTransactionNoSigning(t *testing.T) {
   160  	t.Parallel()
   161  	env := NewIntegrationTestEnv(t)
   162  
   163  	newKey, err := PrivateKeyGenerateEd25519()
   164  	require.NoError(t, err)
   165  
   166  	newBalance := NewHbar(2)
   167  
   168  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   169  
   170  	resp, err := NewAccountCreateTransaction().
   171  		SetKey(newKey.PublicKey()).
   172  		SetNodeAccountIDs(env.NodeAccountIDs).
   173  		SetInitialBalance(newBalance).
   174  		Execute(env.Client)
   175  	require.NoError(t, err)
   176  
   177  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   178  	require.NoError(t, err)
   179  
   180  	accountID := receipt.AccountID
   181  	require.NoError(t, err)
   182  
   183  	acc := *accountID
   184  
   185  	resp, err = NewAccountDeleteTransaction().
   186  		SetAccountID(acc).
   187  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   188  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   189  		Execute(env.Client)
   190  	require.NoError(t, err)
   191  
   192  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   193  	assert.Error(t, err)
   194  	if err != nil {
   195  		assert.Equal(t, "exceptional receipt status: INVALID_SIGNATURE", err.Error())
   196  	}
   197  
   198  	err = CloseIntegrationTestEnv(env, nil)
   199  	require.NoError(t, err)
   200  }
   201  
   202  func TestIntegrationAccountDeleteTransactionCannotDeleteWithPendingAirdrops(t *testing.T) {
   203  	env := NewIntegrationTestEnv(t)
   204  	defer CloseIntegrationTestEnv(env, nil)
   205  
   206  	// Create ft and nft
   207  	tokenID, err := createFungibleToken(&env)
   208  	require.NoError(t, err)
   209  	nftID, err := createNft(&env)
   210  	require.NoError(t, err)
   211  
   212  	// Mint some NFTs
   213  	txResponse, err := NewTokenMintTransaction().
   214  		SetTokenID(nftID).
   215  		SetMetadatas(mintMetadata).
   216  		Execute(env.Client)
   217  	require.NoError(t, err)
   218  
   219  	receipt, err := txResponse.SetValidateStatus(true).GetReceipt(env.Client)
   220  	require.NoError(t, err)
   221  
   222  	nftSerials := receipt.SerialNumbers
   223  
   224  	// Create receiver
   225  	receiver, _, err := createAccount(&env)
   226  	require.NoError(t, err)
   227  
   228  	// Airdrop the tokens
   229  	airdropTx, err := NewTokenAirdropTransaction().
   230  		AddNftTransfer(nftID.Nft(nftSerials[0]), env.OperatorID, receiver).
   231  		AddNftTransfer(nftID.Nft(nftSerials[1]), env.OperatorID, receiver).
   232  		AddTokenTransfer(tokenID, receiver, 100).
   233  		AddTokenTransfer(tokenID, env.OperatorID, -100).
   234  		Execute(env.Client)
   235  	require.NoError(t, err)
   236  
   237  	record, err := airdropTx.SetValidateStatus(true).GetRecord(env.Client)
   238  	require.NoError(t, err)
   239  
   240  	// verify the pending airdrop record
   241  	assert.Equal(t, 3, len(record.PendingAirdropRecords))
   242  
   243  	// Try to delete the sender
   244  	accountDeleteResp, err := NewAccountDeleteTransaction().
   245  		SetAccountID(env.OperatorID).
   246  		SetTransferAccountID(receiver).
   247  		Execute(env.Client)
   248  	receipt, err = accountDeleteResp.SetValidateStatus(true).GetReceipt(env.Client)
   249  	require.ErrorContains(t, err, "ACCOUNT_HAS_PENDING_AIRDROPS")
   250  }