github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_create_transaction_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 "encoding/hex" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 32 "testing" 33 ) 34 35 func TestIntegrationContractCreateTransactionCanExecute(t *testing.T) { 36 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 37 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 38 39 t.Parallel() 40 env := NewIntegrationTestEnv(t) 41 42 resp, err := NewFileCreateTransaction(). 43 SetKeys(env.Client.GetOperatorPublicKey()). 44 SetNodeAccountIDs(env.NodeAccountIDs). 45 SetContents(testContractByteCode). 46 Execute(env.Client) 47 48 require.NoError(t, err) 49 50 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 51 require.NoError(t, err) 52 53 fileID := *receipt.FileID 54 assert.NotNil(t, fileID) 55 56 resp, err = NewContractCreateTransaction(). 57 SetAdminKey(env.Client.GetOperatorPublicKey()). 58 SetNodeAccountIDs([]AccountID{resp.NodeID}). 59 SetGas(100000). 60 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 61 SetBytecodeFileID(fileID). 62 SetContractMemo("hedera-sdk-go::TestContractCreateTransaction_Execute"). 63 Execute(env.Client) 64 require.NoError(t, err) 65 66 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 67 require.NoError(t, err) 68 69 assert.NotNil(t, receipt.ContractID) 70 contractID := *receipt.ContractID 71 72 resp, err = NewContractDeleteTransaction(). 73 SetContractID(contractID). 74 SetTransferAccountID(env.Client.GetOperatorAccountID()). 75 SetNodeAccountIDs([]AccountID{resp.NodeID}). 76 Execute(env.Client) 77 require.NoError(t, err) 78 79 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 80 require.NoError(t, err) 81 82 resp, err = NewFileDeleteTransaction(). 83 SetFileID(fileID). 84 SetNodeAccountIDs([]AccountID{resp.NodeID}). 85 Execute(env.Client) 86 require.NoError(t, err) 87 88 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 89 require.NoError(t, err) 90 91 err = CloseIntegrationTestEnv(env, nil) 92 require.NoError(t, err) 93 } 94 95 func TestIntegrationContractCreateTransactionNoAdminKey(t *testing.T) { 96 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 97 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 98 99 t.Parallel() 100 env := NewIntegrationTestEnv(t) 101 102 resp, err := NewFileCreateTransaction(). 103 SetKeys(env.Client.GetOperatorPublicKey()). 104 SetNodeAccountIDs(env.NodeAccountIDs). 105 SetContents(testContractByteCode). 106 Execute(env.Client) 107 108 require.NoError(t, err) 109 110 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 111 require.NoError(t, err) 112 113 fileID := *receipt.FileID 114 assert.NotNil(t, fileID) 115 116 resp, err = NewContractCreateTransaction(). 117 SetNodeAccountIDs([]AccountID{resp.NodeID}). 118 SetGas(100000). 119 SetBytecodeFileID(fileID). 120 Execute(env.Client) 121 require.NoError(t, err) 122 123 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 124 require.NoError(t, err) 125 126 assert.NotNil(t, receipt.ContractID) 127 contractID := *receipt.ContractID 128 129 info, err := NewContractInfoQuery(). 130 SetNodeAccountIDs([]AccountID{resp.NodeID}). 131 SetContractID(contractID). 132 SetQueryPayment(NewHbar(1)). 133 Execute(env.Client) 134 require.NoError(t, err) 135 136 assert.Equal(t, info.ContractID, contractID) 137 assert.NotNil(t, info.AccountID) 138 assert.Equal(t, info.AccountID.String(), contractID.String()) 139 assert.NotNil(t, info.AdminKey) 140 // assert.Equal(t, info.AdminKey.String(), contractID.String()) 141 142 resp, err = NewContractDeleteTransaction(). 143 SetContractID(contractID). 144 SetTransferAccountID(env.Client.GetOperatorAccountID()). 145 SetNodeAccountIDs([]AccountID{resp.NodeID}). 146 Execute(env.Client) 147 require.NoError(t, err) 148 149 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 150 assert.Error(t, err) 151 if err != nil { 152 assert.Equal(t, "exceptional receipt status: MODIFYING_IMMUTABLE_CONTRACT", err.Error()) 153 } 154 155 resp, err = NewFileDeleteTransaction(). 156 SetFileID(fileID). 157 SetNodeAccountIDs([]AccountID{resp.NodeID}). 158 Execute(env.Client) 159 require.NoError(t, err) 160 161 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 162 require.NoError(t, err) 163 164 err = CloseIntegrationTestEnv(env, nil) 165 require.NoError(t, err) 166 } 167 168 func TestIntegrationContractCreateTransactionNoGas(t *testing.T) { 169 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 170 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 171 172 t.Parallel() 173 env := NewIntegrationTestEnv(t) 174 175 resp, err := NewFileCreateTransaction(). 176 SetKeys(env.Client.GetOperatorPublicKey()). 177 SetNodeAccountIDs(env.NodeAccountIDs). 178 SetContents(testContractByteCode). 179 Execute(env.Client) 180 181 require.NoError(t, err) 182 183 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 184 require.NoError(t, err) 185 186 fileID := *receipt.FileID 187 assert.NotNil(t, fileID) 188 189 _, err = NewContractCreateTransaction(). 190 SetAdminKey(env.Client.GetOperatorPublicKey()). 191 SetNodeAccountIDs([]AccountID{resp.NodeID}). 192 SetBytecodeFileID(fileID). 193 Execute(env.Client) 194 assert.Error(t, err) 195 if err != nil { 196 require.ErrorContains(t, err, "exceptional precheck status INSUFFICIENT_GAS") 197 } 198 199 resp, err = NewFileDeleteTransaction(). 200 SetFileID(fileID). 201 SetNodeAccountIDs([]AccountID{resp.NodeID}). 202 Execute(env.Client) 203 require.NoError(t, err) 204 205 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 206 require.NoError(t, err) 207 208 err = CloseIntegrationTestEnv(env, nil) 209 require.NoError(t, err) 210 } 211 212 func TestIntegrationContractCreateTransactionNoBytecodeFileID(t *testing.T) { 213 // Note: this is the bytecode for the contract found in the example for ./examples/_Create_simpleContract 214 testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`) 215 216 t.Parallel() 217 env := NewIntegrationTestEnv(t) 218 219 resp, err := NewFileCreateTransaction(). 220 SetKeys(env.Client.GetOperatorPublicKey()). 221 SetNodeAccountIDs(env.NodeAccountIDs). 222 SetContents(testContractByteCode). 223 Execute(env.Client) 224 225 require.NoError(t, err) 226 227 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 228 require.NoError(t, err) 229 230 fileID := *receipt.FileID 231 assert.NotNil(t, fileID) 232 233 resp, err = NewContractCreateTransaction(). 234 SetAdminKey(env.Client.GetOperatorPublicKey()). 235 SetNodeAccountIDs([]AccountID{resp.NodeID}). 236 SetGas(100000). 237 Execute(env.Client) 238 require.NoError(t, err) 239 240 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 241 assert.Error(t, err) 242 if err != nil { 243 assert.Equal(t, "exceptional receipt status: INVALID_FILE_ID", err.Error()) 244 } 245 246 resp, err = NewFileDeleteTransaction(). 247 SetFileID(fileID). 248 SetNodeAccountIDs([]AccountID{resp.NodeID}). 249 Execute(env.Client) 250 require.NoError(t, err) 251 252 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 253 require.NoError(t, err) 254 255 err = CloseIntegrationTestEnv(env, nil) 256 require.NoError(t, err) 257 } 258 259 func TestIntegrationContractCreateTransactionSetBytecode(t *testing.T) { 260 t.Parallel() 261 env := NewIntegrationTestEnv(t) 262 263 hexData := `608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029` 264 hexBytecode, err := hex.DecodeString(hexData) 265 require.NoError(t, err) 266 267 resp, err := NewContractCreateTransaction(). 268 SetAdminKey(env.Client.GetOperatorPublicKey()). 269 SetNodeAccountIDs(env.NodeAccountIDs). 270 SetGas(10000000). 271 SetBytecode(hexBytecode). 272 Execute(env.Client) 273 require.NoError(t, err) 274 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 275 require.NoError(t, err) 276 require.NotNil(t, receipt.ContractID) 277 278 err = CloseIntegrationTestEnv(env, nil) 279 require.NoError(t, err) 280 }