dev
dart 134 lines 3.33 KB
Raw
1 import "package:cw_core/cake_hive.dart";
2 import "package:cw_core/db/sqlite.dart";
3 import "package:cw_core/hive_type_ids.dart";
4 import "package:cw_core/spl_token.dart" as spl_new;
5 import "package:cw_core/utils/print_verbose.dart";
6 import "package:cw_core/wallet_info.dart";
7 import "package:cw_core/wallet_type.dart";
8 import "package:hive/hive.dart";
9 import "package:sqflite/sqflite.dart";
10
11 part "spl_token_legacy.part.dart";
12
13 Future<void> performSplTokenHiveMigration() async {
14 try {
15 if (!CakeHive.isAdapterRegistered(SPLToken.typeId)) {
16 CakeHive.registerAdapter(SPLTokenAdapter());
17 }
18
19 final wallets = await WalletInfo.getAll();
20 await SPLToken.migrateAllToSqlite(wallets);
21 } catch (e) {
22 printV("Error performing SPLToken Hive migration: $e, continuing anyway");
23 }
24 }
25
26 // @HiveType(typeId: SPLToken.typeId)
27 class SPLToken extends HiveObject {
28 SPLToken({
29 required this.name,
30 required this.symbol,
31 required this.mintAddress,
32 required this.decimal,
33 required this.mint,
34 this.iconPath,
35 this.tag = "SOL",
36 bool enabled = true,
37 this.isPotentialScam = false,
38 }) : _enabled = enabled;
39 // @HiveField(0)
40 final String name;
41
42 // @HiveField(1)
43 final String symbol;
44
45 // @HiveField(2)
46 final String mintAddress;
47
48 // @HiveField(3)
49 final int decimal;
50
51 // @HiveField(4, defaultValue: true)
52 bool _enabled;
53
54 // @HiveField(5)
55 final String mint;
56
57 // @HiveField(6)
58 final String? iconPath;
59
60 // @HiveField(7)
61 final String? tag;
62
63 // @HiveField(8, defaultValue: false)
64 bool isPotentialScam;
65
66 bool get enabled => _enabled;
67
68 set enabled(bool value) => _enabled = value;
69
70 static const typeId = SPL_TOKEN_TYPE_ID;
71 static const boxName = "SPLTokens";
72
73 static Future<void> migrateAllToSqlite(List<WalletInfo> wallets) async {
74 final sanitizedToRawNames = <String, Set<String>>{};
75 for (final wallet in wallets) {
76 if (wallet.type != WalletType.solana) {
77 continue;
78 }
79
80 sanitizedToRawNames
81 .putIfAbsent(wallet.name.replaceAll(" ", "_"), () => <String>{})
82 .add(wallet.name);
83 }
84
85 for (final entry in sanitizedToRawNames.entries) {
86 final tokenBoxName = "${entry.key}_$boxName";
87 try {
88 if (!await CakeHive.boxExists(tokenBoxName)) {
89 continue;
90 }
91
92 final box = await CakeHive.openBox<SPLToken>(tokenBoxName);
93
94 for (final key in box.keys.toList()) {
95 final token = box.get(key);
96 if (token == null) {
97 continue;
98 }
99
100 for (final rawName in entry.value) {
101 await token.migrateToSqlite(walletName: rawName);
102 }
103 await box.delete(key);
104 }
105
106 await box.deleteFromDisk();
107 } catch (e) {
108 printV("Error migrating spl token box $tokenBoxName: $e, continuing anyway");
109 }
110 }
111 }
112
113 Future<void> migrateToSqlite({required String walletName}) async {
114 final row = spl_new.SPLToken(
115 name: name,
116 symbol: symbol,
117 mintAddress: mintAddress,
118 decimal: decimal,
119 mint: mint,
120 enabled: _enabled,
121 iconPath: iconPath,
122 tag: tag,
123 isPotentialScam: isPotentialScam,
124 walletName: walletName,
125 ).toMap();
126 row[spl_new.SPLToken.selfIdColumn] = null;
127
128 await db!.insert(
129 spl_new.SPLToken.tableName,
130 row,
131 conflictAlgorithm: ConflictAlgorithm.replace,
132 );
133 }
134 }