From d59d25cb04fede317e143c4b377c74805fd94b31 Mon Sep 17 00:00:00 2001
From: julian <julian@cypherstack.com>
Date: Fri, 27 Jan 2023 14:44:00 -0600
Subject: [PATCH] add convenience function to add or update a list of addresses

---
 lib/db/main_db.dart | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/lib/db/main_db.dart b/lib/db/main_db.dart
index 53b6397ab..90293171c 100644
--- a/lib/db/main_db.dart
+++ b/lib/db/main_db.dart
@@ -48,6 +48,27 @@ class MainDB {
         await isar.addresses.putAll(addresses);
       });
 
+  Future<void> updateOrPutAddresses(List<Address> addresses) async {
+    await isar.writeTxn(() async {
+      for (final address in addresses) {
+        final storedAddress = await isar.addresses
+            .getByValueWalletId(address.value, address.walletId);
+
+        if (storedAddress == null) {
+          await isar.addresses.put(address);
+        } else {
+          address.id = storedAddress.id;
+          await storedAddress.transactions.load();
+          final txns = storedAddress.transactions.toList();
+          await isar.addresses.delete(storedAddress.id);
+          await isar.addresses.put(address);
+          address.transactions.addAll(txns);
+          await address.transactions.save();
+        }
+      }
+    });
+  }
+
   Future<Address?> getAddress(String walletId, String address) async {
     return isar.addresses.getByValueWalletId(address, walletId);
   }