github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_create_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  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestIntegrationTokenCreateTransactionCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	tokenID, err := createFungibleToken(&env)
    40  	require.NoError(t, err)
    41  
    42  	err = CloseIntegrationTestEnv(env, &tokenID)
    43  	require.NoError(t, err)
    44  }
    45  
    46  func TestIntegrationTokenCreateTransactionMultipleKeys(t *testing.T) {
    47  	t.Parallel()
    48  	env := NewIntegrationTestEnv(t)
    49  
    50  	keys := make([]PrivateKey, 6)
    51  	pubKeys := make([]PublicKey, 6)
    52  
    53  	for i := range keys {
    54  		newKey, err := PrivateKeyGenerateEd25519()
    55  		require.NoError(t, err)
    56  
    57  		keys[i] = newKey
    58  		pubKeys[i] = newKey.PublicKey()
    59  	}
    60  
    61  	newBalance := NewHbar(2)
    62  
    63  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    64  
    65  	resp, err := NewAccountCreateTransaction().
    66  		SetKey(pubKeys[0]).
    67  		SetNodeAccountIDs(env.NodeAccountIDs).
    68  		SetInitialBalance(newBalance).
    69  		Execute(env.Client)
    70  	require.NoError(t, err)
    71  
    72  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    73  	require.NoError(t, err)
    74  
    75  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
    76  		transaction.
    77  			SetFreezeKey(pubKeys[1]).
    78  			SetWipeKey(pubKeys[2]).
    79  			SetKycKey(pubKeys[3]).
    80  			SetSupplyKey(pubKeys[4]).
    81  			SetMetadataKey(pubKeys[5])
    82  	})
    83  	require.NoError(t, err)
    84  
    85  	err = CloseIntegrationTestEnv(env, &tokenID)
    86  	require.NoError(t, err)
    87  }
    88  
    89  func TestIntegrationTokenCreateTransactionNoKeys(t *testing.T) {
    90  	t.Parallel()
    91  	env := NewIntegrationTestEnv(t)
    92  
    93  	keys := make([]PrivateKey, 6)
    94  	pubKeys := make([]PublicKey, 6)
    95  
    96  	for i := range keys {
    97  		newKey, err := PrivateKeyGenerateEd25519()
    98  		require.NoError(t, err)
    99  
   100  		keys[i] = newKey
   101  		pubKeys[i] = newKey.PublicKey()
   102  	}
   103  
   104  	newBalance := NewHbar(2)
   105  
   106  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   107  
   108  	resp, err := NewAccountCreateTransaction().
   109  		SetKey(pubKeys[0]).
   110  		SetNodeAccountIDs(env.NodeAccountIDs).
   111  		SetInitialBalance(newBalance).
   112  		Execute(env.Client)
   113  	require.NoError(t, err)
   114  
   115  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   116  	require.NoError(t, err)
   117  
   118  	resp, err = NewTokenCreateTransaction().
   119  		SetTokenName("ffff").
   120  		SetTokenSymbol("F").
   121  		SetNodeAccountIDs(env.NodeAccountIDs).
   122  		SetTreasuryAccountID(env.Client.GetOperatorAccountID()).
   123  		Execute(env.Client)
   124  	require.NoError(t, err)
   125  
   126  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   127  	require.NoError(t, err)
   128  
   129  	tokenID := *receipt.TokenID
   130  
   131  	info, err := NewTokenInfoQuery().
   132  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   133  		SetTokenID(tokenID).
   134  		Execute(env.Client)
   135  
   136  	require.NoError(t, err)
   137  	assert.Equal(t, info.Name, "ffff")
   138  	assert.Equal(t, info.Symbol, "F")
   139  	assert.Equal(t, info.Decimals, uint32(0))
   140  	assert.Equal(t, info.TotalSupply, uint64(0))
   141  	assert.Equal(t, info.Treasury.String(), env.Client.GetOperatorAccountID().String())
   142  	assert.Nil(t, info.AdminKey)
   143  	assert.Nil(t, info.FreezeKey)
   144  	assert.Nil(t, info.KycKey)
   145  	assert.Nil(t, info.WipeKey)
   146  	assert.Nil(t, info.SupplyKey)
   147  	assert.Nil(t, info.DefaultFreezeStatus)
   148  	assert.Nil(t, info.DefaultKycStatus)
   149  	assert.NotNil(t, info.AutoRenewPeriod)
   150  	assert.Equal(t, *info.AutoRenewPeriod, 7890000*time.Second)
   151  	assert.NotNil(t, info.AutoRenewAccountID)
   152  	assert.Equal(t, info.AutoRenewAccountID.String(), env.Client.GetOperatorAccountID().String())
   153  	assert.NotNil(t, info.ExpirationTime)
   154  }
   155  
   156  func TestIntegrationTokenCreateTransactionAdminSign(t *testing.T) {
   157  	t.Parallel()
   158  	env := NewIntegrationTestEnv(t)
   159  
   160  	keys := make([]PrivateKey, 6)
   161  	pubKeys := make([]PublicKey, 6)
   162  
   163  	for i := range keys {
   164  		newKey, err := PrivateKeyGenerateEd25519()
   165  		require.NoError(t, err)
   166  
   167  		keys[i] = newKey
   168  		pubKeys[i] = newKey.PublicKey()
   169  	}
   170  
   171  	newBalance := NewHbar(2)
   172  
   173  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   174  
   175  	resp, err := NewAccountCreateTransaction().
   176  		SetKey(pubKeys[0]).
   177  		SetNodeAccountIDs(env.NodeAccountIDs).
   178  		SetInitialBalance(newBalance).
   179  		Execute(env.Client)
   180  	require.NoError(t, err)
   181  
   182  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   183  	require.NoError(t, err)
   184  
   185  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   186  		transaction.
   187  			SetFreezeKey(pubKeys[1]).
   188  			SetWipeKey(pubKeys[2]).
   189  			SetKycKey(pubKeys[3]).
   190  			SetSupplyKey(pubKeys[4]).
   191  			SetMetadataKey(pubKeys[5]).
   192  			FreezeWith(env.Client)
   193  		transaction.
   194  			Sign(keys[0]).
   195  			Sign(keys[1])
   196  	})
   197  
   198  	err = CloseIntegrationTestEnv(env, &tokenID)
   199  	require.NoError(t, err)
   200  }
   201  
   202  func DisabledTestIntegrationTokenNftCreateTransaction(t *testing.T) { // nolint
   203  	t.Parallel()
   204  	env := NewIntegrationTestEnv(t)
   205  
   206  	tokenID, err := createNft(&env)
   207  
   208  	err = CloseIntegrationTestEnv(env, &tokenID)
   209  	require.NoError(t, err)
   210  }
   211  
   212  func TestIntegrationTokenCreateTransactionWithCustomFees(t *testing.T) {
   213  	t.Parallel()
   214  	env := NewIntegrationTestEnv(t)
   215  
   216  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   217  		transaction.
   218  			SetCustomFees([]Fee{
   219  				NewCustomFixedFee().
   220  					SetFeeCollectorAccountID(env.OperatorID).
   221  					SetAmount(10),
   222  				NewCustomFractionalFee().
   223  					SetFeeCollectorAccountID(env.OperatorID).
   224  					SetNumerator(1).
   225  					SetDenominator(20).
   226  					SetMin(1).
   227  					SetAssessmentMethod(true).
   228  					SetMax(10),
   229  			})
   230  	})
   231  
   232  	err = CloseIntegrationTestEnv(env, &tokenID)
   233  	require.NoError(t, err)
   234  }
   235  
   236  func TestIntegrationTokenCreateTransactionWithCustomFeesDenominatorZero(t *testing.T) {
   237  	t.Parallel()
   238  	env := NewIntegrationTestEnv(t)
   239  
   240  	_, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   241  		transaction.
   242  			SetCustomFees([]Fee{
   243  				CustomFixedFee{
   244  					CustomFee: CustomFee{
   245  						FeeCollectorAccountID: &env.OperatorID,
   246  					},
   247  					Amount: 10,
   248  				},
   249  				CustomFractionalFee{
   250  					CustomFee: CustomFee{
   251  						FeeCollectorAccountID: &env.OperatorID,
   252  					},
   253  					Numerator:     1,
   254  					Denominator:   0,
   255  					MinimumAmount: 1,
   256  					MaximumAmount: 10,
   257  				},
   258  			})
   259  	})
   260  	assert.Error(t, err)
   261  	if err != nil {
   262  		assert.Equal(t, "exceptional receipt status: FRACTION_DIVIDES_BY_ZERO", err.Error())
   263  	}
   264  }
   265  
   266  func TestIntegrationTokenCreateTransactionWithInvalidFeeCollectorAccountID(t *testing.T) {
   267  	t.Parallel()
   268  	env := NewIntegrationTestEnv(t)
   269  
   270  	_, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   271  		transaction.
   272  			SetCustomFees([]Fee{
   273  				NewCustomFractionalFee().
   274  					SetFeeCollectorAccountID(AccountID{}).
   275  					SetNumerator(1).
   276  					SetDenominator(20).
   277  					SetMin(1).
   278  					SetMax(10),
   279  			})
   280  	})
   281  	assert.Error(t, err)
   282  	if err != nil {
   283  		assert.Equal(t, "exceptional receipt status: INVALID_CUSTOM_FEE_COLLECTOR", err.Error())
   284  	}
   285  }
   286  
   287  func TestIntegrationTokenCreateTransactionWithMaxLessThanMin(t *testing.T) {
   288  	t.Parallel()
   289  	env := NewIntegrationTestEnv(t)
   290  
   291  	_, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   292  		transaction.
   293  			SetCustomFees([]Fee{
   294  				CustomFractionalFee{
   295  					CustomFee: CustomFee{
   296  						FeeCollectorAccountID: &env.OperatorID,
   297  					},
   298  					Numerator:     1,
   299  					Denominator:   20,
   300  					MinimumAmount: 100,
   301  					MaximumAmount: 10,
   302  				},
   303  			})
   304  	})
   305  	assert.Error(t, err)
   306  	if err != nil {
   307  		assert.Equal(t, "exceptional receipt status: FRACTIONAL_FEE_MAX_AMOUNT_LESS_THAN_MIN_AMOUNT", err.Error())
   308  	}
   309  }
   310  
   311  func TestIntegrationTokenCreateTransactionWithRoyaltyCustomFee(t *testing.T) {
   312  	t.Parallel()
   313  	env := NewIntegrationTestEnv(t)
   314  
   315  	tokenID, err := createNft(&env, func(transaction *TokenCreateTransaction) {
   316  		transaction.
   317  			SetCustomFees([]Fee{
   318  				NewCustomRoyaltyFee().
   319  					SetFeeCollectorAccountID(env.OperatorID).
   320  					SetNumerator(1).
   321  					SetDenominator(20).
   322  					SetFallbackFee(
   323  						NewCustomFixedFee().
   324  							SetFeeCollectorAccountID(env.OperatorID).
   325  							SetAmount(10),
   326  					),
   327  			})
   328  	})
   329  	require.NoError(t, err)
   330  
   331  	err = CloseIntegrationTestEnv(env, &tokenID)
   332  	require.NoError(t, err)
   333  }
   334  
   335  func TestIntegrationTokenCreateTransactionWithRoyaltyCannotExceedOne(t *testing.T) {
   336  	t.Parallel()
   337  	env := NewIntegrationTestEnv(t)
   338  
   339  	_, err := createNft(&env, func(transaction *TokenCreateTransaction) {
   340  		transaction.
   341  			SetCustomFees([]Fee{
   342  				NewCustomRoyaltyFee().
   343  					SetFeeCollectorAccountID(env.OperatorID).
   344  					SetNumerator(2).
   345  					SetDenominator(1),
   346  			})
   347  	})
   348  	assert.Error(t, err)
   349  	if err != nil {
   350  		assert.Equal(t, "exceptional receipt status: ROYALTY_FRACTION_CANNOT_EXCEED_ONE", err.Error())
   351  	}
   352  }
   353  
   354  func TestIntegrationTokenCreateTransactionFeeCollectorMissing(t *testing.T) {
   355  	t.Parallel()
   356  	env := NewIntegrationTestEnv(t)
   357  
   358  	_, err := createNft(&env, func(transaction *TokenCreateTransaction) {
   359  		transaction.
   360  			SetCustomFees([]Fee{
   361  				NewCustomRoyaltyFee().
   362  					SetNumerator(1).
   363  					SetDenominator(20),
   364  			})
   365  	})
   366  	assert.Error(t, err)
   367  	if err != nil {
   368  		assert.Equal(t, "exceptional receipt status: INVALID_CUSTOM_FEE_COLLECTOR", err.Error())
   369  	}
   370  }
   371  
   372  func TestIntegrationTokenCreateTransactionRoyaltyFeeOnlyAllowedForNonFungibleUnique(t *testing.T) {
   373  	t.Parallel()
   374  	env := NewIntegrationTestEnv(t)
   375  
   376  	_, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   377  		transaction.
   378  			SetCustomFees([]Fee{
   379  				NewCustomRoyaltyFee().
   380  					SetFeeCollectorAccountID(env.OperatorID).
   381  					SetNumerator(1).
   382  					SetDenominator(20),
   383  			})
   384  	})
   385  	assert.Error(t, err)
   386  	if err != nil {
   387  		assert.Equal(t, "exceptional receipt status: CUSTOM_ROYALTY_FEE_ONLY_ALLOWED_FOR_NON_FUNGIBLE_UNIQUE", err.Error())
   388  	}
   389  }
   390  
   391  func TestIntegrationTokenAccountStillOwnsNfts(t *testing.T) {
   392  	t.Parallel()
   393  	env := NewIntegrationTestEnv(t)
   394  
   395  	newKey, err := PrivateKeyGenerateEd25519()
   396  	require.NoError(t, err)
   397  
   398  	newBalance := NewHbar(2)
   399  
   400  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   401  
   402  	resp, err := NewAccountCreateTransaction().
   403  		SetKey(newKey).
   404  		SetNodeAccountIDs(env.NodeAccountIDs).
   405  		SetInitialBalance(newBalance).
   406  		Execute(env.Client)
   407  	require.NoError(t, err)
   408  
   409  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   410  	require.NoError(t, err)
   411  
   412  	accountID := *receipt.AccountID
   413  
   414  	tokenID, err := createNft(&env, func(transaction *TokenCreateTransaction) {
   415  		transaction.
   416  			SetTreasuryAccountID(accountID).
   417  			SetAdminKey(newKey.PublicKey()).
   418  			SetFreezeKey(newKey.PublicKey()).
   419  			SetWipeKey(newKey.PublicKey()).
   420  			SetKycKey(newKey.PublicKey()).
   421  			SetSupplyKey(newKey.PublicKey()).
   422  			FreezeWith(env.Client)
   423  
   424  		transaction.Sign(newKey)
   425  	})
   426  	require.NoError(t, err)
   427  
   428  	metaData := make([]byte, 50, 101)
   429  
   430  	mintTx, err := NewTokenMintTransaction().
   431  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   432  		SetTokenID(tokenID).
   433  		SetMetadata(metaData).
   434  		FreezeWith(env.Client)
   435  	require.NoError(t, err)
   436  
   437  	mintTx.Sign(newKey)
   438  
   439  	mint, err := mintTx.Execute(env.Client)
   440  	require.NoError(t, err)
   441  
   442  	_, err = mint.SetValidateStatus(true).GetReceipt(env.Client)
   443  	require.NoError(t, err)
   444  
   445  	deleteTx, err := NewTokenDeleteTransaction().
   446  		SetNodeAccountIDs(env.NodeAccountIDs).
   447  		SetTokenID(tokenID).
   448  		FreezeWith(env.Client)
   449  	require.NoError(t, err)
   450  
   451  	deleteTx.Sign(newKey)
   452  
   453  	resp, err = deleteTx.Execute(env.Client)
   454  	require.NoError(t, err)
   455  
   456  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   457  	require.NoError(t, err)
   458  }
   459  
   460  func TestIntegrationTokenCreateTransactionMetadataKey(t *testing.T) {
   461  	t.Parallel()
   462  	env := NewIntegrationTestEnv(t)
   463  
   464  	newKey, err := PrivateKeyGenerateEd25519()
   465  	require.NoError(t, err)
   466  
   467  	pubKey := newKey.PublicKey()
   468  
   469  	newBalance := NewHbar(2)
   470  
   471  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   472  
   473  	resp, err := NewAccountCreateTransaction().
   474  		SetKey(pubKey).
   475  		SetNodeAccountIDs(env.NodeAccountIDs).
   476  		SetInitialBalance(newBalance).
   477  		Execute(env.Client)
   478  	require.NoError(t, err)
   479  
   480  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   481  	require.NoError(t, err)
   482  
   483  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   484  		transaction.SetMetadataKey(pubKey)
   485  	})
   486  	require.NoError(t, err)
   487  
   488  	info, err := NewTokenInfoQuery().
   489  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   490  		SetMaxQueryPayment(NewHbar(2)).
   491  		SetTokenID(tokenID).
   492  		SetQueryPayment(NewHbar(1)).
   493  		Execute(env.Client)
   494  
   495  	require.NoError(t, err)
   496  	assert.Equal(t, pubKey, info.MetadataKey)
   497  
   498  	err = CloseIntegrationTestEnv(env, &tokenID)
   499  }