Skip to content

Commit 6d0a95e

Browse files
committed
fix mweb output spent status (balance fix)
1 parent f54f9bf commit 6d0a95e

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

lib/wallets/wallet/wallet_mixin_interfaces/mweb_interface.dart

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,28 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
216216
(e) => e.outputId.equals(utxo.outputId),
217217
)).getSingleOrNull();
218218

219-
final newUtxo = MwebUtxosCompanion(
220-
outputId: Value(prev?.outputId ?? utxo.outputId),
221-
address: Value(prev?.address ?? utxo.address),
222-
value: Value(utxo.value.toInt()),
223-
height: Value(utxo.height),
224-
blockTime: Value(utxo.blockTime),
225-
blocked: Value(prev?.blocked ?? false),
226-
used: Value(prev?.used ?? false),
227-
);
219+
if (prev == null) {
220+
final newUtxo = MwebUtxosCompanion(
221+
outputId: Value(utxo.outputId),
222+
address: Value(utxo.address),
223+
value: Value(utxo.value.toInt()),
224+
height: Value(utxo.height),
225+
blockTime: Value(utxo.blockTime),
226+
blocked: const Value(false),
227+
used: const Value(false),
228+
);
228229

229-
await db.into(db.mwebUtxos).insertOnConflictUpdate(newUtxo);
230+
await db.into(db.mwebUtxos).insert(newUtxo);
231+
} else {
232+
await db
233+
.update(db.mwebUtxos)
234+
.replace(
235+
prev.copyWith(
236+
blockTime: utxo.blockTime,
237+
height: utxo.height,
238+
),
239+
);
240+
}
230241
});
231242

232243
Address? addr = await mainDB.getAddress(walletId, utxo.address);
@@ -469,7 +480,19 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
469480
);
470481

471482
// Update used mweb utxos as used in database
472-
await _checkSpentMwebUtxos();
483+
final usedMwebUtxos =
484+
txData.usedUTXOs!.whereType<MwebInput>().map((e) => e.utxo).toList();
485+
486+
Logging.instance.i("Used mweb inputs: $usedMwebUtxos");
487+
488+
if (usedMwebUtxos.isNotEmpty) {
489+
final db = Drift.get(walletId);
490+
await db.transaction(() async {
491+
for (final used in usedMwebUtxos) {
492+
await db.update(db.mwebUtxos).replace(used);
493+
}
494+
});
495+
}
473496

474497
return await updateSentCachedTxData(txData: txData);
475498
} catch (e, s) {
@@ -583,35 +606,6 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
583606
}
584607
}
585608

586-
Future<void> _checkSpentMwebUtxos() async {
587-
try {
588-
final db = Drift.get(walletId);
589-
final mwebUtxos = await db.select(db.mwebUtxos).get();
590-
591-
final client = await _client;
592-
593-
final spent = await client.spent(
594-
SpentRequest(outputId: mwebUtxos.map((e) => e.outputId)),
595-
);
596-
597-
await db.transaction(() async {
598-
for (final utxo in mwebUtxos) {
599-
await db
600-
.into(db.mwebUtxos)
601-
.insertOnConflictUpdate(
602-
utxo
603-
.toCompanion(false)
604-
.copyWith(
605-
used: Value(spent.outputId.contains(utxo.outputId)),
606-
),
607-
);
608-
}
609-
});
610-
} catch (e, s) {
611-
Logging.instance.e("_checkSpentMwebUtxos()", error: e, stackTrace: s);
612-
}
613-
}
614-
615609
Future<void> _checkAddresses() async {
616610
// check change first as it is index 0
617611
Address? changeAddress = await getMwebChangeAddress();
@@ -667,8 +661,6 @@ mixin MwebInterface<T extends ElectrumXCurrencyInterface>
667661
if (info.isMwebEnabled) {
668662
final start = DateTime.now();
669663
try {
670-
await _checkSpentMwebUtxos();
671-
672664
final currentHeight = await chainHeight;
673665
final db = Drift.get(walletId);
674666
final mwebUtxos =

0 commit comments

Comments
 (0)