address balance and txCount fixes, try/catch electrum call

This commit is contained in:
Matthew Fosse 2024-07-11 15:55:37 -07:00
parent c831b18360
commit d81eb0cdfc
2 changed files with 24 additions and 5 deletions

View file

@ -1230,10 +1230,17 @@ abstract class ElectrumWalletBase
@action
Future<List<BitcoinUnspent>> fetchUnspent(BitcoinAddressRecord address) async {
final unspents = await electrumClient.getListUnspent(address.getScriptHash(network));
List<Map<String, dynamic>> unspents = [];
List<BitcoinUnspent> 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);

View file

@ -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) {