2023-01-12 18:46:01 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-01-16 21:04:03 +00:00
|
|
|
import 'package:stackwallet/db/main_db.dart';
|
2023-01-12 18:46:01 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2023-01-13 21:36:19 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2023-01-12 18:46:01 +00:00
|
|
|
|
|
|
|
mixin WalletDB {
|
2023-01-19 20:33:23 +00:00
|
|
|
MainDB? _db;
|
|
|
|
MainDB get db => _db!;
|
2023-01-12 18:46:01 +00:00
|
|
|
|
2023-01-19 20:33:23 +00:00
|
|
|
void isarInit({MainDB? mockableOverride}) async {
|
|
|
|
_db = mockableOverride ?? MainDB.instance;
|
2023-01-12 18:46:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:36:19 +00:00
|
|
|
Future<void> addNewTransactionData(
|
|
|
|
List<Tuple4<Transaction, List<Output>, List<Input>, Address?>>
|
2023-01-16 21:04:03 +00:00
|
|
|
transactionsData,
|
|
|
|
String walletId) async {
|
|
|
|
await db.isar.writeTxn(() async {
|
2023-01-13 21:36:19 +00:00
|
|
|
for (final data in transactionsData) {
|
|
|
|
final tx = data.item1;
|
|
|
|
|
2023-01-16 21:04:03 +00:00
|
|
|
final potentiallyUnconfirmedTx = await db
|
|
|
|
.getTransactions(walletId)
|
|
|
|
.filter()
|
|
|
|
.txidEqualTo(tx.txid)
|
|
|
|
.findFirst();
|
2023-01-13 23:36:59 +00:00
|
|
|
if (potentiallyUnconfirmedTx != null) {
|
|
|
|
// update use id to replace tx
|
|
|
|
tx.id = potentiallyUnconfirmedTx.id;
|
2023-01-16 21:04:03 +00:00
|
|
|
await db.isar.transactions.delete(potentiallyUnconfirmedTx.id);
|
2023-01-13 23:36:59 +00:00
|
|
|
}
|
2023-01-13 21:36:19 +00:00
|
|
|
// save transaction
|
2023-01-16 21:04:03 +00:00
|
|
|
await db.isar.transactions.put(tx);
|
2023-01-13 21:36:19 +00:00
|
|
|
|
|
|
|
// link and save outputs
|
|
|
|
if (data.item2.isNotEmpty) {
|
2023-01-16 21:04:03 +00:00
|
|
|
await db.isar.outputs.putAll(data.item2);
|
2023-01-13 21:36:19 +00:00
|
|
|
tx.outputs.addAll(data.item2);
|
|
|
|
await tx.outputs.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// link and save inputs
|
|
|
|
if (data.item3.isNotEmpty) {
|
2023-01-16 21:04:03 +00:00
|
|
|
await db.isar.inputs.putAll(data.item3);
|
2023-01-13 21:36:19 +00:00
|
|
|
tx.inputs.addAll(data.item3);
|
|
|
|
await tx.inputs.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.item4 != null) {
|
2023-01-16 21:04:03 +00:00
|
|
|
final address = await db
|
|
|
|
.getAddresses(walletId)
|
|
|
|
.filter()
|
2023-01-13 22:11:34 +00:00
|
|
|
.valueEqualTo(data.item4!.value)
|
|
|
|
.findFirst();
|
|
|
|
|
2023-01-13 21:36:19 +00:00
|
|
|
// check if address exists in db and add if it does not
|
2023-01-13 22:11:34 +00:00
|
|
|
if (address == null) {
|
2023-01-16 21:04:03 +00:00
|
|
|
await db.isar.addresses.put(data.item4!);
|
2023-01-13 21:36:19 +00:00
|
|
|
}
|
2023-01-13 22:11:34 +00:00
|
|
|
|
2023-01-13 21:36:19 +00:00
|
|
|
// link and save address
|
2023-01-13 22:11:34 +00:00
|
|
|
tx.address.value = address ?? data.item4!;
|
2023-01-13 21:36:19 +00:00
|
|
|
await tx.address.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-01-12 18:46:01 +00:00
|
|
|
}
|