Skip to content

Commit 92ff602

Browse files
authored
chore: formatted examples/query (#1118)
Signed-off-by: andrerovee <[email protected]> Signed-off-by: Andrea Roveda <[email protected]>
1 parent 2dc761d commit 92ff602

12 files changed

+287
-196
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
3737
- Add PR inactivity reminder bot for stale pull requests `.github/workflows/pr-inactivity-reminder-bot.yml`
3838
- Add comprehensive training documentation for _Executable class `docs/sdk_developers/training/executable.md`
3939
- Added empty `docs/maintainers/good_first_issues.md` file for maintainers to write Good First Issue guidelines (#1034)
40-
-Added new `.github/ISSUE_TEMPLATE/04_good_first_issue_candidate.yml` file (1068)(https://github.com/hiero-ledger/hiero-sdk-python/issues/1068)
40+
- Added new `.github/ISSUE_TEMPLATE/04_good_first_issue_candidate.yml` file (1068)(https://github.com/hiero-ledger/hiero-sdk-python/issues/1068)
4141
- Enhanced `.github/ISSUE_TEMPLATE/01_good_first_issue.yml` with welcoming message and acceptance criteria sections to guide contributors in creating quality GFIs (#1052)
4242
- Add workflow to notify team about P0 issues `bot-p0-issues-notify-team.yml`
4343

@@ -54,6 +54,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
5454
`examples.yml``pr-check-examples.yml`,
5555
`test.yml``pr-check-test.yml` (#1043)
5656
- Cleaned up `token_airdrop_claim_auto` example for pylint compliance (no functional changes). (#1079)
57+
- Formatted `examples/query` using black (#1082)(https://github.com/hiero-ledger/hiero-sdk-python/issues/1082)
5758
- Update team notification script and workflow for P0 issues 'p0_issues_notify_team.js'
5859
- Rename test files across the repository to ensure they consistently end with _test.py (#1055)
5960

examples/query/account_balance_query.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
python examples/query/account_balance_query.py
1313
1414
"""
15+
1516
import os
1617
import sys
1718
import time
@@ -30,7 +31,8 @@
3031
)
3132

3233
load_dotenv()
33-
network_name = os.getenv('NETWORK', 'testnet').lower()
34+
network_name = os.getenv("NETWORK", "testnet").lower()
35+
3436

3537
def setup_client():
3638
"""
@@ -47,12 +49,13 @@ def setup_client():
4749
print(f"Connecting to Hedera {network_name} network!")
4850
client = Client(network)
4951

50-
operator_id_str = os.getenv('OPERATOR_ID')
51-
operator_key_str = os.getenv('OPERATOR_KEY')
52+
operator_id_str = os.getenv("OPERATOR_ID")
53+
operator_key_str = os.getenv("OPERATOR_KEY")
5254

5355
if not operator_id_str or not operator_key_str:
5456
raise ValueError(
55-
"OPERATOR_ID and OPERATOR_KEY environment variables must be set")
57+
"OPERATOR_ID and OPERATOR_KEY environment variables must be set"
58+
)
5659

5760
operator_id = AccountId.from_string(operator_id_str)
5861
operator_key = PrivateKey.from_string(operator_key_str)
@@ -82,9 +85,7 @@ def create_account(client, operator_key, initial_balance=Hbar(10)):
8285

8386
# Create the account creation transaction
8487
transaction = AccountCreateTransaction(
85-
key=new_account_public_key,
86-
initial_balance=initial_balance,
87-
memo="New Account"
88+
key=new_account_public_key, initial_balance=initial_balance, memo="New Account"
8889
).freeze_with(client)
8990

9091
# Sign and execute the transaction
@@ -95,7 +96,8 @@ def create_account(client, operator_key, initial_balance=Hbar(10)):
9596
print(f"✓ Account created successfully")
9697
print(f" Account ID: {new_account_id}")
9798
print(
98-
f" Initial balance: {initial_balance.to_hbars()} hbars ({initial_balance.to_tinybars()} tinybars)\n")
99+
f" Initial balance: {initial_balance.to_hbars()} hbars ({initial_balance.to_tinybars()} tinybars)\n"
100+
)
99101

100102
return new_account_id, new_account_private_key
101103

@@ -136,7 +138,8 @@ def transfer_hbars(client, operator_id, operator_key, recipient_id, amount):
136138
str: The status of the transfer transaction.
137139
"""
138140
print(
139-
f"Transferring {amount.to_tinybars()} tinybars ({amount.to_hbars()} hbars) from {operator_id} to {recipient_id}...")
141+
f"Transferring {amount.to_tinybars()} tinybars ({amount.to_hbars()} hbars) from {operator_id} to {recipient_id}..."
142+
)
140143

141144
# Create transfer transaction
142145
transfer_transaction = (
@@ -166,7 +169,8 @@ def main():
166169

167170
# Create a new account with initial balance
168171
new_account_id, new_account_private_key = create_account(
169-
client, operator_key, initial_balance=Hbar(10))
172+
client, operator_key, initial_balance=Hbar(10)
173+
)
170174

171175
# Query and display the initial balance
172176
print("=" * 60)
@@ -182,7 +186,8 @@ def main():
182186
print("=" * 60)
183187
transfer_amount = Hbar(5)
184188
transfer_status = transfer_hbars(
185-
client, operator_id, operator_key, new_account_id, transfer_amount)
189+
client, operator_id, operator_key, new_account_id, transfer_amount
190+
)
186191
print(f"Transfer transaction status: {transfer_status}")
187192
print("=" * 60 + "\n")
188193

examples/query/account_balance_query_2.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@
1919
TokenInfoQuery,
2020
TokenType,
2121
TokenMintTransaction,
22-
2322
)
2423
from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery
2524
from hiero_sdk_python.tokens.token_id import TokenId
2625

2726

2827
# Load environment variables from .env file
2928
load_dotenv()
30-
network_name = os.getenv('NETWORK', 'testnet').lower()
31-
key_type = os.getenv('KEY_TYPE', 'ecdsa')
29+
network_name = os.getenv("NETWORK", "testnet").lower()
30+
key_type = os.getenv("KEY_TYPE", "ecdsa")
31+
3232

3333
def setup_client():
34-
"""Setup Client """
34+
"""Setup Client"""
3535
network = Network(network_name)
3636
print(f"Connecting to Hedera {network_name} network!")
3737
client = Client(network)
3838

3939
# Get the operator account from the .env file
4040
try:
41-
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID', ''))
42-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY', ''))
41+
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
42+
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
4343
# Set the operator (payer) account for the client
4444
client.set_operator(operator_id, operator_key)
4545
print(f"Client set up with operator id {client.operator_account_id}")
@@ -48,6 +48,7 @@ def setup_client():
4848
print("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
4949
sys.exit(1)
5050

51+
5152
def create_account(client, name, initial_balance=Hbar(10)):
5253
"""Create a test account with initial balance"""
5354
account_private_key = PrivateKey.generate(key_type)
@@ -70,6 +71,7 @@ def create_account(client, name, initial_balance=Hbar(10)):
7071
print(f"{name} account created with id: {account_id}")
7172
return account_id, account_private_key
7273

74+
7375
def create_and_mint_token(treasury_account_id, treasury_account_key, client):
7476
"""Create an NFT collection and mint metadata_list (default 3 items)."""
7577
metadata_list = [b"METADATA_A", b"METADATA_B", b"METADATA_C"]
@@ -79,35 +81,39 @@ def create_and_mint_token(treasury_account_id, treasury_account_key, client):
7981

8082
token_id = (
8183
TokenCreateTransaction()
82-
.set_token_name("My Awesome NFT").set_token_symbol("MANFT")
84+
.set_token_name("My Awesome NFT")
85+
.set_token_symbol("MANFT")
8386
.set_token_type(TokenType.NON_FUNGIBLE_UNIQUE)
8487
.set_treasury_account_id(treasury_account_id)
8588
.set_initial_supply(0)
8689
.set_supply_key(supply_key)
8790
.freeze_with(client)
88-
.sign(treasury_account_key).sign(supply_key).execute(client)
91+
.sign(treasury_account_key)
92+
.sign(supply_key)
93+
.execute(client)
8994
).token_id
9095

91-
TokenMintTransaction() \
92-
.set_token_id(token_id).set_metadata(metadata_list) \
93-
.freeze_with(client).sign(supply_key).execute(client)
96+
TokenMintTransaction().set_token_id(token_id).set_metadata(
97+
metadata_list
98+
).freeze_with(client).sign(supply_key).execute(client)
9499

95-
total_supply = TokenInfoQuery().set_token_id(token_id).execute(client).total_supply
100+
total_supply = (
101+
TokenInfoQuery().set_token_id(token_id).execute(client).total_supply
102+
)
96103
print(f"✅ Created NFT {token_id} — total supply: {total_supply}")
97104
return token_id
98105
except (ValueError, TypeError, RuntimeError, ConnectionError) as error:
99106
print(f"❌ Error creating token: {error}")
100107
sys.exit(1)
101108

109+
102110
def get_account_balance(client: Client, account_id: AccountId):
103111
"""Get account balance using CryptoGetAccountBalanceQuery"""
104112
print(f"Retrieving account balance for account id: {account_id} ...")
105113
try:
106114
# Use CryptoGetAccountBalanceQuery to get the account balance
107115
account_balance = (
108-
CryptoGetAccountBalanceQuery()
109-
.set_account_id(account_id)
110-
.execute(client)
116+
CryptoGetAccountBalanceQuery().set_account_id(account_id).execute(client)
111117
)
112118
print("✅ Account balance retrieved successfully!")
113119
print(f"💰 HBAR Balance for {account_id}: {account_balance.hbars} hbars")
@@ -120,10 +126,11 @@ def get_account_balance(client: Client, account_id: AccountId):
120126
print(f"Error retrieving account balance: {error}")
121127
sys.exit(1)
122128

123-
#OPTIONAL comparison function
124-
def compare_token_balances(client, treasury_id: AccountId,
125-
receiver_id: AccountId,
126-
token_id: TokenId):
129+
130+
# OPTIONAL comparison function
131+
def compare_token_balances(
132+
client, treasury_id: AccountId, receiver_id: AccountId, token_id: TokenId
133+
):
127134
"""Compare token balances between two accounts"""
128135
print(
129136
f"\n🔎 Comparing token balances for Token ID {token_id} "
@@ -139,6 +146,7 @@ def compare_token_balances(client, treasury_id: AccountId,
139146
print(f"🏷️ Token balance for Treasury ({treasury_id}): {treasury_token_balance}")
140147
print(f"🏷️ Token balance for Receiver ({receiver_id}): {receiver_token_balance}")
141148

149+
142150
def main():
143151
"""Main function to run the account balance query example
144152
1-Create test account with intial balance
@@ -151,14 +159,14 @@ def main():
151159
test_account_id, test_account_key = create_account(client, "Test Account")
152160
# Create the tokens with the test account as the treasury so minted tokens
153161
# will be owned by the test account and show up in its token balances.
154-
token_id = create_and_mint_token(
155-
test_account_id,
156-
test_account_key,
157-
client)
162+
token_id = create_and_mint_token(test_account_id, test_account_key, client)
158163
# Retrieve and display account balance for the test account
159164
get_account_balance(client, test_account_id)
160-
#OPTIONAL comparison of token balances between test account and operator account
161-
compare_token_balances(client, test_account_id, client.operator_account_id, token_id)
165+
# OPTIONAL comparison of token balances between test account and operator account
166+
compare_token_balances(
167+
client, test_account_id, client.operator_account_id, token_id
168+
)
169+
162170

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

0 commit comments

Comments
 (0)