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