From 9307c08d4ded8d4f1ecc50227526151c77f9733b Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Wed, 29 Jun 2022 18:07:34 -0300 Subject: [PATCH] Treat some wallet-is-locked errors as trivial --- .../src/main/java/bisq/bots/AbstractBot.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/java-examples/src/main/java/bisq/bots/AbstractBot.java b/java-examples/src/main/java/bisq/bots/AbstractBot.java index 48425f0..94d8413 100644 --- a/java-examples/src/main/java/bisq/bots/AbstractBot.java +++ b/java-examples/src/main/java/bisq/bots/AbstractBot.java @@ -345,6 +345,19 @@ public abstract class AbstractBot { } } + /** + * Return true if gRPC StatusRuntimeException indicates wallet is locked. Sometimes this is a trivial error. + * + * @param grpcException a StatusRuntimeException + * @return true if gRPC StatusRuntimeException indicates wallet is locked + */ + protected boolean walletIsLocked(StatusRuntimeException grpcException) { + var errorMsg = toCleanErrorMessage.apply(grpcException).toLowerCase(); + return (exceptionHasStatus.test(grpcException, FAILED_PRECONDITION) + && errorMsg.contains("wallet") + && errorMsg.contains("locked")); + } + /** * Attempt to unlock the wallet for 1 second using the given password, and shut down the bot if the * password check fails for any reason. @@ -575,6 +588,22 @@ public abstract class AbstractBot { BotUtils.printTradesSummary(category, trades); } + /** + * Print the completed trades since midnight today, if the wallet is unlocked, else log an error message. + */ + protected void printTradesSummaryForTodayIfWalletIsUnlocked() { + try { + log.info("Here are today's completed trades:"); + printTradesSummaryForToday(CLOSED); + } catch (StatusRuntimeException grpcException) { + if (walletIsLocked(grpcException)) { + log.error("Cannot retrieve trades while API daemon's wallet is locked."); + } else { + throw grpcException; + } + } + } + /** * Print list of today's trade summaries to stdout. * @@ -813,9 +842,7 @@ public abstract class AbstractBot { * @param maxTakeOffers the max number of offers that can be taken during bot run */ protected void maybeShutdownAfterSuccessfulTradeCreation(int numOffersTaken, int maxTakeOffers) { - log.info("Here are today's completed trades:"); - printTradesSummaryForToday(CLOSED); - + printTradesSummaryForTodayIfWalletIsUnlocked(); if (!isDryRun) { // If the bot is not in dryrun mode, lock the wallet. If dryrun=true, leave the wallet unlocked until the // timeout expires, so the user can look at data in the daemon with the CLI (a convenience in dev/test).