Skip to content

Commit c76ffa3

Browse files
fix: implement retry mechanism for FeeEstimateQuery in example
1 parent 3d6b748 commit c76ffa3

1 file changed

Lines changed: 46 additions & 35 deletions

File tree

examples/src/main/java/com/hedera/hashgraph/sdk/examples/FeeEstimateQueryExample.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ public static void main(String[] args) throws Exception {
6161
TransferTransaction tx = createTransferTransaction(client, recipientId);
6262
FeeEstimateResponse stateEstimate = estimateWithStateMode(client, tx);
6363
FeeEstimateResponse intrinsicEstimate = estimateWithIntrinsicMode(client, tx);
64-
if (stateEstimate != null && intrinsicEstimate != null) {
65-
compareEstimates(stateEstimate, intrinsicEstimate);
66-
} else {
67-
System.out.println("\n=== Skipping Comparison due to missing estimate ===");
68-
}
64+
65+
compareEstimates(stateEstimate, intrinsicEstimate);
66+
6967
demonstrateTokenCreationEstimate(client);
7068

7169
client.close();
@@ -104,24 +102,19 @@ private static TransferTransaction createTransferTransaction(Client client, Acco
104102
private static FeeEstimateResponse estimateWithStateMode(Client client, TransferTransaction tx) throws Exception {
105103
System.out.println("\n=== Estimating Fees with STATE Mode ===");
106104

107-
try {
108-
FeeEstimateResponse stateEstimate = new FeeEstimateQuery()
109-
.setMode(FeeEstimateMode.STATE)
110-
.setTransaction(tx)
111-
.execute(client);
112-
113-
printNetworkFee(stateEstimate);
114-
printNodeFee(stateEstimate);
115-
printServiceFee(stateEstimate);
116-
printTotalFee(stateEstimate);
117-
System.out.println("\nHigh Volume Multiplier: " + stateEstimate.getHighVolumeMultiplier());
118-
119-
return stateEstimate;
120-
} catch (IllegalStateException e) {
121-
System.err.println("Failed to estimate fees with STATE mode: " + e.getMessage());
122-
System.err.println("This might be due to compatibility issues with the Mirror Node version.");
123-
return null;
124-
}
105+
FeeEstimateQuery query = new FeeEstimateQuery()
106+
.setMode(FeeEstimateMode.STATE)
107+
.setTransaction(tx);
108+
109+
FeeEstimateResponse stateEstimate = executeWithRetry(query, client);
110+
111+
printNetworkFee(stateEstimate);
112+
printNodeFee(stateEstimate);
113+
printServiceFee(stateEstimate);
114+
printTotalFee(stateEstimate);
115+
System.out.println("\nHigh Volume Multiplier: " + stateEstimate.getHighVolumeMultiplier());
116+
117+
return stateEstimate;
125118
}
126119

127120
private static void printNetworkFee(FeeEstimateResponse estimate) {
@@ -161,10 +154,11 @@ private static FeeEstimateResponse estimateWithIntrinsicMode(Client client, Tran
161154
throws Exception {
162155
System.out.println("\n=== Estimating Fees with INTRINSIC Mode ===");
163156

164-
FeeEstimateResponse intrinsicEstimate = new FeeEstimateQuery()
157+
FeeEstimateQuery query = new FeeEstimateQuery()
165158
.setMode(FeeEstimateMode.INTRINSIC)
166-
.setTransaction(tx)
167-
.execute(client);
159+
.setTransaction(tx);
160+
161+
FeeEstimateResponse intrinsicEstimate = executeWithRetry(query, client);
168162

169163
System.out.println(
170164
"Network Fee Subtotal: " + intrinsicEstimate.getNetwork().getSubtotal() + " tinycents");
@@ -197,16 +191,33 @@ private static void demonstrateTokenCreationEstimate(Client client) throws Excep
197191
.freezeWith(client)
198192
.signWithOperator(client);
199193

200-
try {
201-
FeeEstimateResponse tokenEstimate = new FeeEstimateQuery()
202-
.setMode(FeeEstimateMode.STATE)
203-
.setTransaction(tokenTx)
204-
.execute(client);
194+
FeeEstimateQuery query = new FeeEstimateQuery()
195+
.setMode(FeeEstimateMode.STATE)
196+
.setTransaction(tokenTx);
197+
198+
FeeEstimateResponse tokenEstimate = executeWithRetry(query, client);
199+
200+
System.out.println("Token Creation Estimated Fee: " + tokenEstimate.getTotal() + " tinycents");
201+
System.out.println("Token Creation Estimated Fee: " + Hbar.fromTinybars(tokenEstimate.getTotal() / 100));
202+
}
205203

206-
System.out.println("Token Creation Estimated Fee: " + tokenEstimate.getTotal() + " tinycents");
207-
System.out.println("Token Creation Estimated Fee: " + Hbar.fromTinybars(tokenEstimate.getTotal() / 100));
208-
} catch (IllegalStateException e) {
209-
System.err.println("Failed to estimate token creation fees: " + e.getMessage());
204+
private static FeeEstimateResponse executeWithRetry(FeeEstimateQuery query, Client client) throws Exception {
205+
int maxAttempts = 5;
206+
int waitMillis = 2000;
207+
IllegalStateException lastException = null;
208+
209+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
210+
try {
211+
return query.execute(client);
212+
} catch (IllegalStateException e) {
213+
lastException = e;
214+
System.err.println("Attempt " + attempt + " failed: " + e.getMessage());
215+
if (attempt < maxAttempts) {
216+
System.out.println("Waiting " + (waitMillis / 1000) + " seconds before retry...");
217+
Thread.sleep(waitMillis);
218+
}
219+
}
210220
}
221+
throw new RuntimeException("Failed to execute FeeEstimateQuery after " + maxAttempts + " attempts", lastException);
211222
}
212223
}

0 commit comments

Comments
 (0)