1+ """
2+ uv run examples/account/account_id_populate_from_mirror.py
3+ python examples/account/account_id_populate_from_mirror.py
4+
5+ This example demonstrates how to populate AccountId fields
6+ using mirror node lookups:
7+
8+ 1. Create an AccountId from an EVM address
9+ 2. Trigger auto account creation via HBAR transfer
10+ 3. Populate account number (num) from mirror node
11+ 4. Populate EVM address from mirror node
12+ """
13+
14+ import sys
15+ import time
16+
17+ from hiero_sdk_python import (
18+ Client ,
19+ AccountId ,
20+ PrivateKey ,
21+ TransferTransaction ,
22+ Hbar ,
23+ TransactionGetReceiptQuery
24+ )
25+
26+ def generate_evm_address ():
27+ """
28+ Generates a new ECDSA key pair and returns its EVM address.
29+ """
30+ private_key = PrivateKey .generate_ecdsa ()
31+ return private_key .public_key ().to_evm_address ()
32+
33+
34+ def auto_create_account (client , evm_address ):
35+ """
36+ Triggers auto account creation by transferring HBAR
37+ to an EVM address.
38+ """
39+ print ("\n Auto Account Creation..." )
40+
41+ try :
42+ evm_account_id = AccountId .from_evm_address (evm_address , 0 , 0 )
43+
44+ transfer_tx = (
45+ TransferTransaction ()
46+ .add_hbar_transfer (evm_account_id , Hbar (1 ).to_tinybars ())
47+ .add_hbar_transfer (client .operator_account_id , Hbar (- 1 ).to_tinybars ())
48+ .execute (client )
49+ )
50+
51+ receipt = (
52+ TransactionGetReceiptQuery ()
53+ .set_transaction_id (transfer_tx .transaction_id )
54+ .set_include_children (True )
55+ .execute (client )
56+ )
57+ except Exception as e :
58+ print (f"Failed during auto account creation tx: { e } " )
59+ sys .exit (1 )
60+
61+ if not receipt .children :
62+ print ("Auto account creation failed: no child receipts found" )
63+ sys .exit (1 )
64+
65+ account_id = receipt .children [0 ].account_id
66+ print (f"Auto-created account: { account_id } " )
67+ return account_id
68+
69+
70+ def populate_account_num_example (client , evm_address , created_account_id ):
71+ """
72+ Demonstrates populating AccountId.num from the mirror node.
73+ """
74+ print ("\n Example 1: Populate Account Number from Mirror Node..." )
75+
76+ mirror_account_id = AccountId .from_evm_address (evm_address , 0 , 0 )
77+ print (f"Before populate: num = { mirror_account_id .num } " )
78+
79+ time .sleep (5 )
80+
81+ try :
82+ mirror_account_id .populate_account_num (client )
83+ except Exception as e :
84+ print (f"Failed to populate account number: { e } " )
85+ sys .exit (1 )
86+
87+ print ("After populate:" )
88+ print (f" Shard: { mirror_account_id .shard } " )
89+ print (f" Realm: { mirror_account_id .realm } " )
90+ print (f" Num: { mirror_account_id .num } " )
91+
92+ if mirror_account_id .num != created_account_id .num :
93+ print (
94+ "Account number mismatch:\n "
95+ f" Expected: { created_account_id .num } \n "
96+ f" Got: { mirror_account_id .num } "
97+ )
98+ sys .exit (1 )
99+
100+
101+ def populate_evm_address_example (client , created_account_id , evm_address ):
102+ """
103+ Demonstrates populating AccountId.evm_address from the mirror node.
104+ """
105+ print ("\n Example 2: Populate EVM Address from Mirror Node" )
106+
107+ print (f"Before populate: evm_address = { created_account_id .evm_address } " )
108+
109+ time .sleep (5 )
110+
111+ try :
112+ created_account_id .populate_evm_address (client )
113+ except Exception as e :
114+ print (f"Failed to populate EVM address: { e } " )
115+ sys .exit (1 )
116+
117+ print (f"After populate: evm_address = { created_account_id .evm_address } " )
118+
119+ if created_account_id .evm_address != evm_address :
120+ print (
121+ "EVM address mismatch:\n "
122+ f" Expected: { evm_address } \n "
123+ f" Got: { created_account_id .evm_address } "
124+ )
125+ sys .exit (1 )
126+
127+
128+ def main ():
129+ client = Client .from_env ()
130+
131+ print (f"Client set up with operator id { client .operator_account_id } " )
132+
133+ evm_address = generate_evm_address ()
134+ print (f"Generated EVM address: { evm_address } " )
135+
136+ created_account_id = auto_create_account (client , evm_address )
137+
138+ populate_account_num_example (client , evm_address , created_account_id )
139+ populate_evm_address_example (client , created_account_id , evm_address )
140+
141+
142+
143+ if __name__ == "__main__" :
144+ main ()
0 commit comments