Skip to content

Commit 82ee2a8

Browse files
refactor examples/account/account_create_trasaction_without_alias
Signed-off-by: tech0priyanshu <[email protected]>
1 parent ec903c7 commit 82ee2a8

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10+
- Refactored `account_create_transaction_without_alias.py` into smaller, modular functions.
1011
- Enable auto assignment to good first issues (#1312), archived good first issue support team notification. Changed templates with new assign instruction.
1112
- Added unit test for 'endpoint.py' to increase coverage.
1213
- Automated assignment guard for `advanced` issues; requires completion of at least one `good first issue` and one `intermediate` issue before assignment (exempts maintainers, committers, and triage members). (#1142)

examples/account/account_create_transaction_without_alias.py

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,63 +48,72 @@ def setup_client():
4848
print("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
4949
sys.exit(1)
5050

51-
def create_account_without_alias(client: Client) -> None:
52-
"""Create an account explicitly without an alias."""
53-
try:
54-
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
55-
account_private_key = PrivateKey.generate()
56-
account_public_key = account_private_key.public_key()
57-
58-
print(f"✅ Account public key (no alias): {account_public_key}")
59-
60-
print("\nSTEP 2: Creating the account without setting any alias...")
61-
62-
transaction = (
63-
AccountCreateTransaction(
64-
initial_balance=Hbar(5),
65-
memo="Account created without alias",
66-
)
67-
.set_key_without_alias(account_private_key)
51+
def generate_account_key():
52+
"""Generate a key pair for the account."""
53+
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
54+
account_private_key = PrivateKey.generate()
55+
account_public_key = account_private_key.public_key()
56+
print(f"✅ Account public key (no alias): {account_public_key}")
57+
return account_private_key
58+
59+
def create_account_without_alias(client: Client, account_private_key) -> str:
60+
"""Create an account without setting any alias."""
61+
print("\nSTEP 2: Creating the account without setting any alias...")
62+
63+
transaction = (
64+
AccountCreateTransaction(
65+
initial_balance=Hbar(5),
66+
memo="Account created without alias",
6867
)
68+
.set_key_without_alias(account_private_key)
69+
)
6970

70-
transaction = (
71-
transaction.freeze_with(client)
72-
.sign(account_private_key)
73-
)
74-
75-
response = transaction.execute(client)
76-
new_account_id = response.account_id
71+
transaction = (
72+
transaction.freeze_with(client)
73+
.sign(account_private_key)
74+
)
7775

78-
if new_account_id is None:
79-
raise RuntimeError(
80-
"AccountID not found in receipt. Account may not have been created."
81-
)
76+
response = transaction.execute(client)
77+
new_account_id = response.account_id
8278

83-
print(f"✅ Account created with ID: {new_account_id}\n")
84-
85-
account_info = (
86-
AccountInfoQuery()
87-
.set_account_id(new_account_id)
88-
.execute(client)
79+
if new_account_id is None:
80+
raise RuntimeError(
81+
"AccountID not found in receipt. Account may not have been created."
8982
)
9083

91-
out = info_to_dict(account_info)
92-
print("Account Info:")
93-
print(json.dumps(out, indent=2) + "\n")
94-
95-
print(
96-
"✅ contract_account_id (no alias, zero-padded): "
97-
f"{account_info.contract_account_id}"
98-
)
84+
print(f"✅ Account created with ID: {new_account_id}\n")
85+
return new_account_id
86+
87+
def fetch_account_info(client: Client, account_id):
88+
"""Fetch account information."""
89+
account_info = (
90+
AccountInfoQuery()
91+
.set_account_id(account_id)
92+
.execute(client)
93+
)
94+
return account_info
95+
96+
def print_account_summary(account_info):
97+
"""Print account summary information."""
98+
out = info_to_dict(account_info)
99+
print("Account Info:")
100+
print(json.dumps(out, indent=2) + "\n")
101+
print(
102+
"✅ contract_account_id (no alias, zero-padded): "
103+
f"{account_info.contract_account_id}"
104+
)
99105

106+
def main():
107+
"""Main entry point."""
108+
try:
109+
client = setup_client()
110+
account_private_key = generate_account_key()
111+
new_account_id = create_account_without_alias(client, account_private_key)
112+
account_info = fetch_account_info(client, new_account_id)
113+
print_account_summary(account_info)
100114
except Exception as error:
101115
print(f"❌ Error: {error}")
102116
sys.exit(1)
103117

104-
def main():
105-
"""Main entry point."""
106-
client = setup_client()
107-
create_account_without_alias(client)
108-
109118
if __name__ == "__main__":
110119
main()

0 commit comments

Comments
 (0)