diff --git a/lib/db/hive/db.dart b/lib/db/hive/db.dart index 1ae6de2c2..6f4ebd3c3 100644 --- a/lib/db/hive/db.dart +++ b/lib/db/hive/db.dart @@ -350,5 +350,4 @@ abstract class DBKeys { static const String isFavorite = "isFavorite"; static const String id = "id"; static const String storedChainHeight = "storedChainHeight"; - static const String ethTokenContracts = "ethTokenContracts"; } diff --git a/lib/db/migrate_wallets_to_isar.dart b/lib/db/migrate_wallets_to_isar.dart index 50ead2110..ddaea3f21 100644 --- a/lib/db/migrate_wallets_to_isar.dart +++ b/lib/db/migrate_wallets_to_isar.dart @@ -4,9 +4,10 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:isar/isar.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/db/isar/main_db.dart'; -import 'package:stackwallet/models/isar/models/transaction_note.dart'; +import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; +import 'package:stackwallet/wallets/isar/models/token_wallet_info.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info_meta.dart'; import 'package:stackwallet/wallets/wallet/supporting/epiccash_wallet_info_extension.dart'; @@ -49,6 +50,7 @@ Future migrateWalletsToIsar({ (await Hive.openBox(DB.boxNameFavoriteWallets)).values.toList(); final List<(WalletInfo, WalletInfoMeta)> newInfo = []; + final List tokenInfo = []; final List migratedNotes = []; // @@ -89,10 +91,30 @@ Future migrateWalletsToIsar({ // Map otherData = {}; - otherData[WalletInfoKeys.tokenContractAddresses] = walletBox.get( - DBKeys.ethTokenContracts, + final List? tokenContractAddresses = walletBox.get( + "ethTokenContracts", ) as List?; + if (tokenContractAddresses?.isNotEmpty == true) { + otherData[WalletInfoKeys.tokenContractAddresses] = tokenContractAddresses; + + for (final address in tokenContractAddresses!) { + final contract = await MainDB.instance.isar.ethContracts + .where() + .addressEqualTo(address) + .findFirst(); + if (contract != null) { + tokenInfo.add( + TokenWalletInfo( + walletId: old.walletId, + tokenAddress: address, + tokenFractionDigits: contract.decimals, + ), + ); + } + } + } + // epiccash specifics if (old.coin == Coin.epicCash) { final epicWalletInfo = ExtraEpiccashWalletInfo.fromMap({ @@ -149,12 +171,18 @@ Future migrateWalletsToIsar({ }); } - await MainDB.instance.isar.writeTxn(() async { - await MainDB.instance.isar.walletInfo - .putAll(newInfo.map((e) => e.$1).toList()); - await MainDB.instance.isar.walletInfoMeta - .putAll(newInfo.map((e) => e.$2).toList()); - }); + if (newInfo.isNotEmpty) { + await MainDB.instance.isar.writeTxn(() async { + await MainDB.instance.isar.walletInfo + .putAll(newInfo.map((e) => e.$1).toList()); + await MainDB.instance.isar.walletInfoMeta + .putAll(newInfo.map((e) => e.$2).toList()); + + if (tokenInfo.isNotEmpty) { + await MainDB.instance.isar.tokenWalletInfo.putAll(tokenInfo); + } + }); + } await _cleanupOnSuccess( walletIds: newInfo.map((e) => e.$1.walletId).toList()); diff --git a/lib/pages/add_wallet_views/select_wallet_for_token_view.dart b/lib/pages/add_wallet_views/select_wallet_for_token_view.dart index c395bbf7d..d0f466a0c 100644 --- a/lib/pages/add_wallet_views/select_wallet_for_token_view.dart +++ b/lib/pages/add_wallet_views/select_wallet_for_token_view.dart @@ -10,7 +10,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/models/add_wallet_list_entity/sub_classes/coin_entity.dart'; import 'package:stackwallet/models/add_wallet_list_entity/sub_classes/eth_token_entity.dart'; import 'package:stackwallet/pages/add_wallet_views/add_token_view/edit_wallet_tokens_view.dart'; @@ -20,6 +19,7 @@ import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/wallets/isar/providers/all_wallets_info_provider.dart'; +import 'package:stackwallet/wallets/isar/providers/wallet_info_provider.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; @@ -85,13 +85,8 @@ class _SelectWalletForTokenViewState final List ethWalletIds = []; - // TODO: proper wallet data class instead of this Hive silliness for (final walletId in ethWalletInfos.map((e) => e.walletId).toList()) { - final walletContracts = DB.instance.get( - boxName: walletId, - key: DBKeys.ethTokenContracts, - ) as List? ?? - []; + final walletContracts = ref.read(pWalletTokenAddresses(walletId)); if (!walletContracts.contains(widget.entity.token.address)) { ethWalletIds.add(walletId); } diff --git a/lib/services/mixins/wallet_cache.dart b/lib/services/mixins/wallet_cache.dart index 69ebce551..5d60686e4 100644 --- a/lib/services/mixins/wallet_cache.dart +++ b/lib/services/mixins/wallet_cache.dart @@ -127,22 +127,4 @@ mixin WalletCache { value: balance.toJsonIgnoreCoin(), ); } - - // Ethereum specific - List getWalletTokenContractAddresses() { - return DB.instance.get( - boxName: _walletId, - key: DBKeys.ethTokenContracts, - ) as List? ?? - []; - } - - Future updateWalletTokenContractAddresses( - List contractAddresses) async { - await DB.instance.put( - boxName: _walletId, - key: DBKeys.ethTokenContracts, - value: contractAddresses, - ); - } } diff --git a/lib/wallets/isar/models/token_wallet_info.dart b/lib/wallets/isar/models/token_wallet_info.dart index 78378db41..45c4747a3 100644 --- a/lib/wallets/isar/models/token_wallet_info.dart +++ b/lib/wallets/isar/models/token_wallet_info.dart @@ -67,7 +67,7 @@ class TokenWalletInfo implements IsarId { // // ensure we are updating using the latest entry of this in the db final thisEntry = await isar.tokenWalletInfo .where() - .walletIdEqualToTokenAddressNotEqualTo(walletId, tokenAddress) + .walletIdTokenAddressEqualTo(walletId, tokenAddress) .findFirst(); if (thisEntry == null) { throw Exception(