| 1 | import 'dart:convert'; |
| 2 | import 'dart:core'; |
| 3 | import 'dart:developer'; |
| 4 | import 'package:cw_core/pathForWallet.dart'; |
| 5 | import 'package:cw_core/wallet_info.dart'; |
| 6 | import 'package:cw_core/encryption_file_utils.dart'; |
| 7 | import 'package:cw_tron/tron_transaction_info.dart'; |
| 8 | import 'package:mobx/mobx.dart'; |
| 9 | import 'package:cw_core/transaction_history.dart'; |
| 10 | |
| 11 | part 'tron_transaction_history.g.dart'; |
| 12 | |
| 13 | class TronTransactionHistory = TronTransactionHistoryBase with _$TronTransactionHistory; |
| 14 | |
| 15 | abstract class TronTransactionHistoryBase extends TransactionHistoryBase<TronTransactionInfo> |
| 16 | with Store { |
| 17 | TronTransactionHistoryBase( |
| 18 | {required this.walletInfo, required String password, required this.encryptionFileUtils}) |
| 19 | : _password = password { |
| 20 | transactions = ObservableMap<String, TronTransactionInfo>(); |
| 21 | } |
| 22 | |
| 23 | String _password; |
| 24 | |
| 25 | final WalletInfo walletInfo; |
| 26 | final EncryptionFileUtils encryptionFileUtils; |
| 27 | |
| 28 | Future<void> init() async { |
| 29 | clear(); |
| 30 | await _load(); |
| 31 | } |
| 32 | |
| 33 | @override |
| 34 | Future<void> save() async { |
| 35 | String transactionsHistoryFileNameForWallet = 'tron_transactions.json'; |
| 36 | try { |
| 37 | final dirPath = await pathForWalletDir(name: walletInfo.name, type: walletInfo.type); |
| 38 | String path = '$dirPath/$transactionsHistoryFileNameForWallet'; |
| 39 | final transactionMaps = transactions.map((key, value) => MapEntry(key, value.toJson())); |
| 40 | final data = json.encode({'transactions': transactionMaps}); |
| 41 | await encryptionFileUtils.write(path: path, password: _password, data: data); |
| 42 | } catch (e, s) { |
| 43 | log('Error while saving ${walletInfo.type.name} transaction history: ${e.toString()}'); |
| 44 | log(s.toString()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @override |
| 49 | void addOne(TronTransactionInfo transaction) => transactions[transaction.id] = transaction; |
| 50 | |
| 51 | @override |
| 52 | void addMany(Map<String, TronTransactionInfo> transactions) => |
| 53 | this.transactions.addAll(transactions); |
| 54 | |
| 55 | Future<Map<String, dynamic>> _read() async { |
| 56 | String transactionsHistoryFileNameForWallet = 'tron_transactions.json'; |
| 57 | final dirPath = await pathForWalletDir(name: walletInfo.name, type: walletInfo.type); |
| 58 | String path = '$dirPath/$transactionsHistoryFileNameForWallet'; |
| 59 | final content = await encryptionFileUtils.read(path: path, password: _password); |
| 60 | if (content.isEmpty) { |
| 61 | return {}; |
| 62 | } |
| 63 | return json.decode(content) as Map<String, dynamic>; |
| 64 | } |
| 65 | |
| 66 | Future<void> _load() async { |
| 67 | try { |
| 68 | final content = await _read(); |
| 69 | final txs = content['transactions'] as Map<String, dynamic>? ?? {}; |
| 70 | |
| 71 | for (var entry in txs.entries) { |
| 72 | final val = entry.value; |
| 73 | |
| 74 | if (val is Map<String, dynamic>) { |
| 75 | final tx = TronTransactionInfo.fromJson(val); |
| 76 | _update(tx); |
| 77 | } |
| 78 | } |
| 79 | } catch (e) { |
| 80 | log(e.toString()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void _update(TronTransactionInfo transaction) => transactions[transaction.id] = transaction; |
| 85 | } |