|
| 1 | +""" |
| 2 | +Example demonstrating contract creation with constructor parameters on the network. |
| 3 | +
|
| 4 | +This module shows how to create a stateful smart contract by: |
| 5 | +1. Setting up a client with operator credentials |
| 6 | +2. Creating a file containing contract bytecode |
| 7 | +3. Creating a contract using the file and constructor parameters |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | +# Import the bytecode for a stateful smart contract (StatefulContract.sol) that can be deployed |
| 14 | +# The contract bytecode is pre-compiled from Solidity source code |
| 15 | +from contracts import STATEFUL_CONTRACT_BYTECODE |
| 16 | +from dotenv import load_dotenv |
| 17 | + |
| 18 | +from hiero_sdk_python import AccountId, Client, Network, PrivateKey |
| 19 | +from hiero_sdk_python.contract.contract_create_transaction import ( |
| 20 | + ContractCreateTransaction, |
| 21 | +) |
| 22 | +from hiero_sdk_python.contract.contract_function_parameters import ( |
| 23 | + ContractFunctionParameters, |
| 24 | +) |
| 25 | +from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction |
| 26 | +from hiero_sdk_python.response_code import ResponseCode |
| 27 | + |
| 28 | +load_dotenv(override=True) |
| 29 | + |
| 30 | + |
| 31 | +def setup_client(): |
| 32 | + """Initialize and set up the client with operator account""" |
| 33 | + network = Network(network="testnet") |
| 34 | + client = Client(network) |
| 35 | + |
| 36 | + operator_id = AccountId.from_string(os.getenv("OPERATOR_ID")) |
| 37 | + operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY")) |
| 38 | + client.set_operator(operator_id, operator_key) |
| 39 | + |
| 40 | + return client |
| 41 | + |
| 42 | + |
| 43 | +def create_contract_file(client): |
| 44 | + """Create a file containing the stateful contract bytecode""" |
| 45 | + file_receipt = ( |
| 46 | + FileCreateTransaction() |
| 47 | + .set_keys(client.operator_private_key.public_key()) |
| 48 | + .set_contents(STATEFUL_CONTRACT_BYTECODE) |
| 49 | + .set_file_memo("Stateful contract bytecode file") |
| 50 | + .execute(client) |
| 51 | + ) |
| 52 | + |
| 53 | + # Check if file creation was successful |
| 54 | + if file_receipt.status != ResponseCode.SUCCESS: |
| 55 | + print( |
| 56 | + f"File creation failed with status: {ResponseCode(file_receipt.status).name}" |
| 57 | + ) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + return file_receipt.file_id |
| 61 | + |
| 62 | + |
| 63 | +def contract_create_constructor(): |
| 64 | + """ |
| 65 | + Demonstrates creating a stateful contract with constructor parameters by: |
| 66 | + 1. Setting up client with operator account |
| 67 | + 2. Creating a file containing stateful contract bytecode |
| 68 | + 3. Creating a contract using the file with constructor parameters |
| 69 | + """ |
| 70 | + client = setup_client() |
| 71 | + |
| 72 | + file_id = create_contract_file(client) |
| 73 | + |
| 74 | + # Prepare constructor parameters for the stateful contract |
| 75 | + # The contract's constructor expects a bytes32 parameter that will be stored |
| 76 | + # We need to: |
| 77 | + # 1. Convert our string message to UTF-8 bytes (encode) |
| 78 | + # 2. Pass those bytes to add_bytes32() to properly format for the contract |
| 79 | + # NOTE: If message exceeds 32 bytes, it will be truncated. |
| 80 | + # If message is less than 32 bytes, it will be padded with zeros. |
| 81 | + initial_message = "Initial message from constructor".encode("utf-8") |
| 82 | + truncated_message = initial_message[:32] |
| 83 | + initial_message_bytes32 = truncated_message.ljust(32, b"\0") |
| 84 | + |
| 85 | + # Create ContractFunctionParameters object and add the bytes32 parameter |
| 86 | + # This will be passed to setConstructorParameters() when creating the contract |
| 87 | + constructor_params = ContractFunctionParameters().add_bytes32( |
| 88 | + initial_message_bytes32 |
| 89 | + ) |
| 90 | + |
| 91 | + # Create contract using the file with constructor parameters |
| 92 | + receipt = ( |
| 93 | + ContractCreateTransaction() |
| 94 | + .set_admin_key(client.operator_private_key) |
| 95 | + .set_gas(2000000) # 2M gas |
| 96 | + .set_bytecode_file_id(file_id) |
| 97 | + .set_constructor_parameters(constructor_params) |
| 98 | + .set_contract_memo("Stateful smart contract with constructor") |
| 99 | + .execute(client) |
| 100 | + ) |
| 101 | + |
| 102 | + # Check if contract creation was successful |
| 103 | + if receipt.status != ResponseCode.SUCCESS: |
| 104 | + print( |
| 105 | + f"Contract creation failed with status: {ResponseCode(receipt.status).name}" |
| 106 | + ) |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + contract_id = receipt.contract_id |
| 110 | + print(f"Stateful contract created successfully with ID: {contract_id}") |
| 111 | + print( |
| 112 | + f"Initial message set in constructor: '{initial_message_bytes32.decode('utf-8')}'" |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + contract_create_constructor() |
0 commit comments