-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add ContractCreateTransaction #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dosik13
merged 32 commits into
hiero-ledger:main
from
Dosik13:feature/add-ContractCreateTransaction
Aug 3, 2025
+3,997
−1
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b9da071
feat: add ContractSelector class
Dosik13 0c71444
feat: add ContractFunctionParamters
Dosik13 e9336cb
feat: add ContractId class
Dosik13 8347e4b
test: add unit tests for contractId
Dosik13 ed66460
feat: export contract_id in transaction_receipt
Dosik13 176018f
feat: add ContractCreateTransaction class
Dosik13 0c37662
test: add unit tests for ContractCreateTransaction
Dosik13 76d1bfa
test: add integration tests forContractCreateTransaction
Dosik13 f995fac
docs: add ContractCreateTransaction in examples README
Dosik13 6a9fda6
docs: add contract create example
Dosik13 2f01c48
feat: add contract bytecode files (SimpleContract for basic testing, …
Dosik13 14574b7
feat: add contract bytecode loader utility and examples module initia…
Dosik13 bc6abbc
test: update ContractCreate integration tests to use contract utils a…
Dosik13 cbb5e89
chore: fix linting issues in contract utilities
Dosik13 0762866
docs: update contract create example
Dosik13 a94c714
docs: add contract create with constructor example
Dosik13 49d6d09
chore: address PR feedback for contract create
Dosik13 2857dc7
refactor: migrate ContractFunctionParameters to use eth-abi for param…
Dosik13 02ae3f3
feat: add type stub file for ContractFunctionParameters with eth-abi …
Dosik13 b8a17c6
docs: update examples
Dosik13 ad13d2b
docs: add contract creation example with direct bytecode usage
Dosik13 f893875
chore: address PR feedback
Dosik13 ea5a44a
fix: remove unnecessary added add_function() method from ContractFunc…
Dosik13 a0c55a2
feat: add ConstructorTestContract smart contract for testing Contract…
Dosik13 4673816
test: add integration tests for ContracFunctionParameters
Dosik13 2f957f1
chore: add ContractCreateTransaction and ContractFunctionParameters t…
Dosik13 11ae8a6
feat: add eth-abi dependency for contract parameter encoding
Dosik13 19b212b
fix: update ConstructorTestContract bytecode to match compiled version
Dosik13 acd1198
docs: add parameter order comments in contract test
Dosik13 010308c
docs: add more documentaton in contract utils
Dosik13 c3c581e
chore: address PR feedback
Dosik13 ee4b724
chore: address incorrect error raising
Dosik13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
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(network="testnet") | ||
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
""" | ||
Example demonstrating contract creation with constructor parameters on the network. | ||
|
||
This module shows how to create a stateful 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 and constructor parameters | ||
|
||
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 | ||
python -m examples.contract_create_constructor | ||
|
||
""" | ||
|
||
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.contract.contract_function_parameters import ( | ||
ContractFunctionParameters, | ||
) | ||
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction | ||
from hiero_sdk_python.response_code import ResponseCode | ||
|
||
# Import the bytecode for a stateful smart contract (StatefulContract.sol) that can be deployed | ||
# The contract bytecode is pre-compiled from Solidity source code | ||
from .contracts import STATEFUL_CONTRACT_BYTECODE | ||
|
||
load_dotenv() | ||
|
||
|
||
def setup_client(): | ||
"""Initialize and set up the client with operator account""" | ||
network = Network(network="testnet") | ||
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 stateful contract bytecode""" | ||
file_receipt = ( | ||
FileCreateTransaction() | ||
.set_keys(client.operator_private_key.public_key()) | ||
.set_contents(STATEFUL_CONTRACT_BYTECODE) | ||
.set_file_memo("Stateful 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_constructor(): | ||
""" | ||
Demonstrates creating a stateful contract with constructor parameters by: | ||
1. Setting up client with operator account | ||
2. Creating a file containing stateful contract bytecode | ||
3. Creating a contract using the file with constructor parameters | ||
""" | ||
client = setup_client() | ||
|
||
file_id = create_contract_file(client) | ||
|
||
# Prepare constructor parameters for the stateful contract | ||
# The contract's constructor expects a bytes32 parameter that will be stored | ||
# We need to: | ||
# 1. Convert our string message to UTF-8 bytes (encode) | ||
# 2. Pass those bytes to add_bytes32() to properly format for the contract | ||
# NOTE: If message exceeds 32 bytes, it will raise an error. | ||
# If message is less than 32 bytes, it will be padded with zeros. | ||
initial_message = "Initial message from constructor".encode("utf-8") | ||
|
||
# Create ContractFunctionParameters object and add the bytes32 parameter | ||
# This will be passed to setConstructorParameters() when creating the contract | ||
constructor_params = ContractFunctionParameters().add_bytes32(initial_message) | ||
|
||
# Create contract using the file with constructor parameters | ||
receipt = ( | ||
ContractCreateTransaction() | ||
.set_admin_key(client.operator_private_key.public_key()) | ||
.set_gas(2000000) # 2M gas | ||
.set_bytecode_file_id(file_id) | ||
.set_constructor_parameters(constructor_params) | ||
.set_contract_memo("Stateful smart contract with constructor") | ||
.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"Stateful contract created successfully with ID: {contract_id}") | ||
print(f"Initial message set in constructor: '{initial_message.decode('utf-8')}'") | ||
|
||
|
||
if __name__ == "__main__": | ||
contract_create_constructor() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.