github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_associate_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 TestIntegrationTokenAssociateTransactionCanExecute(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  	println(env.Client.GetOperatorAccountID().String())
    58  
    59  	tokenID, err := createFungibleToken(&env)
    60  	require.NoError(t, err)
    61  
    62  	transaction, err := NewTokenAssociateTransaction().
    63  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    64  		SetAccountID(accountID).
    65  		SetTokenIDs(tokenID).
    66  		FreezeWith(env.Client)
    67  	require.NoError(t, err)
    68  
    69  	resp, err = transaction.
    70  		Sign(newKey).
    71  		Execute(env.Client)
    72  	require.NoError(t, err)
    73  
    74  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	dissociate, err := NewTokenDissociateTransaction().
    78  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    79  		SetAccountID(accountID).
    80  		SetTokenIDs(tokenID).
    81  		FreezeWith(env.Client)
    82  	require.NoError(t, err)
    83  
    84  	resp, err = dissociate.
    85  		Sign(newKey).
    86  		Execute(env.Client)
    87  	require.NoError(t, err)
    88  
    89  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    90  	require.NoError(t, err)
    91  
    92  	err = CloseIntegrationTestEnv(env, &tokenID)
    93  	require.NoError(t, err)
    94  }
    95  
    96  func TestIntegrationTokenAssociateTransactionNoAccountID(t *testing.T) {
    97  	t.Parallel()
    98  	env := NewIntegrationTestEnv(t)
    99  
   100  	resp, err := NewTokenAssociateTransaction().
   101  		SetNodeAccountIDs(env.NodeAccountIDs).
   102  		Execute(env.Client)
   103  	assert.Error(t, err)
   104  	if err != nil {
   105  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID received for transaction %s", resp.TransactionID), err.Error())
   106  	}
   107  
   108  	err = CloseIntegrationTestEnv(env, nil)
   109  	require.NoError(t, err)
   110  }
   111  
   112  func TestIntegrationTokenAssociateTransactionNoTokenID(t *testing.T) {
   113  	t.Parallel()
   114  	env := NewIntegrationTestEnv(t)
   115  
   116  	newKey, err := PrivateKeyGenerateEd25519()
   117  	require.NoError(t, err)
   118  
   119  	newBalance := NewHbar(2)
   120  
   121  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   122  
   123  	resp, err := NewAccountCreateTransaction().
   124  		SetKey(newKey.PublicKey()).
   125  		SetNodeAccountIDs(env.NodeAccountIDs).
   126  		SetInitialBalance(newBalance).
   127  		Execute(env.Client)
   128  	require.NoError(t, err)
   129  
   130  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   131  	require.NoError(t, err)
   132  
   133  	accountID := *receipt.AccountID
   134  
   135  	tokenID, err := createFungibleToken(&env)
   136  	require.NoError(t, err)
   137  
   138  	transaction, err := NewTokenAssociateTransaction().
   139  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   140  		SetAccountID(accountID).
   141  		FreezeWith(env.Client)
   142  	require.NoError(t, err)
   143  
   144  	resp, err = transaction.
   145  		Sign(newKey).
   146  		Execute(env.Client)
   147  	require.NoError(t, err)
   148  
   149  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   150  	require.NoError(t, err)
   151  
   152  	tx, err := NewAccountDeleteTransaction().
   153  		SetAccountID(accountID).
   154  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   155  		FreezeWith(env.Client)
   156  	require.NoError(t, err)
   157  
   158  	resp, err = tx.
   159  		Sign(newKey).
   160  		Execute(env.Client)
   161  	require.NoError(t, err)
   162  
   163  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   164  	require.NoError(t, err)
   165  
   166  	err = CloseIntegrationTestEnv(env, &tokenID)
   167  	require.NoError(t, err)
   168  }
   169  
   170  func TestIntegrationTokenAssociateTransactionAutoAssociate(t *testing.T) {
   171  	t.Parallel()
   172  	env := NewIntegrationTestEnv(t)
   173  
   174  	newKey, err := PrivateKeyGenerateEd25519()
   175  	require.NoError(t, err)
   176  
   177  	newBalance := NewHbar(2)
   178  
   179  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   180  
   181  	resp, err := NewAccountCreateTransaction().
   182  		SetKey(newKey.PublicKey()).
   183  		SetMaxAutomaticTokenAssociations(2).
   184  		SetNodeAccountIDs(env.NodeAccountIDs).
   185  		SetInitialBalance(newBalance).
   186  		Execute(env.Client)
   187  	require.NoError(t, err)
   188  
   189  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   190  	require.NoError(t, err)
   191  
   192  	accountID := *receipt.AccountID
   193  
   194  	toke, err := NewTokenCreateTransaction().
   195  		SetTokenName("ffff").
   196  		SetTokenSymbol("F").
   197  		SetDecimals(3).
   198  		SetInitialSupply(2).
   199  		SetTreasuryAccountID(accountID).
   200  		SetAdminKey(newKey.PublicKey()).
   201  		SetFreezeKey(newKey.PublicKey()).
   202  		SetWipeKey(newKey.PublicKey()).
   203  		SetKycKey(newKey.PublicKey()).
   204  		SetSupplyKey(newKey.PublicKey()).
   205  		SetFreezeDefault(false).
   206  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   207  		FreezeWith(env.Client)
   208  	require.NoError(t, err)
   209  
   210  	toke.Sign(newKey)
   211  
   212  	resp, err = toke.Execute(env.Client)
   213  	require.NoError(t, err)
   214  
   215  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   216  	require.NoError(t, err)
   217  
   218  	assert.NotNil(t, receipt.TokenID)
   219  
   220  	record, err := NewTransactionRecordQuery().
   221  		SetTransactionID(resp.TransactionID).
   222  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   223  		Execute(env.Client)
   224  	require.NoError(t, err)
   225  
   226  	for _, s := range record.AutomaticTokenAssociations {
   227  		assert.Equal(t, accountID.String(), s.AccountID.String())
   228  		assert.Equal(t, receipt.TokenID.String(), s.TokenID.String())
   229  	}
   230  
   231  	err = CloseIntegrationTestEnv(env, nil)
   232  	require.NoError(t, err)
   233  }