btc update utxo set

This commit is contained in:
julian 2023-03-07 09:00:00 -06:00
parent 2ccf6bfc71
commit 6465faa4e1
3 changed files with 70 additions and 32 deletions

View file

@ -160,6 +160,28 @@ class MainDB {
await isar.utxos.putAll(utxos);
});
Future<void> updateUTXOs(String walletId, List<UTXO> utxos) async {
await isar.writeTxn(() async {
final set = utxos.toSet();
for (final utxo in utxos) {
// check if utxo exists in db and update accordingly
final storedUtxo = await isar.utxos
.where()
.txidWalletIdVoutEqualTo(utxo.txid, utxo.walletId, utxo.vout)
.findFirst();
if (storedUtxo != null) {
// update
set.remove(utxo);
set.add(storedUtxo);
}
}
await isar.utxos.where().walletIdEqualTo(walletId).deleteAll();
await isar.utxos.putAll(set.toList());
});
}
// transaction notes
QueryBuilder<TransactionNote, TransactionNote, QAfterWhereClause>
getTransactionNotes(String walletId) =>

View file

@ -4,7 +4,7 @@ import 'package:isar/isar.dart';
part 'utxo.g.dart';
@Collection(accessor: "utxos")
@Collection(accessor: "utxos", inheritance: false)
class UTXO {
UTXO({
required this.walletId,
@ -84,4 +84,16 @@ class UTXO {
"address: $address, "
"otherData: $otherData, "
"}";
@override
bool operator ==(Object other) {
return other is UTXO &&
other.walletId == walletId &&
other.txid == txid &&
other.vout == vout;
}
@override
@ignore
int get hashCode => Object.hashAll([walletId, txid, vout]);
}

View file

@ -1796,13 +1796,7 @@ class BitcoinWallet extends CoinServiceAPI
}
}
final currentChainHeight = await chainHeight;
final List<isar_models.UTXO> outputArray = [];
int satoshiBalanceTotal = 0;
int satoshiBalancePending = 0;
int satoshiBalanceSpendable = 0;
int satoshiBalanceBlocked = 0;
for (int i = 0; i < fetchedUtxoList.length; i++) {
for (int j = 0; j < fetchedUtxoList[i].length; j++) {
@ -1863,6 +1857,33 @@ class BitcoinWallet extends CoinServiceAPI
address: utxoOwnerAddress,
);
outputArray.add(utxo);
}
}
Logging.instance
.log('Outputs fetched: $outputArray', level: LogLevel.Info);
await db.updateUTXOs(walletId, outputArray);
// finally update balance
await _updateBalance();
} catch (e, s) {
Logging.instance
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
}
}
Future<void> _updateBalance() async {
final utxos = await db.getUTXOs(walletId).findAll();
final currentChainHeight = await chainHeight;
int satoshiBalanceTotal = 0;
int satoshiBalancePending = 0;
int satoshiBalanceSpendable = 0;
int satoshiBalanceBlocked = 0;
for (final utxo in utxos) {
satoshiBalanceTotal += utxo.value;
if (utxo.isBlocked) {
@ -1874,21 +1895,8 @@ class BitcoinWallet extends CoinServiceAPI
satoshiBalancePending += utxo.value;
}
}
outputArray.add(utxo);
}
}
Logging.instance
.log('Outputs fetched: $outputArray', level: LogLevel.Info);
// TODO move this out of here and into IDB
await db.isar.writeTxn(() async {
await db.isar.utxos.where().walletIdEqualTo(walletId).deleteAll();
await db.isar.utxos.putAll(outputArray);
});
// finally update balance
_balance = Balance(
coin: coin,
total: satoshiBalanceTotal,
@ -1897,10 +1905,6 @@ class BitcoinWallet extends CoinServiceAPI
pendingSpendable: satoshiBalancePending,
);
await updateCachedBalance(_balance!);
} catch (e, s) {
Logging.instance
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
}
}
@override