github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_grant_kyc_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 TestIntegrationTokenGrantKycTransactionCanExecute(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  		SetNodeAccountIDs(env.NodeAccountIDs).
    48  		SetKey(newKey.PublicKey()).
    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, func(transaction *TokenCreateTransaction) {
    59  		transaction.SetKycKey(env.Client.GetOperatorPublicKey())
    60  	})
    61  	require.NoError(t, err)
    62  
    63  	transaction, err := NewTokenAssociateTransaction().
    64  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    65  		SetAccountID(accountID).
    66  		SetTokenIDs(tokenID).
    67  		FreezeWith(env.Client)
    68  	require.NoError(t, err)
    69  
    70  	resp, err = transaction.
    71  		Sign(newKey).
    72  		Execute(env.Client)
    73  	require.NoError(t, err)
    74  
    75  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    76  	require.NoError(t, err)
    77  
    78  	resp, err = NewTokenGrantKycTransaction().
    79  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    80  		SetAccountID(accountID).
    81  		SetTokenID(tokenID).
    82  		Execute(env.Client)
    83  	require.NoError(t, err)
    84  
    85  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    86  	require.NoError(t, err)
    87  
    88  	tx, err := NewAccountDeleteTransaction().
    89  		SetAccountID(accountID).
    90  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    91  		FreezeWith(env.Client)
    92  	require.NoError(t, err)
    93  
    94  	resp, err = tx.
    95  		Sign(newKey).
    96  		Execute(env.Client)
    97  	require.NoError(t, err)
    98  
    99  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   100  	require.NoError(t, err)
   101  
   102  	err = CloseIntegrationTestEnv(env, &tokenID)
   103  	require.NoError(t, err)
   104  }
   105  
   106  func TestIntegrationTokenGrantKycTransactionNoTokenID(t *testing.T) {
   107  	t.Parallel()
   108  	env := NewIntegrationTestEnv(t)
   109  
   110  	newKey, err := PrivateKeyGenerateEd25519()
   111  	require.NoError(t, err)
   112  
   113  	newBalance := NewHbar(2)
   114  
   115  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   116  
   117  	resp, err := NewAccountCreateTransaction().
   118  		SetNodeAccountIDs(env.NodeAccountIDs).
   119  		SetKey(newKey.PublicKey()).
   120  		SetInitialBalance(newBalance).
   121  		Execute(env.Client)
   122  	require.NoError(t, err)
   123  
   124  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   125  	require.NoError(t, err)
   126  
   127  	accountID := *receipt.AccountID
   128  
   129  	tokenID, err := createFungibleToken(&env)
   130  	require.NoError(t, err)
   131  
   132  	transaction, err := NewTokenAssociateTransaction().
   133  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   134  		SetAccountID(accountID).
   135  		SetTokenIDs(tokenID).
   136  		FreezeWith(env.Client)
   137  	require.NoError(t, err)
   138  
   139  	resp, err = transaction.
   140  		Sign(newKey).
   141  		Execute(env.Client)
   142  	require.NoError(t, err)
   143  
   144  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   145  	require.NoError(t, err)
   146  
   147  	resp2, err := NewTokenGrantKycTransaction().
   148  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   149  		SetAccountID(accountID).
   150  		Execute(env.Client)
   151  	assert.Error(t, err)
   152  	if err != nil {
   153  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_TOKEN_ID received for transaction %s", resp2.TransactionID), err.Error())
   154  	}
   155  
   156  	tx, err := NewAccountDeleteTransaction().
   157  		SetAccountID(accountID).
   158  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   159  		FreezeWith(env.Client)
   160  	require.NoError(t, err)
   161  
   162  	resp, err = tx.
   163  		Sign(newKey).
   164  		Execute(env.Client)
   165  	require.NoError(t, err)
   166  
   167  	err = CloseIntegrationTestEnv(env, &tokenID)
   168  	require.NoError(t, err)
   169  }
   170  
   171  func TestIntegrationTokenGrantKycTransactionNoAccountID(t *testing.T) {
   172  	t.Parallel()
   173  	env := NewIntegrationTestEnv(t)
   174  
   175  	newKey, err := PrivateKeyGenerateEd25519()
   176  	require.NoError(t, err)
   177  
   178  	newBalance := NewHbar(2)
   179  
   180  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   181  
   182  	resp, err := NewAccountCreateTransaction().
   183  		SetNodeAccountIDs(env.NodeAccountIDs).
   184  		SetKey(newKey.PublicKey()).
   185  		SetInitialBalance(newBalance).
   186  		Execute(env.Client)
   187  	require.NoError(t, err)
   188  
   189  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   190  	require.NoError(t, err)
   191  
   192  	accountID := *receipt.AccountID
   193  
   194  	tokenID, err := createFungibleToken(&env)
   195  	require.NoError(t, err)
   196  
   197  	transaction, err := NewTokenAssociateTransaction().
   198  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   199  		SetAccountID(accountID).
   200  		SetTokenIDs(tokenID).
   201  		FreezeWith(env.Client)
   202  	require.NoError(t, err)
   203  
   204  	resp, err = transaction.
   205  		Sign(newKey).
   206  		Execute(env.Client)
   207  	require.NoError(t, err)
   208  
   209  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   210  	require.NoError(t, err)
   211  
   212  	resp2, err := NewTokenGrantKycTransaction().
   213  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   214  		SetTokenID(tokenID).
   215  		Execute(env.Client)
   216  	assert.Error(t, err)
   217  	if err != nil {
   218  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID received for transaction %s", resp2.TransactionID), err.Error())
   219  	}
   220  
   221  	tx, err := NewAccountDeleteTransaction().
   222  		SetAccountID(accountID).
   223  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   224  		FreezeWith(env.Client)
   225  	require.NoError(t, err)
   226  
   227  	resp, err = tx.
   228  		Sign(newKey).
   229  		Execute(env.Client)
   230  	require.NoError(t, err)
   231  
   232  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   233  	require.NoError(t, err)
   234  
   235  	err = CloseIntegrationTestEnv(env, &tokenID)
   236  	require.NoError(t, err)
   237  }
   238  
   239  func TestIntegrationTokenGrantKycTransactionNoKycSet(t *testing.T) {
   240  	t.Parallel()
   241  	env := NewIntegrationTestEnv(t)
   242  
   243  	newKey, err := PrivateKeyGenerateEd25519()
   244  	require.NoError(t, err)
   245  
   246  	newBalance := NewHbar(2)
   247  
   248  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   249  
   250  	resp, err := NewAccountCreateTransaction().
   251  		SetNodeAccountIDs(env.NodeAccountIDs).
   252  		SetKey(newKey.PublicKey()).
   253  		SetInitialBalance(newBalance).
   254  		Execute(env.Client)
   255  	require.NoError(t, err)
   256  
   257  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   258  	require.NoError(t, err)
   259  
   260  	accountID := *receipt.AccountID
   261  
   262  	tokenID, err := createFungibleToken(&env, func(transaction *TokenCreateTransaction) {
   263  		transaction.SetKycKey(nil)
   264  	})
   265  	require.NoError(t, err)
   266  
   267  	transaction, err := NewTokenAssociateTransaction().
   268  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   269  		SetAccountID(accountID).
   270  		SetTokenIDs(tokenID).
   271  		FreezeWith(env.Client)
   272  	require.NoError(t, err)
   273  
   274  	resp, err = transaction.
   275  		Sign(newKey).
   276  		Execute(env.Client)
   277  	require.NoError(t, err)
   278  
   279  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   280  	require.NoError(t, err)
   281  
   282  	resp, err = NewTokenGrantKycTransaction().
   283  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   284  		SetAccountID(accountID).
   285  		SetTokenID(tokenID).
   286  		Execute(env.Client)
   287  	require.NoError(t, err)
   288  
   289  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   290  	assert.Error(t, err)
   291  	assert.Equal(t, "exceptional receipt status: TOKEN_HAS_NO_KYC_KEY", err.Error())
   292  
   293  	tx, err := NewAccountDeleteTransaction().
   294  		SetAccountID(accountID).
   295  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   296  		FreezeWith(env.Client)
   297  	require.NoError(t, err)
   298  
   299  	resp, err = tx.
   300  		Sign(newKey).
   301  		Execute(env.Client)
   302  	require.NoError(t, err)
   303  
   304  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   305  	require.NoError(t, err)
   306  
   307  	err = CloseIntegrationTestEnv(env, &tokenID)
   308  	require.NoError(t, err)
   309  }