github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_mint_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 TestUnitTokenMintTransactionValidate(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  	tokenMint := NewTokenMintTransaction().
    49  		SetTokenID(tokenID)
    50  
    51  	err = tokenMint.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitTokenMintTransactionValidateWrong(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  	tokenMint := NewTokenMintTransaction().
    66  		SetTokenID(tokenID)
    67  
    68  	err = tokenMint.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 TestUnitTokenMintTransactionGet(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 := NewTokenMintTransaction().
    84  		SetTransactionID(transactionID).
    85  		SetNodeAccountIDs(nodeAccountID).
    86  		SetTokenID(tokenID).
    87  		SetAmount(123).
    88  		SetMetadata([]byte{123, 123}).
    89  		SetMetadatas([][]byte{{123, 123}, {13, 13}}).
    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.GetMetadatas()
   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 TestUnitTokenMintTransactionNothingSet(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 := NewTokenMintTransaction().
   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.GetMetadatas()
   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 TestUnitTokenMintTransactionProtoCheck(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 := NewTokenMintTransaction().
   155  		SetTransactionID(transactionID).
   156  		SetNodeAccountIDs(nodeAccountID).
   157  		SetTokenID(tokenID).
   158  		SetAmount(323).
   159  		SetMetadatas([][]byte{{50}, {50}}).
   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().GetTokenMint()
   171  	require.Equal(t, proto.Token.String(), tokenID._ToProtobuf().String())
   172  	require.Equal(t, proto.Amount, uint64(323))
   173  	require.Equal(t, proto.Metadata, [][]byte{{50}, {50}})
   174  }
   175  
   176  func TestUnitTokenMintTransactionCoverage(t *testing.T) {
   177  	t.Parallel()
   178  
   179  	checksum := "dmqui"
   180  	grpc := time.Second * 30
   181  	token := TokenID{Token: 3, checksum: &checksum}
   182  	nodeAccountID := []AccountID{{Account: 10}}
   183  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   184  
   185  	newKey, err := PrivateKeyGenerateEd25519()
   186  	require.NoError(t, err)
   187  
   188  	client, err := _NewMockClient()
   189  	client.SetLedgerID(*NewLedgerIDTestnet())
   190  	require.NoError(t, err)
   191  	client.SetAutoValidateChecksums(true)
   192  
   193  	transaction, err := NewTokenMintTransaction().
   194  		SetTransactionID(transactionID).
   195  		SetNodeAccountIDs(nodeAccountID).
   196  		SetTokenID(token).
   197  		SetMetadata([]byte{1}).
   198  		SetMetadatas([][]byte{{1, 0}}).
   199  		SetAmount(5).
   200  		SetGrpcDeadline(&grpc).
   201  		SetMaxTransactionFee(NewHbar(3)).
   202  		SetMaxRetry(3).
   203  		SetMaxBackoff(time.Second * 30).
   204  		SetMinBackoff(time.Second * 10).
   205  		SetTransactionMemo("no").
   206  		SetTransactionValidDuration(time.Second * 30).
   207  		SetRegenerateTransactionID(false).
   208  		Freeze()
   209  	require.NoError(t, err)
   210  
   211  	err = transaction.validateNetworkOnIDs(client)
   212  	require.NoError(t, err)
   213  	_, err = transaction.Schedule()
   214  	require.NoError(t, err)
   215  	transaction.GetTransactionID()
   216  	transaction.GetNodeAccountIDs()
   217  	transaction.GetMaxRetry()
   218  	transaction.GetMaxTransactionFee()
   219  	transaction.GetMaxBackoff()
   220  	transaction.GetMinBackoff()
   221  	transaction.GetRegenerateTransactionID()
   222  	byt, err := transaction.ToBytes()
   223  	require.NoError(t, err)
   224  	txFromBytes, err := TransactionFromBytes(byt)
   225  	require.NoError(t, err)
   226  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   227  	require.NoError(t, err)
   228  
   229  	_, err = transaction.GetTransactionHash()
   230  	require.NoError(t, err)
   231  	transaction.GetMaxTransactionFee()
   232  	transaction.GetTransactionMemo()
   233  	transaction.GetRegenerateTransactionID()
   234  	transaction.GetTokenID()
   235  	transaction.GetMetadatas()
   236  	transaction.GetAmount()
   237  	_, err = transaction.GetSignatures()
   238  	require.NoError(t, err)
   239  	transaction.getName()
   240  	switch b := txFromBytes.(type) {
   241  	case TokenMintTransaction:
   242  		b.AddSignature(newKey.PublicKey(), sig)
   243  	}
   244  }
   245  
   246  func TestUnitTokenMintTransactionMock(t *testing.T) {
   247  	t.Parallel()
   248  
   249  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   250  	require.NoError(t, err)
   251  
   252  	call := func(request *services.Transaction) *services.TransactionResponse {
   253  		require.NotEmpty(t, request.SignedTransactionBytes)
   254  		signedTransaction := services.SignedTransaction{}
   255  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   256  
   257  		require.NotEmpty(t, signedTransaction.BodyBytes)
   258  		transactionBody := services.TransactionBody{}
   259  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   260  
   261  		require.NotNil(t, transactionBody.TransactionID)
   262  		transactionId := transactionBody.TransactionID.String()
   263  		require.NotEqual(t, "", transactionId)
   264  
   265  		sigMap := signedTransaction.GetSigMap()
   266  		require.NotNil(t, sigMap)
   267  
   268  		return &services.TransactionResponse{
   269  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   270  		}
   271  	}
   272  	responses := [][]interface{}{{
   273  		call,
   274  	}}
   275  
   276  	client, server := NewMockClientAndServer(responses)
   277  	defer server.Close()
   278  
   279  	checksum := "dmqui"
   280  	token := TokenID{Token: 3, checksum: &checksum}
   281  
   282  	freez, err := NewTokenMintTransaction().
   283  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   284  		SetTokenID(token).
   285  		SetMetadata([]byte{1}).
   286  		SetAmount(5).
   287  		FreezeWith(client)
   288  	require.NoError(t, err)
   289  
   290  	_, err = freez.Sign(newKey).Execute(client)
   291  	require.NoError(t, err)
   292  }