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

     1  //go:build all || unit
     2  // +build all unit
     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  	"encoding/hex"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/hashgraph/hedera-protobufs-go/services"
    32  	"google.golang.org/grpc/codes"
    33  	"google.golang.org/grpc/status"
    34  
    35  	"github.com/stretchr/testify/assert"
    36  
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  func TestUnitAccountCreateTransactionValidate(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	client, err := _NewMockClient()
    44  	client.SetLedgerID(*NewLedgerIDTestnet())
    45  	require.NoError(t, err)
    46  	client.SetAutoValidateChecksums(true)
    47  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  
    50  	createAccount := NewAccountCreateTransaction().
    51  		SetProxyAccountID(accountID)
    52  
    53  	err = createAccount.validateNetworkOnIDs(client)
    54  	require.NoError(t, err)
    55  }
    56  
    57  func TestUnitAccountCreateTransactionValidateWrong(t *testing.T) {
    58  	t.Parallel()
    59  
    60  	client, err := _NewMockClient()
    61  	client.SetLedgerID(*NewLedgerIDTestnet())
    62  	require.NoError(t, err)
    63  	client.SetAutoValidateChecksums(true)
    64  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    65  	require.NoError(t, err)
    66  
    67  	createAccount := NewAccountCreateTransaction().
    68  		SetProxyAccountID(accountID)
    69  
    70  	err = createAccount.validateNetworkOnIDs(client)
    71  	assert.Error(t, err)
    72  	if err != nil {
    73  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    74  	}
    75  }
    76  
    77  func TestUnitAccountCreateTransactionMock(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	responses := [][]interface{}{{
    81  		status.New(codes.Unavailable, "node is UNAVAILABLE").Err(),
    82  		status.New(codes.Internal, "Received RST_STREAM with code 0").Err(),
    83  		&services.TransactionResponse{
    84  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY,
    85  		},
    86  		&services.TransactionResponse{
    87  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
    88  		},
    89  		&services.Response{
    90  			Response: &services.Response_TransactionGetReceipt{
    91  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
    92  					Header: &services.ResponseHeader{
    93  						Cost:         0,
    94  						ResponseType: services.ResponseType_COST_ANSWER,
    95  					},
    96  				},
    97  			},
    98  		},
    99  		&services.Response{
   100  			Response: &services.Response_TransactionGetReceipt{
   101  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
   102  					Header: &services.ResponseHeader{
   103  						Cost:         0,
   104  						ResponseType: services.ResponseType_ANSWER_ONLY,
   105  					},
   106  					Receipt: &services.TransactionReceipt{
   107  						Status: services.ResponseCodeEnum_RECEIPT_NOT_FOUND,
   108  					},
   109  				},
   110  			},
   111  		},
   112  		&services.Response{
   113  			Response: &services.Response_TransactionGetReceipt{
   114  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
   115  					Header: &services.ResponseHeader{
   116  						Cost:         0,
   117  						ResponseType: services.ResponseType_ANSWER_ONLY,
   118  					},
   119  					Receipt: &services.TransactionReceipt{
   120  						Status: services.ResponseCodeEnum_SUCCESS,
   121  						AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{
   122  							AccountNum: 234,
   123  						}},
   124  					},
   125  				},
   126  			},
   127  		},
   128  	}}
   129  
   130  	client, server := NewMockClientAndServer(responses)
   131  	defer server.Close()
   132  
   133  	newKey, err := PrivateKeyGenerateEd25519()
   134  	require.NoError(t, err)
   135  
   136  	newBalance := NewHbar(2)
   137  
   138  	tran := TransactionIDGenerate(AccountID{Account: 3})
   139  
   140  	resp, err := NewAccountCreateTransaction().
   141  		SetNodeAccountIDs([]AccountID{{Account: 3}, {Account: 4}}).
   142  		SetKey(newKey).
   143  		SetTransactionID(tran).
   144  		SetInitialBalance(newBalance).
   145  		SetMaxAutomaticTokenAssociations(100).
   146  		Execute(client)
   147  	require.NoError(t, err)
   148  
   149  	receipt, err := resp.GetReceipt(client)
   150  	require.NoError(t, err)
   151  	require.Equal(t, receipt.AccountID, &AccountID{Account: 234})
   152  }
   153  
   154  func TestUnitAccountCreateTransactionGet(t *testing.T) {
   155  	t.Parallel()
   156  
   157  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   158  
   159  	key, err := PrivateKeyGenerateEd25519()
   160  
   161  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   162  
   163  	transaction, err := NewAccountCreateTransaction().
   164  		SetTransactionID(transactionID).
   165  		SetNodeAccountIDs(nodeAccountID).
   166  		SetKey(key).
   167  		SetAccountMemo("").
   168  		SetReceiverSignatureRequired(true).
   169  		SetMaxAutomaticTokenAssociations(2).
   170  		SetAutoRenewPeriod(60 * time.Second).
   171  		SetTransactionMemo("").
   172  		SetTransactionValidDuration(60 * time.Second).
   173  		Freeze()
   174  	require.NoError(t, err)
   175  
   176  	transaction.GetTransactionID()
   177  	transaction.GetNodeAccountIDs()
   178  
   179  	_, err = transaction.GetTransactionHash()
   180  	require.NoError(t, err)
   181  
   182  	transaction.GetMaxTransactionFee()
   183  	transaction.GetTransactionMemo()
   184  	transaction.GetRegenerateTransactionID()
   185  	_, err = transaction.GetSignatures()
   186  	require.NoError(t, err)
   187  	transaction.GetRegenerateTransactionID()
   188  	transaction.GetAccountMemo()
   189  	transaction.GetMaxTransactionFee()
   190  	transaction.GetMaxAutomaticTokenAssociations()
   191  	transaction.GetRegenerateTransactionID()
   192  	transaction.GetKey()
   193  	transaction.GetInitialBalance()
   194  	transaction.GetAutoRenewPeriod()
   195  	transaction.GetReceiverSignatureRequired()
   196  }
   197  
   198  func TestUnitAccountCreateTransactionSetNothing(t *testing.T) {
   199  	t.Parallel()
   200  
   201  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   202  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   203  
   204  	transaction, err := NewAccountCreateTransaction().
   205  		SetTransactionID(transactionID).
   206  		SetNodeAccountIDs(nodeAccountID).
   207  		Freeze()
   208  	require.NoError(t, err)
   209  
   210  	transaction.GetTransactionID()
   211  	transaction.GetNodeAccountIDs()
   212  
   213  	_, err = transaction.GetTransactionHash()
   214  	require.NoError(t, err)
   215  
   216  	transaction.GetMaxTransactionFee()
   217  	transaction.GetTransactionMemo()
   218  	transaction.GetRegenerateTransactionID()
   219  	_, err = transaction.GetSignatures()
   220  	require.NoError(t, err)
   221  	transaction.GetRegenerateTransactionID()
   222  	transaction.GetAccountMemo()
   223  	transaction.GetMaxTransactionFee()
   224  	transaction.GetMaxAutomaticTokenAssociations()
   225  	transaction.GetProxyAccountID()
   226  	transaction.GetRegenerateTransactionID()
   227  	transaction.GetKey()
   228  	transaction.GetInitialBalance()
   229  	transaction.GetAutoRenewPeriod()
   230  	transaction.GetReceiverSignatureRequired()
   231  }
   232  
   233  func TestUnitAccountCreateTransactionProtoCheck(t *testing.T) {
   234  	t.Parallel()
   235  
   236  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   237  	stackedAccountID := AccountID{Account: 5}
   238  
   239  	key, err := PrivateKeyGenerateEd25519()
   240  	require.NoError(t, err)
   241  
   242  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   243  
   244  	alias := "5c562e90feaf0eebd33ea75d21024f249d451417"
   245  
   246  	transaction, err := NewAccountCreateTransaction().
   247  		SetTransactionID(transactionID).
   248  		SetNodeAccountIDs(nodeAccountID).
   249  		SetKey(key).
   250  		SetInitialBalance(NewHbar(3)).
   251  		SetAccountMemo("ty").
   252  		SetReceiverSignatureRequired(true).
   253  		SetMaxAutomaticTokenAssociations(2).
   254  		SetStakedAccountID(stackedAccountID).
   255  		SetDeclineStakingReward(true).
   256  		SetAutoRenewPeriod(60 * time.Second).
   257  		SetTransactionMemo("").
   258  		SetTransactionValidDuration(60 * time.Second).
   259  		SetAlias(alias).
   260  		Freeze()
   261  	require.NoError(t, err)
   262  
   263  	transaction.GetTransactionID()
   264  	transaction.GetNodeAccountIDs()
   265  
   266  	proto := transaction.build().GetCryptoCreateAccount()
   267  	require.Equal(t, proto.Key.String(), key._ToProtoKey().String())
   268  	require.Equal(t, proto.InitialBalance, uint64(NewHbar(3).AsTinybar()))
   269  	require.Equal(t, proto.Memo, "ty")
   270  	require.Equal(t, proto.ReceiverSigRequired, true)
   271  	require.Equal(t, proto.MaxAutomaticTokenAssociations, int32(2))
   272  	require.Equal(t, proto.StakedId.(*services.CryptoCreateTransactionBody_StakedAccountId).StakedAccountId.String(),
   273  		stackedAccountID._ToProtobuf().String())
   274  	require.Equal(t, proto.DeclineReward, true)
   275  	require.Equal(t, proto.AutoRenewPeriod.String(), _DurationToProtobuf(60*time.Second).String())
   276  	require.Equal(t, hex.EncodeToString(proto.Alias), alias)
   277  }
   278  
   279  func TestUnitAccountCreateTransactionCoverage(t *testing.T) {
   280  	t.Parallel()
   281  
   282  	checksum := "dmqui"
   283  	account := AccountID{Account: 3, checksum: &checksum}
   284  	nodeAccountID := []AccountID{{Account: 10}}
   285  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   286  
   287  	key, err := PrivateKeyGenerateEd25519()
   288  	require.NoError(t, err)
   289  
   290  	alias := "5c562e90feaf0eebd33ea75d21024f249d451417"
   291  
   292  	client, err := _NewMockClient()
   293  	client.SetLedgerID(*NewLedgerIDTestnet())
   294  	require.NoError(t, err)
   295  	client.SetAutoValidateChecksums(true)
   296  
   297  	trx, err := NewAccountCreateTransaction().
   298  		SetTransactionID(transactionID).
   299  		SetNodeAccountIDs(nodeAccountID).
   300  		SetKey(key).
   301  		SetInitialBalance(NewHbar(3)).
   302  		SetAccountMemo("ty").
   303  		SetReceiverSignatureRequired(true).
   304  		SetMaxAutomaticTokenAssociations(2).
   305  		SetStakedAccountID(account).
   306  		SetStakedNodeID(4).
   307  		SetDeclineStakingReward(true).
   308  		SetAutoRenewPeriod(60 * time.Second).
   309  		SetTransactionMemo("").
   310  		SetTransactionValidDuration(60 * time.Second).
   311  		SetMaxTransactionFee(NewHbar(3)).
   312  		SetMaxRetry(3).
   313  		SetMaxBackoff(time.Second * 30).
   314  		SetMinBackoff(time.Second * 10).
   315  		SetTransactionMemo("no").
   316  		SetTransactionValidDuration(time.Second * 30).
   317  		SetRegenerateTransactionID(false).
   318  		SetAlias(alias).
   319  		Freeze()
   320  	require.NoError(t, err)
   321  
   322  	trx.validateNetworkOnIDs(client)
   323  	_, err = trx.Schedule()
   324  	require.NoError(t, err)
   325  	trx.GetTransactionID()
   326  	trx.GetNodeAccountIDs()
   327  	trx.GetMaxRetry()
   328  	trx.GetMaxTransactionFee()
   329  	trx.GetMaxBackoff()
   330  	trx.GetMinBackoff()
   331  	trx.GetRegenerateTransactionID()
   332  	byt, err := trx.ToBytes()
   333  	require.NoError(t, err)
   334  	txFromBytes, err := TransactionFromBytes(byt)
   335  	require.NoError(t, err)
   336  	sig, err := key.SignTransaction(&trx.Transaction)
   337  	require.NoError(t, err)
   338  
   339  	_, err = trx.GetTransactionHash()
   340  	require.NoError(t, err)
   341  	trx.GetMaxTransactionFee()
   342  	trx.GetTransactionMemo()
   343  	trx.GetRegenerateTransactionID()
   344  	trx.GetStakedAccountID()
   345  	trx.GetStakedNodeID()
   346  	trx.GetDeclineStakingReward()
   347  	trx.GetAlias()
   348  	_, err = trx.GetSignatures()
   349  	require.NoError(t, err)
   350  	trx.getName()
   351  	switch b := txFromBytes.(type) {
   352  	case AccountCreateTransaction:
   353  		b.AddSignature(key.PublicKey(), sig)
   354  	}
   355  }
   356  
   357  func TestUnitAccountCreateSetStakedNodeID(t *testing.T) {
   358  	t.Parallel()
   359  
   360  	checksum := "dmqui"
   361  	account := AccountID{Account: 3, checksum: &checksum}
   362  	tx := NewAccountCreateTransaction().
   363  		SetStakedAccountID(account).
   364  		SetStakedNodeID(4)
   365  
   366  	require.Equal(t, AccountID{}, tx.GetStakedAccountID())
   367  	require.Equal(t, int64(4), tx.GetStakedNodeID())
   368  }
   369  func TestUnitAccountCreateSetStakedAccountID(t *testing.T) {
   370  	t.Parallel()
   371  
   372  	checksum := "dmqui"
   373  	account := AccountID{Account: 3, checksum: &checksum}
   374  	tx := NewAccountCreateTransaction().
   375  		SetStakedNodeID(4).
   376  		SetStakedAccountID(account)
   377  
   378  	require.Equal(t, int64(0), tx.GetStakedNodeID())
   379  	require.Equal(t, account, tx.GetStakedAccountID())
   380  }