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