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.py
More file actions
101 lines (77 loc) · 2.92 KB
/
contract_create.py
File metadata and controls
101 lines (77 loc) · 2.92 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
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 file containing contract bytecode
3. Creating a contract using the file
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
python -m examples.contract_create
"""
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.file.file_create_transaction import FileCreateTransaction
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 create_contract_file(client):
"""Create a file containing the contract bytecode"""
file_receipt = (
FileCreateTransaction()
.set_keys(client.operator_private_key.public_key())
.set_contents(SIMPLE_CONTRACT_BYTECODE)
.set_file_memo("Contract bytecode file")
.execute(client)
)
# Check if file creation was successful
if file_receipt.status != ResponseCode.SUCCESS:
print(
f"File creation failed with status: {ResponseCode(file_receipt.status).name}"
)
sys.exit(1)
return file_receipt.file_id
def contract_create():
"""
Demonstrates creating a contract on the network by:
1. Setting up client with operator account
2. Creating a file containing contract bytecode
3. Creating a contract using the file
"""
client = setup_client()
file_id = create_contract_file(client)
# Create contract using the file
receipt = (
ContractCreateTransaction()
.set_bytecode_file_id(file_id)
.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()