diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 2cbccb8df..2f49ae7b8 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1230,10 +1230,17 @@ abstract class ElectrumWalletBase @action Future> fetchUnspent(BitcoinAddressRecord address) async { - final unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); - + List> unspents = []; List updatedUnspentCoins = []; + try { + unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); + } catch (e, s) { + print(e); + print(s); + return []; + } + await Future.wait(unspents.map((unspent) async { try { final coin = BitcoinUnspent.fromJSON(address, unspent); diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index 4e5d1266f..b45e7dec4 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -216,7 +216,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { transactionHistory.clear(); mwebUtxosHeight = height; await walletInfo.updateRestoreHeight(height); - // processMwebUtxos(); print("STARTING SYNC"); await startSync(); } @@ -268,10 +267,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { final addressRecord = walletAddresses.allAddresses .firstWhereOrNull((addressRecord) => addressRecord.address == utxo.address); if (addressRecord == null) { - print("addressRecord is null! TODO: handle this case 1"); return; } - if (!(tx.inputAddresses?.contains(utxo.address) ?? false)) addressRecord.txCount++; + if (!(tx.inputAddresses?.contains(utxo.address) ?? false)) { + addressRecord.txCount++; + } addressRecord.balance += utxo.value.toInt(); addressRecord.setAsUsed(); @@ -288,6 +288,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { final req = UtxosRequest(scanSecret: hex.decode(scanSecret), fromHeight: restoreHeight); bool initDone = false; + // reset address balances and tx counts: + walletAddresses.allAddresses.forEach((addressRecord) { + addressRecord.balance = 0; + addressRecord.txCount = 0; + }); + for (final utxo in mwebUtxosBox.values) { if (utxo.address.isEmpty) { initDone = true; @@ -319,6 +325,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { value: sUtxo.value.toInt(), ); + if (mwebUtxosBox.containsKey(utxo.outputId)) { + // we've already stored this utxo, skip it: + continue; + } + if (utxo.address.isEmpty) { await updateUnspent(); await updateBalance(); @@ -488,6 +499,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { unspentCoins.forEach((coin) { if (coin.bitcoinAddressRecord is! BitcoinSilentPaymentAddressRecord) coin.bitcoinAddressRecord.balance = 0; + coin.bitcoinAddressRecord.txCount = 0; }); unspentCoins.forEach((coin) {