github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_mint_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/assert"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestIntegrationTokenMintTransactionCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	newKey, err := PrivateKeyGenerateEd25519()
    40  	require.NoError(t, err)
    41  
    42  	newBalance := NewHbar(2)
    43  
    44  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    45  
    46  	resp, err := NewAccountCreateTransaction().
    47  		SetKey(newKey.PublicKey()).
    48  		SetNodeAccountIDs(env.NodeAccountIDs).
    49  		SetInitialBalance(newBalance).
    50  		Execute(env.Client)
    51  	require.NoError(t, err)
    52  
    53  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    54  	require.NoError(t, err)
    55  
    56  	accountID := *receipt.AccountID
    57  
    58  	tokenID, err := createFungibleToken(&env)
    59  	require.NoError(t, err)
    60  
    61  	resp, err = NewTokenMintTransaction().
    62  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    63  		SetAmount(10).
    64  		SetTokenID(tokenID).
    65  		Execute(env.Client)
    66  	require.NoError(t, err)
    67  
    68  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    69  	require.NoError(t, err)
    70  
    71  	info, err := NewTokenInfoQuery().
    72  		SetTokenID(tokenID).
    73  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    74  		Execute(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	assert.Equal(t, uint64(1000010), info.TotalSupply)
    78  
    79  	tx, err := NewAccountDeleteTransaction().
    80  		SetAccountID(accountID).
    81  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    82  		FreezeWith(env.Client)
    83  	require.NoError(t, err)
    84  
    85  	resp, err = tx.
    86  		Sign(newKey).
    87  		Execute(env.Client)
    88  	require.NoError(t, err)
    89  
    90  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    91  	require.NoError(t, err)
    92  
    93  	err = CloseIntegrationTestEnv(env, &tokenID)
    94  	require.NoError(t, err)
    95  }
    96  
    97  func TestIntegrationTokenMintTransactionNoAmount(t *testing.T) {
    98  	t.Parallel()
    99  	env := NewIntegrationTestEnv(t)
   100  
   101  	newKey, err := PrivateKeyGenerateEd25519()
   102  	require.NoError(t, err)
   103  
   104  	newBalance := NewHbar(2)
   105  
   106  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   107  
   108  	resp, err := NewAccountCreateTransaction().
   109  		SetNodeAccountIDs(env.NodeAccountIDs).
   110  		SetKey(newKey.PublicKey()).
   111  		SetInitialBalance(newBalance).
   112  		Execute(env.Client)
   113  	require.NoError(t, err)
   114  
   115  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   116  	require.NoError(t, err)
   117  
   118  	accountID := *receipt.AccountID
   119  
   120  	tokenID, err := createFungibleToken(&env)
   121  	require.NoError(t, err)
   122  
   123  	_, err = NewTokenMintTransaction().
   124  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   125  		SetTokenID(tokenID).
   126  		Execute(env.Client)
   127  	// It is now possible to perform Zero Token Operations https://hips.hedera.com/hip/hip-564
   128  	require.NoError(t, err)
   129  
   130  	tx, err := NewAccountDeleteTransaction().
   131  		SetAccountID(accountID).
   132  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   133  		FreezeWith(env.Client)
   134  	require.NoError(t, err)
   135  
   136  	resp, err = tx.
   137  		Sign(newKey).
   138  		Execute(env.Client)
   139  	require.NoError(t, err)
   140  
   141  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   142  	require.NoError(t, err)
   143  
   144  	err = CloseIntegrationTestEnv(env, &tokenID)
   145  	require.NoError(t, err)
   146  }
   147  
   148  func TestIntegrationTokenMintTransactionNoTokenID(t *testing.T) {
   149  	t.Parallel()
   150  	env := NewIntegrationTestEnv(t)
   151  
   152  	newKey, err := PrivateKeyGenerateEd25519()
   153  	require.NoError(t, err)
   154  
   155  	newBalance := NewHbar(2)
   156  
   157  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   158  
   159  	resp, err := NewAccountCreateTransaction().
   160  		SetNodeAccountIDs(env.NodeAccountIDs).
   161  		SetKey(newKey.PublicKey()).
   162  		SetInitialBalance(newBalance).
   163  		Execute(env.Client)
   164  	require.NoError(t, err)
   165  
   166  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   167  	require.NoError(t, err)
   168  
   169  	accountID := *receipt.AccountID
   170  
   171  	tokenID, err := createFungibleToken(&env)
   172  	require.NoError(t, err)
   173  
   174  	resp2, err := NewTokenMintTransaction().
   175  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   176  		SetAmount(10).
   177  		Execute(env.Client)
   178  	assert.Error(t, err)
   179  	if err != nil {
   180  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_TOKEN_ID received for transaction %s", resp2.TransactionID), err.Error())
   181  	}
   182  
   183  	tx, err := NewAccountDeleteTransaction().
   184  		SetAccountID(accountID).
   185  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   186  		FreezeWith(env.Client)
   187  	require.NoError(t, err)
   188  
   189  	resp, err = tx.
   190  		Sign(newKey).
   191  		Execute(env.Client)
   192  	require.NoError(t, err)
   193  
   194  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   195  	require.NoError(t, err)
   196  
   197  	err = CloseIntegrationTestEnv(env, &tokenID)
   198  	require.NoError(t, err)
   199  }
   200  
   201  func TestIntegrationTokenMintTransactionMaxReached(t *testing.T) {
   202  	t.Parallel()
   203  	env := NewIntegrationTestEnv(t)
   204  
   205  	newKey, err := PrivateKeyGenerateEd25519()
   206  	require.NoError(t, err)
   207  
   208  	newBalance := NewHbar(2)
   209  
   210  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   211  
   212  	resp, err := NewAccountCreateTransaction().
   213  		SetKey(newKey.PublicKey()).
   214  		SetNodeAccountIDs(env.NodeAccountIDs).
   215  		SetInitialBalance(newBalance).
   216  		Execute(env.Client)
   217  	require.NoError(t, err)
   218  
   219  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   220  	require.NoError(t, err)
   221  
   222  	accountID := *receipt.AccountID
   223  
   224  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   225  		transaction.
   226  			SetDecimals(3).
   227  			SetInitialSupply(10).
   228  			SetSupplyType(TokenSupplyTypeFinite).
   229  			SetMaxSupply(30)
   230  	})
   231  	require.NoError(t, err)
   232  
   233  	resp, err = NewTokenMintTransaction().
   234  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   235  		SetAmount(10000000).
   236  		SetTokenID(tokenID).
   237  		Execute(env.Client)
   238  	require.NoError(t, err)
   239  
   240  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   241  	assert.Error(t, err)
   242  	if err != nil {
   243  		assert.Equal(t, "exceptional receipt status: TOKEN_MAX_SUPPLY_REACHED", err.Error())
   244  	}
   245  
   246  	tx, err := NewAccountDeleteTransaction().
   247  		SetAccountID(accountID).
   248  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   249  		FreezeWith(env.Client)
   250  	require.NoError(t, err)
   251  
   252  	resp, err = tx.
   253  		Sign(newKey).
   254  		Execute(env.Client)
   255  	require.NoError(t, err)
   256  
   257  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   258  	require.NoError(t, err)
   259  
   260  	err = CloseIntegrationTestEnv(env, &tokenID)
   261  	require.NoError(t, err)
   262  }
   263  
   264  func DisabledTestIntegrationTokenMintTransactionMetadataTooLong(t *testing.T) { // nolint
   265  	t.Parallel()
   266  	env := NewIntegrationTestEnv(t)
   267  
   268  	newBalance := NewHbar(2)
   269  
   270  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   271  
   272  	tokenID, err := createNft(&env)
   273  	require.NoError(t, err)
   274  
   275  	metaData := make([]byte, 50, 101)
   276  
   277  	mint, err := NewTokenMintTransaction().
   278  		SetTokenID(tokenID).
   279  		SetMetadata(metaData).
   280  		Execute(env.Client)
   281  	require.NoError(t, err)
   282  
   283  	mintReceipt, err := mint.SetValidateStatus(true).GetReceipt(env.Client)
   284  	require.NoError(t, err)
   285  
   286  	nftID := tokenID.Nft(mintReceipt.SerialNumbers[0])
   287  
   288  	nftID.SerialNumber = 3
   289  
   290  	_, err = NewTokenNftInfoQuery().
   291  		SetNftID(nftID).
   292  		Execute(env.Client)
   293  	assert.Error(t, err)
   294  }
   295  
   296  func DisabledTestIntegrationTokenMintTransactionInvalidMetadata(t *testing.T) { // nolint
   297  	t.Parallel()
   298  	env := NewIntegrationTestEnv(t)
   299  
   300  	newBalance := NewHbar(2)
   301  
   302  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   303  
   304  	tokenID, err := createNft(&env)
   305  	require.NoError(t, err)
   306  
   307  	mint, err := NewTokenMintTransaction().
   308  		SetTokenID(tokenID).
   309  		SetAmount(1).
   310  		Execute(env.Client)
   311  	require.NoError(t, err)
   312  
   313  	_, err = mint.SetValidateStatus(true).GetReceipt(env.Client)
   314  	assert.Error(t, err)
   315  	if err != nil {
   316  		assert.Equal(t, "exceptional receipt status: INVALID_TOKEN_MINT_METADATA", err.Error())
   317  	}
   318  }