github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_dissociate_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 TestIntegrationTokenDissociateTransactionCanExecute(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  
    58  	tokenID, err := createFungibleToken(&env)
    59  	require.NoError(t, err)
    60  
    61  	associateTx, 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 = associateTx.
    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  	dissociateTx, err := NewTokenDissociateTransaction().
    77  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    78  		SetAccountID(accountID).
    79  		SetTokenIDs(tokenID).
    80  		FreezeWith(env.Client)
    81  	require.NoError(t, err)
    82  
    83  	resp, err = dissociateTx.
    84  		Sign(newKey).
    85  		Execute(env.Client)
    86  	require.NoError(t, err)
    87  
    88  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    89  	require.NoError(t, err)
    90  
    91  	tx, err := NewAccountDeleteTransaction().
    92  		SetAccountID(accountID).
    93  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    94  		FreezeWith(env.Client)
    95  	require.NoError(t, err)
    96  
    97  	resp, err = tx.
    98  		Sign(newKey).
    99  		Execute(env.Client)
   100  	require.NoError(t, err)
   101  
   102  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   103  	require.NoError(t, err)
   104  
   105  	err = CloseIntegrationTestEnv(env, &tokenID)
   106  	require.NoError(t, err)
   107  }
   108  
   109  func TestIntegrationTokenDissociateTransactionNoSigningOne(t *testing.T) {
   110  	t.Parallel()
   111  	env := NewIntegrationTestEnv(t)
   112  
   113  	newKey, err := PrivateKeyGenerateEd25519()
   114  	require.NoError(t, err)
   115  
   116  	newBalance := NewHbar(2)
   117  
   118  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   119  
   120  	resp, err := NewAccountCreateTransaction().
   121  		SetKey(newKey.PublicKey()).
   122  		SetNodeAccountIDs(env.NodeAccountIDs).
   123  		SetInitialBalance(newBalance).
   124  		Execute(env.Client)
   125  	require.NoError(t, err)
   126  
   127  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   128  	require.NoError(t, err)
   129  
   130  	accountID := *receipt.AccountID
   131  
   132  	tokenID, err := createFungibleToken(&env)
   133  	require.NoError(t, err)
   134  
   135  	_, err = NewTokenAssociateTransaction().
   136  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   137  		SetAccountID(accountID).
   138  		SetTokenIDs(tokenID).
   139  		Execute(env.Client)
   140  	require.NoError(t, err)
   141  
   142  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   143  	require.NoError(t, err)
   144  
   145  	_, err = NewTokenDissociateTransaction().
   146  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   147  		SetAccountID(accountID).
   148  		SetTokenIDs(tokenID).
   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  	_, err = NewAccountDeleteTransaction().
   156  		SetAccountID(accountID).
   157  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   158  		Execute(env.Client)
   159  	require.NoError(t, err)
   160  
   161  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   162  	require.NoError(t, err)
   163  
   164  	err = CloseIntegrationTestEnv(env, &tokenID)
   165  	require.NoError(t, err)
   166  }
   167  
   168  func TestIntegrationTokenDissociateTransactionNoTokenID(t *testing.T) {
   169  	t.Parallel()
   170  	env := NewIntegrationTestEnv(t)
   171  
   172  	newKey, err := PrivateKeyGenerateEd25519()
   173  	require.NoError(t, err)
   174  
   175  	newBalance := NewHbar(2)
   176  
   177  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   178  
   179  	resp, err := NewAccountCreateTransaction().
   180  		SetNodeAccountIDs(env.NodeAccountIDs).
   181  		SetKey(newKey.PublicKey()).
   182  		SetInitialBalance(newBalance).
   183  		Execute(env.Client)
   184  	require.NoError(t, err)
   185  
   186  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   187  	require.NoError(t, err)
   188  
   189  	accountID := *receipt.AccountID
   190  
   191  	tokenID, err := createFungibleToken(&env)
   192  	require.NoError(t, err)
   193  
   194  	associateTx, err := NewTokenAssociateTransaction().
   195  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   196  		SetAccountID(accountID).
   197  		SetTokenIDs(tokenID).
   198  		FreezeWith(env.Client)
   199  	require.NoError(t, err)
   200  
   201  	resp, err = associateTx.
   202  		Sign(newKey).
   203  		Execute(env.Client)
   204  	require.NoError(t, err)
   205  
   206  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   207  	require.NoError(t, err)
   208  
   209  	dissociateTx, err := NewTokenDissociateTransaction().
   210  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   211  		SetAccountID(accountID).
   212  		FreezeWith(env.Client)
   213  	require.NoError(t, err)
   214  
   215  	resp, err = dissociateTx.
   216  		Sign(newKey).
   217  		Execute(env.Client)
   218  	require.NoError(t, err)
   219  
   220  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   221  	require.NoError(t, err)
   222  
   223  	_, err = NewAccountDeleteTransaction().
   224  		SetAccountID(accountID).
   225  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   226  		Execute(env.Client)
   227  	require.NoError(t, err)
   228  
   229  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   230  	require.NoError(t, err)
   231  
   232  	err = CloseIntegrationTestEnv(env, &tokenID)
   233  	require.NoError(t, err)
   234  }
   235  
   236  func TestIntegrationTokenDissociateTransactionNoAccountID(t *testing.T) {
   237  	t.Parallel()
   238  	env := NewIntegrationTestEnv(t)
   239  
   240  	newKey, err := PrivateKeyGenerateEd25519()
   241  	require.NoError(t, err)
   242  
   243  	newBalance := NewHbar(2)
   244  
   245  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   246  
   247  	resp, err := NewAccountCreateTransaction().
   248  		SetNodeAccountIDs(env.NodeAccountIDs).
   249  		SetKey(newKey.PublicKey()).
   250  		SetInitialBalance(newBalance).
   251  		Execute(env.Client)
   252  	require.NoError(t, err)
   253  
   254  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   255  	require.NoError(t, err)
   256  
   257  	accountID := *receipt.AccountID
   258  
   259  	tokenID, err := createFungibleToken(&env)
   260  	require.NoError(t, err)
   261  
   262  	associateTx, err := NewTokenAssociateTransaction().
   263  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   264  		SetAccountID(accountID).
   265  		SetTokenIDs(tokenID).
   266  		FreezeWith(env.Client)
   267  	require.NoError(t, err)
   268  
   269  	resp, err = associateTx.
   270  		Sign(newKey).
   271  		Execute(env.Client)
   272  	require.NoError(t, err)
   273  
   274  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   275  	require.NoError(t, err)
   276  
   277  	dissociateTx, err := NewTokenDissociateTransaction().
   278  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   279  		FreezeWith(env.Client)
   280  	require.NoError(t, err)
   281  
   282  	resp2, err := dissociateTx.
   283  		Sign(newKey).
   284  		Execute(env.Client)
   285  	assert.Error(t, err)
   286  	if err != nil {
   287  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID received for transaction %s", resp2.TransactionID), err.Error())
   288  	}
   289  
   290  	_, err = NewAccountDeleteTransaction().
   291  		SetAccountID(accountID).
   292  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   293  		Execute(env.Client)
   294  	require.NoError(t, err)
   295  
   296  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   297  	require.NoError(t, err)
   298  
   299  	err = CloseIntegrationTestEnv(env, &tokenID)
   300  	require.NoError(t, err)
   301  }