github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_execute_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 TestIntegrationContractExecuteTransactionCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	// Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract
    40  	testContractByteCode := []byte(`608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033`)
    41  
    42  	resp, err := NewFileCreateTransaction().
    43  		SetKeys(env.Client.GetOperatorPublicKey()).
    44  		SetNodeAccountIDs(env.NodeAccountIDs).
    45  		SetContents(testContractByteCode).
    46  		Execute(env.Client)
    47  
    48  	require.NoError(t, err)
    49  
    50  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    51  	require.NoError(t, err)
    52  
    53  	fileID := *receipt.FileID
    54  	assert.NotNil(t, fileID)
    55  
    56  	resp, err = NewContractCreateTransaction().
    57  		SetAdminKey(env.Client.GetOperatorPublicKey()).
    58  		SetGas(200000).
    59  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    60  		SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
    61  		SetBytecodeFileID(fileID).
    62  		SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
    63  		Execute(env.Client)
    64  	require.NoError(t, err)
    65  
    66  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    67  	require.NoError(t, err)
    68  
    69  	assert.NotNil(t, receipt.ContractID)
    70  	contractID := *receipt.ContractID
    71  
    72  	resp, err = NewContractExecuteTransaction().
    73  		SetContractID(contractID).
    74  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    75  		SetGas(200000).
    76  		SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
    77  		Execute(env.Client)
    78  	require.NoError(t, err)
    79  
    80  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    81  	require.NoError(t, err)
    82  
    83  	resp, err = NewContractDeleteTransaction().
    84  		SetContractID(contractID).
    85  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    86  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    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  	resp, err = NewFileDeleteTransaction().
    94  		SetFileID(fileID).
    95  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    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, nil)
   103  	require.NoError(t, err)
   104  }
   105  
   106  func TestIntegrationContractExecuteTransactionNoContractID(t *testing.T) {
   107  	t.Parallel()
   108  	env := NewIntegrationTestEnv(t)
   109  
   110  	resp, err := NewContractExecuteTransaction().
   111  		SetGas(100000).
   112  		SetNodeAccountIDs(env.NodeAccountIDs).
   113  		SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
   114  		Execute(env.Client)
   115  
   116  	if err != nil {
   117  		assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_CONTRACT_ID received for transaction %s", resp.TransactionID), err.Error())
   118  	}
   119  }
   120  
   121  func TestIntegrationContractExecuteTransactionNoGas(t *testing.T) {
   122  	t.Parallel()
   123  	env := NewIntegrationTestEnv(t)
   124  
   125  	// Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract
   126  	testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)
   127  
   128  	resp, err := NewFileCreateTransaction().
   129  		SetKeys(env.Client.GetOperatorPublicKey()).
   130  		SetNodeAccountIDs(env.NodeAccountIDs).
   131  		SetContents(testContractByteCode).
   132  		Execute(env.Client)
   133  
   134  	require.NoError(t, err)
   135  
   136  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   137  	require.NoError(t, err)
   138  
   139  	fileID := *receipt.FileID
   140  	assert.NotNil(t, fileID)
   141  
   142  	node_ids := []AccountID{resp.NodeID}
   143  
   144  	resp, err = NewContractCreateTransaction().
   145  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   146  		SetGas(100000).
   147  		SetNodeAccountIDs(node_ids).
   148  		SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
   149  		SetBytecodeFileID(fileID).
   150  		SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
   151  		Execute(env.Client)
   152  	require.NoError(t, err)
   153  
   154  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   155  	require.NoError(t, err)
   156  
   157  	assert.NotNil(t, receipt.ContractID)
   158  	contractID := *receipt.ContractID
   159  
   160  	resp, err = NewContractExecuteTransaction().
   161  		SetContractID(contractID).
   162  		SetNodeAccountIDs(node_ids).
   163  		SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
   164  		Execute(env.Client)
   165  
   166  	assert.Error(t, err)
   167  	if err != nil {
   168  		assert.Contains(t, err.Error(), "INSUFFICIENT_GAS")
   169  	}
   170  
   171  	resp, err = NewContractDeleteTransaction().
   172  		SetContractID(contractID).
   173  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   174  		SetNodeAccountIDs(node_ids).
   175  		Execute(env.Client)
   176  	require.NoError(t, err)
   177  
   178  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   179  	require.NoError(t, err)
   180  
   181  	resp, err = NewFileDeleteTransaction().
   182  		SetFileID(fileID).
   183  		SetNodeAccountIDs(node_ids).
   184  		Execute(env.Client)
   185  	require.NoError(t, err)
   186  
   187  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   188  	require.NoError(t, err)
   189  
   190  	err = CloseIntegrationTestEnv(env, nil)
   191  	require.NoError(t, err)
   192  }
   193  
   194  func TestIntegrationContractExecuteTransactionNoFunction(t *testing.T) {
   195  	t.Parallel()
   196  	env := NewIntegrationTestEnv(t)
   197  
   198  	// Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract
   199  	testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)
   200  
   201  	resp, err := NewFileCreateTransaction().
   202  		SetKeys(env.Client.GetOperatorPublicKey()).
   203  		SetNodeAccountIDs(env.NodeAccountIDs).
   204  		SetContents(testContractByteCode).
   205  		Execute(env.Client)
   206  
   207  	require.NoError(t, err)
   208  
   209  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   210  	require.NoError(t, err)
   211  
   212  	fileID := *receipt.FileID
   213  	assert.NotNil(t, fileID)
   214  
   215  	resp, err = NewContractCreateTransaction().
   216  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   217  		SetGas(100000).
   218  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   219  		SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
   220  		SetBytecodeFileID(fileID).
   221  		SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
   222  		Execute(env.Client)
   223  	require.NoError(t, err)
   224  
   225  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   226  	require.NoError(t, err)
   227  
   228  	assert.NotNil(t, receipt.ContractID)
   229  	contractID := *receipt.ContractID
   230  
   231  	resp, err = NewContractExecuteTransaction().
   232  		SetContractID(contractID).
   233  		SetGas(100000).
   234  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   235  		Execute(env.Client)
   236  	require.NoError(t, err)
   237  
   238  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   239  	assert.Error(t, err)
   240  	if err != nil {
   241  		assert.Equal(t, "exceptional receipt status: CONTRACT_REVERT_EXECUTED", err.Error())
   242  	}
   243  
   244  	resp, err = NewContractDeleteTransaction().
   245  		SetContractID(contractID).
   246  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   247  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   248  		Execute(env.Client)
   249  	require.NoError(t, err)
   250  
   251  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   252  	require.NoError(t, err)
   253  
   254  	resp, err = NewFileDeleteTransaction().
   255  		SetFileID(fileID).
   256  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   257  		Execute(env.Client)
   258  	require.NoError(t, err)
   259  
   260  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   261  	require.NoError(t, err)
   262  
   263  	err = CloseIntegrationTestEnv(env, nil)
   264  	require.NoError(t, err)
   265  }
   266  
   267  func DisabledTestIntegrationContractExecuteTransactionID(t *testing.T) { // nolint
   268  	t.Parallel()
   269  	env := NewIntegrationTestEnv(t)
   270  
   271  	// Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract
   272  	testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)
   273  
   274  	resp, err := NewFileCreateTransaction().
   275  		SetKeys(env.Client.GetOperatorPublicKey()).
   276  		SetNodeAccountIDs(env.NodeAccountIDs).
   277  		SetContents(testContractByteCode).
   278  		Execute(env.Client)
   279  	require.NoError(t, err)
   280  
   281  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   282  	require.NoError(t, err)
   283  
   284  	fileID := *receipt.FileID
   285  	assert.NotNil(t, fileID)
   286  
   287  	transactionID := resp.TransactionID
   288  	assert.NotNil(t, transactionID)
   289  
   290  	nodeIDs := make([]AccountID, 1)
   291  	nodeIDs[0] = resp.NodeID
   292  
   293  	resp, err = NewContractCreateTransaction().
   294  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   295  		SetGas(100000).
   296  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   297  		SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
   298  		SetBytecodeFileID(fileID).
   299  		SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
   300  		Execute(env.Client)
   301  	require.NoError(t, err)
   302  
   303  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   304  	require.NoError(t, err)
   305  
   306  	assert.NotNil(t, receipt.ContractID)
   307  	contractID := *receipt.ContractID
   308  
   309  	resp, err = NewContractExecuteTransaction().
   310  		SetContractID(contractID).
   311  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   312  		SetGas(100000).
   313  		SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
   314  		Execute(env.Client)
   315  	require.NoError(t, err)
   316  
   317  	record, err := resp.GetRecord(env.Client)
   318  	require.NoError(t, err)
   319  	assert.NotNil(t, record)
   320  
   321  	receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   322  	require.NoError(t, err)
   323  	_, err = record.GetContractExecuteResult()
   324  	require.NoError(t, err)
   325  
   326  	resp, err = NewContractDeleteTransaction().
   327  		SetContractID(contractID).
   328  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   329  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   330  		Execute(env.Client)
   331  	require.NoError(t, err)
   332  
   333  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   334  	require.NoError(t, err)
   335  
   336  	resp, err = NewFileDeleteTransaction().
   337  		SetFileID(fileID).
   338  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   339  		Execute(env.Client)
   340  	require.NoError(t, err)
   341  
   342  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   343  	require.NoError(t, err)
   344  
   345  	err = CloseIntegrationTestEnv(env, nil)
   346  	require.NoError(t, err)
   347  }