github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_info_query_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 "testing" 28 29 "github.com/stretchr/testify/assert" 30 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestIntegrationContractInfoQueryCanExecute(t *testing.T) { 35 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 36 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 37 38 t.Parallel() 39 env := NewIntegrationTestEnv(t) 40 41 resp, err := NewFileCreateTransaction(). 42 SetNodeAccountIDs(env.NodeAccountIDs). 43 SetKeys(env.Client.GetOperatorPublicKey()). 44 SetContents(testContractByteCode). 45 Execute(env.Client) 46 require.NoError(t, err) 47 48 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 49 require.NoError(t, err) 50 51 fileID := *receipt.FileID 52 assert.NotNil(t, fileID) 53 54 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 55 require.NoError(t, err) 56 57 resp, err = NewContractCreateTransaction(). 58 SetAdminKey(env.Client.GetOperatorPublicKey()). 59 SetGas(100000). 60 SetNodeAccountIDs([]AccountID{resp.NodeID}). 61 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 62 SetBytecodeFileID(fileID). 63 SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute"). 64 Execute(env.Client) 65 require.NoError(t, err) 66 67 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 68 require.NoError(t, err) 69 70 assert.NotNil(t, receipt.ContractID) 71 contractID := *receipt.ContractID 72 73 info, err := NewContractInfoQuery(). 74 SetContractID(contractID). 75 SetMaxQueryPayment(NewHbar(1)). 76 SetQueryPayment(HbarFromTinybar(25)). 77 SetNodeAccountIDs([]AccountID{resp.NodeID}). 78 Execute(env.Client) 79 require.NoError(t, err) 80 81 assert.Equal(t, contractID, info.ContractID) 82 assert.Equal(t, env.Client.GetOperatorPublicKey(), info.AdminKey) 83 assert.Equal(t, "hedera-sdk-go::TestContractInfoQuery_Execute", info.ContractMemo) 84 85 resp, err = NewContractDeleteTransaction(). 86 SetContractID(contractID). 87 SetTransferAccountID(env.Client.GetOperatorAccountID()). 88 SetNodeAccountIDs([]AccountID{resp.NodeID}). 89 Execute(env.Client) 90 require.NoError(t, err) 91 92 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 93 require.NoError(t, err) 94 95 resp, err = NewFileDeleteTransaction(). 96 SetFileID(fileID). 97 SetNodeAccountIDs([]AccountID{resp.NodeID}). 98 Execute(env.Client) 99 require.NoError(t, err) 100 101 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 102 require.NoError(t, err) 103 104 err = CloseIntegrationTestEnv(env, nil) 105 require.NoError(t, err) 106 } 107 108 func TestIntegrationContractInfoQueryGetCost(t *testing.T) { 109 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 110 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 111 112 t.Parallel() 113 env := NewIntegrationTestEnv(t) 114 115 resp, err := NewFileCreateTransaction(). 116 SetKeys(env.Client.GetOperatorPublicKey()). 117 SetNodeAccountIDs(env.NodeAccountIDs). 118 SetContents(testContractByteCode). 119 Execute(env.Client) 120 require.NoError(t, err) 121 122 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 123 require.NoError(t, err) 124 125 fileID := *receipt.FileID 126 assert.NotNil(t, fileID) 127 128 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 129 require.NoError(t, err) 130 131 resp, err = NewContractCreateTransaction(). 132 SetAdminKey(env.Client.GetOperatorPublicKey()). 133 SetGas(100000). 134 SetNodeAccountIDs([]AccountID{resp.NodeID}). 135 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 136 SetBytecodeFileID(fileID). 137 SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute"). 138 Execute(env.Client) 139 require.NoError(t, err) 140 141 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 142 require.NoError(t, err) 143 144 assert.NotNil(t, receipt.ContractID) 145 contractID := *receipt.ContractID 146 147 contractInfo := NewContractInfoQuery(). 148 SetContractID(contractID). 149 SetMaxQueryPayment(NewHbar(1)). 150 SetNodeAccountIDs([]AccountID{resp.NodeID}) 151 152 cost, err := contractInfo.GetCost(env.Client) 153 require.NoError(t, err) 154 155 info, err := contractInfo.SetQueryPayment(cost).Execute(env.Client) 156 require.NoError(t, err) 157 158 assert.Equal(t, contractID, info.ContractID) 159 assert.Equal(t, env.Client.GetOperatorPublicKey(), info.AdminKey) 160 assert.Equal(t, "hedera-sdk-go::TestContractInfoQuery_Execute", info.ContractMemo) 161 162 resp, err = NewContractDeleteTransaction(). 163 SetContractID(contractID). 164 SetTransferAccountID(env.Client.GetOperatorAccountID()). 165 SetNodeAccountIDs([]AccountID{resp.NodeID}). 166 Execute(env.Client) 167 require.NoError(t, err) 168 169 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 170 require.NoError(t, err) 171 172 resp, err = NewFileDeleteTransaction(). 173 SetFileID(fileID). 174 SetNodeAccountIDs([]AccountID{resp.NodeID}). 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 err = CloseIntegrationTestEnv(env, nil) 182 require.NoError(t, err) 183 } 184 185 func TestIntegrationContractInfoQuerySetBigMaxPayment(t *testing.T) { 186 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 187 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 188 189 t.Parallel() 190 env := NewIntegrationTestEnv(t) 191 192 resp, err := NewFileCreateTransaction(). 193 SetKeys(env.Client.GetOperatorPublicKey()). 194 SetNodeAccountIDs(env.NodeAccountIDs). 195 SetContents(testContractByteCode). 196 Execute(env.Client) 197 require.NoError(t, err) 198 199 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 200 require.NoError(t, err) 201 202 fileID := *receipt.FileID 203 assert.NotNil(t, fileID) 204 205 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 206 require.NoError(t, err) 207 208 resp, err = NewContractCreateTransaction(). 209 SetAdminKey(env.Client.GetOperatorPublicKey()). 210 SetGas(100000). 211 SetNodeAccountIDs([]AccountID{resp.NodeID}). 212 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 213 SetBytecodeFileID(fileID). 214 SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute"). 215 Execute(env.Client) 216 require.NoError(t, err) 217 218 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 219 require.NoError(t, err) 220 221 assert.NotNil(t, receipt.ContractID) 222 contractID := *receipt.ContractID 223 224 contractInfo := NewContractInfoQuery(). 225 SetContractID(contractID). 226 SetMaxQueryPayment(NewHbar(100000)). 227 SetNodeAccountIDs([]AccountID{resp.NodeID}) 228 229 cost, err := contractInfo.GetCost(env.Client) 230 require.NoError(t, err) 231 232 info, err := contractInfo.SetQueryPayment(cost).Execute(env.Client) 233 require.NoError(t, err) 234 235 assert.Equal(t, contractID, info.ContractID) 236 assert.Equal(t, env.Client.GetOperatorPublicKey(), info.AdminKey) 237 assert.Equal(t, "hedera-sdk-go::TestContractInfoQuery_Execute", info.ContractMemo) 238 239 resp, err = NewContractDeleteTransaction(). 240 SetContractID(contractID). 241 SetTransferAccountID(env.Client.GetOperatorAccountID()). 242 SetNodeAccountIDs([]AccountID{resp.NodeID}). 243 Execute(env.Client) 244 require.NoError(t, err) 245 246 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 247 require.NoError(t, err) 248 249 resp, err = NewFileDeleteTransaction(). 250 SetFileID(fileID). 251 SetNodeAccountIDs([]AccountID{resp.NodeID}). 252 Execute(env.Client) 253 require.NoError(t, err) 254 255 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 256 require.NoError(t, err) 257 258 err = CloseIntegrationTestEnv(env, nil) 259 require.NoError(t, err) 260 } 261 262 func TestIntegrationContractInfoQuerySetSmallMaxPayment(t *testing.T) { 263 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 264 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 265 266 t.Parallel() 267 env := NewIntegrationTestEnv(t) 268 269 resp, err := NewFileCreateTransaction(). 270 SetKeys(env.Client.GetOperatorPublicKey()). 271 SetNodeAccountIDs(env.NodeAccountIDs). 272 SetContents(testContractByteCode). 273 Execute(env.Client) 274 require.NoError(t, err) 275 276 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 277 require.NoError(t, err) 278 279 fileID := *receipt.FileID 280 assert.NotNil(t, fileID) 281 282 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 283 require.NoError(t, err) 284 285 resp, err = NewContractCreateTransaction(). 286 SetAdminKey(env.Client.GetOperatorPublicKey()). 287 SetGas(100000). 288 SetNodeAccountIDs([]AccountID{resp.NodeID}). 289 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 290 SetBytecodeFileID(fileID). 291 SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute"). 292 Execute(env.Client) 293 require.NoError(t, err) 294 295 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 296 require.NoError(t, err) 297 298 assert.NotNil(t, receipt.ContractID) 299 contractID := *receipt.ContractID 300 301 contractInfo := NewContractInfoQuery(). 302 SetContractID(contractID). 303 SetMaxQueryPayment(HbarFromTinybar(1)). 304 SetNodeAccountIDs([]AccountID{resp.NodeID}) 305 306 cost, err := contractInfo.GetCost(env.Client) 307 require.NoError(t, err) 308 309 _, err = contractInfo.Execute(env.Client) 310 assert.Error(t, err) 311 if err != nil { 312 assert.Equal(t, "cost of ContractInfoQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tℏ", err.Error()) 313 } 314 315 resp, err = NewContractDeleteTransaction(). 316 SetContractID(contractID). 317 SetTransferAccountID(env.Client.GetOperatorAccountID()). 318 SetNodeAccountIDs([]AccountID{resp.NodeID}). 319 Execute(env.Client) 320 require.NoError(t, err) 321 322 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 323 require.NoError(t, err) 324 325 resp, err = NewFileDeleteTransaction(). 326 SetFileID(fileID). 327 SetNodeAccountIDs([]AccountID{resp.NodeID}). 328 Execute(env.Client) 329 require.NoError(t, err) 330 331 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 332 require.NoError(t, err) 333 334 err = CloseIntegrationTestEnv(env, nil) 335 require.NoError(t, err) 336 } 337 338 func TestIntegrationContractInfoQueryInsufficientFee(t *testing.T) { 339 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 340 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 341 342 t.Parallel() 343 env := NewIntegrationTestEnv(t) 344 345 resp, err := NewFileCreateTransaction(). 346 SetKeys(env.Client.GetOperatorPublicKey()). 347 SetNodeAccountIDs(env.NodeAccountIDs). 348 SetContents(testContractByteCode). 349 Execute(env.Client) 350 require.NoError(t, err) 351 352 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 353 require.NoError(t, err) 354 355 fileID := *receipt.FileID 356 assert.NotNil(t, fileID) 357 358 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 359 require.NoError(t, err) 360 361 resp, err = NewContractCreateTransaction(). 362 SetAdminKey(env.Client.GetOperatorPublicKey()). 363 SetGas(100000). 364 SetNodeAccountIDs([]AccountID{resp.NodeID}). 365 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 366 SetBytecodeFileID(fileID). 367 SetContractMemo("hedera-sdk-go::TestContractInfoQuery_Execute"). 368 Execute(env.Client) 369 require.NoError(t, err) 370 371 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 372 require.NoError(t, err) 373 374 assert.NotNil(t, receipt.ContractID) 375 contractID := *receipt.ContractID 376 377 contractInfo := NewContractInfoQuery(). 378 SetContractID(contractID). 379 SetMaxQueryPayment(NewHbar(1)). 380 SetNodeAccountIDs([]AccountID{resp.NodeID}) 381 382 _, err = contractInfo.GetCost(env.Client) 383 require.NoError(t, err) 384 385 _, err = contractInfo.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client) 386 if err != nil { 387 assert.Equal(t, "exceptional precheck status INSUFFICIENT_TX_FEE", err.Error()) 388 } 389 390 resp, err = NewContractDeleteTransaction(). 391 SetContractID(contractID). 392 SetTransferAccountID(env.Client.GetOperatorAccountID()). 393 SetNodeAccountIDs([]AccountID{resp.NodeID}). 394 Execute(env.Client) 395 require.NoError(t, err) 396 397 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 398 require.NoError(t, err) 399 400 resp, err = NewFileDeleteTransaction(). 401 SetFileID(fileID). 402 SetNodeAccountIDs([]AccountID{resp.NodeID}). 403 Execute(env.Client) 404 require.NoError(t, err) 405 406 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 407 require.NoError(t, err) 408 409 err = CloseIntegrationTestEnv(env, nil) 410 require.NoError(t, err) 411 } 412 413 func TestIntegrationContractInfoQueryNoContractID(t *testing.T) { 414 t.Parallel() 415 env := NewIntegrationTestEnv(t) 416 417 _, err := NewContractInfoQuery(). 418 SetMaxQueryPayment(NewHbar(2)). 419 SetNodeAccountIDs(env.NodeAccountIDs). 420 Execute(env.Client) 421 assert.Error(t, err) 422 if err != nil { 423 assert.Equal(t, "exceptional precheck status INVALID_CONTRACT_ID", err.Error()) 424 } 425 426 err = CloseIntegrationTestEnv(env, nil) 427 require.NoError(t, err) 428 }