From 88f0ad543a2e67db6b7d399962b1061bf52fcaa0 Mon Sep 17 00:00:00 2001
From: woodser <woodser@protonmail.com>
Date: Fri, 10 Feb 2023 09:02:26 -0500
Subject: [PATCH] sync idle trades once in background after active trades

---
 core/src/main/java/bisq/core/trade/Trade.java | 20 +++++++++++--------
 .../java/bisq/core/trade/TradeManager.java    |  7 +++++++
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/core/src/main/java/bisq/core/trade/Trade.java b/core/src/main/java/bisq/core/trade/Trade.java
index 5053e5d017..f608faf2cc 100644
--- a/core/src/main/java/bisq/core/trade/Trade.java
+++ b/core/src/main/java/bisq/core/trade/Trade.java
@@ -743,6 +743,10 @@ public abstract class Trade implements Tradable, Model {
         }
     }
 
+    public boolean isIdling() {
+        return this instanceof ArbitratorTrade && isDepositsConfirmed() && walletExists(); // arbitrator idles trade after deposits confirm
+    }
+
     public void syncWallet() {
         if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId());
         if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId());
@@ -750,7 +754,6 @@ public abstract class Trade implements Tradable, Model {
         getWallet().sync();
         pollWallet();
         log.info("Done syncing wallet for {} {}", getClass().getSimpleName(), getId());
-        updateWalletRefreshPeriod();
     }
 
     private void trySyncWallet() {
@@ -1634,11 +1637,16 @@ public abstract class Trade implements Tradable, Model {
     }
 
     private void updateSyncing() {
-        if (!isIdling()) trySyncWallet();
-        else {
+        if (!isIdling()) {
+            trySyncWallet();
+            updateWalletRefreshPeriod();
+        }  else {
             long startSyncingInMs = ThreadLocalRandom.current().nextLong(0, getWalletRefreshPeriod()); // random time to start syncing
             UserThread.runAfter(() -> {
-                if (isInitialized) trySyncWallet();
+                if (isInitialized) {
+                    trySyncWallet();
+                    updateWalletRefreshPeriod();
+                }
             }, startSyncingInMs / 1000l);
         }
     }
@@ -1744,10 +1752,6 @@ public abstract class Trade implements Tradable, Model {
         return xmrWalletService.getConnectionsService().getDefaultRefreshPeriodMs();
     }
 
-    private boolean isIdling() {
-        return this instanceof ArbitratorTrade && isDepositsConfirmed(); // arbitrator idles trade after deposits confirm
-    }
-
     private void setStateDepositsPublished() {
         if (!isDepositsPublished()) setState(State.DEPOSIT_TXS_SEEN_IN_NETWORK);
     }
diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java
index 6adc459063..fe18d3d487 100644
--- a/core/src/main/java/bisq/core/trade/TradeManager.java
+++ b/core/src/main/java/bisq/core/trade/TradeManager.java
@@ -417,6 +417,13 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
         String referralId = referralIdService.getOptionalReferralId().orElse(null);
         boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
         tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
+
+        // sync idle trades once in background after active trades
+        for (Trade trade : trades) {
+            if (trade.isIdling())  {
+                HavenoUtils.submitTask(() -> trade.syncWallet());
+            }
+        }
     }
 
     private void initPersistedTrade(Trade trade) {