github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_unfreeze_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 TestIntegrationTokenUnfreezeTransactionCanExecute(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  		SetNodeAccountIDs(env.NodeAccountIDs).
    48  		SetKey(newKey.PublicKey()).
    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  
    58  	tokenID, err := createFungibleToken(&env)
    59  	require.NoError(t, err)
    60  
    61  	transaction, err := NewTokenAssociateTransaction().
    62  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    63  		SetAccountID(accountID).
    64  		SetTokenIDs(tokenID).
    65  		FreezeWith(env.Client)
    66  	require.NoError(t, err)
    67  
    68  	resp, err = transaction.
    69  		Sign(newKey).
    70  		Execute(env.Client)
    71  	require.NoError(t, err)
    72  
    73  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    74  	require.NoError(t, err)
    75  
    76  	resp, err = NewTokenFreezeTransaction().
    77  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    78  		SetAccountID(accountID).
    79  		SetTokenID(tokenID).
    80  		Execute(env.Client)
    81  	require.NoError(t, err)
    82  
    83  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    84  	require.NoError(t, err)
    85  
    86  	resp, err = NewTokenUnfreezeTransaction().
    87  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    88  		SetAccountID(accountID).
    89  		SetTokenID(tokenID).
    90  		Execute(env.Client)
    91  	require.NoError(t, err)
    92  
    93  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    94  	require.NoError(t, err)
    95  
    96  	tx, err := NewAccountDeleteTransaction().
    97  		SetAccountID(accountID).
    98  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    99  		FreezeWith(env.Client)
   100  	require.NoError(t, err)
   101  
   102  	resp, err = tx.
   103  		Sign(newKey).
   104  		Execute(env.Client)
   105  	require.NoError(t, err)
   106  
   107  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   108  	require.NoError(t, err)
   109  
   110  	err = CloseIntegrationTestEnv(env, &tokenID)
   111  	require.NoError(t, err)
   112  }
   113  
   114  func TestIntegrationTokenUnfreezeTransactionNoTokenID(t *testing.T) {
   115  	t.Parallel()
   116  	env := NewIntegrationTestEnv(t)
   117  
   118  	newKey, err := PrivateKeyGenerateEd25519()
   119  	require.NoError(t, err)
   120  
   121  	newBalance := NewHbar(2)
   122  
   123  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   124  
   125  	resp, err := NewAccountCreateTransaction().
   126  		SetNodeAccountIDs(env.NodeAccountIDs).
   127  		SetKey(newKey.PublicKey()).
   128  		SetInitialBalance(newBalance).
   129  		Execute(env.Client)
   130  	require.NoError(t, err)
   131  
   132  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   133  	require.NoError(t, err)
   134  
   135  	accountID := *receipt.AccountID
   136  
   137  	tokenID, err := createFungibleToken(&env)
   138  	require.NoError(t, err)
   139  
   140  	transaction, err := NewTokenAssociateTransaction().
   141  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   142  		SetAccountID(accountID).
   143  		SetTokenIDs(tokenID).
   144  		FreezeWith(env.Client)
   145  	require.NoError(t, err)
   146  
   147  	resp, err = transaction.
   148  		Sign(newKey).
   149  		Execute(env.Client)
   150  	require.NoError(t, err)
   151  
   152  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   153  	require.NoError(t, err)
   154  
   155  	resp, err = NewTokenFreezeTransaction().
   156  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   157  		SetAccountID(accountID).
   158  		SetTokenID(tokenID).
   159  		Execute(env.Client)
   160  	require.NoError(t, err)
   161  
   162  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   163  	require.NoError(t, err)
   164  
   165  	resp2, err := NewTokenUnfreezeTransaction().
   166  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   167  		SetAccountID(accountID).
   168  		Execute(env.Client)
   169  	assert.Error(t, err)
   170  	if err != nil {
   171  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_TOKEN_ID received for transaction %s", resp2.TransactionID), err.Error())
   172  	}
   173  
   174  	tx, err := NewAccountDeleteTransaction().
   175  		SetAccountID(accountID).
   176  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   177  		FreezeWith(env.Client)
   178  	require.NoError(t, err)
   179  
   180  	resp, err = tx.
   181  		Sign(newKey).
   182  		Execute(env.Client)
   183  	require.NoError(t, err)
   184  
   185  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   186  	require.NoError(t, err)
   187  
   188  	err = CloseIntegrationTestEnv(env, &tokenID)
   189  	require.NoError(t, err)
   190  }
   191  
   192  func TestIntegrationTokenUnfreezeTransactionNoAccountID(t *testing.T) {
   193  	t.Parallel()
   194  	env := NewIntegrationTestEnv(t)
   195  
   196  	newKey, err := PrivateKeyGenerateEd25519()
   197  	require.NoError(t, err)
   198  
   199  	newBalance := NewHbar(2)
   200  
   201  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   202  
   203  	resp, err := NewAccountCreateTransaction().
   204  		SetNodeAccountIDs(env.NodeAccountIDs).
   205  		SetKey(newKey.PublicKey()).
   206  		SetInitialBalance(newBalance).
   207  		Execute(env.Client)
   208  	require.NoError(t, err)
   209  
   210  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   211  	require.NoError(t, err)
   212  
   213  	accountID := *receipt.AccountID
   214  
   215  	tokenID, err := createFungibleToken(&env)
   216  	require.NoError(t, err)
   217  
   218  	transaction, err := NewTokenAssociateTransaction().
   219  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   220  		SetAccountID(accountID).
   221  		SetTokenIDs(tokenID).
   222  		FreezeWith(env.Client)
   223  	require.NoError(t, err)
   224  
   225  	resp, err = transaction.
   226  		Sign(newKey).
   227  		Execute(env.Client)
   228  	require.NoError(t, err)
   229  
   230  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   231  	require.NoError(t, err)
   232  
   233  	resp, err = NewTokenFreezeTransaction().
   234  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   235  		SetAccountID(accountID).
   236  		SetTokenID(tokenID).
   237  		Execute(env.Client)
   238  	require.NoError(t, err)
   239  
   240  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   241  	require.NoError(t, err)
   242  
   243  	resp2, err := NewTokenUnfreezeTransaction().
   244  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   245  		SetTokenID(tokenID).
   246  		Execute(env.Client)
   247  	assert.Error(t, err)
   248  	if err != nil {
   249  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID received for transaction %s", resp2.TransactionID), err.Error())
   250  	}
   251  
   252  	tx, err := NewAccountDeleteTransaction().
   253  		SetAccountID(accountID).
   254  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   255  		FreezeWith(env.Client)
   256  	require.NoError(t, err)
   257  
   258  	resp, err = tx.
   259  		Sign(newKey).
   260  		Execute(env.Client)
   261  	require.NoError(t, err)
   262  
   263  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   264  	require.NoError(t, err)
   265  
   266  	err = CloseIntegrationTestEnv(env, &tokenID)
   267  	require.NoError(t, err)
   268  }