|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | import 'dart:async'; |
| 12 | +import 'dart:math' as math; |
12 | 13 |
|
13 | 14 | import 'package:drift/drift.dart'; |
14 | 15 | import 'package:drift_flutter/drift_flutter.dart'; |
@@ -44,13 +45,56 @@ class SparkNames extends Table { |
44 | 45 | Set<Column> get primaryKey => {name}; |
45 | 46 | } |
46 | 47 |
|
47 | | -@DriftDatabase(tables: [SparkNames]) |
| 48 | +class MwebUtxos extends Table { |
| 49 | + TextColumn get outputId => text()(); |
| 50 | + TextColumn get address => text()(); |
| 51 | + IntColumn get value => integer()(); |
| 52 | + IntColumn get height => integer()(); |
| 53 | + IntColumn get blockTime => integer()(); |
| 54 | + BoolColumn get blocked => boolean()(); |
| 55 | + BoolColumn get used => boolean()(); |
| 56 | + |
| 57 | + @override |
| 58 | + Set<Column> get primaryKey => {outputId}; |
| 59 | +} |
| 60 | + |
| 61 | +extension MwebUtxoExt on MwebUtxo { |
| 62 | + int getConfirmations(int currentChainHeight) { |
| 63 | + if (blockTime <= 0) return 0; |
| 64 | + if (height <= 0) return 0; |
| 65 | + return math.max(0, currentChainHeight - (height - 1)); |
| 66 | + } |
| 67 | + |
| 68 | + bool isConfirmed( |
| 69 | + int currentChainHeight, |
| 70 | + int minimumConfirms, { |
| 71 | + int? overrideMinConfirms, |
| 72 | + }) { |
| 73 | + final confirmations = getConfirmations(currentChainHeight); |
| 74 | + |
| 75 | + if (overrideMinConfirms != null) { |
| 76 | + return confirmations >= overrideMinConfirms; |
| 77 | + } |
| 78 | + return confirmations >= minimumConfirms; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@DriftDatabase(tables: [SparkNames, MwebUtxos]) |
48 | 83 | final class WalletDatabase extends _$WalletDatabase { |
49 | 84 | WalletDatabase._(String walletId, [QueryExecutor? executor]) |
50 | 85 | : super(executor ?? _openConnection(walletId)); |
51 | 86 |
|
52 | 87 | @override |
53 | | - int get schemaVersion => 1; |
| 88 | + int get schemaVersion => 2; |
| 89 | + |
| 90 | + @override |
| 91 | + MigrationStrategy get migration => MigrationStrategy( |
| 92 | + onUpgrade: (m, from, to) async { |
| 93 | + if (from == 1 && to == 2) { |
| 94 | + await m.createTable(mwebUtxos); |
| 95 | + } |
| 96 | + }, |
| 97 | + ); |
54 | 98 |
|
55 | 99 | static QueryExecutor _openConnection(String walletId) { |
56 | 100 | return driftDatabase( |
|
0 commit comments