Skip to content

Commit 264d954

Browse files
fix: align setup_client usage across token examples (hiero-ledger#1406)
Signed-off-by: Kushagra Kaushik <kushagra.k25239@nst.rishihood.edu.in> Signed-off-by: kushagrakaushik <kushagra.k25239@nst.rishihood.edu.in>
1 parent 200f1cb commit 264d954

File tree

44 files changed

+363
-1039
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+363
-1039
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Changelog
1+
# Changelog
22

33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](https://semver.org).
@@ -186,6 +186,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
186186
- Fixed `cron-check-broken-links.yml` string parsing issue in context input `dry_run` (#1235)
187187
- Flaky tests by disabling TLS in mock Hedera nodes in `mock_server.py`
188188
- Fixed LinkBot permission issue for fork PRs by changing trigger to pull_request_target and adding proper permissions.
189+
- Fixed token examples to consistently use setup_client without tuple unpacking.(#1397)
189190
- Fixed duplicate comment prevention in issue reminder bot by adding hidden HTML marker for reliable comment detection (.github/scripts/bot-issue-reminder-no-pr.sh) (#1372)
190191
- Fixed bot-pr-missing-linked-issue to skip commenting on pull requests created by automated bots. (#1382)
191192
- Updated `.github/scripts/bot-community-calls.sh` to skip posting reminders on issues created by bot accounts. (#1383)

examples/tokens/account_allowance_approve_transaction.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
66
"""
77

8-
import os
98
import sys
109

11-
from dotenv import load_dotenv
1210

13-
from hiero_sdk_python import AccountId, Client, Hbar, Network, PrivateKey, TransactionId
11+
from hiero_sdk_python import Client, Hbar, PrivateKey, TransactionId
1412
from hiero_sdk_python.account.account_allowance_approve_transaction import (
1513
AccountAllowanceApproveTransaction,
1614
)
@@ -24,22 +22,11 @@
2422
from hiero_sdk_python.tokens.token_type import TokenType
2523
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction
2624

27-
load_dotenv()
28-
29-
network_name = os.getenv("NETWORK", "testnet").lower()
30-
3125

3226
def setup_client():
33-
"""Initialize and set up the client with operator account"""
34-
network = Network(network_name)
35-
print(f"Connecting to Hedera {network_name} network!")
36-
client = Client(network)
37-
38-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
39-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
40-
client.set_operator(operator_id, operator_key)
27+
client = Client.from_env()
28+
print(f"Network: {client.network.network}")
4129
print(f"Client set up with operator id {client.operator_account_id}")
42-
4330
return client
4431

4532

examples/tokens/custom_fee_fixed.py

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
python examples/tokens/custom_fixed_fee.py
55
"""
66

7-
import os
87
import sys
9-
from dotenv import load_dotenv
108

11-
from hiero_sdk_python.client.network import Network
129
from hiero_sdk_python.crypto.private_key import PrivateKey
1310
from hiero_sdk_python.client.client import Client
1411
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
15-
from hiero_sdk_python.account.account_id import AccountId
1612
from hiero_sdk_python.response_code import ResponseCode
1713
from hiero_sdk_python.tokens.supply_type import SupplyType
1814
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
@@ -21,43 +17,11 @@
2117
from hiero_sdk_python.tokens.token_type import TokenType
2218

2319

24-
load_dotenv()
25-
network_name = os.getenv("NETWORK", "testnet").lower()
26-
27-
2820
def setup_client():
29-
"""Initialize and set up the client with operator account"""
30-
# Initialize network and client
31-
network = Network(network_name)
32-
print(f"Connecting to Hedera {network_name} network!")
33-
client = Client(network)
34-
35-
# This disables the SSL error in the local development environment (Keep commented for production) #
36-
37-
# client.set_transport_security(False)
38-
# client.set_verify_certificates(False)
39-
40-
try:
41-
operator_id_str = os.getenv("OPERATOR_ID")
42-
operator_key_str = os.getenv("OPERATOR_KEY")
43-
44-
if not operator_id_str or not operator_key_str:
45-
raise ValueError(
46-
"Environment variables OPERATOR_ID or OPERATOR_KEY are missing."
47-
)
48-
49-
operator_id = AccountId.from_string(operator_id_str)
50-
operator_key = PrivateKey.from_string(operator_key_str)
51-
52-
client.set_operator(operator_id, operator_key)
53-
print(f"Client set up with operator id {client.operator_account_id}")
54-
return client, operator_id, operator_key
55-
56-
except (TypeError, ValueError) as e:
57-
print(f"Error: {e}")
58-
print("Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
59-
sys.exit(1)
60-
21+
client = Client.from_env()
22+
print(f"Network: {client.network.network}")
23+
print(f"Client set up with operator id {client.operator_account_id}")
24+
return client
6125

6226
def custom_fixed_fee_example():
6327
"""
@@ -66,32 +30,32 @@ def custom_fixed_fee_example():
6630
6731
"""
6832

69-
client, operator_id, operator_key = setup_client()
33+
client = setup_client()
7034

7135
print("\n--- Creating Custom Fixed Fee ---")
7236

7337
fixed_fee = CustomFixedFee(
7438
amount=Hbar(1).to_tinybars(),
75-
fee_collector_account_id=operator_id,
39+
fee_collector_account_id=client.operator_account_id,
7640
all_collectors_are_exempt=False,
7741
)
7842

79-
print(f"Fee Definition: Pay 1 HBAR to {operator_id}")
43+
print(f"Fee Definition: Pay 1 HBAR to {client.operator_account_id}")
8044

8145
print("\n--- Creating Token with Fee ---")
8246
transaction = (
8347
TokenCreateTransaction()
8448
.set_token_name("Fixed Fee Example Token")
8549
.set_token_symbol("FFET")
8650
.set_decimals(2)
87-
.set_treasury_account_id(operator_id)
51+
.set_treasury_account_id(client.operator_account_id)
8852
.set_token_type(TokenType.FUNGIBLE_COMMON)
8953
.set_supply_type(SupplyType.INFINITE)
9054
.set_initial_supply(1000)
91-
.set_admin_key(operator_key)
55+
.set_admin_key(client.operator_private_key)
9256
.set_custom_fees([fixed_fee])
9357
.freeze_with(client)
94-
.sign(operator_key)
58+
.sign(client.operator_private_key)
9559
)
9660

9761
try:

examples/tokens/custom_fee_fractional.py

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""
22
Run with:
3-
uv run examples/tokens/custom_fractional_fee.py
43
python examples/tokens/custom_fractional_fee.py
4+
uv run examples/tokens/custom_fractional_fee.py
55
"""
66

7-
import os
87
import sys
9-
from dotenv import load_dotenv
108

119
from hiero_sdk_python.tokens.token_create_transaction import (
1210
TokenCreateTransaction,
@@ -15,65 +13,42 @@
1513
from hiero_sdk_python.tokens.custom_fractional_fee import CustomFractionalFee
1614
from hiero_sdk_python.tokens.fee_assessment_method import FeeAssessmentMethod
1715
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
18-
from hiero_sdk_python.crypto.private_key import PrivateKey
1916
from hiero_sdk_python.account.account_id import AccountId
2017
from hiero_sdk_python.response_code import ResponseCode
21-
from hiero_sdk_python.client.network import Network
2218
from hiero_sdk_python.client.client import Client
2319
from hiero_sdk_python.tokens.token_type import TokenType
2420
from hiero_sdk_python.tokens.supply_type import SupplyType
2521

2622

27-
load_dotenv()
28-
29-
3023
def setup_client():
31-
network_name = os.getenv("NETWORK", "testnet")
32-
33-
# Validate environment variables
34-
if not os.getenv("OPERATOR_ID") or not os.getenv("OPERATOR_KEY"):
35-
print("❌ Missing OPERATOR_ID or OPERATOR_KEY in .env file.")
36-
sys.exit(1)
37-
38-
try:
39-
network = Network(network_name)
40-
print(f"Connecting to Hedera {network_name} network!")
41-
client = Client(network)
42-
43-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
44-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
45-
client.set_operator(operator_id, operator_key)
46-
print(f"Client set up with operator id {client.operator_account_id}")
47-
48-
except Exception as e:
49-
raise ConnectionError(f"Error initializing client: {e}") from e
50-
51-
print(f"✅ Connected to Hedera {network_name} network as operator: {operator_id}")
52-
return client, operator_id, operator_key
24+
client = Client.from_env()
25+
print(f"Network: {client.network.network}")
26+
print(f"Client set up with operator id {client.operator_account_id}")
27+
return client
5328

5429

55-
def build_fractional_fee(operator_account: AccountId) -> CustomFractionalFee:
30+
def build_fractional_fee(operator_account_id: AccountId) -> CustomFractionalFee:
5631
"""Creates a CustomFractionalFee instance."""
5732
return CustomFractionalFee(
5833
numerator=1,
5934
denominator=10,
6035
min_amount=1,
6136
max_amount=100,
6237
assessment_method=FeeAssessmentMethod.INCLUSIVE,
63-
fee_collector_account_id=operator_account,
38+
fee_collector_account_id=operator_account_id,
6439
all_collectors_are_exempt=True,
6540
)
6641

6742

68-
def create_token_with_fee_key(client, operator_id, fractional_fee: CustomFractionalFee):
43+
def create_token_with_fee_key(client, fractional_fee: CustomFractionalFee):
6944
"""Create a fungible token with a fee_schedule_key."""
7045
print("Creating fungible token with fee_schedule_key...")
7146
fractional_fee = [fractional_fee]
7247

7348
token_params = TokenParams(
7449
token_name="Fee Key Token",
7550
token_symbol="FKT",
76-
treasury_account_id=operator_id,
51+
treasury_account_id=client.operator_account_id,
7752
initial_supply=1000,
7853
decimals=2,
7954
token_type=TokenType.FUNGIBLE_COMMON,
@@ -112,10 +87,10 @@ def query_and_validate_fractional_fee(client: Client, token_id):
11287

11388

11489
def main():
115-
client, operator_id, _ = setup_client()
90+
client = setup_client()
11691
# Build fractional fee
117-
fractional_fee = build_fractional_fee(operator_id)
118-
token_id = create_token_with_fee_key(client, operator_id, fractional_fee)
92+
fractional_fee = build_fractional_fee(client.operator_account_id)
93+
token_id = create_token_with_fee_key(client, fractional_fee)
11994

12095
# Query and validate fractional fee
12196
token_info = query_and_validate_fractional_fee(client, token_id)

examples/tokens/custom_royalty_fee.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,66 @@
44
python examples/tokens/custom_royalty_fee.py
55
"""
66

7-
import os
87
import sys
9-
from dotenv import load_dotenv
108

11-
from hiero_sdk_python.client.network import Network
129
from hiero_sdk_python.crypto.private_key import PrivateKey
1310
from hiero_sdk_python.client.client import Client
14-
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
15-
from hiero_sdk_python.account.account_id import AccountId
1611
from hiero_sdk_python.response_code import ResponseCode
1712
from hiero_sdk_python.tokens.supply_type import SupplyType
13+
from hiero_sdk_python.query.token_info_query import TokenInfoQuery
1814
from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee
1915
from hiero_sdk_python.tokens.custom_royalty_fee import CustomRoyaltyFee
2016
from hiero_sdk_python.hbar import Hbar
2117
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
2218
from hiero_sdk_python.tokens.token_type import TokenType
2319

24-
load_dotenv()
25-
2620

2721
def setup_client():
28-
"""Initialize and set up the client with operator account"""
29-
30-
try:
31-
network_name = os.getenv("NETWORK", "testnet").lower()
32-
network = Network(network_name)
33-
print(f"Connecting to the Hedera {network_name} network")
34-
client = Client(network)
35-
36-
operator_id_str = os.getenv("OPERATOR_ID")
37-
operator_key_str = os.getenv("OPERATOR_KEY")
38-
39-
if not operator_id_str or not operator_key_str:
40-
raise ValueError(
41-
"Environment variables OPERATOR_ID or OPERATOR_KEY are missing."
42-
)
43-
44-
operator_id = AccountId.from_string(operator_id_str)
45-
operator_key = PrivateKey.from_string(operator_key_str)
46-
47-
client.set_operator(operator_id, operator_key)
48-
print(f"Client set up with operator id {client.operator_account_id}")
49-
return client, operator_id, operator_key
50-
51-
except (TypeError, ValueError) as e:
52-
print(f"Error: {e}")
53-
sys.exit(1)
22+
client = Client.from_env()
23+
print(f"Network: {client.network.network}")
24+
print(f"Client set up with operator id {client.operator_account_id}")
25+
return client
5426

5527

56-
def create_royalty_fee_object(operator_id):
28+
def create_royalty_fee_object(client):
5729
"""Creates the CustomRoyaltyFee object with a fallback fee."""
5830
fallback_fee = CustomFixedFee(
5931
amount=Hbar(1).to_tinybars(),
60-
fee_collector_account_id=operator_id,
32+
fee_collector_account_id=client.operator_account_id,
6133
all_collectors_are_exempt=False,
6234
)
6335

6436
royalty_fee = CustomRoyaltyFee(
6537
numerator=5,
6638
denominator=100,
6739
fallback_fee=fallback_fee,
68-
fee_collector_account_id=operator_id,
40+
fee_collector_account_id=client.operator_account_id,
6941
all_collectors_are_exempt=False,
7042
)
7143
print(f"Royalty Fee Configured: {royalty_fee.numerator}/{royalty_fee.denominator}")
7244
print(f"Fallback Fee: {Hbar.from_tinybars(fallback_fee.amount)} HBAR")
7345
return royalty_fee
7446

7547

76-
def create_token_with_fee(client, operator_id, operator_key, royalty_fee):
48+
def create_token_with_fee(client, royalty_fee):
7749
"""Creates a token with the specified royalty fee attached."""
7850

7951
print("\n--- Creating Token with Royalty Fee ---")
8052
transaction = (
8153
TokenCreateTransaction()
8254
.set_token_name("Royalty NFT Collection")
8355
.set_token_symbol("RNFT")
84-
.set_treasury_account_id(operator_id)
85-
.set_admin_key(operator_key)
86-
.set_supply_key(operator_key)
56+
.set_treasury_account_id(client.operator_account_id)
57+
.set_admin_key(client.operator_private_key)
58+
.set_supply_key(client.operator_private_key)
8759
.set_token_type(TokenType.NON_FUNGIBLE_UNIQUE)
8860
.set_decimals(0)
8961
.set_initial_supply(0)
9062
.set_supply_type(SupplyType.FINITE)
9163
.set_max_supply(100)
9264
.set_custom_fees([royalty_fee])
9365
.freeze_with(client)
94-
.sign(operator_key)
66+
.sign(client.operator_private_key)
9567
)
9668

9769
receipt = transaction.execute(client)
@@ -124,14 +96,12 @@ def verify_token_fee(client, token_id):
12496

12597
def main():
12698
"""Main execution flow."""
127-
client, operator_id, operator_key = setup_client()
99+
client = setup_client()
128100

129101
with client:
130102
try:
131-
royalty_fee = create_royalty_fee_object(operator_id)
132-
token_id = create_token_with_fee(
133-
client, operator_id, operator_key, royalty_fee
134-
)
103+
royalty_fee = create_royalty_fee_object(client)
104+
token_id = create_token_with_fee(client, royalty_fee)
135105
verify_token_fee(client, token_id)
136106
except Exception as e:
137107
print(f"Execution failed: {e}")

0 commit comments

Comments
 (0)