Skip to content

Commit d63c982

Browse files
fix: handle FeeEstimateQuery failures and avoid duplicate transactions
Signed-off-by: imshubham22apr-gif <imshubham.22apr@gmail.com>
1 parent e4b9560 commit d63c982

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ 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-
compareEstimates(stateEstimate, intrinsicEstimate);
64+
if (stateEstimate != null && intrinsicEstimate != null) {
65+
compareEstimates(stateEstimate, intrinsicEstimate);
66+
} else {
67+
System.out.println("\n=== Skipping Comparison due to missing estimate ===");
68+
}
6569
demonstrateTokenCreationEstimate(client);
6670

6771
client.close();
@@ -100,18 +104,24 @@ private static TransferTransaction createTransferTransaction(Client client, Acco
100104
private static FeeEstimateResponse estimateWithStateMode(Client client, TransferTransaction tx) throws Exception {
101105
System.out.println("\n=== Estimating Fees with STATE Mode ===");
102106

103-
FeeEstimateResponse stateEstimate = new FeeEstimateQuery()
104-
.setMode(FeeEstimateMode.STATE)
105-
.setTransaction(tx)
106-
.execute(client);
107-
108-
printNetworkFee(stateEstimate);
109-
printNodeFee(stateEstimate);
110-
printServiceFee(stateEstimate);
111-
printTotalFee(stateEstimate);
112-
System.out.println("\nHigh Volume Multiplier: " + stateEstimate.getHighVolumeMultiplier());
113-
114-
return stateEstimate;
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+
}
115125
}
116126

117127
private static void printNetworkFee(FeeEstimateResponse estimate) {
@@ -187,12 +197,16 @@ private static void demonstrateTokenCreationEstimate(Client client) throws Excep
187197
.freezeWith(client)
188198
.signWithOperator(client);
189199

190-
FeeEstimateResponse tokenEstimate = new FeeEstimateQuery()
191-
.setMode(FeeEstimateMode.STATE)
192-
.setTransaction(tokenTx)
193-
.execute(client);
200+
try {
201+
FeeEstimateResponse tokenEstimate = new FeeEstimateQuery()
202+
.setMode(FeeEstimateMode.STATE)
203+
.setTransaction(tokenTx)
204+
.execute(client);
194205

195-
System.out.println("Token Creation Estimated Fee: " + tokenEstimate.getTotal() + " tinycents");
196-
System.out.println("Token Creation Estimated Fee: " + Hbar.fromTinybars(tokenEstimate.getTotal() / 100));
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());
210+
}
197211
}
198212
}

sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ContractFunctionParametersIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,8 @@ void canCallContractFunctionInt200Min() throws Exception {
28352835
void canCallContractFunctionInt200Max() throws Exception {
28362836
BigInteger int200Max = new BigInteger("803469022129495137770981046170581301261101496891396417650687");
28372837

2838+
Thread.sleep(10); // Avoid DUPLICATE_TRANSACTION
2839+
28382840
var response = new ContractCallQuery()
28392841
.setContractId(contractId)
28402842
.setGas(30_000)

0 commit comments

Comments
 (0)