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

     1  //go:build all || e2e
     2  // +build all e2e
     3  
     4  package hedera
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  /*-
    14   *
    15   * Hedera Go SDK
    16   *
    17   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    18   *
    19   * Licensed under the Apache License, Version 2.0 (the "License");
    20   * you may not use this file except in compliance with the License.
    21   * You may obtain a copy of the License at
    22   *
    23   *      http://www.apache.org/licenses/LICENSE-2.0
    24   *
    25   * Unless required by applicable law or agreed to in writing, software
    26   * distributed under the License is distributed on an "AS IS" BASIS,
    27   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    28   * See the License for the specific language governing permissions and
    29   * limitations under the License.
    30   *
    31   */
    32  
    33  // Limited max auto association tests
    34  func TestLimitedMaxAutoAssociationsFungibleTokensFlow(t *testing.T) {
    35  	t.Parallel()
    36  	env := NewIntegrationTestEnv(t)
    37  
    38  	// create token1
    39  	tokenID1, err := createFungibleToken(&env)
    40  	require.NoError(t, err)
    41  
    42  	// create token2
    43  	tokenID2, err := createFungibleToken(&env)
    44  	require.NoError(t, err)
    45  
    46  	// account create with 1 max auto associations
    47  	receiver, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
    48  		tx.SetMaxAutomaticTokenAssociations(1)
    49  	})
    50  	require.NoError(t, err)
    51  
    52  	// transfer token1 to receiver account
    53  	tokenTransferTransaction, err := NewTransferTransaction().
    54  		AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -10).
    55  		AddTokenTransfer(tokenID1, receiver, 10).
    56  		Execute(env.Client)
    57  	require.NoError(t, err)
    58  
    59  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
    60  	require.NoError(t, err)
    61  
    62  	// transfer token2 to the receiver should fail with NO_REMAINING_AUTOMATIC_ASSOCIATIONS
    63  	tokenTransferTransaction2, err := NewTransferTransaction().
    64  		AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -10).
    65  		AddTokenTransfer(tokenID2, receiver, 10).
    66  		Execute(env.Client)
    67  	require.NoError(t, err)
    68  
    69  	_, err = tokenTransferTransaction2.SetValidateStatus(true).GetReceipt(env.Client)
    70  	require.ErrorContains(t, err, "NO_REMAINING_AUTOMATIC_ASSOCIATIONS")
    71  }
    72  
    73  func TestLimitedMaxAutoAssociationsNFTsFlow(t *testing.T) {
    74  	t.Parallel()
    75  	env := NewIntegrationTestEnv(t)
    76  
    77  	// create 2 NFT collections and mint 10 NFTs for each collection
    78  	nftID1, err := createNft(&env)
    79  	require.NoError(t, err)
    80  
    81  	mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client)
    82  	receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client)
    83  	require.NoError(t, err)
    84  
    85  	nftID2, err := createNft(&env)
    86  	require.NoError(t, err)
    87  
    88  	mint, err = NewTokenMintTransaction().SetTokenID(nftID2).SetMetadatas(mintMetadata).Execute(env.Client)
    89  	receipt, err = mint.SetValidateStatus(true).GetReceipt(env.Client)
    90  	require.NoError(t, err)
    91  
    92  	serials := receipt.SerialNumbers
    93  
    94  	// account create with 1 max auto associations
    95  	receiver, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
    96  		tx.SetMaxAutomaticTokenAssociations(1)
    97  	})
    98  	require.NoError(t, err)
    99  
   100  	// transfer nftID1 nfts to receiver account
   101  	tokenTransferTransaction, err := NewTransferTransaction().
   102  		AddNftTransfer(nftID1.Nft(serials[0]), env.OperatorID, receiver).
   103  		AddNftTransfer(nftID1.Nft(serials[1]), env.OperatorID, receiver).
   104  		AddNftTransfer(nftID1.Nft(serials[2]), env.OperatorID, receiver).
   105  		AddNftTransfer(nftID1.Nft(serials[3]), env.OperatorID, receiver).
   106  		Execute(env.Client)
   107  	require.NoError(t, err)
   108  
   109  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   110  	require.NoError(t, err)
   111  
   112  	// transfer nftID2 nft to receiver should fail with NO_REMAINING_AUTOMATIC_ASSOCIATIONS
   113  	tokenTransferTransaction2, err := NewTransferTransaction().
   114  		AddNftTransfer(nftID2.Nft(serials[0]), env.OperatorID, receiver).
   115  		Execute(env.Client)
   116  	require.NoError(t, err)
   117  
   118  	_, err = tokenTransferTransaction2.SetValidateStatus(true).GetReceipt(env.Client)
   119  	require.ErrorContains(t, err, "NO_REMAINING_AUTOMATIC_ASSOCIATIONS")
   120  }
   121  
   122  func TestLimitedMaxAutoAssociationsFungibleTokensWithManualAssociate(t *testing.T) {
   123  	t.Parallel()
   124  	env := NewIntegrationTestEnv(t)
   125  
   126  	// create token1
   127  	tokenID1, err := createFungibleToken(&env)
   128  
   129  	// account create
   130  	receiver, key, err := createAccount(&env)
   131  	require.NoError(t, err)
   132  
   133  	frozenAssociateTxn, err := NewTokenAssociateTransaction().SetAccountID(receiver).AddTokenID(tokenID1).FreezeWith(env.Client)
   134  	require.NoError(t, err)
   135  	resp, err := frozenAssociateTxn.Sign(key).Execute(env.Client)
   136  	require.NoError(t, err)
   137  
   138  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   139  	require.NoError(t, err)
   140  
   141  	// transfer token1 to receiver account
   142  	tokenTransferTransaction, err := NewTransferTransaction().
   143  		AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -10).
   144  		AddTokenTransfer(tokenID1, receiver, 10).
   145  		Execute(env.Client)
   146  	require.NoError(t, err)
   147  
   148  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   149  	require.NoError(t, err)
   150  
   151  	// verify the balance of the receiver is 10
   152  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(receiver).Execute(env.Client)
   153  	require.NoError(t, err)
   154  	assert.Equal(t, uint64(10), tokenBalance.Tokens.Get(tokenID1))
   155  }
   156  
   157  func TestLimitedMaxAutoAssociationsNFTsManualAssociate(t *testing.T) {
   158  	t.Parallel()
   159  	env := NewIntegrationTestEnv(t)
   160  
   161  	// create NFT collection and mint 10
   162  	nftID1, err := createNft(&env)
   163  	require.NoError(t, err)
   164  
   165  	mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client)
   166  	receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client)
   167  	require.NoError(t, err)
   168  
   169  	serials := receipt.SerialNumbers
   170  
   171  	// account create
   172  	receiver, key, err := createAccount(&env)
   173  	require.NoError(t, err)
   174  
   175  	frozenAssociateTxn, err := NewTokenAssociateTransaction().SetAccountID(receiver).AddTokenID(nftID1).FreezeWith(env.Client)
   176  	require.NoError(t, err)
   177  	resp, err := frozenAssociateTxn.Sign(key).Execute(env.Client)
   178  	require.NoError(t, err)
   179  
   180  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   181  	require.NoError(t, err)
   182  
   183  	// transfer nftID1 nfts to receiver account
   184  	tokenTransferTransaction, err := NewTransferTransaction().
   185  		AddNftTransfer(nftID1.Nft(serials[0]), env.OperatorID, receiver).
   186  		AddNftTransfer(nftID1.Nft(serials[1]), env.OperatorID, receiver).
   187  		AddNftTransfer(nftID1.Nft(serials[2]), env.OperatorID, receiver).
   188  		AddNftTransfer(nftID1.Nft(serials[3]), env.OperatorID, receiver).
   189  		Execute(env.Client)
   190  	require.NoError(t, err)
   191  
   192  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   193  	require.NoError(t, err)
   194  }
   195  
   196  // HIP-904 Unlimited max auto association tests
   197  func TestUnlimitedMaxAutoAssociationsExecutes(t *testing.T) {
   198  	t.Parallel()
   199  	env := NewIntegrationTestEnv(t)
   200  
   201  	// account create with unlimited max auto associations - verify it executes
   202  	_, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   203  		tx.SetMaxAutomaticTokenAssociations(-1)
   204  	})
   205  	require.NoError(t, err)
   206  
   207  	accountID, newKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   208  		tx.SetMaxAutomaticTokenAssociations(100)
   209  	})
   210  	require.NoError(t, err)
   211  
   212  	// update the account with unlimited max auto associations
   213  	accountUpdateFrozen, err := NewAccountUpdateTransaction().
   214  		SetMaxAutomaticTokenAssociations(-1).
   215  		SetAccountID(accountID).
   216  		FreezeWith(env.Client)
   217  	require.NoError(t, err)
   218  
   219  	accountUpdate, err := accountUpdateFrozen.Sign(newKey).Execute(env.Client)
   220  	require.NoError(t, err)
   221  
   222  	_, err = accountUpdate.SetValidateStatus(true).GetReceipt(env.Client)
   223  	require.NoError(t, err)
   224  }
   225  
   226  func TestUnlimitedMaxAutoAssociationsAllowsToTransferFungibleTokens(t *testing.T) {
   227  	t.Parallel()
   228  	env := NewIntegrationTestEnv(t)
   229  
   230  	// create token1
   231  	tokenID1, err := createFungibleToken(&env)
   232  	require.NoError(t, err)
   233  
   234  	// create token2
   235  	tokenID2, err := createFungibleToken(&env)
   236  	require.NoError(t, err)
   237  
   238  	// account create with unlimited max auto associations
   239  	accountID1, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   240  		tx.SetMaxAutomaticTokenAssociations(-1)
   241  	})
   242  	require.NoError(t, err)
   243  	// create account with 100 max auto associations
   244  	accountID2, newKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   245  		tx.SetMaxAutomaticTokenAssociations(100)
   246  	})
   247  	require.NoError(t, err)
   248  
   249  	// update the account with unlimited max auto associations
   250  	accountUpdateFrozen, err := NewAccountUpdateTransaction().
   251  		SetMaxAutomaticTokenAssociations(-1).
   252  		SetAccountID(accountID2).
   253  		FreezeWith(env.Client)
   254  	require.NoError(t, err)
   255  
   256  	accountUpdate, err := accountUpdateFrozen.Sign(newKey).Execute(env.Client)
   257  	require.NoError(t, err)
   258  
   259  	_, err = accountUpdate.SetValidateStatus(true).GetReceipt(env.Client)
   260  	require.NoError(t, err)
   261  
   262  	// transfer to both receivers some token1 tokens
   263  	tokenTransferTransaction, err := NewTransferTransaction().
   264  		AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -1000).
   265  		AddTokenTransfer(tokenID1, accountID1, 1000).
   266  		AddTokenTransfer(tokenID1, env.Client.GetOperatorAccountID(), -1000).
   267  		AddTokenTransfer(tokenID1, accountID2, 1000).
   268  		Execute(env.Client)
   269  	require.NoError(t, err)
   270  
   271  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   272  	require.NoError(t, err)
   273  
   274  	// transfer to both receivers some token2 tokens
   275  	tokenTransferTransaction, err = NewTransferTransaction().
   276  		AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -1000).
   277  		AddTokenTransfer(tokenID2, accountID1, 1000).
   278  		AddTokenTransfer(tokenID2, env.Client.GetOperatorAccountID(), -1000).
   279  		AddTokenTransfer(tokenID2, accountID2, 1000).
   280  		Execute(env.Client)
   281  	require.NoError(t, err)
   282  
   283  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   284  	require.NoError(t, err)
   285  
   286  	// verify the balance of the receivers is 1000
   287  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(accountID1).Execute(env.Client)
   288  	require.NoError(t, err)
   289  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID1))
   290  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID2))
   291  
   292  	tokenBalance, err = NewAccountBalanceQuery().SetAccountID(accountID2).Execute(env.Client)
   293  	require.NoError(t, err)
   294  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID1))
   295  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID2))
   296  }
   297  
   298  func TestUnlimitedMaxAutoAssociationsAllowsToTransferFungibleTokensWithDecimals(t *testing.T) {
   299  	t.Parallel()
   300  	env := NewIntegrationTestEnv(t)
   301  
   302  	// create token1
   303  	tokenID1, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   304  		transaction.SetDecimals(10)
   305  	})
   306  	require.NoError(t, err)
   307  
   308  	// create token2
   309  	tokenID2, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   310  		transaction.SetDecimals(10)
   311  	})
   312  	require.NoError(t, err)
   313  
   314  	// account create with unlimited max auto associations
   315  	accountID, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   316  		tx.SetMaxAutomaticTokenAssociations(-1)
   317  	})
   318  	require.NoError(t, err)
   319  
   320  	// transfer some token1 and token2 tokens
   321  	tokenTransferTransaction, err := NewTransferTransaction().
   322  		AddTokenTransferWithDecimals(tokenID1, env.Client.GetOperatorAccountID(), -1000, 10).
   323  		AddTokenTransferWithDecimals(tokenID1, accountID, 1000, 10).
   324  		AddTokenTransferWithDecimals(tokenID2, env.Client.GetOperatorAccountID(), -1000, 10).
   325  		AddTokenTransferWithDecimals(tokenID2, accountID, 1000, 10).
   326  		Execute(env.Client)
   327  	require.NoError(t, err)
   328  
   329  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   330  	require.NoError(t, err)
   331  
   332  	// verify the balance of the receiver is 1000
   333  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(accountID).Execute(env.Client)
   334  	require.NoError(t, err)
   335  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID1))
   336  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID2))
   337  }
   338  
   339  func TestUnlimitedMaxAutoAssociationsAllowsToTransferFromFungibleTokens(t *testing.T) {
   340  	t.Parallel()
   341  	env := NewIntegrationTestEnv(t)
   342  
   343  	// create spender account which will be approved to spend
   344  	spender, spenderKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   345  		tx.SetMaxAutomaticTokenAssociations(10)
   346  	})
   347  	require.NoError(t, err)
   348  
   349  	// create token1
   350  	tokenID1, err := createFungibleToken(&env)
   351  	require.NoError(t, err)
   352  
   353  	// create token2
   354  	tokenID2, err := createFungibleToken(&env)
   355  	require.NoError(t, err)
   356  
   357  	// account create with unlimited max auto associations
   358  	accountID, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   359  		tx.SetMaxAutomaticTokenAssociations(-1)
   360  	})
   361  	require.NoError(t, err)
   362  
   363  	// approve the spender
   364  	approve, err := NewAccountAllowanceApproveTransaction().
   365  		AddTokenApproval(tokenID1, spender, 2000).
   366  		AddTokenApproval(tokenID2, spender, 2000).
   367  		Execute(env.Client)
   368  	require.NoError(t, err)
   369  
   370  	_, err = approve.SetValidateStatus(true).GetReceipt(env.Client)
   371  	require.NoError(t, err)
   372  
   373  	// transferFrom some token1 and token2 tokens
   374  	env.Client.SetOperator(spender, spenderKey)
   375  	tokenTransferTransactionFrozen, err := NewTransferTransaction().
   376  		AddApprovedTokenTransfer(tokenID1, env.OperatorID, -1000, true).
   377  		AddTokenTransfer(tokenID1, accountID, 1000).
   378  		AddApprovedTokenTransfer(tokenID2, env.OperatorID, -1000, true).
   379  		AddTokenTransfer(tokenID2, accountID, 1000).
   380  		FreezeWith(env.Client)
   381  	require.NoError(t, err)
   382  
   383  	tokenTransferTransaction, err := tokenTransferTransactionFrozen.Sign(spenderKey).Execute(env.Client)
   384  	require.NoError(t, err)
   385  
   386  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   387  	require.NoError(t, err)
   388  
   389  	env.Client.SetOperator(env.OperatorID, env.OperatorKey)
   390  
   391  	// verify the balance of the receiver is 1000
   392  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(accountID).Execute(env.Client)
   393  	require.NoError(t, err)
   394  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID1))
   395  	assert.Equal(t, uint64(1000), tokenBalance.Tokens.Get(tokenID2))
   396  }
   397  
   398  func TestUnlimitedMaxAutoAssociationsAllowsToTransferNFTs(t *testing.T) {
   399  	t.Parallel()
   400  	env := NewIntegrationTestEnv(t)
   401  
   402  	// create 2 NFT collections and mint 10 NFTs for each collection
   403  	nftID1, err := createNft(&env)
   404  	require.NoError(t, err)
   405  
   406  	mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client)
   407  	receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client)
   408  	require.NoError(t, err)
   409  
   410  	nftID2, err := createNft(&env)
   411  	require.NoError(t, err)
   412  
   413  	mint, err = NewTokenMintTransaction().SetTokenID(nftID2).SetMetadatas(mintMetadata).Execute(env.Client)
   414  	receipt, err = mint.SetValidateStatus(true).GetReceipt(env.Client)
   415  	require.NoError(t, err)
   416  
   417  	serials := receipt.SerialNumbers
   418  
   419  	// account create with unlimited max auto associations
   420  	accountID1, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   421  		tx.SetMaxAutomaticTokenAssociations(-1)
   422  	})
   423  	require.NoError(t, err)
   424  
   425  	accountID2, newKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   426  		tx.SetMaxAutomaticTokenAssociations(100)
   427  	})
   428  	require.NoError(t, err)
   429  
   430  	// account update with unlimited max auto associations
   431  	accountUpdateFrozen, err := NewAccountUpdateTransaction().
   432  		SetMaxAutomaticTokenAssociations(-1).
   433  		SetAccountID(accountID2).
   434  		FreezeWith(env.Client)
   435  	require.NoError(t, err)
   436  
   437  	accountUpdate, err := accountUpdateFrozen.Sign(newKey).Execute(env.Client)
   438  	require.NoError(t, err)
   439  
   440  	_, err = accountUpdate.SetValidateStatus(true).GetReceipt(env.Client)
   441  	require.NoError(t, err)
   442  
   443  	// transfer nft1 to both receivers, 2 for each
   444  	tokenTransferTransaction, err := NewTransferTransaction().
   445  		AddNftTransfer(nftID1.Nft(serials[0]), env.OperatorID, accountID1).
   446  		AddNftTransfer(nftID1.Nft(serials[1]), env.OperatorID, accountID1).
   447  		AddNftTransfer(nftID1.Nft(serials[2]), env.OperatorID, accountID2).
   448  		AddNftTransfer(nftID1.Nft(serials[3]), env.OperatorID, accountID2).
   449  		Execute(env.Client)
   450  
   451  	require.NoError(t, err)
   452  
   453  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   454  	require.NoError(t, err)
   455  
   456  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   457  	require.NoError(t, err)
   458  
   459  	// transfer nft2 to both receivers, 2 for each
   460  	tokenTransferTransaction, err = NewTransferTransaction().
   461  		AddNftTransfer(nftID2.Nft(serials[0]), env.OperatorID, accountID1).
   462  		AddNftTransfer(nftID2.Nft(serials[1]), env.OperatorID, accountID1).
   463  		AddNftTransfer(nftID2.Nft(serials[2]), env.OperatorID, accountID2).
   464  		AddNftTransfer(nftID2.Nft(serials[3]), env.OperatorID, accountID2).
   465  		Execute(env.Client)
   466  
   467  	require.NoError(t, err)
   468  
   469  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   470  	require.NoError(t, err)
   471  
   472  	// verify the balance of the receivers is 2
   473  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(accountID1).Execute(env.Client)
   474  	require.NoError(t, err)
   475  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID1))
   476  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID2))
   477  
   478  	tokenBalance, err = NewAccountBalanceQuery().SetAccountID(accountID2).Execute(env.Client)
   479  	require.NoError(t, err)
   480  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID1))
   481  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID2))
   482  }
   483  
   484  func TestUnlimitedMaxAutoAssociationsAllowsToTransferFromNFTs(t *testing.T) {
   485  	t.Parallel()
   486  	env := NewIntegrationTestEnv(t)
   487  
   488  	// create spender account which will be approved to spend
   489  	spender, spenderKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   490  		tx.SetMaxAutomaticTokenAssociations(10)
   491  	})
   492  	require.NoError(t, err)
   493  
   494  	// create 2 NFT collections and mint 10 NFTs for each collection
   495  	nftID1, err := createNft(&env)
   496  	require.NoError(t, err)
   497  
   498  	mint, err := NewTokenMintTransaction().SetTokenID(nftID1).SetMetadatas(mintMetadata).Execute(env.Client)
   499  	receipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client)
   500  	require.NoError(t, err)
   501  
   502  	nftID2, err := createNft(&env)
   503  	require.NoError(t, err)
   504  
   505  	mint, err = NewTokenMintTransaction().SetTokenID(nftID2).SetMetadatas(mintMetadata).Execute(env.Client)
   506  	receipt, err = mint.SetValidateStatus(true).GetReceipt(env.Client)
   507  	require.NoError(t, err)
   508  
   509  	serials := receipt.SerialNumbers
   510  
   511  	// account create with unlimited max auto associations
   512  	accountID, _, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   513  		tx.SetMaxAutomaticTokenAssociations(-1)
   514  	})
   515  	require.NoError(t, err)
   516  
   517  	// approve the spender
   518  	approve, err := NewAccountAllowanceApproveTransaction().
   519  		AddAllTokenNftApproval(nftID1, spender).
   520  		AddAllTokenNftApproval(nftID2, spender).
   521  		Execute(env.Client)
   522  	require.NoError(t, err)
   523  
   524  	_, err = approve.SetValidateStatus(true).GetReceipt(env.Client)
   525  	require.NoError(t, err)
   526  
   527  	// transferFrom some nft1 nfts
   528  	env.Client.SetOperator(spender, spenderKey)
   529  	tokenTransferTransactionFrozen, err := NewTransferTransaction().
   530  		AddApprovedNftTransfer(nftID1.Nft(serials[0]), env.OperatorID, accountID, true).
   531  		AddApprovedNftTransfer(nftID1.Nft(serials[1]), env.OperatorID, accountID, true).
   532  		AddApprovedNftTransfer(nftID2.Nft(serials[0]), env.OperatorID, accountID, true).
   533  		AddApprovedNftTransfer(nftID2.Nft(serials[1]), env.OperatorID, accountID, true).
   534  		FreezeWith(env.Client)
   535  	require.NoError(t, err)
   536  
   537  	tokenTransferTransaction, err := tokenTransferTransactionFrozen.Sign(spenderKey).Execute(env.Client)
   538  
   539  	_, err = tokenTransferTransaction.SetValidateStatus(true).GetReceipt(env.Client)
   540  	require.NoError(t, err)
   541  
   542  	env.Client.SetOperator(env.OperatorID, env.OperatorKey)
   543  
   544  	// verify the balance of the receiver is 2
   545  	tokenBalance, err := NewAccountBalanceQuery().SetAccountID(accountID).Execute(env.Client)
   546  	require.NoError(t, err)
   547  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID1))
   548  	assert.Equal(t, uint64(2), tokenBalance.Tokens.Get(nftID2))
   549  }
   550  
   551  func TestUnlimitedMaxAutoAssociationsFailsWithInvalid(t *testing.T) {
   552  	t.Parallel()
   553  	env := NewIntegrationTestEnv(t)
   554  
   555  	// account create with -2 and with -1000 max auto associations
   556  	newKey, err := PrivateKeyGenerateEd25519()
   557  	require.NoError(t, err)
   558  
   559  	_, err = NewAccountCreateTransaction().
   560  		SetKey(newKey).
   561  		SetNodeAccountIDs(env.NodeAccountIDs).
   562  		SetInitialBalance(NewHbar(0)).
   563  		SetMaxAutomaticTokenAssociations(-2).
   564  		Execute(env.Client)
   565  	require.ErrorContains(t, err, "INVALID_MAX_AUTO_ASSOCIATIONS")
   566  
   567  	_, err = NewAccountCreateTransaction().
   568  		SetKey(newKey).
   569  		SetNodeAccountIDs(env.NodeAccountIDs).
   570  		SetInitialBalance(NewHbar(0)).
   571  		SetMaxAutomaticTokenAssociations(-1000).
   572  		Execute(env.Client)
   573  	require.ErrorContains(t, err, "INVALID_MAX_AUTO_ASSOCIATIONS")
   574  
   575  	// create account with 100 max auto associations
   576  	accountID, newKey, err := createAccount(&env, func(tx *AccountCreateTransaction) {
   577  		tx.SetMaxAutomaticTokenAssociations(100)
   578  	})
   579  	require.NoError(t, err)
   580  
   581  	// account update with -2 max auto associations - should fail
   582  	accountUpdateFrozen, err := NewAccountUpdateTransaction().
   583  		SetMaxAutomaticTokenAssociations(-2).
   584  		SetAccountID(accountID).
   585  		FreezeWith(env.Client)
   586  	require.NoError(t, err)
   587  
   588  	tx, err := accountUpdateFrozen.Sign(newKey).Execute(env.Client)
   589  	require.NoError(t, err)
   590  
   591  	_, err = tx.SetValidateStatus(true).GetReceipt(env.Client)
   592  	require.ErrorContains(t, err, "INVALID_MAX_AUTO_ASSOCIATIONS")
   593  
   594  	// account update with -1000 max auto associations - should fail
   595  	accountUpdateFrozen, err = NewAccountUpdateTransaction().
   596  		SetMaxAutomaticTokenAssociations(-1000).
   597  		SetAccountID(accountID).
   598  		FreezeWith(env.Client)
   599  	require.NoError(t, err)
   600  
   601  	tx, err = accountUpdateFrozen.Sign(newKey).Execute(env.Client)
   602  	require.NoError(t, err)
   603  
   604  	_, err = tx.SetValidateStatus(true).GetReceipt(env.Client)
   605  	require.ErrorContains(t, err, "INVALID_MAX_AUTO_ASSOCIATIONS")
   606  }