Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
- Added dry-run support and refactored `.github/workflows/bot-workflows.yml` to use dedicated script `.github/scripts/bot-workflows.js` for improved maintainability and testability. (`#1288`)

### Changed
- Refactored `examples/consensus/topic_create_transaction.py` to use `Client.from_env()` (#1611)
- chore: format tests/unit/mock_server.py with black (#1542)
- Updated actions/checkout to v6.0.1 and actions/github-script v8.0.0 in bot-next-issue-recommendation workflow (#1586)
- Expanded inactivity bot messages to include `/unassign` command information for contributors (#1555)
Expand Down
64 changes: 11 additions & 53 deletions examples/consensus/topic_create_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,19 @@
uv run examples/consensus/topic_create_transaction.py
python examples/consensus/topic_create_transaction.py
"""
from hiero_sdk_python import Client, TopicCreateTransaction

import os
import sys
from typing import Tuple
from dotenv import load_dotenv

from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
TopicCreateTransaction,
Network,
)

# Load environment variables from .env file
load_dotenv()
network_name = os.getenv("NETWORK", "testnet").lower()


def setup_client() -> Tuple[Client, PrivateKey]:
def setup_client():
"""
Sets up and configures the Hiero client for the testnet.
Reads OPERATOR_ID and OPERATOR_KEY from environment variables.
Sets up and configures the Hiero client.
Reads OPERATOR_ID and OPERATOR_KEY from environment variables via Client.from_env().
"""
network = Network(network_name)
print(f"Connecting to Hedera {network_name} network!")
client = Client(network)

operator_id_str = os.getenv("OPERATOR_ID")
operator_key_str = os.getenv("OPERATOR_KEY")

# Check if the environment variables are loaded correctly
if not operator_id_str or not operator_key_str:
print("Error: OPERATOR_ID or OPERATOR_KEY not found in environment.")
print("Please create a .env file in the project's root directory with:")
print("\nOPERATOR_ID=your_id_here")
print("OPERATOR_KEY=your_key_here\n")
sys.exit(1)

try:
operator_id = AccountId.from_string(operator_id_str)
operator_key = PrivateKey.from_string(operator_key_str)
except (TypeError, ValueError) as e:
print(f"Error: Invalid OPERATOR_ID or OPERATOR_KEY format: {e}")
sys.exit(1)

client.set_operator(operator_id, operator_key)
client = Client.from_env()
print(f"Network: {client.network.network}")
print(f"Client set up with operator id {client.operator_account_id}")
return client, operator_key

return client, client.operator_private_key

def create_topic(client: Client, operator_key: PrivateKey):
def create_topic(client: Client, operator_key: "PrivateKey"):
"""
Builds, signs, and executes a new topic creation transaction.
"""
Expand All @@ -64,18 +25,16 @@ def create_topic(client: Client, operator_key: PrivateKey):
.freeze_with(client)
.sign(operator_key)
)

try:
receipt = transaction.execute(client)
if receipt and receipt.topic_id:
print(f"Success! Topic created with ID: {receipt.topic_id}")
else:
print("Topic creation failed: Topic ID not returned in receipt.")
sys.exit(1)
raise SystemExit(1)
except Exception as e:
print(f"Topic creation failed: {str(e)}")
sys.exit(1)

raise SystemExit(1)

def main():
"""
Expand All @@ -84,6 +43,5 @@ def main():
client, operator_key = setup_client()
create_topic(client, operator_key)


if __name__ == "__main__":
main()
main()