Skip to content

Commit 47cadeb

Browse files
refactor examples/account/account_create_trasaction_without_alias
Signed-off-by: tech0priyanshu <[email protected]>
1 parent 7f35aef commit 47cadeb

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

CHANGELOG.md

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

99
### Added
1010
- Beginner issue documentation and updated GFI and GFIC templates and documentation
11+
- Refactored `account_create_transaction_without_alias.py` into smaller, modular functions.
1112
- Enable auto assignment to good first issues (#1312), archived good first issue support team notification. Changed templates with new assign instruction.
1213
- Added unit test for 'endpoint.py' to increase coverage.
1314
- 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 & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,56 +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-
52-
def create_account_without_alias(client: Client) -> None:
53-
"""Create an account explicitly without an alias."""
54-
try:
55-
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
56-
account_private_key = PrivateKey.generate()
57-
account_public_key = account_private_key.public_key()
58-
59-
print(f"✅ Account public key (no alias): {account_public_key}")
60-
61-
print("\nSTEP 2: Creating the account without setting any alias...")
62-
63-
transaction = AccountCreateTransaction(
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(
6465
initial_balance=Hbar(5),
6566
memo="Account created without alias",
66-
).set_key_without_alias(account_private_key)
67-
68-
transaction = transaction.freeze_with(client).sign(account_private_key)
69-
70-
response = transaction.execute(client)
71-
new_account_id = response.account_id
72-
73-
if new_account_id is None:
74-
raise RuntimeError(
75-
"AccountID not found in receipt. Account may not have been created."
76-
)
77-
78-
print(f"✅ Account created with ID: {new_account_id}\n")
67+
)
68+
.set_key_without_alias(account_private_key)
69+
)
7970

80-
account_info = AccountInfoQuery().set_account_id(new_account_id).execute(client)
71+
transaction = (
72+
transaction.freeze_with(client)
73+
.sign(account_private_key)
74+
)
8175

82-
out = info_to_dict(account_info)
83-
print("Account Info:")
84-
print(json.dumps(out, indent=2) + "\n")
76+
response = transaction.execute(client)
77+
new_account_id = response.account_id
8578

86-
print(
87-
"✅ contract_account_id (no alias, zero-padded): "
88-
f"{account_info.contract_account_id}"
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-
except Exception as error:
92-
print(f"❌ Error: {error}")
93-
sys.exit(1)
94-
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+
)
95105

96106
def main():
97107
"""Main entry point."""
98-
client = setup_client()
99-
create_account_without_alias(client)
100-
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)
114+
except Exception as error:
115+
print(f"❌ Error: {error}")
116+
sys.exit(1)
101117

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

0 commit comments

Comments
 (0)