github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_wipe_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  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitTokenWipeTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  	tokenID, err := TokenIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  
    50  	tokenWipe := NewTokenWipeTransaction().
    51  		SetTokenID(tokenID).
    52  		SetAccountID(accountID)
    53  
    54  	err = tokenWipe.validateNetworkOnIDs(client)
    55  	require.NoError(t, err)
    56  }
    57  
    58  func TestUnitTokenWipeTransactionValidateWrong(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	client, err := _NewMockClient()
    62  	client.SetLedgerID(*NewLedgerIDTestnet())
    63  	require.NoError(t, err)
    64  	client.SetAutoValidateChecksums(true)
    65  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    66  	require.NoError(t, err)
    67  	tokenID, err := TokenIDFromString("0.0.123-rmkykd")
    68  	require.NoError(t, err)
    69  
    70  	tokenWipe := NewTokenWipeTransaction().
    71  		SetTokenID(tokenID).
    72  		SetAccountID(accountID)
    73  
    74  	err = tokenWipe.validateNetworkOnIDs(client)
    75  	assert.Error(t, err)
    76  	if err != nil {
    77  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    78  	}
    79  }
    80  
    81  func TestUnitTokenWipeTransactionGet(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	tokenID := TokenID{Token: 7}
    85  
    86  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    87  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    88  
    89  	transaction, err := NewTokenWipeTransaction().
    90  		SetTransactionID(transactionID).
    91  		SetNodeAccountIDs(nodeAccountID).
    92  		SetTokenID(tokenID).
    93  		SetAccountID(AccountID{Account: 7}).
    94  		SetAmount(323).
    95  		SetSerialNumbers([]int64{123, 3123, 31}).
    96  		SetMaxTransactionFee(NewHbar(10)).
    97  		SetTransactionMemo("").
    98  		SetTransactionValidDuration(60 * time.Second).
    99  		SetRegenerateTransactionID(false).
   100  		Freeze()
   101  	require.NoError(t, err)
   102  
   103  	transaction.GetTransactionID()
   104  	transaction.GetNodeAccountIDs()
   105  
   106  	_, err = transaction.GetTransactionHash()
   107  	require.NoError(t, err)
   108  
   109  	transaction.GetTokenID()
   110  	transaction.GetAccountID()
   111  	transaction.GetAmount()
   112  	transaction.GetSerialNumbers()
   113  	transaction.GetMaxTransactionFee()
   114  	transaction.GetTransactionMemo()
   115  	transaction.GetRegenerateTransactionID()
   116  	_, err = transaction.GetSignatures()
   117  	require.NoError(t, err)
   118  	transaction.GetRegenerateTransactionID()
   119  	transaction.GetMaxTransactionFee()
   120  	transaction.GetRegenerateTransactionID()
   121  }
   122  
   123  func TestUnitTokenWipeTransactionNothingSet(t *testing.T) {
   124  	t.Parallel()
   125  
   126  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   127  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   128  
   129  	transaction, err := NewTokenWipeTransaction().
   130  		SetTransactionID(transactionID).
   131  		SetNodeAccountIDs(nodeAccountID).
   132  		Freeze()
   133  	require.NoError(t, err)
   134  
   135  	transaction.GetTransactionID()
   136  	transaction.GetNodeAccountIDs()
   137  
   138  	_, err = transaction.GetTransactionHash()
   139  	require.NoError(t, err)
   140  
   141  	transaction.GetTokenID()
   142  	transaction.GetAccountID()
   143  	transaction.GetAmount()
   144  	transaction.GetSerialNumbers()
   145  	transaction.GetMaxTransactionFee()
   146  	transaction.GetTransactionMemo()
   147  	transaction.GetRegenerateTransactionID()
   148  	_, err = transaction.GetSignatures()
   149  	require.NoError(t, err)
   150  	transaction.GetRegenerateTransactionID()
   151  	transaction.GetMaxTransactionFee()
   152  	transaction.GetRegenerateTransactionID()
   153  }
   154  
   155  func TestUnitTokenWipeTransactionProtoCheck(t *testing.T) {
   156  	t.Parallel()
   157  
   158  	tokenID := TokenID{Token: 7}
   159  	accountID := AccountID{Account: 7}
   160  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   161  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   162  
   163  	transaction, err := NewTokenWipeTransaction().
   164  		SetTransactionID(transactionID).
   165  		SetNodeAccountIDs(nodeAccountID).
   166  		SetTokenID(tokenID).
   167  		SetAccountID(accountID).
   168  		SetAmount(323).
   169  		SetSerialNumbers([]int64{123, 3123, 31}).
   170  		SetMaxTransactionFee(NewHbar(10)).
   171  		SetTransactionMemo("").
   172  		SetTransactionValidDuration(60 * time.Second).
   173  		SetRegenerateTransactionID(false).
   174  		Freeze()
   175  	require.NoError(t, err)
   176  
   177  	transaction.GetTransactionID()
   178  	transaction.GetNodeAccountIDs()
   179  
   180  	proto := transaction.build().GetTokenWipe()
   181  	require.Equal(t, proto.Token.String(), tokenID._ToProtobuf().String())
   182  	require.Equal(t, proto.Account.String(), accountID._ToProtobuf().String())
   183  	require.Equal(t, proto.Amount, uint64(323))
   184  	require.Equal(t, proto.SerialNumbers, []int64{123, 3123, 31})
   185  }
   186  
   187  func TestUnitTokenWipeTransactionCoverage(t *testing.T) {
   188  	t.Parallel()
   189  
   190  	checksum := "dmqui"
   191  	grpc := time.Second * 30
   192  	token := TokenID{Token: 3, checksum: &checksum}
   193  	account := AccountID{Account: 3, checksum: &checksum}
   194  	nodeAccountID := []AccountID{{Account: 10}}
   195  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   196  
   197  	newKey, err := PrivateKeyGenerateEd25519()
   198  	require.NoError(t, err)
   199  
   200  	client, err := _NewMockClient()
   201  	client.SetLedgerID(*NewLedgerIDTestnet())
   202  	require.NoError(t, err)
   203  	client.SetAutoValidateChecksums(true)
   204  
   205  	transaction, err := NewTokenWipeTransaction().
   206  		SetTransactionID(transactionID).
   207  		SetNodeAccountIDs(nodeAccountID).
   208  		SetTokenID(token).
   209  		SetAccountID(account).
   210  		SetAmount(5).
   211  		SetSerialNumbers([]int64{34}).
   212  		SetGrpcDeadline(&grpc).
   213  		SetMaxTransactionFee(NewHbar(3)).
   214  		SetMaxRetry(3).
   215  		SetMaxBackoff(time.Second * 30).
   216  		SetMinBackoff(time.Second * 10).
   217  		SetTransactionMemo("no").
   218  		SetTransactionValidDuration(time.Second * 30).
   219  		SetRegenerateTransactionID(false).
   220  		Freeze()
   221  	require.NoError(t, err)
   222  
   223  	err = transaction.validateNetworkOnIDs(client)
   224  	require.NoError(t, err)
   225  	_, err = transaction.Schedule()
   226  	require.NoError(t, err)
   227  	transaction.GetTransactionID()
   228  	transaction.GetNodeAccountIDs()
   229  	transaction.GetMaxRetry()
   230  	transaction.GetMaxTransactionFee()
   231  	transaction.GetMaxBackoff()
   232  	transaction.GetMinBackoff()
   233  	transaction.GetRegenerateTransactionID()
   234  	byt, err := transaction.ToBytes()
   235  	require.NoError(t, err)
   236  	txFromBytes, err := TransactionFromBytes(byt)
   237  	require.NoError(t, err)
   238  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   239  	require.NoError(t, err)
   240  
   241  	_, err = transaction.GetTransactionHash()
   242  	require.NoError(t, err)
   243  	transaction.GetMaxTransactionFee()
   244  	transaction.GetTransactionMemo()
   245  	transaction.GetRegenerateTransactionID()
   246  	transaction.GetTokenID()
   247  	transaction.GetAccountID()
   248  	transaction.GetAmount()
   249  	transaction.GetSerialNumbers()
   250  	_, err = transaction.GetSignatures()
   251  	require.NoError(t, err)
   252  	transaction.getName()
   253  	switch b := txFromBytes.(type) {
   254  	case TokenWipeTransaction:
   255  		b.AddSignature(newKey.PublicKey(), sig)
   256  	}
   257  }
   258  
   259  func TestUnitTokenWipeTransactionMock(t *testing.T) {
   260  	t.Parallel()
   261  
   262  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   263  	require.NoError(t, err)
   264  
   265  	call := func(request *services.Transaction) *services.TransactionResponse {
   266  		require.NotEmpty(t, request.SignedTransactionBytes)
   267  		signedTransaction := services.SignedTransaction{}
   268  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   269  
   270  		require.NotEmpty(t, signedTransaction.BodyBytes)
   271  		transactionBody := services.TransactionBody{}
   272  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   273  
   274  		require.NotNil(t, transactionBody.TransactionID)
   275  		transactionId := transactionBody.TransactionID.String()
   276  		require.NotEqual(t, "", transactionId)
   277  
   278  		sigMap := signedTransaction.GetSigMap()
   279  		require.NotNil(t, sigMap)
   280  
   281  		return &services.TransactionResponse{
   282  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   283  		}
   284  	}
   285  	responses := [][]interface{}{{
   286  		call,
   287  	}}
   288  
   289  	client, server := NewMockClientAndServer(responses)
   290  	defer server.Close()
   291  
   292  	checksum := "dmqui"
   293  	token := TokenID{Token: 3, checksum: &checksum}
   294  	account := AccountID{Account: 3, checksum: &checksum}
   295  
   296  	freez, err := NewTokenWipeTransaction().
   297  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   298  		SetTokenID(token).
   299  		SetAccountID(account).
   300  		FreezeWith(client)
   301  	require.NoError(t, err)
   302  
   303  	_, err = freez.Sign(newKey).Execute(client)
   304  	require.NoError(t, err)
   305  }