11from __future__ import annotations
22
3+ import datetime
4+ import time
5+
36import pytest
47
58from hiero_sdk_python .account .account_create_transaction import AccountCreateTransaction
912from hiero_sdk_python .fees .fee_estimate_mode import FeeEstimateMode
1013from hiero_sdk_python .file .file_append_transaction import FileAppendTransaction
1114from hiero_sdk_python .file .file_id import FileId
15+ from hiero_sdk_python .hbar import Hbar
1216from hiero_sdk_python .query .fee_estimate_query import FeeEstimateQuery
17+ from hiero_sdk_python .transaction .transfer_transaction import TransferTransaction
18+
19+
20+ _fee_estimation_ready = False
21+ _fee_estimation_error : Exception | None = None
22+
23+
24+ def wait_for_mirror_node_sync ():
25+ """Create delay to allow the MirrorNode to update its internal state."""
26+ time .sleep (2.0 )
27+
28+
29+ def wait_for_fee_estimation_service_ready (env ):
30+ """
31+ Blocks until the Mirror Node's FeeEstimationService is ready.
32+ with a 10-minute timeout.
33+ """
34+ global _fee_estimation_ready , _fee_estimation_error
35+
36+ if _fee_estimation_ready :
37+ return
38+ if _fee_estimation_error :
39+ raise _fee_estimation_error
40+
41+ deadline = datetime .now () + datetime .timedelta (seconds = 600.0 )
42+ attempts = 0
43+ last_error = None
44+
45+ print ("Waiting for FeeEstimationService to get ready" )
46+
47+ while datetime .now () < deadline :
48+ attempts += 1
49+ try :
50+ probe = (
51+ TransferTransaction ()
52+ .add_hbar_transfer (env .operator_id , Hbar (- 1 ))
53+ .add_hbar_transfer (env .operator_id , Hbar (1 ))
54+ )
55+
56+ (FeeEstimateQuery ().set_mode (FeeEstimateMode .INTRINSIC ).set_transaction (probe ).execute (env .client ))
57+
58+ _fee_estimation_ready = True
59+
60+ print (f"FeeEstimationService ready after { attempts } attempts." )
61+ return
62+
63+ except Exception as e :
64+ last_error = e
65+ time .sleep (5.0 )
66+
67+ _fee_estimation_error = Exception (
68+ f"FeeEstimationService not became ready after { attempts } attempts. Last error: { last_error } "
69+ )
70+
71+ raise _fee_estimation_error
1372
1473
1574@pytest .mark .integration
1675def test_can_execute_fee_estimation_query (env ):
1776 """
1877 Integration test that verifies a fee estimation query executes successfully and returns a non-null result.
1978 """
79+ wait_for_fee_estimation_service_ready ()
2080 tx = AccountCreateTransaction ().set_key_without_alias (PrivateKey .generate_ed25519 ()).set_initial_balance (1 )
81+ wait_for_mirror_node_sync ()
2182 query = FeeEstimateQuery ().set_transaction (tx )
2283 result = query .execute (env .client )
2384
@@ -29,7 +90,9 @@ def test_can_execute_fee_estimation_query2(env):
2990 """Integration test that verifies a state-mode fee estimation query
3091 executes successfully and returns a non-null result.
3192 """
93+ wait_for_fee_estimation_service_ready ()
3294 tx = AccountCreateTransaction ().set_key_without_alias (PrivateKey .generate_ed25519 ()).set_initial_balance (1 )
95+ wait_for_mirror_node_sync ()
3396 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
3497 result = query .execute (env .client )
3598
@@ -41,7 +104,9 @@ def test__fee_estimation_query_chunk_tx_can_execute(env):
41104 """Integration test that verifies a state-mode fee estimation query executes
42105 successfully for a chunked file append transaction and returns a non-null result.
43106 """
107+ wait_for_fee_estimation_service_ready ()
44108 tx = FileAppendTransaction ().set_file_id (FileId (0 , 0 , 2 )).set_chunk_size (10 ).set_contents ("s" * 33 ) # 4 chunks
109+ wait_for_mirror_node_sync ()
45110
46111 tx .freeze_with (env .client )
47112 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
@@ -55,12 +120,11 @@ def test_can_execute_fee_estimation_query_chunk_tx(env):
55120 """Integration test that verifies a state-mode fee estimation query executes successfully
56121 for a chunked topic message submit transaction and returns a non-null result.
57122 """
123+ wait_for_fee_estimation_service_ready ()
58124 tx = (
59125 TopicMessageSubmitTransaction ().set_topic_id (TopicId (0 , 0 , 2 )).set_chunk_size (10 ).set_message ("s" * 20 )
60126 ) # 2 chunks
61-
62- # 2. IMPORTANT: Let freeze_with generate the valid transaction ID sequence
63- # This ensures tx._transaction_ids is populated correctly.
127+ wait_for_mirror_node_sync ()
64128
65129 tx .freeze_with (env .client )
66130 query = FeeEstimateQuery ().set_mode (FeeEstimateMode .STATE ).set_transaction (tx )
0 commit comments