forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract_create_with_bytecode.py
More file actions
81 lines (60 loc) · 2.37 KB
/
contract_create_with_bytecode.py
File metadata and controls
81 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Example demonstrating contract creation on the network.
This module shows how to create a smart contract by:
1. Setting up a client with operator credentials
2. Creating a contract using the bytecode
Usage:
# Due to the way the script is structured, it must be run as a module
# from the project root directory
# Run from the project root directory
uv run -m examples.contract_create_with_bytecode
python -m examples.contract_create_with_bytecode
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import AccountId, Client, Network, PrivateKey
from hiero_sdk_python.contract.contract_create_transaction import (
ContractCreateTransaction,
)
from hiero_sdk_python.response_code import ResponseCode
# Import the bytecode for a basic smart contract (SimpleContract.sol) that can be deployed
# The contract bytecode is pre-compiled from Solidity source code
from .contracts import SIMPLE_CONTRACT_BYTECODE
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
network = Network(os.getenv('NETWORK'))
client = Client(network)
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID"))
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY"))
client.set_operator(operator_id, operator_key)
return client
def contract_create_with_bytecode():
"""
Demonstrates creating a contract on the network by:
1. Setting up client with operator account
2. Creating a contract using the bytecode
"""
client = setup_client()
# Convert the contract bytecode from hex string to bytes format
# This is required because set_bytecode() expects bytes, not a hex string
bytecode = bytes.fromhex(SIMPLE_CONTRACT_BYTECODE)
# Create contract using the bytecode
receipt = (
ContractCreateTransaction()
.set_bytecode(bytecode)
.set_gas(2000000) # 2M gas
.set_contract_memo("My first smart contract")
.execute(client)
)
# Check if contract creation was successful
if receipt.status != ResponseCode.SUCCESS:
print(
f"Contract creation failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)
contract_id = receipt.contract_id
print(f"Contract created successfully with ID: {contract_id}")
if __name__ == "__main__":
contract_create_with_bytecode()