github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_call_query_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 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 "bytes" 28 "testing" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 "google.golang.org/protobuf/types/known/wrapperspb" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitContractCallQueryValidate(t *testing.T) { 39 t.Parallel() 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 client.SetAutoValidateChecksums(true) 45 contractID, err := ContractIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 48 contractCall := NewContractCallQuery(). 49 SetContractID(contractID) 50 51 err = contractCall.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitContractCallQueryValidateWrong(t *testing.T) { 56 t.Parallel() 57 58 client, err := _NewMockClient() 59 client.SetLedgerID(*NewLedgerIDTestnet()) 60 require.NoError(t, err) 61 client.SetAutoValidateChecksums(true) 62 contractID, err := ContractIDFromString("0.0.123-rmkykd") 63 require.NoError(t, err) 64 65 contractCall := NewContractCallQuery(). 66 SetContractID(contractID) 67 68 err = contractCall.validateNetworkOnIDs(client) 69 assert.Error(t, err) 70 if err != nil { 71 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 72 } 73 } 74 75 func TestUnitMockContractCallQuery(t *testing.T) { 76 t.Parallel() 77 78 message := "getMessage" 79 params := NewContractFunctionParameters() 80 params._Build(&message) 81 82 responses := [][]interface{}{{ 83 &services.Response{ 84 Response: &services.Response_ContractCallLocal{ 85 ContractCallLocal: &services.ContractCallLocalResponse{ 86 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY, ResponseType: services.ResponseType_ANSWER_ONLY}, 87 }, 88 }, 89 }, 90 &services.Response{ 91 Response: &services.Response_ContractCallLocal{ 92 ContractCallLocal: &services.ContractCallLocalResponse{ 93 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 94 FunctionResult: &services.ContractFunctionResult{ 95 ContractID: &services.ContractID{Contract: &services.ContractID_ContractNum{ContractNum: 123}}, 96 GasUsed: 75000, 97 ContractCallResult: params._Build(&message), 98 SignerNonce: wrapperspb.Int64(0), 99 }, 100 }, 101 }, 102 }, 103 }} 104 105 client, server := NewMockClientAndServer(responses) 106 defer server.Close() 107 108 result, err := NewContractCallQuery(). 109 SetNodeAccountIDs([]AccountID{{Account: 3}}). 110 SetContractID(ContractID{Contract: 123}). 111 SetQueryPayment(NewHbar(1)). 112 SetGas(100000). 113 SetFunction(message, nil). 114 SetMaxQueryPayment(NewHbar(5)). 115 Execute(client) 116 require.NoError(t, err) 117 118 require.Equal(t, bytes.Compare(result.ContractCallResult, params._Build(&message)), 0) 119 require.Equal(t, result.GasUsed, uint64(75000)) 120 require.Equal(t, result.ContractID.Contract, uint64(123)) 121 } 122 123 func TestUnitContractCallQueryGet(t *testing.T) { 124 t.Parallel() 125 126 spenderContractID := ContractID{Contract: 7} 127 128 balance := NewContractCallQuery(). 129 SetContractID(spenderContractID). 130 SetQueryPayment(NewHbar(2)). 131 SetGas(100000). 132 SetFunction("getMessage", nil). 133 SetFunctionParameters([]byte{}). 134 SetMaxQueryPayment(NewHbar(10)). 135 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}) 136 137 balance.GetContractID() 138 balance.GetFunctionParameters() 139 balance.GetNodeAccountIDs() 140 balance.GetMinBackoff() 141 balance.GetMaxBackoff() 142 balance.GetMaxRetryCount() 143 balance.GetPaymentTransactionID() 144 balance.GetQueryPayment() 145 balance.GetMaxQueryPayment() 146 } 147 148 func TestUnitContractCallQuerySetNothing(t *testing.T) { 149 t.Parallel() 150 151 balance := NewContractCallQuery() 152 153 balance.GetContractID() 154 balance.GetFunctionParameters() 155 balance.GetNodeAccountIDs() 156 balance.GetMinBackoff() 157 balance.GetMaxBackoff() 158 balance.GetMaxRetryCount() 159 balance.GetPaymentTransactionID() 160 balance.GetQueryPayment() 161 balance.GetMaxQueryPayment() 162 }