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