github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/transfer_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/require"
    31  )
    32  
    33  func TestIntegrationTransferTransactionCanTransferHbar(t *testing.T) {
    34  	t.Parallel()
    35  	env := NewIntegrationTestEnv(t)
    36  
    37  	resp, err := NewTransferTransaction().
    38  		SetNodeAccountIDs(env.NodeAccountIDs).
    39  		AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-1)).
    40  		AddHbarTransfer(AccountID{Account: 3}, NewHbar(1)).
    41  		Execute(env.Client)
    42  	require.NoError(t, err)
    43  
    44  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    45  	require.NoError(t, err)
    46  
    47  	err = CloseIntegrationTestEnv(env, nil)
    48  	require.NoError(t, err)
    49  }
    50  
    51  func TestIntegrationTransferTransactionTransferHbarNothingSet(t *testing.T) {
    52  	t.Parallel()
    53  	env := NewIntegrationTestEnv(t)
    54  
    55  	resp, err := NewTransferTransaction().
    56  		SetNodeAccountIDs(env.NodeAccountIDs).
    57  		Execute(env.Client)
    58  	require.NoError(t, err)
    59  
    60  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    61  	require.NoError(t, err)
    62  
    63  	err = CloseIntegrationTestEnv(env, nil)
    64  	require.NoError(t, err)
    65  }
    66  
    67  func TestIntegrationTransferTransactionTransferHbarPositiveFlippedAmount(t *testing.T) {
    68  	t.Parallel()
    69  	env := NewIntegrationTestEnv(t)
    70  
    71  	newKey, err := PrivateKeyGenerateEd25519()
    72  	require.NoError(t, err)
    73  
    74  	newBalance := NewHbar(10)
    75  
    76  	resp, err := NewAccountCreateTransaction().
    77  		SetKey(newKey).
    78  		SetNodeAccountIDs(env.NodeAccountIDs).
    79  		SetInitialBalance(newBalance).
    80  		Execute(env.Client)
    81  	require.NoError(t, err)
    82  
    83  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    84  	require.NoError(t, err)
    85  
    86  	accountID := *receipt.AccountID
    87  
    88  	frozen, err := NewTransferTransaction().
    89  		SetNodeAccountIDs(env.NodeAccountIDs).
    90  		AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(10)).
    91  		AddHbarTransfer(accountID, NewHbar(-10)).
    92  		SetMaxTransactionFee(NewHbar(1)).
    93  		FreezeWith(env.Client)
    94  	require.NoError(t, err)
    95  
    96  	frozen.Sign(newKey)
    97  
    98  	resp, err = frozen.Execute(env.Client)
    99  	require.NoError(t, err)
   100  
   101  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   102  	require.NoError(t, err)
   103  
   104  	err = CloseIntegrationTestEnv(env, nil)
   105  	require.NoError(t, err)
   106  }
   107  
   108  func DisabledTestIntegrationTransferTransactionTransferHbarLoadOf1000(t *testing.T) { // nolint
   109  	t.Parallel()
   110  	env := NewIntegrationTestEnv(t)
   111  	var err error
   112  	tx := make([]*TransferTransaction, 500)
   113  	response := make([]TransactionResponse, len(tx))
   114  	receipt := make([]TransactionReceipt, len(tx))
   115  
   116  	for i := 0; i < len(tx); i++ {
   117  		tx[i], err = NewTransferTransaction().
   118  			AddHbarTransfer(env.Client.GetOperatorAccountID(), HbarFromTinybar(-10)).
   119  			AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(10)).
   120  			FreezeWith(env.Client)
   121  		if err != nil {
   122  			panic(err)
   123  		}
   124  
   125  		_, err = tx[i].SignWithOperator(env.Client)
   126  		if err != nil {
   127  			panic(err)
   128  		}
   129  
   130  		response[i], err = tx[i].Execute(env.Client)
   131  		if err != nil {
   132  			panic(err)
   133  		}
   134  
   135  		receipt[i], err = response[i].SetValidateStatus(true).GetReceipt(env.Client)
   136  		if err != nil {
   137  			panic(err)
   138  		}
   139  
   140  		fmt.Printf("\r%v", i)
   141  	}
   142  }
   143  
   144  func TestIntegrationTransferTransactionCanTransferFromBytes(t *testing.T) {
   145  	t.Parallel()
   146  	env := NewIntegrationTestEnv(t)
   147  
   148  	newKey, err := GeneratePrivateKey()
   149  	require.NoError(t, err)
   150  
   151  	newBalance := NewHbar(10)
   152  
   153  	resp, err := NewAccountCreateTransaction().
   154  		SetKey(newKey).
   155  		SetNodeAccountIDs(env.NodeAccountIDs).
   156  		SetInitialBalance(newBalance).
   157  		Execute(env.Client)
   158  	require.NoError(t, err)
   159  
   160  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   161  	require.NoError(t, err)
   162  
   163  	accountID := *receipt.AccountID
   164  
   165  	transferTx, err := NewTransferTransaction().
   166  		SetNodeAccountIDs(env.NodeAccountIDs).
   167  		AddHbarTransfer(accountID, NewHbar(-1)).
   168  		AddHbarTransfer(AccountID{Account: 3}, NewHbar(1)).
   169  		FreezeWith(env.Client)
   170  	require.NoError(t, err)
   171  
   172  	transferTx.Sign(newKey)
   173  
   174  	transferTxBytes, err := transferTx.ToBytes()
   175  	require.NoError(t, err)
   176  
   177  	transactionInterface, err := TransactionFromBytes(transferTxBytes)
   178  	require.NoError(t, err)
   179  
   180  	resp, err = TransactionExecute(transactionInterface, env.Client)
   181  	require.NoError(t, err)
   182  
   183  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   184  	require.NoError(t, err)
   185  
   186  	err = CloseIntegrationTestEnv(env, nil)
   187  	require.NoError(t, err)
   188  }
   189  
   190  func TestIntegrationTransferTransactionCanTransferFromBytesAfter(t *testing.T) {
   191  	t.Parallel()
   192  	env := NewIntegrationTestEnv(t)
   193  
   194  	newKey, err := GeneratePrivateKey()
   195  	require.NoError(t, err)
   196  
   197  	newBalance := NewHbar(10)
   198  
   199  	resp, err := NewAccountCreateTransaction().
   200  		SetKey(newKey).
   201  		SetNodeAccountIDs(env.NodeAccountIDs).
   202  		SetInitialBalance(newBalance).
   203  		Execute(env.Client)
   204  	require.NoError(t, err)
   205  
   206  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   207  	require.NoError(t, err)
   208  
   209  	accountID := *receipt.AccountID
   210  
   211  	transferTx, err := NewTransferTransaction().
   212  		SetNodeAccountIDs(env.NodeAccountIDs).
   213  		AddHbarTransfer(accountID, NewHbar(-1)).
   214  		AddHbarTransfer(AccountID{Account: 3}, NewHbar(1)).
   215  		FreezeWith(env.Client)
   216  	require.NoError(t, err)
   217  
   218  	transferTxBytes, err := transferTx.ToBytes()
   219  	require.NoError(t, err)
   220  
   221  	transactionInterface, err := TransactionFromBytes(transferTxBytes)
   222  	require.NoError(t, err)
   223  
   224  	signedTx, err := TransactionSign(transactionInterface, newKey)
   225  	require.NoError(t, err)
   226  
   227  	resp, err = TransactionExecute(signedTx, env.Client)
   228  	require.NoError(t, err)
   229  
   230  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   231  	require.NoError(t, err)
   232  
   233  	err = CloseIntegrationTestEnv(env, nil)
   234  	require.NoError(t, err)
   235  }
   236  
   237  func TestIntegrationTransferTransactionCanTransferSignature(t *testing.T) {
   238  	t.Parallel()
   239  	env := NewIntegrationTestEnv(t)
   240  
   241  	newKey, err := GeneratePrivateKey()
   242  	require.NoError(t, err)
   243  
   244  	newBalance := NewHbar(10)
   245  
   246  	resp, err := NewAccountCreateTransaction().
   247  		SetKey(newKey).
   248  		SetNodeAccountIDs(env.NodeAccountIDs).
   249  		SetInitialBalance(newBalance).
   250  		Execute(env.Client)
   251  	require.NoError(t, err)
   252  
   253  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   254  	require.NoError(t, err)
   255  
   256  	accountID := *receipt.AccountID
   257  
   258  	transferTx, err := NewTransferTransaction().
   259  		SetNodeAccountIDs(env.NodeAccountIDs).
   260  		AddHbarTransfer(accountID, NewHbar(-1)).
   261  		AddHbarTransfer(AccountID{Account: 3}, NewHbar(1)).
   262  		FreezeWith(env.Client)
   263  	require.NoError(t, err)
   264  
   265  	transferTxBytes, err := transferTx.ToBytes()
   266  	require.NoError(t, err)
   267  
   268  	signature, err := newKey.SignTransaction(&transferTx.Transaction)
   269  
   270  	transactionInterface, err := TransactionFromBytes(transferTxBytes)
   271  	require.NoError(t, err)
   272  
   273  	signedTx, err := TransactionAddSignature(transactionInterface, newKey.PublicKey(), signature)
   274  	require.NoError(t, err)
   275  
   276  	resp, err = TransactionExecute(signedTx, env.Client)
   277  	require.NoError(t, err)
   278  
   279  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   280  	require.NoError(t, err)
   281  
   282  	err = CloseIntegrationTestEnv(env, nil)
   283  	require.NoError(t, err)
   284  }
   285  
   286  func TestIntegrationTransferTransactionCanTransferHbarWithAliasID(t *testing.T) {
   287  	t.Parallel()
   288  	env := NewIntegrationTestEnv(t)
   289  
   290  	key, err := GeneratePrivateKey()
   291  	require.NoError(t, err)
   292  	aliasAccountID := key.ToAccountID(0, 0)
   293  
   294  	resp, err := NewTransferTransaction().
   295  		AddHbarTransfer(env.OperatorID, NewHbar(1).Negated()).
   296  		AddHbarTransfer(*aliasAccountID, NewHbar(1)).
   297  		Execute(env.Client)
   298  	require.NoError(t, err)
   299  
   300  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   301  	require.NoError(t, err)
   302  
   303  	err = CloseIntegrationTestEnv(env, nil)
   304  	require.NoError(t, err)
   305  }