Skip to content

Commit

Permalink
contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Mar 28, 2020
1 parent 439c01e commit 444d1fc
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 0 deletions.
57 changes: 57 additions & 0 deletions contract_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -31,3 +32,59 @@ func TestSerializeContractCreateTransaction(t *testing.T) {

assert.Equal(t, `bodyBytes:"\n\016\n\010\010\334\311\007\020\333\237\t\022\002\030\003\022\002\030\003\030\300\204=\"\002\010xB7\n\002\030\004\032\"\022\344\361\300\353L}\315\303\347\353\021p\263\010\212=\022\242\227\364\243\353\342\362\205\003\375g5F\355\216d(\350\0072\002\030\003B\004\010\200\352I"sigMap:<sigPair:<pubKeyPrefix:"\344\361\300\353L}\315\303\347\353\021p\263\010\212=\022\242\227\364\243\353\342\362\205\003\375g5F\355\216"ed25519:"\335\035\364\306\364\013\204\232\376\343\320b\270\310\017I\020\223\306K\316\264\277\363x\251\3418\004>\352\272\264\202\343\260\344\264\344<l\004H\200\253m\306\305)\304t\245\227z\305\267\270w\353\326\255\340\374\005">>transactionID:<transactionValidStart:<seconds:124124nanos:151515>accountID:<accountNum:3>>nodeAccountID:<accountNum:3>transactionFee:1000000transactionValidDuration:<seconds:120>contractCreateInstance:<fileID:<fileNum:4>adminKey:<ed25519:"\344\361\300\353L}\315\303\347\353\021p\263\010\212=\022\242\227\364\243\353\342\362\205\003\375g5F\355\216">gas:100initialBalance:1000proxyAccountID:<accountNum:3>autoRenewPeriod:<seconds:1209600>>`, strings.ReplaceAll(strings.ReplaceAll(tx.String(), " ", ""), "\n", ""))
}

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

operatorAccountID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
assert.NoError(t, err)

operatorPrivateKey, err := Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
assert.NoError(t, err)

client := ClientForTestnet().
SetOperator(operatorAccountID, operatorPrivateKey)

txID, err := NewFileCreateTransaction().
AddKey(operatorPrivateKey.PublicKey()).
SetContents(testContractByteCode).
SetMaxTransactionFee(NewHbar(3)).
Execute(client)
assert.NoError(t, err)

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

fileID := receipt.GetFileID()
assert.NotNil(t, fileID)

txID, err = NewContractCreateTransaction().
SetAdminKey(operatorPrivateKey.PublicKey()).
SetGas(2000).
SetConstructorParams(NewContractFunctionParams().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractCreateTransaction_Execute").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

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

contractID := receipt.GetContractID()
assert.NotNil(t, contractID)

_, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

_, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

}
70 changes: 70 additions & 0 deletions contract_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -67,3 +68,72 @@ func TestSerializeContractDeleteTransaction_WithContractIDObtainer(t *testing.T)

assert.Equal(t, `bodyBytes:"\n\016\n\010\010\334\311\007\020\333\237\t\022\002\030\003\022\002\030\003\030\300\204=\"\002\010x\262\001\010\n\002\030\005\032\002\030\003"sigMap:<sigPair:<pubKeyPrefix:"\344\361\300\353L}\315\303\347\353\021p\263\010\212=\022\242\227\364\243\353\342\362\205\003\375g5F\355\216"ed25519:"\265\353ah\312\304mn\206ul\234\341F[pJ\"\342\352\220&wl\315\310UD\352:$GQ\326U\204\003\177\204\215\315k\277\342\376W]\377\312\037\237D\230aa\032\370>t\203\345\310c\017">>transactionID:<transactionValidStart:<seconds:124124nanos:151515>accountID:<accountNum:3>>nodeAccountID:<accountNum:3>transactionFee:1000000transactionValidDuration:<seconds:120>contractDeleteInstance:<contractID:<contractNum:5>transferContractID:<contractNum:3>>`, strings.ReplaceAll(strings.ReplaceAll(tx.String(), " ", ""), "\n", ""))
}

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

operatorAccountID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
assert.NoError(t, err)

operatorPrivateKey, err := Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
assert.NoError(t, err)

client := ClientForTestnet().
SetOperator(operatorAccountID, operatorPrivateKey)

txID, err := NewFileCreateTransaction().
AddKey(operatorPrivateKey.PublicKey()).
SetContents(testContractByteCode).
SetMaxTransactionFee(NewHbar(3)).
Execute(client)
assert.NoError(t, err)

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

fileID := receipt.GetFileID()
assert.NotNil(t, fileID)

txID, err = NewContractCreateTransaction().
SetAdminKey(operatorPrivateKey.PublicKey()).
SetGas(2000).
SetConstructorParams(NewContractFunctionParams().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

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

contractID := receipt.GetContractID()
assert.NotNil(t, contractID)

txID, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

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

_, err = NewContractInfoQuery().
SetContractID(contractID).
SetMaxQueryPayment(NewHbar(2)).
Execute(client)
// an error should occur if the contract was properly deleted
assert.Error(t, err)

status := err.(ErrHederaPreCheckStatus).Status
assert.Equal(t, status, StatusContractDeleted)

_, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

}
93 changes: 93 additions & 0 deletions contract_execute_transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package hedera

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -31,3 +33,94 @@ func TestSerializeContractExecuteTransaction(t *testing.T) {

assert.Equal(t, `bodyBytes:"\n\016\n\010\010\334\311\007\020\333\237\t\022\002\030\003\022\002\030\003\030\300\204=\"\002\010x:p\n\002\030\005\020\215\001\030\220N\"d7Q\372\261\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003\030+\013\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"sigMap:<sigPair:<pubKeyPrefix:"\344\361\300\353L}\315\303\347\353\021p\263\010\212=\022\242\227\364\243\353\342\362\205\003\375g5F\355\216"ed25519:"pHk\026\274\217\017\216\235\324\220\275\366\236\304a\371\021No\030@R\360\225\035^\325a\377\024\246Y\373\023\204\330\3079M$:\252\277\341\264\013\037Q\037\311\310\214^\n\326\003\320O\001\215R\217\017">>transactionID:<transactionValidStart:<seconds:124124nanos:151515>accountID:<accountNum:3>>nodeAccountID:<accountNum:3>transactionFee:1000000transactionValidDuration:<seconds:120>contractCall:<contractID:<contractNum:5>gas:141amount:10000functionParameters:"7Q\372\261\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003\030+\013\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000">`, strings.ReplaceAll(strings.ReplaceAll(tx.String(), " ", ""), "\n", ""))
}

func TestContractExecuteTransaction_Execute(t *testing.T) {
type contract struct {
Abi string `json:"abi"`
Bin string `json:"bin"`
}

type contracts struct {
Contracts map[string]contract `json:"contracts"`
Version string `json:"version"`
}
var smartContract contracts = contracts{}

// Note: this is the same contract used for the example in ./examples/create_stateful_contract
rawContract := `{"contracts":{"stateful.sol:StatefulContract":{"abi":"[{\"inputs\":[{\"internalType\":\"string\",\"name\":\"message_\",\"type\":\"string\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[],\"name\":\"getMessage\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"kill\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"message_\",\"type\":\"string\"}],\"name\":\"setMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]","bin":"608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033"}},"version":"0.6.0+commit.26b70077.Linux.g++"}`

err := json.Unmarshal([]byte(rawContract), &smartContract)
assert.NoError(t, err)

testContractByteCode := []byte(smartContract.Contracts["stateful.sol:StatefulContract"].Bin)

operatorAccountID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
assert.NoError(t, err)

operatorPrivateKey, err := Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
assert.NoError(t, err)

client := ClientForTestnet().
SetOperator(operatorAccountID, operatorPrivateKey)

txID, err := NewFileCreateTransaction().
AddKey(operatorPrivateKey.PublicKey()).
SetContents(testContractByteCode).
SetMaxTransactionFee(NewHbar(3)).
Execute(client)
assert.NoError(t, err)

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

fileID := receipt.GetFileID()
assert.NotNil(t, fileID)

txID, err = NewContractCreateTransaction().
SetAdminKey(operatorPrivateKey.PublicKey()).
SetGas(2000).
SetConstructorParams(NewContractFunctionParams().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractExecuteTransaction_Execute").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

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

contractID := receipt.GetContractID()
assert.NotNil(t, contractID)

contractFunctionParams := NewContractFunctionParams().
AddString("Hello from Hedera again!")

txID, err = NewContractExecuteTransaction().
SetContractID(contractID).
SetMaxTransactionFee(NewHbar(5)).
SetGas(7000).
SetFunction("setMessage", contractFunctionParams).
SetTransactionMemo("hedera-sdk-go::TestContractExecuteTransaction_Execute").
Execute(client)
assert.NoError(t, err)

record, err := txID.GetRecord(client)
assert.NoError(t, err)

result, err := record.GetContractExecuteResult()
assert.NoError(t, err)

assert.NotZero(t, result.GasUsed)

_, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

_, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)
}
65 changes: 65 additions & 0 deletions contract_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
Expand All @@ -16,3 +17,67 @@ func TestNewContractInfoQuery(t *testing.T) {

assert.Equal(t, `contractGetInfo:{header:{payment:{bodyBytes:"\n\x0e\n\x08\x08\xdc\xc9\x07\x10۟\t\x12\x02\x18\x03\x12\x02\x18\x03\x18\x80\xc2\xd7/\"\x02\x08xr\x14\n\x12\n\x07\n\x02\x18\x02\x10\xc7\x01\n\x07\n\x02\x18\x03\x10\xc8\x01"sigMap:{sigPair:{pubKeyPrefix:"\xe4\xf1\xc0\xebL}\xcd\xc3\xe7\xeb\x11p\xb3\x08\x8a=\x12\xa2\x97\xf4\xa3\xeb\xe2\xf2\x85\x03\xfdg5F\xed\x8e"ed25519:"\x12&5\x96\xfb\xb4\x1c]P\xbb%\xecP\x9bk͙\x0b߼\xac)\xa6+\xd2<\x97+\xbb\x8c\x8af\xcb\xdai\x17T4{\xf7\xf3UYn\n\x8f\xabep\x04\xf6\x83\x0f\xbaFUP\xa3\xd1/\x1d\x9d\x1a\x0b"}}}}contractID:{contractNum:3}}`, strings.ReplaceAll(query.QueryBuilder.pb.String(), " ", ""))
}

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

operatorAccountID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
assert.NoError(t, err)

operatorPrivateKey, err := Ed25519PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
assert.NoError(t, err)

client := ClientForTestnet().
SetOperator(operatorAccountID, operatorPrivateKey)

txID, err := NewFileCreateTransaction().
AddKey(operatorPrivateKey.PublicKey()).
SetContents(testContractByteCode).
SetMaxTransactionFee(NewHbar(3)).
Execute(client)
assert.NoError(t, err)

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

fileID := receipt.GetFileID()
assert.NotNil(t, fileID)

txID, err = NewContractCreateTransaction().
SetAdminKey(operatorPrivateKey.PublicKey()).
SetGas(2000).
SetConstructorParams(NewContractFunctionParams().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

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

contractID := receipt.GetContractID()
assert.NotNil(t, contractID)

info, err := NewContractInfoQuery().
SetContractID(contractID).
SetMaxQueryPayment(NewHbar(2)).
Execute(client)

assert.Equal(t, contractID, info.ContractID)
assert.Equal(t, operatorPrivateKey.PublicKey(), info.AdminKey)
assert.Equal(t, "hedera-sdk-go::TestContractInfoQuery_Execute", info.ContractMemo)

_, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

_, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)
}

0 comments on commit 444d1fc

Please sign in to comment.