dev
dart 168 lines 4.73 KB
Raw
1 import 'package:cake_wallet/core/secure_storage.dart';
2 import 'package:cake_wallet/entities/get_encryption_key.dart';
3 import 'package:cake_wallet/exchange/exchange_provider_description.dart';
4 import 'package:cake_wallet/exchange/trade.dart';
5 import 'package:cake_wallet/exchange/trade_currency_snapshot.dart';
6 import 'package:cake_wallet/exchange/trade_state.dart';
7 import 'package:cw_core/cake_hive.dart';
8 import 'package:cw_core/crypto_currency.dart';
9 import 'package:cw_core/hive_type_ids.dart';
10 import 'package:cw_core/utils/print_verbose.dart';
11 import 'package:hive/hive.dart';
12
13 part 'trade_legacy.part.dart';
14
15 Future<void> performTradeHiveMigration(SecureStorage secureStorage) async {
16 try {
17 if (!CakeHive.isAdapterRegistered(TradeLegacy.typeId)) {
18 CakeHive.registerAdapter(TradeLegacyAdapter());
19 }
20 final tradesBoxKey = await getEncryptionKey(secureStorage: secureStorage, forKey: Trade.boxKey);
21 final box = await CakeHive.openBox<TradeLegacy>(Trade.boxName, encryptionKey: tradesBoxKey);
22 await TradeLegacy.migrateAllToSqlite(box);
23 } catch (e) {
24 printV('Trade Hive migration error: $e');
25 }
26 }
27
28 class TradeLegacy extends HiveObject {
29 TradeLegacy({
30 required this.id,
31 required this.amount,
32 ExchangeProviderDescription? provider,
33 CryptoCurrency? from,
34 CryptoCurrency? to,
35 TradeState? state,
36 this.receiveAmount,
37 this.createdAt,
38 this.expiredAt,
39 this.inputAddress,
40 this.extraId,
41 this.outputTransaction,
42 this.refundAddress,
43 this.walletId,
44 this.payoutAddress,
45 this.password,
46 this.providerId,
47 this.providerName,
48 this.fromWalletAddress,
49 this.memo,
50 this.fee,
51 this.txId,
52 this.isRefund,
53 this.isSendAll,
54 this.router,
55 this.userCurrencyFromRaw,
56 this.userCurrencyToRaw,
57 this.needToRegisterInSwapXyz,
58 this.sourceTokenAddress,
59 this.sourceTokenDecimals,
60 this.routerData,
61 this.routerValue,
62 this.routerChainId,
63 this.sourceTokenAmountRaw,
64 this.requiresTokenApproval,
65 this.chainId,
66 }) {
67 if (provider != null) providerRaw = provider.raw;
68 fromRaw = from?.raw ?? -1;
69 toRaw = to?.raw ?? -1;
70 if (state != null) stateRaw = state.raw;
71 }
72
73 static const typeId = TRADE_TYPE_ID;
74
75 String id;
76 late int providerRaw;
77 int fromRaw = -1;
78 int toRaw = -1;
79 late String stateRaw;
80 DateTime? createdAt;
81 DateTime? expiredAt;
82 String amount;
83 String? receiveAmount;
84 String? inputAddress;
85 String? extraId;
86 String? outputTransaction;
87 String? refundAddress;
88 String? walletId;
89 String? payoutAddress;
90 String? password;
91 String? providerId;
92 String? providerName;
93 String? fromWalletAddress;
94 String? memo;
95 String? txId;
96 bool? isRefund;
97 bool? isSendAll;
98 String? router;
99 String? userCurrencyFromRaw;
100 String? userCurrencyToRaw;
101 bool? needToRegisterInSwapXyz;
102 String? sourceTokenAddress;
103 int? sourceTokenDecimals;
104 String? routerData;
105 String? routerValue;
106 int? routerChainId;
107 String? sourceTokenAmountRaw;
108 bool? requiresTokenApproval;
109 int? chainId;
110 double? fee;
111
112 Future<void> migrateToSqlite() async {
113 final trade = Trade(
114 id: id,
115 amount: amount,
116 receiveAmount: receiveAmount,
117 createdAt: createdAt,
118 expiredAt: expiredAt,
119 inputAddress: inputAddress,
120 extraId: extraId,
121 outputTransaction: outputTransaction,
122 refundAddress: refundAddress,
123 walletId: walletId,
124 payoutAddress: payoutAddress,
125 password: password,
126 providerId: providerId,
127 providerName: providerName,
128 fromWalletAddress: fromWalletAddress,
129 memo: memo,
130 fee: fee,
131 txId: txId,
132 isRefund: isRefund,
133 isSendAll: isSendAll,
134 router: router,
135 from:
136 TradeCurrencySnapshot.fromLegacyHive(raw: fromRaw, displayTitleTag: userCurrencyFromRaw),
137 to: TradeCurrencySnapshot.fromLegacyHive(raw: toRaw, displayTitleTag: userCurrencyToRaw),
138 needToRegisterInSwapXyz: needToRegisterInSwapXyz,
139 sourceTokenAddress: sourceTokenAddress,
140 sourceTokenDecimals: sourceTokenDecimals,
141 routerData: routerData,
142 routerValue: routerValue,
143 routerChainId: routerChainId,
144 sourceTokenAmountRaw: sourceTokenAmountRaw,
145 requiresTokenApproval: requiresTokenApproval,
146 chainId: chainId,
147 );
148 trade.providerRaw = providerRaw;
149 trade.stateRaw = stateRaw;
150 await trade.save();
151 }
152
153 static Future<void> migrateAllToSqlite(
154 Box<TradeLegacy> box,
155 ) async {
156 printV('Migrating Trades to SQLite: start');
157 final list = box.values.toList();
158 for (final trade in list) {
159 try {
160 await trade.migrateToSqlite();
161 await trade.delete();
162 } catch (e) {
163 printV('Error migrating trade ${trade.id}: $e');
164 }
165 }
166 printV('Migrating Trades to SQLite: end');
167 }
168 }