Skip to content

Commit

Permalink
feat: Added contract_execute_transaction_test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 authored and janaakhterov committed Oct 29, 2020
1 parent 1447649 commit a9cec57
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions contract_execute_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package hedera

import (
"github.com/stretchr/testify/assert"
"os"

"testing"
)

func TestContractExecuteTransaction_Execute(t *testing.T) {
// Note: this is the bytecode for the contract found in the example for ./examples/create_simple_contract
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
}

configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")

if configOperatorID != "" && configOperatorKey != "" {
operatorAccountID, err := AccountIDFromString(configOperatorID)
assert.NoError(t, err)

operatorKey, err := PrivateKeyFromString(configOperatorKey)
assert.NoError(t, err)

client.SetOperator(operatorAccountID, operatorKey)
}

resp, err := NewFileCreateTransaction().
SetKeys(client.GetOperatorKey()).
SetContents(testContractByteCode).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)

assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

fileID := *receipt.FileID
assert.NotNil(t, fileID)

nodeIDs := make([]AccountID, 1)
nodeIDs[0] = resp.NodeID

resp, err = NewContractCreateTransaction().
SetAdminKey(client.GetOperatorKey()).
SetGas(2000).
SetNodeAccountIDs(nodeIDs).
SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

receipt, err = resp.GetReceipt(client)
assert.NoError(t, err)

contractID := *receipt.ContractID
assert.NotNil(t, contractID)

resp, err = NewContractExecuteTransaction().
SetContractID(contractID).
SetNodeAccountIDs(nodeIDs).
SetGas(10000).
SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetNodeAccountIDs(nodeIDs).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetNodeAccountIDs(nodeIDs).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

0 comments on commit a9cec57

Please sign in to comment.