From dadae35c955fa77074ebdbae2a0a7ed00391af82 Mon Sep 17 00:00:00 2001
From: Matthew Fosse <matt@fosse.co>
Date: Tue, 27 Aug 2024 11:44:18 -0400
Subject: [PATCH] fix regression

---
 cw_bitcoin/lib/electrum_wallet.dart | 24 +++++------
 cw_bitcoin/lib/litecoin_wallet.dart | 66 +++++++++++++++--------------
 2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart
index 1952c78da..f1aea380c 100644
--- a/cw_bitcoin/lib/electrum_wallet.dart
+++ b/cw_bitcoin/lib/electrum_wallet.dart
@@ -1262,23 +1262,20 @@ abstract class ElectrumWalletBase
       updatedUnspentCoins.addAll(await fetchUnspent(address));
     }));
 
+    unspentCoins = updatedUnspentCoins;
+
     if (unspentCoinsInfo.length != updatedUnspentCoins.length) {
-      updatedUnspentCoins.forEach((coin) => addCoinInfo(coin));
-    }
-
-    await updateCoins(updatedUnspentCoins, set: true);
-    await _refreshUnspentCoinsInfo();
-  }
-
-  Future<void> updateCoins(List<BitcoinUnspent> newUnspentCoins, {bool set = false}) async {
-    if (newUnspentCoins.isEmpty) {
+      unspentCoins.forEach((coin) => addCoinInfo(coin));
       return;
     }
 
-    if (set) {
-      unspentCoins = newUnspentCoins;
-    } else {
-      unspentCoins.addAll(newUnspentCoins);
+    await updateCoins(unspentCoins);
+    await _refreshUnspentCoinsInfo();
+  }
+
+  Future<void> updateCoins(List<BitcoinUnspent> newUnspentCoins) async {
+    if (newUnspentCoins.isEmpty) {
+      return;
     }
 
     newUnspentCoins.forEach((coin) {
@@ -1467,7 +1464,6 @@ abstract class ElectrumWalletBase
       // Create a list of available outputs
       final outputs = <BitcoinOutput>[];
       for (final out in bundle.originalTransaction.outputs) {
-
         // Check if the script contains OP_RETURN
         final script = out.scriptPubKey.script;
         if (script.contains('OP_RETURN') && memo == null) {
diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart
index ea3c395cc..824d8d6da 100644
--- a/cw_bitcoin/lib/litecoin_wallet.dart
+++ b/cw_bitcoin/lib/litecoin_wallet.dart
@@ -226,40 +226,43 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
     await updateBalance();
 
     _syncTimer?.cancel();
-    _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async {
-      if (syncStatus is FailedSyncStatus) return;
-      final nodeHeight = await electrumClient.getCurrentBlockChainTip() ?? 0;
-      final resp = await _stub.status(StatusRequest());
+    // delay the timer by a second so we don't overrride the restoreheight if one is set
+    Timer(const Duration(seconds: 1), () async {
+      _syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async {
+        if (syncStatus is FailedSyncStatus) return;
+        final nodeHeight = await electrumClient.getCurrentBlockChainTip() ?? 0;
+        final resp = await _stub.status(StatusRequest());
 
-      if (resp.blockHeaderHeight < nodeHeight) {
-        int h = resp.blockHeaderHeight;
-        syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight);
-      } else if (resp.mwebHeaderHeight < nodeHeight) {
-        int h = resp.mwebHeaderHeight;
-        syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight);
-      } else if (resp.mwebUtxosHeight < nodeHeight) {
-        syncStatus = SyncingSyncStatus(1, 0.999);
-      } else {
-        // prevent unnecessary reaction triggers:
-        if (syncStatus is! SyncedSyncStatus) {
-          syncStatus = SyncedSyncStatus();
-        }
-
-        if (resp.mwebUtxosHeight > walletInfo.restoreHeight) {
-          await walletInfo.updateRestoreHeight(resp.mwebUtxosHeight);
-          await checkMwebUtxosSpent();
-          // update the confirmations for each transaction:
-          for (final transaction in transactionHistory.transactions.values) {
-            if (transaction.isPending) continue;
-            int txHeight = transaction.height ?? resp.mwebUtxosHeight;
-            final confirmations = (resp.mwebUtxosHeight - txHeight) + 1;
-            if (transaction.confirmations == confirmations) continue;
-            transaction.confirmations = confirmations;
-            transactionHistory.addOne(transaction);
+        if (resp.blockHeaderHeight < nodeHeight) {
+          int h = resp.blockHeaderHeight;
+          syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight);
+        } else if (resp.mwebHeaderHeight < nodeHeight) {
+          int h = resp.mwebHeaderHeight;
+          syncStatus = SyncingSyncStatus(nodeHeight - h, h / nodeHeight);
+        } else if (resp.mwebUtxosHeight < nodeHeight) {
+          syncStatus = SyncingSyncStatus(1, 0.999);
+        } else {
+          // prevent unnecessary reaction triggers:
+          if (syncStatus is! SyncedSyncStatus) {
+            syncStatus = SyncedSyncStatus();
+          }
+
+          if (resp.mwebUtxosHeight > walletInfo.restoreHeight) {
+            await walletInfo.updateRestoreHeight(resp.mwebUtxosHeight);
+            await checkMwebUtxosSpent();
+            // update the confirmations for each transaction:
+            for (final transaction in transactionHistory.transactions.values) {
+              if (transaction.isPending) continue;
+              int txHeight = transaction.height ?? resp.mwebUtxosHeight;
+              final confirmations = (resp.mwebUtxosHeight - txHeight) + 1;
+              if (transaction.confirmations == confirmations) continue;
+              transaction.confirmations = confirmations;
+              transactionHistory.addOne(transaction);
+            }
+            await transactionHistory.save();
           }
-          await transactionHistory.save();
         }
-      }
+      });
     });
     // this runs in the background and processes new utxos as they come in:
     processMwebUtxos();
@@ -305,6 +308,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
   }) async {
     await mwebUtxosBox.clear();
     transactionHistory.clear();
+    _syncTimer?.cancel();
     await walletInfo.updateRestoreHeight(height);
 
     // reset coin balances and txCount to 0: