github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_balance_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  	"strings"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestIntegrationAccountBalanceQueryCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	_, err := NewAccountBalanceQuery().
    40  		SetAccountID(env.OriginalOperatorID).
    41  		SetNodeAccountIDs(env.NodeAccountIDs).
    42  		Execute(env.Client)
    43  	require.NoError(t, err)
    44  
    45  	_, err = NewAccountBalanceQuery().
    46  		SetAccountID(env.OperatorID).
    47  		SetNodeAccountIDs(env.NodeAccountIDs).
    48  		Execute(env.Client)
    49  	require.NoError(t, err)
    50  
    51  	err = CloseIntegrationTestEnv(env, nil)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestIntegrationAccountBalanceQueryCanGetTokenBalance(t *testing.T) {
    56  	t.Parallel()
    57  	env := NewIntegrationTestEnv(t)
    58  
    59  	tokenID, err := createFungibleToken(&env)
    60  	require.NoError(t, err)
    61  
    62  	balance, err := NewAccountBalanceQuery().
    63  		SetNodeAccountIDs(env.NodeAccountIDs).
    64  		SetAccountID(env.Client.GetOperatorAccountID()).
    65  		Execute(env.Client)
    66  	require.NoError(t, err)
    67  
    68  	assert.Equal(t, balance, balance)
    69  	// TODO: assert.Equal(t, uint64(1000000), balance.Tokens.Get(*tokenID))
    70  	// TODO: assert.Equal(t, uint64(3), balance.TokenDecimals.Get(*tokenID))
    71  	err = CloseIntegrationTestEnv(env, &tokenID)
    72  	require.NoError(t, err)
    73  }
    74  
    75  func TestIntegrationAccountBalanceQueryGetCost(t *testing.T) {
    76  	t.Parallel()
    77  	env := NewIntegrationTestEnv(t)
    78  
    79  	balance := NewAccountBalanceQuery().
    80  		SetNodeAccountIDs(env.NodeAccountIDs).
    81  		SetAccountID(env.Client.GetOperatorAccountID())
    82  
    83  	cost, err := balance.GetCost(env.Client)
    84  	require.NoError(t, err)
    85  
    86  	_, err = balance.SetMaxQueryPayment(cost).
    87  		Execute(env.Client)
    88  	require.NoError(t, err)
    89  
    90  	err = CloseIntegrationTestEnv(env, nil)
    91  	require.NoError(t, err)
    92  }
    93  
    94  func TestIntegrationAccountBalanceQuerySetBigMaxPayment(t *testing.T) {
    95  	t.Parallel()
    96  	env := NewIntegrationTestEnv(t)
    97  
    98  	balance := NewAccountBalanceQuery().
    99  		SetMaxQueryPayment(NewHbar(10000)).
   100  		SetNodeAccountIDs(env.NodeAccountIDs).
   101  		SetAccountID(env.Client.GetOperatorAccountID())
   102  
   103  	cost, err := balance.GetCost(env.Client)
   104  	require.NoError(t, err)
   105  
   106  	_, err = balance.SetQueryPayment(cost).Execute(env.Client)
   107  	require.NoError(t, err)
   108  
   109  	err = CloseIntegrationTestEnv(env, nil)
   110  	require.NoError(t, err)
   111  }
   112  
   113  func TestIntegrationAccountBalanceQuerySetSmallMaxPayment(t *testing.T) {
   114  	t.Parallel()
   115  	env := NewIntegrationTestEnv(t)
   116  
   117  	balance := NewAccountBalanceQuery().
   118  		SetMaxQueryPayment(HbarFromTinybar(1)).
   119  		SetNodeAccountIDs(env.NodeAccountIDs).
   120  		SetAccountID(env.Client.GetOperatorAccountID())
   121  
   122  	_, err := balance.GetCost(env.Client)
   123  	require.NoError(t, err)
   124  
   125  	_, err = balance.Execute(env.Client)
   126  	require.NoError(t, err)
   127  
   128  	err = CloseIntegrationTestEnv(env, nil)
   129  	require.NoError(t, err)
   130  }
   131  
   132  func TestIntegrationAccountBalanceQueryCanSetQueryPayment(t *testing.T) {
   133  	t.Parallel()
   134  	env := NewIntegrationTestEnv(t)
   135  
   136  	balance := NewAccountBalanceQuery().
   137  		SetMaxQueryPayment(NewHbar(10000)).
   138  		SetQueryPayment(NewHbar(0)).
   139  		SetNodeAccountIDs(env.NodeAccountIDs).
   140  		SetAccountID(env.OperatorID)
   141  
   142  	cost, err := balance.GetCost(env.Client)
   143  	require.NoError(t, err)
   144  
   145  	_, err = balance.SetQueryPayment(cost).Execute(env.Client)
   146  	require.NoError(t, err)
   147  
   148  	err = CloseIntegrationTestEnv(env, nil)
   149  	require.NoError(t, err)
   150  }
   151  
   152  func TestIntegrationAccountBalanceQueryCostCanSetPaymentOneTinybar(t *testing.T) {
   153  	t.Parallel()
   154  	env := NewIntegrationTestEnv(t)
   155  
   156  	balance := NewAccountBalanceQuery().
   157  		SetMaxQueryPayment(NewHbar(10000)).
   158  		SetQueryPayment(NewHbar(0)).
   159  		SetNodeAccountIDs(env.NodeAccountIDs).
   160  		SetAccountID(env.Client.GetOperatorAccountID())
   161  
   162  	_, err := balance.GetCost(env.Client)
   163  	require.NoError(t, err)
   164  
   165  	_, err = balance.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client)
   166  	require.NoError(t, err)
   167  
   168  	err = CloseIntegrationTestEnv(env, nil)
   169  	require.NoError(t, err)
   170  }
   171  
   172  func TestIntegrationAccountBalanceQueryNoAccountIDError(t *testing.T) {
   173  	t.Parallel()
   174  	env := NewIntegrationTestEnv(t)
   175  
   176  	_, err := NewAccountBalanceQuery().
   177  		SetNodeAccountIDs(env.NodeAccountIDs).
   178  		Execute(env.Client)
   179  	assert.Error(t, err)
   180  	assert.True(t, err.Error() == "exceptional precheck status INVALID_ACCOUNT_ID")
   181  
   182  	err = CloseIntegrationTestEnv(env, nil)
   183  	require.NoError(t, err)
   184  }
   185  func TestIntegrationAccountBalanceQueryWorksWithHollowAccountAlias(t *testing.T) {
   186  	t.Parallel()
   187  	env := NewIntegrationTestEnv(t)
   188  
   189  	privateKey, err := PrivateKeyGenerateEcdsa()
   190  	require.NoError(t, err)
   191  
   192  	// Extract the ECDSA public key public key
   193  	publicKey := privateKey.PublicKey()
   194  	// Extract the Ethereum public address
   195  	aliasAccountId := *publicKey.ToAccountID(0, 0)
   196  	evmAddress := publicKey.ToEvmAddress()
   197  
   198  	evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
   199  	require.NoError(t, err)
   200  
   201  	// Transfer tokens using the `TransferTransaction` to the Etherum Account Address
   202  	tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(4)).
   203  		AddHbarTransfer(env.OperatorID, NewHbar(-4)).Execute(env.Client)
   204  	require.NoError(t, err)
   205  
   206  	// Get the child receipt or child record to return the Hedera Account ID for the new account that was created
   207  	_, err = tx.SetIncludeChildren(true).SetValidateStatus(true).GetReceipt(env.Client)
   208  	require.NoError(t, err)
   209  
   210  	_, err = NewAccountBalanceQuery().SetAccountID(aliasAccountId).Execute(env.Client)
   211  	require.NoError(t, err)
   212  
   213  	err = CloseIntegrationTestEnv(env, nil)
   214  	require.NoError(t, err)
   215  }
   216  
   217  func TestIntegrationAccountBalanceQueryCanConnectToMainnetTls(t *testing.T) {
   218  	t.Parallel()
   219  	client := ClientForMainnet()
   220  	client.SetTransportSecurity(true)
   221  
   222  	succeededOnce := false
   223  	for address, accountID := range client.GetNetwork() {
   224  
   225  		if !strings.HasSuffix(address, ":50212") {
   226  			t.Errorf("Expected entry key to end with ':50212', but got %s", address)
   227  		}
   228  
   229  		accountIDs := []AccountID{accountID}
   230  		_, err := NewAccountBalanceQuery().
   231  			SetNodeAccountIDs(accountIDs).
   232  			SetAccountID(accountID).
   233  			Execute(client)
   234  		if err == nil {
   235  			succeededOnce = true
   236  		}
   237  	}
   238  	assert.True(t, succeededOnce)
   239  }