11from __future__ import annotations
22
3+ import time
4+
35import pytest
46
57from hiero_sdk_python .account .account_create_transaction import AccountCreateTransaction
911from hiero_sdk_python .fees .fee_estimate_mode import FeeEstimateMode
1012from hiero_sdk_python .file .file_append_transaction import FileAppendTransaction
1113from hiero_sdk_python .file .file_id import FileId
14+ from hiero_sdk_python .hbar import Hbar
1215from hiero_sdk_python .query .fee_estimate_query import FeeEstimateQuery
16+ from hiero_sdk_python .transaction .transfer_transaction import TransferTransaction
17+
18+
19+ _fee_estimation_ready = False
20+ _fee_estimation_error : Exception | None = None
21+
22+
23+ # Wait until the mirror_node FeeEstimationService can return a good mock query.
24+ def wait_for_fee_estimation_service_ready (env ):
25+ """Wait until the FeeEstimationService is ready."""
26+ global _fee_estimation_ready , _fee_estimation_error
27+
28+ if _fee_estimation_ready :
29+ return
30+ if _fee_estimation_error :
31+ raise _fee_estimation_error
32+
33+ attempts = 0
34+ last_error = None
35+
36+ print ("Waiting for FeeEstimationService to get ready..." )
37+
38+ deadline = time .monotonic () + 600.0
39+ while time .monotonic () < deadline :
40+ attempts += 1
41+ try :
42+ probe = (
43+ TransferTransaction ()
44+ .add_hbar_transfer (env .operator_id , Hbar (- 1 ))
45+ .add_hbar_transfer (env .operator_id , Hbar (1 ))
46+ )
47+
48+ FeeEstimateQuery ().set_mode (FeeEstimateMode .INTRINSIC ).set_transaction (probe ).execute (env .client )
49+
50+ _fee_estimation_ready = True
51+
52+ print (f"FeeEstimationService ready after { attempts } attempts." )
53+ return
54+
55+ except Exception as e :
56+ last_error = e
57+ time .sleep (5.0 )
58+
59+ _fee_estimation_error = Exception (
60+ f"FeeEstimationService not became ready after { attempts } attempts. Last error: { last_error } "
61+ )
62+
63+ raise _fee_estimation_error
64+
65+
66+ def wait_for_sync ():
67+ """Additional wait to ensure the mirror_node sync."""
68+ time .sleep (2.0 )
1369
1470
1571@pytest .mark .integration
1672def test_can_execute_fee_estimation_query (env ):
1773 """
1874 Integration test that verifies a fee estimation query executes successfully and returns a non-null result.
1975 """
76+ wait_for_fee_estimation_service_ready (env )
2077 tx = AccountCreateTransaction ().set_key_without_alias (PrivateKey .generate_ed25519 ()).set_initial_balance (1 )
78+ wait_for_sync ()
79+
2180 query = FeeEstimateQuery ().set_transaction (tx )
2281 result = query .execute (env .client )
2382
@@ -29,7 +88,10 @@ def test_can_execute_fee_estimation_query2(env):
2988 """Integration test that verifies a state-mode fee estimation query
3089 executes successfully and returns a non-null result.
3190 """
91+ wait_for_fee_estimation_service_ready (env )
3292 tx = AccountCreateTransaction ().set_key_without_alias (PrivateKey .generate_ed25519 ()).set_initial_balance (1 )
93+ wait_for_sync ()
94+
3395 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
3496 result = query .execute (env .client )
3597
@@ -41,9 +103,11 @@ def test__fee_estimation_query_chunk_tx_can_execute(env):
41103 """Integration test that verifies a state-mode fee estimation query executes
42104 successfully for a chunked file append transaction and returns a non-null result.
43105 """
106+ wait_for_fee_estimation_service_ready (env )
44107 tx = FileAppendTransaction ().set_file_id (FileId (0 , 0 , 2 )).set_chunk_size (10 ).set_contents ("s" * 33 ) # 4 chunks
45-
46108 tx .freeze_with (env .client )
109+ wait_for_sync ()
110+
47111 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
48112 result = query .execute (env .client )
49113
@@ -55,14 +119,13 @@ def test_can_execute_fee_estimation_query_chunk_tx(env):
55119 """Integration test that verifies a state-mode fee estimation query executes successfully
56120 for a chunked topic message submit transaction and returns a non-null result.
57121 """
122+ wait_for_fee_estimation_service_ready (env )
58123 tx = (
59124 TopicMessageSubmitTransaction ().set_topic_id (TopicId (0 , 0 , 2 )).set_chunk_size (10 ).set_message ("s" * 20 )
60125 ) # 2 chunks
61-
62- # 2. IMPORTANT: Let freeze_with generate the valid transaction ID sequence
63- # This ensures tx._transaction_ids is populated correctly.
64-
65126 tx .freeze_with (env .client )
127+ wait_for_sync ()
128+
66129 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
67130 result = query .execute (env .client )
68131
0 commit comments