github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_info_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 "testing" 28 "time" 29 30 protobuf "google.golang.org/protobuf/proto" 31 32 "github.com/hashgraph/hedera-protobufs-go/services" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func TestUnitContractInfoQueryValidate(t *testing.T) { 37 t.Parallel() 38 39 client, err := _NewMockClient() 40 client.SetLedgerID(*NewLedgerIDTestnet()) 41 require.NoError(t, err) 42 client.SetAutoValidateChecksums(true) 43 contractID, err := ContractIDFromString("0.0.123-esxsf") 44 require.NoError(t, err) 45 46 contractInfoQuery := NewContractInfoQuery(). 47 SetContractID(contractID) 48 49 err = contractInfoQuery.validateNetworkOnIDs(client) 50 require.NoError(t, err) 51 } 52 53 func TestUnitContractInfoQueryValidateWrong(t *testing.T) { 54 t.Parallel() 55 56 client, err := _NewMockClient() 57 client.SetLedgerID(*NewLedgerIDTestnet()) 58 require.NoError(t, err) 59 client.SetAutoValidateChecksums(true) 60 contractID, err := ContractIDFromString("0.0.123-rmkykd") 61 require.NoError(t, err) 62 63 contractInfoQuery := NewContractInfoQuery(). 64 SetContractID(contractID) 65 66 err = contractInfoQuery.validateNetworkOnIDs(client) 67 require.Error(t, err) 68 if err != nil { 69 require.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 70 } 71 } 72 73 func TestUnitContractInfoQueryMock(t *testing.T) { 74 t.Parallel() 75 76 responses := [][]interface{}{{ 77 &services.Response{ 78 Response: &services.Response_ContractGetInfo{ 79 ContractGetInfo: &services.ContractGetInfoResponse{ 80 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 81 }, 82 }, 83 }, 84 &services.Response{ 85 Response: &services.Response_ContractGetInfo{ 86 ContractGetInfo: &services.ContractGetInfoResponse{ 87 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 88 }, 89 }, 90 }, 91 &services.Response{ 92 Response: &services.Response_ContractGetInfo{ 93 ContractGetInfo: &services.ContractGetInfoResponse{ 94 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 95 ContractInfo: &services.ContractGetInfoResponse_ContractInfo{ 96 ContractID: &services.ContractID{Contract: &services.ContractID_ContractNum{ContractNum: 3}}, 97 AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 4}}, 98 ContractAccountID: "", 99 AdminKey: nil, 100 ExpirationTime: nil, 101 AutoRenewPeriod: nil, 102 Storage: 0, 103 Memo: "yes", 104 Balance: 0, 105 Deleted: false, 106 TokenRelationships: nil, 107 LedgerId: nil, 108 }, 109 }, 110 }, 111 }, 112 }} 113 114 client, server := NewMockClientAndServer(responses) 115 defer server.Close() 116 117 query := NewContractInfoQuery(). 118 SetContractID(ContractID{Contract: 3}). 119 SetMaxQueryPayment(NewHbar(1)). 120 SetNodeAccountIDs([]AccountID{{Account: 3}}) 121 122 cost, err := query.GetCost(client) 123 require.NoError(t, err) 124 require.Equal(t, cost, HbarFromTinybar(2)) 125 126 result, err := query.Execute(client) 127 require.NoError(t, err) 128 129 require.Equal(t, result.ContractID.Contract, uint64(3)) 130 require.Equal(t, result.AccountID.Account, uint64(4)) 131 require.Equal(t, result.ContractMemo, "yes") 132 } 133 134 func TestUnitContractInfoQueryGetTransactionIDMock(t *testing.T) { 135 t.Skip("Skipping test as it is currently broken with the addition of generating new payment transactions for queries") 136 t.Parallel() 137 138 transactionID := TransactionIDGenerate(AccountID{Account: 123}) 139 call := func(request *services.Query) *services.Response { 140 if query, ok := request.Query.(*services.Query_ContractGetInfo); ok { 141 paymentTransacction := query.ContractGetInfo.Header.Payment 142 143 require.NotEmpty(t, paymentTransacction.BodyBytes) 144 transactionBody := services.TransactionBody{} 145 _ = protobuf.Unmarshal(paymentTransacction.BodyBytes, &transactionBody) 146 147 require.NotNil(t, transactionBody.TransactionID) 148 tempTransactionID := _TransactionIDFromProtobuf(transactionBody.TransactionID) 149 require.Equal(t, transactionID.String(), tempTransactionID.String()) 150 } 151 152 return &services.Response{ 153 Response: &services.Response_ContractGetInfo{ 154 ContractGetInfo: &services.ContractGetInfoResponse{ 155 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 156 ContractInfo: &services.ContractGetInfoResponse_ContractInfo{ 157 ContractID: &services.ContractID{Contract: &services.ContractID_ContractNum{ContractNum: 3}}, 158 AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 4}}, 159 ContractAccountID: "", 160 AdminKey: nil, 161 ExpirationTime: nil, 162 AutoRenewPeriod: nil, 163 Storage: 0, 164 Memo: "yes", 165 Balance: 0, 166 Deleted: false, 167 TokenRelationships: nil, 168 LedgerId: nil, 169 }, 170 }, 171 }, 172 } 173 } 174 responses := [][]interface{}{{ 175 call, 176 }} 177 178 client, server := NewMockClientAndServer(responses) 179 180 result, err := NewContractInfoQuery(). 181 SetContractID(ContractID{Contract: 3}). 182 SetMaxQueryPayment(NewHbar(1)). 183 SetPaymentTransactionID(transactionID). 184 SetQueryPayment(HbarFromTinybar(25)). 185 SetNodeAccountIDs([]AccountID{{Account: 3}}). 186 Execute(client) 187 require.NoError(t, err) 188 189 require.Equal(t, result.ContractID.Contract, uint64(3)) 190 require.Equal(t, result.AccountID.Account, uint64(4)) 191 require.Equal(t, result.ContractMemo, "yes") 192 193 server.Close() 194 } 195 196 func TestUnitContractInfoQueryGet(t *testing.T) { 197 t.Parallel() 198 199 spenderContractID := ContractID{Contract: 7} 200 201 balance := NewContractInfoQuery(). 202 SetContractID(spenderContractID). 203 SetQueryPayment(NewHbar(2)). 204 SetMaxQueryPayment(NewHbar(1)). 205 SetQueryPayment(HbarFromTinybar(25)). 206 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}) 207 208 require.Equal(t, spenderContractID, balance.GetContractID()) 209 require.Equal(t, []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}, balance.GetNodeAccountIDs()) 210 require.Equal(t, 250*time.Millisecond, balance.GetMinBackoff()) 211 require.Equal(t, 8*time.Second, balance.GetMaxBackoff()) 212 require.Equal(t, 10, balance.GetMaxRetryCount()) 213 require.Equal(t, TransactionID{}, balance.GetPaymentTransactionID()) 214 require.Equal(t, HbarFromTinybar(25), balance.GetQueryPayment()) 215 require.Equal(t, NewHbar(1), balance.GetMaxQueryPayment()) 216 217 } 218 219 func TestUnitContractInfoQueryCoverage(t *testing.T) { 220 t.Parallel() 221 222 checksum := "dmqui" 223 deadline := time.Second * 3 224 contract := ContractID{Contract: 3, checksum: &checksum} 225 nodeAccountID := []AccountID{{Account: 10}} 226 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 227 228 client, err := _NewMockClient() 229 client.SetLedgerID(*NewLedgerIDTestnet()) 230 require.NoError(t, err) 231 client.SetAutoValidateChecksums(true) 232 233 query := NewContractInfoQuery(). 234 SetMaxRetry(3). 235 SetMaxBackoff(time.Second * 30). 236 SetMinBackoff(time.Second * 10). 237 SetContractID(contract). 238 SetNodeAccountIDs(nodeAccountID). 239 SetPaymentTransactionID(transactionID). 240 SetMaxQueryPayment(NewHbar(23)). 241 SetQueryPayment(NewHbar(3)). 242 SetGrpcDeadline(&deadline) 243 244 err = query.validateNetworkOnIDs(client) 245 require.NoError(t, err) 246 247 require.Equal(t, nodeAccountID, query.GetNodeAccountIDs()) 248 require.Equal(t, time.Second*30, query.GetMaxBackoff()) 249 require.Equal(t, time.Second*10, query.GetMinBackoff()) 250 require.Equal(t, contract, query.GetContractID()) 251 require.Equal(t, &deadline, query.GetGrpcDeadline()) 252 }