Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallets refactor #717

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/db/isar/main_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ class MainDB {
});
return ids;
} catch (e) {
throw MainDBException("failed updateOrPutAddresses: $transactions", e);
throw MainDBException(
"failed updateOrPutTransactionV2s: $transactions", e);
}
}

Expand Down
7 changes: 0 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,6 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
_desktopHasPassword =
await ref.read(storageCryptoHandlerProvider).hasPassword();
}

ref
.read(priceAnd24hChangeNotifierProvider)
.tokenContractAddressesToCheck
.addAll(
await MainDB.instance.getEthContracts().addressProperty().findAll(),
);
}

Future<void> load() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:stackwallet/pages/add_wallet_views/add_token_view/sub_widgets/ad
import 'package:stackwallet/pages/add_wallet_views/add_token_view/sub_widgets/add_token_text.dart';
import 'package:stackwallet/pages/home_view/home_view.dart';
import 'package:stackwallet/pages_desktop_specific/desktop_home_view.dart';
import 'package:stackwallet/providers/global/price_provider.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/assets.dart';
Expand Down Expand Up @@ -151,6 +152,7 @@ class _EditWalletTokensViewState extends ConsumerState<EditWalletTokensView> {

if (contract != null) {
await MainDB.instance.putEthContract(contract);
unawaited(ref.read(priceAnd24hChangeNotifierProvider).updatePrice());
if (mounted) {
setState(() {
if (tokenEntities
Expand All @@ -175,7 +177,8 @@ class _EditWalletTokensViewState extends ConsumerState<EditWalletTokensView> {

if (contracts.isEmpty) {
contracts.addAll(DefaultTokens.list);
MainDB.instance.putEthContracts(contracts);
MainDB.instance.putEthContracts(contracts).then(
(_) => ref.read(priceAnd24hChangeNotifierProvider).updatePrice());
}

tokenEntities.addAll(contracts.map((e) => AddTokenListElementData(e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
*/

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -107,6 +108,7 @@ class _AddWalletViewState extends ConsumerState<AddWalletView> {

if (contract != null) {
await MainDB.instance.putEthContract(contract);
unawaited(ref.read(priceAnd24hChangeNotifierProvider).updatePrice());
if (mounted) {
setState(() {
if (tokenEntities
Expand Down Expand Up @@ -143,7 +145,8 @@ class _AddWalletViewState extends ConsumerState<AddWalletView> {

if (contracts.isEmpty) {
contracts.addAll(DefaultTokens.list);
MainDB.instance.putEthContracts(contracts);
MainDB.instance.putEthContracts(contracts).then(
(value) => ref.read(priceAnd24hChangeNotifierProvider).updatePrice());
}

tokenEntities.addAll(contracts.map((e) => EthTokenEntity(e)));
Expand Down
21 changes: 14 additions & 7 deletions lib/pages/pinpad_views/lock_screen_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart';
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
Expand Down Expand Up @@ -98,14 +99,20 @@ class _LockscreenViewState extends ConsumerState<LockscreenView> {
final walletId = widget.routeOnSuccessArguments as String;

final wallet = ref.read(pWallets).getWallet(walletId);
if (wallet.info.coin == Coin.monero) {
await showLoading(
opaqueBG: true,
whileFuture: wallet.init(),
context: context,
message: "Loading ${wallet.info.coin.prettyName} wallet...",
);
final Future<void> loadFuture;
if (wallet is CwBasedInterface) {
loadFuture =
wallet.init().then((value) async => await (wallet).open());
} else {
loadFuture = wallet.init();
}

await showLoading(
opaqueBG: true,
whileFuture: loadFuture,
context: context,
message: "Loading ${wallet.info.coin.prettyName} wallet...",
);
}

if (mounted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
import 'package:stackwallet/wallets/wallet/impl/epiccash_wallet.dart';
import 'package:stackwallet/wallets/wallet/wallet.dart';
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart';
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/mnemonic_interface.dart';
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/private_key_interface.dart';
import 'package:tuple/tuple.dart';
Expand Down Expand Up @@ -368,7 +370,7 @@ abstract class SWB {
return backupJson;
}

static Future<bool> asyncRestore(
static Future<bool> _asyncRestore(
Tuple2<dynamic, WalletInfo> tuple,
Prefs prefs,
NodeService nodeService,
Expand Down Expand Up @@ -422,6 +424,12 @@ abstract class SWB {
restoreHeight = walletbackup['storedChainHeight'] as int? ?? 0;
}

Future<void>? restoringFuture;

if (!(wallet is CwBasedInterface || wallet is EpiccashWallet)) {
restoringFuture = wallet.recover(isRescan: false);
}

uiState?.update(
walletId: info.walletId,
restoringStatus: StackRestoringStatus.restoring,
Expand Down Expand Up @@ -466,6 +474,8 @@ abstract class SWB {
return false;
}

await restoringFuture;

Logging.instance.log(
"SWB restored: ${info.walletId} ${info.name} ${info.coin.prettyName}",
level: LogLevel.Info);
Expand Down Expand Up @@ -690,13 +700,34 @@ abstract class SWB {
// TODO: use these for monero and possibly other coins later on?
// final List<String> txidList = List<String>.from(walletbackup['txidList'] as List? ?? []);

Map<String, dynamic>? otherData;
try {
if (walletbackup["otherDataJsonString"] is String) {
final data =
jsonDecode(walletbackup["otherDataJsonString"] as String);
otherData = Map<String, dynamic>.from(data as Map);
}
} catch (e, s) {
Logging.instance.log(
"SWB restore walletinfo otherdata error: $e\n$s",
level: LogLevel.Error,
);
}

if (coin == Coin.firo) {
otherData ??= {};
// swb will do a restore so this flag should be set to false so another
// rescan/restore isn't done when opening the wallet
otherData[WalletInfoKeys.lelantusCoinIsarRescanRequired] = false;
}

final info = WalletInfo(
coinName: coin.name,
walletId: walletId,
name: walletName,
mainAddressType: coin.primaryAddressType,
restoreHeight: walletbackup['restoreHeight'] as int? ?? 0,
otherDataJsonString: walletbackup["otherDataJsonString"] as String?,
otherDataJsonString: otherData == null ? null : jsonEncode(otherData),
cachedChainHeight: walletbackup['storedChainHeight'] as int? ?? 0,
);

Expand Down Expand Up @@ -757,7 +788,7 @@ abstract class SWB {
)) {
return false;
}
final bools = await asyncRestore(
final bools = await _asyncRestore(
tuple,
_prefs,
nodeService,
Expand Down Expand Up @@ -796,10 +827,10 @@ abstract class SWB {
}

Logging.instance.log("done with SWB restore", level: LogLevel.Warning);
if (Util.isDesktop) {
await Wallets.sharedInstance
.loadAfterStackRestore(_prefs, uiState?.wallets ?? []);
}

await Wallets.sharedInstance
.loadAfterStackRestore(_prefs, uiState?.wallets ?? [], Util.isDesktop);

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ class _StackRestoreProgressViewState

void _addWalletsToHomeView() {
ref.read(pWallets).loadAfterStackRestore(
ref.read(prefsChangeNotifierProvider),
ref.read(stackRestoringUIStateProvider).wallets,
);
ref.read(prefsChangeNotifierProvider),
ref.read(stackRestoringUIStateProvider).wallets,
Util.isDesktop);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class _TransactionCardStateV2 extends ConsumerState<TransactionCardV2> {

final price = ref
.watch(priceAnd24hChangeNotifierProvider.select((value) => isTokenTx
? value.getTokenPrice(_transaction.otherData!)
? value.getTokenPrice(tokenContract!.address)
: value.getPrice(coin)))
.item1;

Expand Down
22 changes: 13 additions & 9 deletions lib/pages/wallets_view/sub_widgets/favorite_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,21 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
child: GestureDetector(
onTap: () async {
final wallet = ref.read(pWallets).getWallet(walletId);
await wallet.init();

final Future<void> loadFuture;
if (wallet is CwBasedInterface) {
if (mounted) {
await showLoading(
whileFuture: wallet.open(),
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);
}
loadFuture =
wallet.init().then((value) async => await (wallet).open());
} else {
loadFuture = wallet.init();
}
await showLoading(
whileFuture: loadFuture,
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);

if (mounted) {
if (Util.isDesktop) {
await Navigator.of(context).pushNamed(
Expand Down
22 changes: 12 additions & 10 deletions lib/pages/wallets_view/sub_widgets/wallet_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WalletListItem extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
debugPrint("BUILD: $runtimeType");
// debugPrint("BUILD: $runtimeType");
final walletCountString =
walletCount == 1 ? "$walletCount wallet" : "$walletCount wallets";
final currency = ref
Expand All @@ -63,17 +63,19 @@ class WalletListItem extends ConsumerWidget {
.read(pWallets)
.wallets
.firstWhere((e) => e.info.coin == coin);
await wallet.init();
final Future<void> loadFuture;
if (wallet is CwBasedInterface) {
if (context.mounted) {
await showLoading(
whileFuture: wallet.open(),
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);
}
loadFuture =
wallet.init().then((value) async => await (wallet).open());
} else {
loadFuture = wallet.init();
}
await showLoading(
whileFuture: loadFuture,
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);
if (context.mounted) {
unawaited(
Navigator.of(context).pushNamed(
Expand Down
21 changes: 12 additions & 9 deletions lib/pages_desktop_specific/my_stack_view/coin_wallets_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ class CoinWalletsTable extends ConsumerWidget {

final wallet =
ref.read(pWallets).getWallet(walletIds[i]);
await wallet.init();
final Future<void> loadFuture;
if (wallet is CwBasedInterface) {
if (context.mounted) {
await showLoading(
whileFuture: wallet.open(),
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);
}
loadFuture = wallet
.init()
.then((value) async => await (wallet).open());
} else {
loadFuture = wallet.init();
}
await showLoading(
whileFuture: loadFuture,
context: context,
message: 'Opening ${wallet.info.name}',
isDesktop: Util.isDesktop,
);

if (context.mounted) {
await Navigator.of(context).pushNamed(
Expand Down
14 changes: 10 additions & 4 deletions lib/services/price_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import 'dart:async';

import 'package:decimal/decimal.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/price.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:tuple/tuple.dart';

class PriceService extends ChangeNotifier {
late final String baseTicker;
final Set<String> tokenContractAddressesToCheck = {};
Future<Set<String>> get tokenContractAddressesToCheck async =>
(await MainDB.instance.getEthContracts().addressProperty().findAll())
.toSet();
final Duration updateInterval = const Duration(seconds: 60);

Timer? _timer;
Expand Down Expand Up @@ -52,9 +56,11 @@ class PriceService extends ChangeNotifier {
}
}

if (tokenContractAddressesToCheck.isNotEmpty) {
final _tokenContractAddressesToCheck = await tokenContractAddressesToCheck;

if (_tokenContractAddressesToCheck.isNotEmpty) {
final tokenPriceMap = await _priceAPI.getPricesAnd24hChangeForEthTokens(
contractAddresses: tokenContractAddressesToCheck,
contractAddresses: _tokenContractAddressesToCheck,
baseCurrency: baseTicker,
);

Expand Down
Loading
Loading