12
import 'package:cw_haven/api/structs/pending_transaction.dart';
13
import 'package:flutter/foundation.dart';
14
import 'package:mobx/mobx.dart';
15
-import 'package:cw_haven/api/transaction_history.dart'
16
- as haven_transaction_history;
15
+import 'package:cw_haven/api/transaction_history.dart' as haven_transaction_history;
16
//import 'package:cw_haven/wallet.dart';
17
import 'package:cw_haven/api/wallet.dart' as haven_wallet;
18
import 'package:cw_haven/api/transaction_history.dart' as transaction_history;
36
37
class HavenWallet = HavenWalletBase with _$HavenWallet;
38
40
-abstract class HavenWalletBase extends WalletBase<MoneroBalance,
41
- HavenTransactionHistory, HavenTransactionInfo> with Store {
39
+abstract class HavenWalletBase
40
+ extends WalletBase<MoneroBalance, HavenTransactionHistory, HavenTransactionInfo> with Store {
41
HavenWalletBase({required WalletInfo walletInfo})
42
: balance = ObservableMap.of(getHavenBalance(accountIndex: 0)),
43
_isTransactionUpdating = false,
46
syncStatus = NotConnectedSyncStatus(),
47
super(walletInfo) {
48
transactionHistory = HavenTransactionHistory();
50
- _onAccountChangeReaction = reaction((_) => walletAddresses.account,
51
- (Account? account) {
49
+ _onAccountChangeReaction = reaction((_) => walletAddresses.account, (Account? account) {
50
if (account == null) {
51
return;
52
}
94
haven_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery);
95
96
if (haven_wallet.getCurrentHeight() <= 1) {
99
- haven_wallet.setRefreshFromBlockHeight(
100
- height: walletInfo.restoreHeight);
97
+ haven_wallet.setRefreshFromBlockHeight(height: walletInfo.restoreHeight);
98
}
99
}
100
104
- _autoSaveTimer = Timer.periodic(
105
- Duration(seconds: _autoSaveInterval),
106
- (_) async => await save());
101
+ _autoSaveTimer =
102
+ Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save());
103
}
104
105
@override
111
_onAccountChangeReaction?.reaction.dispose();
112
_autoSaveTimer?.cancel();
113
}
118
-
114
+
115
@override
116
Future<void> connectToNode({required Node node}) async {
117
try {
166
}
167
168
if (hasMultiDestination) {
173
- if (outputs.any((item) => item.sendAll
174
- || (item.formattedCryptoAmount ?? 0) <= 0)) {
175
- throw HavenTransactionCreationException('You do not have enough coins to send this amount.');
169
+ if (outputs.any((item) => item.sendAll || (item.formattedCryptoAmount ?? 0) <= 0)) {
170
+ throw HavenTransactionCreationException(
171
+ 'You do not have enough coins to send this amount.');
172
}
173
178
- final int totalAmount = outputs.fold(0, (acc, value) =>
179
- acc + (value.formattedCryptoAmount ?? 0));
174
+ final int totalAmount =
175
+ outputs.fold(0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0));
176
177
if (unlockedBalance < totalAmount) {
182
- throw HavenTransactionCreationException('You do not have enough coins to send this amount.');
178
+ throw HavenTransactionCreationException(
179
+ 'You do not have enough coins to send this amount.');
180
}
181
185
- final moneroOutputs = outputs.map((output) =>
186
- MoneroOutput(
187
- address: output.address,
188
- amount: output.cryptoAmount!.replaceAll(',', '.')))
182
+ final moneroOutputs = outputs
183
+ .map((output) => MoneroOutput(
184
+ address: output.address, amount: output.cryptoAmount!.replaceAll(',', '.')))
185
.toList();
186
191
- pendingTransactionDescription =
192
- await transaction_history.createTransactionMultDest(
187
+ pendingTransactionDescription = await transaction_history.createTransactionMultDest(
188
outputs: moneroOutputs,
189
priorityRaw: _credentials.priority.serialize(),
190
accountIndex: walletAddresses.account!.id);
193
final address = output.isParsedAddress && (output.extractedAddress?.isNotEmpty ?? false)
194
? output.extractedAddress!
195
: output.address;
201
- final amount = output.sendAll
202
- ? null
203
- : output.cryptoAmount!.replaceAll(',', '.');
204
- final int? formattedAmount = output.sendAll
205
- ? null
206
- : output.formattedCryptoAmount;
196
+ final amount = output.sendAll ? null : output.cryptoAmount!.replaceAll(',', '.');
197
+ final int? formattedAmount = output.sendAll ? null : output.formattedCryptoAmount;
198
199
if ((formattedAmount != null && unlockedBalance < formattedAmount) ||
200
(formattedAmount == null && unlockedBalance <= 0)) {
204
'You do not have enough unlocked balance. Unlocked: $formattedBalance. Transaction amount: ${output.cryptoAmount}.');
205
}
206
216
- pendingTransactionDescription =
217
- await transaction_history.createTransaction(
207
+ pendingTransactionDescription = await transaction_history.createTransaction(
208
address: address,
209
assetType: _credentials.assetType,
210
amount: amount,
297
}
298
299
String getTransactionAddress(int accountIndex, int addressIndex) =>
310
- haven_wallet.getAddress(
311
- accountIndex: accountIndex,
312
- addressIndex: addressIndex);
300
+ haven_wallet.getAddress(accountIndex: accountIndex, addressIndex: addressIndex);
301
302
@override
303
Future<Map<String, HavenTransactionInfo>> fetchTransactions() async {
304
haven_transaction_history.refreshTransactions();
317
- return _getAllTransactions(null).fold<Map<String, HavenTransactionInfo>>(
318
- <String, HavenTransactionInfo>{},
319
- (Map<String, HavenTransactionInfo> acc, HavenTransactionInfo tx) {
305
+ return _getAllTransactions(null)
306
+ .fold<Map<String, HavenTransactionInfo>>(<String, HavenTransactionInfo>{},
307
+ (Map<String, HavenTransactionInfo> acc, HavenTransactionInfo tx) {
308
acc[tx.id] = tx;
309
return acc;
310
});
328
}
329
330
List<HavenTransactionInfo> _getAllTransactions(dynamic _) => haven_transaction_history
343
- .getAllTransations()
344
- .map((row) => HavenTransactionInfo.fromRow(row))
345
- .toList();
331
+ .getAllTransations()
332
+ .map((row) => HavenTransactionInfo.fromRow(row))
333
+ .toList();
334
335
void _setListeners() {
336
_listener?.stop();
352
}
353
354
int _getHeightDistance(DateTime date) {
367
- final distance =
368
- DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
355
+ final distance = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
356
final daysTmp = (distance / 86400).round();
357
final days = daysTmp < 1 ? 1 : daysTmp;
358
373
void _askForUpdateBalance() =>
374
balance.addAll(getHavenBalance(accountIndex: walletAddresses.account!.id));
375
389
- Future<void> _askForUpdateTransactionHistory() async =>
390
- await updateTransactions();
376
+ Future<void> _askForUpdateTransactionHistory() async => await updateTransactions();
377
378
void _onNewBlock(int height, int blocksLeft, double ptc) async {
379
try {
390
syncStatus = SyncedSyncStatus();
391
392
if (!_hasSyncAfterStartup) {
407
- _hasSyncAfterStartup = true;
408
- await save();
409
- }
393
+ _hasSyncAfterStartup = true;
394
+ await save();
395
+ }
396
397
if (walletInfo.isRecovery) {
398
await setAsRecovered();