github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_info_query_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  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestIntegrationAccountInfoQueryCanExecute(t *testing.T) {
    35  	t.Parallel()
    36  	env := NewIntegrationTestEnv(t)
    37  
    38  	newKey, err := PrivateKeyGenerateEd25519()
    39  	require.NoError(t, err)
    40  
    41  	newBalance := NewHbar(2)
    42  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    43  
    44  	resp, err := NewAccountCreateTransaction().
    45  		SetKey(newKey.PublicKey()).
    46  		SetInitialBalance(newBalance).
    47  		Execute(env.Client)
    48  	require.NoError(t, err)
    49  
    50  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    51  	require.NoError(t, err)
    52  
    53  	accountID := *receipt.AccountID
    54  	require.NoError(t, err)
    55  
    56  	info, err := NewAccountInfoQuery().
    57  		SetAccountID(accountID).
    58  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    59  		SetMaxQueryPayment(NewHbar(1)).
    60  		SetQueryPayment(HbarFromTinybar(25)).
    61  		Execute(env.Client)
    62  	require.NoError(t, err)
    63  
    64  	assert.Equal(t, accountID, info.AccountID)
    65  	assert.Equal(t, false, info.IsDeleted)
    66  	assert.Equal(t, newKey.PublicKey(), info.Key)
    67  	assert.Equal(t, newBalance.tinybar, info.Balance.tinybar)
    68  
    69  	tx, err := NewAccountDeleteTransaction().
    70  		SetAccountID(accountID).
    71  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    72  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    73  		SetTransactionID(TransactionIDGenerate(accountID)).
    74  		FreezeWith(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	resp, err = tx.
    78  		Sign(newKey).
    79  		Execute(env.Client)
    80  	require.NoError(t, err)
    81  
    82  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    83  	require.NoError(t, err)
    84  
    85  	//err = CloseIntegrationTestEnv(env, nil)
    86  	//require.NoError(t, err)
    87  }
    88  
    89  func TestIntegrationAccountInfoQueryGetCost(t *testing.T) {
    90  	t.Parallel()
    91  	env := NewIntegrationTestEnv(t)
    92  
    93  	newKey, err := PrivateKeyGenerateEd25519()
    94  	require.NoError(t, err)
    95  
    96  	newBalance := NewHbar(2)
    97  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    98  
    99  	resp, err := NewAccountCreateTransaction().
   100  		SetKey(newKey.PublicKey()).
   101  		SetInitialBalance(newBalance).
   102  		SetNodeAccountIDs(env.NodeAccountIDs).
   103  		Execute(env.Client)
   104  	require.NoError(t, err)
   105  
   106  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   107  	require.NoError(t, err)
   108  
   109  	accountID := *receipt.AccountID
   110  	require.NoError(t, err)
   111  
   112  	accountInfo := NewAccountInfoQuery().
   113  		SetAccountID(accountID).
   114  		SetMaxQueryPayment(NewHbar(1)).
   115  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   116  
   117  	cost, err := accountInfo.GetCost(env.Client)
   118  	require.NoError(t, err)
   119  
   120  	info, err := accountInfo.SetQueryPayment(cost).Execute(env.Client)
   121  	require.NoError(t, err)
   122  
   123  	assert.Equal(t, accountID, info.AccountID)
   124  	assert.Equal(t, false, info.IsDeleted)
   125  	assert.Equal(t, newKey.PublicKey(), info.Key)
   126  	assert.Equal(t, newBalance.tinybar, info.Balance.tinybar)
   127  
   128  	tx, err := NewAccountDeleteTransaction().
   129  		SetAccountID(accountID).
   130  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   131  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   132  		SetTransactionID(TransactionIDGenerate(accountID)).
   133  		FreezeWith(env.Client)
   134  	require.NoError(t, err)
   135  
   136  	resp, err = tx.
   137  		Sign(newKey).
   138  		Execute(env.Client)
   139  	require.NoError(t, err)
   140  
   141  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   142  	require.NoError(t, err)
   143  
   144  	err = CloseIntegrationTestEnv(env, nil)
   145  	require.NoError(t, err)
   146  }
   147  
   148  func TestIntegrationAccountInfoQueryInsufficientFee(t *testing.T) {
   149  	t.Parallel()
   150  	env := NewIntegrationTestEnv(t)
   151  
   152  	newKey, err := PrivateKeyGenerateEd25519()
   153  	require.NoError(t, err)
   154  
   155  	newBalance := NewHbar(2)
   156  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   157  
   158  	resp, err := NewAccountCreateTransaction().
   159  		SetKey(newKey.PublicKey()).
   160  		SetNodeAccountIDs(env.NodeAccountIDs).
   161  		SetInitialBalance(newBalance).
   162  		Execute(env.Client)
   163  	require.NoError(t, err)
   164  
   165  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   166  	require.NoError(t, err)
   167  
   168  	accountID := *receipt.AccountID
   169  	require.NoError(t, err)
   170  
   171  	accountInfo := NewAccountInfoQuery().
   172  		SetAccountID(accountID).
   173  		SetMaxQueryPayment(NewHbar(1)).
   174  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   175  
   176  	_, err = accountInfo.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client)
   177  	assert.Error(t, err)
   178  	if err != nil {
   179  		assert.Equal(t, "exceptional precheck status INSUFFICIENT_TX_FEE", err.Error())
   180  	}
   181  
   182  	tx, err := NewAccountDeleteTransaction().
   183  		SetAccountID(accountID).
   184  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   185  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   186  		SetTransactionID(TransactionIDGenerate(accountID)).
   187  		FreezeWith(env.Client)
   188  	require.NoError(t, err)
   189  
   190  	resp, err = tx.
   191  		Sign(newKey).
   192  		Execute(env.Client)
   193  	require.NoError(t, err)
   194  
   195  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   196  	require.NoError(t, err)
   197  
   198  	err = CloseIntegrationTestEnv(env, nil)
   199  	require.NoError(t, err)
   200  }
   201  
   202  func TestIntegrationAccountInfoQuerySetBigMaxPayment(t *testing.T) {
   203  	t.Parallel()
   204  	env := NewIntegrationTestEnv(t)
   205  
   206  	newKey, err := PrivateKeyGenerateEd25519()
   207  	require.NoError(t, err)
   208  
   209  	newBalance := NewHbar(2)
   210  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   211  
   212  	resp, err := NewAccountCreateTransaction().
   213  		SetKey(newKey.PublicKey()).
   214  		SetNodeAccountIDs(env.NodeAccountIDs).
   215  		SetInitialBalance(newBalance).
   216  		Execute(env.Client)
   217  	require.NoError(t, err)
   218  
   219  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   220  	require.NoError(t, err)
   221  
   222  	accountID := *receipt.AccountID
   223  	require.NoError(t, err)
   224  
   225  	accountInfo := NewAccountInfoQuery().
   226  		SetAccountID(accountID).
   227  		SetMaxQueryPayment(NewHbar(1000000)).
   228  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   229  
   230  	_, err = accountInfo.GetCost(env.Client)
   231  	require.NoError(t, err)
   232  
   233  	info, err := accountInfo.SetQueryPayment(NewHbar(1)).Execute(env.Client)
   234  	require.NoError(t, err)
   235  
   236  	assert.Equal(t, accountID, info.AccountID)
   237  	assert.Equal(t, false, info.IsDeleted)
   238  	assert.Equal(t, newKey.PublicKey(), info.Key)
   239  	assert.Equal(t, newBalance.tinybar, info.Balance.tinybar)
   240  
   241  	tx, err := NewAccountDeleteTransaction().
   242  		SetAccountID(accountID).
   243  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   244  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   245  		SetTransactionID(TransactionIDGenerate(accountID)).
   246  		FreezeWith(env.Client)
   247  	require.NoError(t, err)
   248  
   249  	resp, err = tx.
   250  		Sign(newKey).
   251  		Execute(env.Client)
   252  	require.NoError(t, err)
   253  
   254  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   255  	require.NoError(t, err)
   256  
   257  	err = CloseIntegrationTestEnv(env, nil)
   258  	require.NoError(t, err)
   259  }
   260  
   261  func TestIntegrationAccountInfoQuerySetSmallMaxPayment(t *testing.T) {
   262  	t.Parallel()
   263  	env := NewIntegrationTestEnv(t)
   264  
   265  	newKey, err := PrivateKeyGenerateEd25519()
   266  	require.NoError(t, err)
   267  
   268  	newBalance := NewHbar(2)
   269  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   270  
   271  	resp, err := NewAccountCreateTransaction().
   272  		SetKey(newKey.PublicKey()).
   273  		SetNodeAccountIDs(env.NodeAccountIDs).
   274  		SetInitialBalance(newBalance).
   275  		Execute(env.Client)
   276  	require.NoError(t, err)
   277  
   278  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   279  	require.NoError(t, err)
   280  
   281  	accountID := *receipt.AccountID
   282  	require.NoError(t, err)
   283  
   284  	accountInfo := NewAccountInfoQuery().
   285  		SetAccountID(accountID).
   286  		SetMaxQueryPayment(HbarFromTinybar(1)).
   287  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   288  
   289  	cost, err := accountInfo.GetCost(env.Client)
   290  	require.NoError(t, err)
   291  
   292  	_, err = accountInfo.Execute(env.Client)
   293  	assert.Error(t, err)
   294  	if err != nil {
   295  		assert.Equal(t, "cost of AccountInfoQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tℏ", err.Error())
   296  	}
   297  
   298  	tx, err := NewAccountDeleteTransaction().
   299  		SetAccountID(accountID).
   300  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   301  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   302  		SetTransactionID(TransactionIDGenerate(accountID)).
   303  		FreezeWith(env.Client)
   304  	require.NoError(t, err)
   305  
   306  	resp, err = tx.
   307  		Sign(newKey).
   308  		Execute(env.Client)
   309  	require.NoError(t, err)
   310  
   311  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   312  	require.NoError(t, err)
   313  
   314  	err = CloseIntegrationTestEnv(env, nil)
   315  	require.NoError(t, err)
   316  }
   317  
   318  func TestIntegrationAccountInfoQueryNoAccountID(t *testing.T) {
   319  	t.Parallel()
   320  	env := NewIntegrationTestEnv(t)
   321  
   322  	_, err := NewAccountInfoQuery().
   323  		SetNodeAccountIDs(env.NodeAccountIDs).
   324  		Execute(env.Client)
   325  	assert.Error(t, err)
   326  	if err != nil {
   327  		assert.Equal(t, "exceptional precheck status INVALID_ACCOUNT_ID", err.Error())
   328  	}
   329  
   330  	err = CloseIntegrationTestEnv(env, nil)
   331  	require.NoError(t, err)
   332  }
   333  func TestIntegrationAccountInfoQueryTokenRelationshipInfo(t *testing.T) {
   334  	t.Parallel()
   335  	env := NewIntegrationTestEnv(t)
   336  
   337  	newKey, err := PrivateKeyGenerateEd25519()
   338  	require.NoError(t, err)
   339  
   340  	newBalance := NewHbar(2)
   341  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   342  
   343  	resp, err := NewAccountCreateTransaction().
   344  		SetKey(newKey.PublicKey()).
   345  		SetNodeAccountIDs(env.NodeAccountIDs).
   346  		SetMaxAutomaticTokenAssociations(10).
   347  		SetInitialBalance(newBalance).
   348  		Execute(env.Client)
   349  	require.NoError(t, err)
   350  
   351  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   352  	require.NoError(t, err)
   353  
   354  	accountID := *receipt.AccountID
   355  	require.NoError(t, err)
   356  
   357  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   358  		transaction.
   359  			SetFreezeKey(newKey).
   360  			SetWipeKey(newKey).
   361  			SetKycKey(newKey).
   362  			SetSupplyKey(newKey).
   363  			SetMetadataKey(newKey).
   364  			SetFreezeDefault(true)
   365  	})
   366  
   367  	require.NoError(t, err)
   368  
   369  	associateTxn, err := NewTokenAssociateTransaction().
   370  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   371  		SetAccountID(accountID).
   372  		SetTokenIDs(tokenID).
   373  		FreezeWith(env.Client)
   374  	require.NoError(t, err)
   375  
   376  	resp, err = associateTxn.
   377  		Sign(newKey).
   378  		Execute(env.Client)
   379  	require.NoError(t, err)
   380  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   381  	require.NoError(t, err)
   382  
   383  	info, err := NewAccountInfoQuery().
   384  		SetNodeAccountIDs(env.NodeAccountIDs).
   385  		SetAccountID(accountID).
   386  		Execute(env.Client)
   387  	assert.NoError(t, err)
   388  
   389  	assert.Equal(t, accountID, info.AccountID)
   390  	assert.Equal(t, 1, len(info.TokenRelationships))
   391  	assert.Equal(t, true, *info.TokenRelationships[0].FreezeStatus)
   392  	assert.Equal(t, false, *info.TokenRelationships[0].KycStatus)
   393  	assert.Equal(t, uint64(0), info.TokenRelationships[0].Balance)
   394  	assert.Equal(t, "F", info.TokenRelationships[0].Symbol)
   395  	assert.Equal(t, tokenID, info.TokenRelationships[0].TokenID)
   396  	assert.Equal(t, false, info.TokenRelationships[0].AutomaticAssociation)
   397  
   398  	unfreezeTxn, err := NewTokenUnfreezeTransaction().
   399  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   400  		SetAccountID(accountID).
   401  		SetTokenID(tokenID).
   402  		FreezeWith(env.Client)
   403  
   404  	require.NoError(t, err)
   405  	resp, err = unfreezeTxn.
   406  		Sign(newKey).
   407  		Execute(env.Client)
   408  	require.NoError(t, err)
   409  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   410  	require.NoError(t, err)
   411  
   412  	kycUpdateTxn, err := NewTokenGrantKycTransaction().
   413  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   414  		SetAccountID(accountID).
   415  		SetTokenID(tokenID).
   416  		FreezeWith(env.Client)
   417  
   418  	require.NoError(t, err)
   419  	resp, err = kycUpdateTxn.
   420  		Sign(newKey).
   421  		Execute(env.Client)
   422  	require.NoError(t, err)
   423  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   424  	require.NoError(t, err)
   425  
   426  	info, err = NewAccountInfoQuery().
   427  		SetNodeAccountIDs(env.NodeAccountIDs).
   428  		SetAccountID(accountID).
   429  		Execute(env.Client)
   430  	assert.NoError(t, err)
   431  
   432  	assert.Equal(t, accountID, info.AccountID)
   433  	assert.Equal(t, 1, len(info.TokenRelationships))
   434  	assert.Equal(t, false, *info.TokenRelationships[0].FreezeStatus)
   435  	assert.Equal(t, true, *info.TokenRelationships[0].KycStatus)
   436  	assert.Equal(t, uint64(0), info.TokenRelationships[0].Balance)
   437  	assert.Equal(t, "F", info.TokenRelationships[0].Symbol)
   438  	assert.Equal(t, tokenID, info.TokenRelationships[0].TokenID)
   439  	assert.Equal(t, false, info.TokenRelationships[0].AutomaticAssociation)
   440  }
   441  
   442  func TestIntegrationAccountInfoQueryTokenRelationshipsLength(t *testing.T) {
   443  	t.Parallel()
   444  	env := NewIntegrationTestEnv(t)
   445  
   446  	newKey, err := PrivateKeyGenerateEd25519()
   447  	require.NoError(t, err)
   448  
   449  	newBalance := NewHbar(2)
   450  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   451  
   452  	resp, err := NewAccountCreateTransaction().
   453  		SetKey(newKey.PublicKey()).
   454  		SetNodeAccountIDs(env.NodeAccountIDs).
   455  		SetMaxAutomaticTokenAssociations(10).
   456  		SetInitialBalance(newBalance).
   457  		Execute(env.Client)
   458  	require.NoError(t, err)
   459  
   460  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   461  	require.NoError(t, err)
   462  
   463  	accountID := *receipt.AccountID
   464  	require.NoError(t, err)
   465  
   466  	firstTokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   467  		transaction.
   468  			SetFreezeKey(newKey).
   469  			SetWipeKey(newKey).
   470  			SetKycKey(newKey).
   471  			SetSupplyKey(newKey).
   472  			SetMetadataKey(newKey).
   473  			SetDecimals(3)
   474  	})
   475  	require.NoError(t, err)
   476  
   477  	associateTxn, err := NewTokenAssociateTransaction().
   478  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   479  		SetAccountID(accountID).
   480  		SetTokenIDs(firstTokenID).
   481  		FreezeWith(env.Client)
   482  	require.NoError(t, err)
   483  
   484  	resp, err = associateTxn.
   485  		Sign(newKey).
   486  		Execute(env.Client)
   487  	require.NoError(t, err)
   488  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   489  	require.NoError(t, err)
   490  
   491  	info, err := NewAccountInfoQuery().
   492  		SetNodeAccountIDs(env.NodeAccountIDs).
   493  		SetAccountID(accountID).
   494  		Execute(env.Client)
   495  	assert.NoError(t, err)
   496  
   497  	assert.Equal(t, 1, len(info.TokenRelationships))
   498  	assert.Equal(t, uint32(3), info.TokenRelationships[0].Decimals)
   499  
   500  	secondTokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   501  		transaction.
   502  			SetFreezeKey(newKey).
   503  			SetWipeKey(newKey).
   504  			SetKycKey(newKey).
   505  			SetSupplyKey(newKey).
   506  			SetMetadataKey(newKey)
   507  	})
   508  	require.NoError(t, err)
   509  
   510  	associateTxn, err = NewTokenAssociateTransaction().
   511  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   512  		SetAccountID(accountID).
   513  		SetTokenIDs(secondTokenID).
   514  		FreezeWith(env.Client)
   515  	require.NoError(t, err)
   516  
   517  	resp, err = associateTxn.
   518  		Sign(newKey).
   519  		Execute(env.Client)
   520  	require.NoError(t, err)
   521  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   522  	require.NoError(t, err)
   523  
   524  	info, err = NewAccountInfoQuery().
   525  		SetNodeAccountIDs(env.NodeAccountIDs).
   526  		SetAccountID(accountID).
   527  		Execute(env.Client)
   528  	assert.NoError(t, err)
   529  
   530  	assert.Equal(t, 2, len(info.TokenRelationships))
   531  	// the new token should be first in the list
   532  	assert.Equal(t, uint32(18), info.TokenRelationships[0].Decimals)
   533  
   534  	dissociateTxn, err := NewTokenDissociateTransaction().
   535  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   536  		SetAccountID(accountID).
   537  		SetTokenIDs(secondTokenID, firstTokenID).
   538  		FreezeWith(env.Client)
   539  	require.NoError(t, err)
   540  
   541  	resp, err = dissociateTxn.
   542  		Sign(newKey).
   543  		Execute(env.Client)
   544  	require.NoError(t, err)
   545  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   546  	require.NoError(t, err)
   547  
   548  	info, err = NewAccountInfoQuery().
   549  		SetNodeAccountIDs(env.NodeAccountIDs).
   550  		SetAccountID(accountID).
   551  		Execute(env.Client)
   552  	assert.NoError(t, err)
   553  
   554  	assert.Equal(t, accountID, info.AccountID)
   555  	assert.Equal(t, 0, len(info.TokenRelationships))
   556  }