pass abi parsing errors (or other token init errors) up and show a simple dialog

This commit is contained in:
julian 2023-05-01 11:20:40 -06:00
parent 35ea94a209
commit b9211ad2c3
3 changed files with 99 additions and 21 deletions

View file

@ -14,6 +14,8 @@ import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/dialogs/basic_dialog.dart';
import 'package:stackwallet/widgets/icon_widgets/eth_token_icon.dart'; import 'package:stackwallet/widgets/icon_widgets/eth_token_icon.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart';
@ -36,6 +38,36 @@ class _MyTokenSelectItemState extends ConsumerState<MyTokenSelectItem> {
late final CachedEthTokenBalance cachedBalance; late final CachedEthTokenBalance cachedBalance;
Future<bool> _loadTokenWallet(
BuildContext context,
WidgetRef ref,
) async {
try {
await ref.read(tokenServiceProvider)!.initialize();
return true;
} catch (_) {
await showDialog<void>(
barrierDismissible: false,
context: context,
builder: (context) => BasicDialog(
title: "Failed to load token data",
desktopHeight: double.infinity,
desktopWidth: 450,
rightButton: PrimaryButton(
label: "OK",
onPressed: () {
Navigator.of(context).pop();
if (!isDesktop) {
Navigator.of(context).pop();
}
},
),
),
);
return false;
}
}
void _onPressed() async { void _onPressed() async {
ref.read(tokenServiceStateProvider.state).state = EthTokenWallet( ref.read(tokenServiceStateProvider.state).state = EthTokenWallet(
token: widget.token, token: widget.token,
@ -49,13 +81,17 @@ class _MyTokenSelectItemState extends ConsumerState<MyTokenSelectItem> {
), ),
); );
await showLoading<void>( final success = await showLoading<bool>(
whileFuture: ref.read(tokenServiceProvider)!.initialize(), whileFuture: _loadTokenWallet(context, ref),
context: context, context: context,
isDesktop: isDesktop, isDesktop: isDesktop,
message: "Loading ${widget.token.name}", message: "Loading ${widget.token.name}",
); );
if (!success) {
return;
}
if (mounted) { if (mounted) {
await Navigator.of(context).pushNamed( await Navigator.of(context).pushNamed(
isDesktop ? DesktopTokenView.routeName : TokenView.routeName, isDesktop ? DesktopTokenView.routeName : TokenView.routeName,

View file

@ -253,6 +253,7 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
); );
_credentials = web3dart.EthPrivateKey.fromHex(privateKey); _credentials = web3dart.EthPrivateKey.fromHex(privateKey);
try {
_deployedContract = web3dart.DeployedContract( _deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList( ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!, jsonList: tokenContract.abi!,
@ -260,6 +261,9 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
), ),
contractAddress, contractAddress,
); );
} catch (_) {
rethrow;
}
try { try {
_sendFunction = _deployedContract.function('transfer'); _sendFunction = _deployedContract.function('transfer');
@ -328,6 +332,7 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
//==================================================================== //====================================================================
} }
try {
_deployedContract = web3dart.DeployedContract( _deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList( ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!, jsonList: tokenContract.abi!,
@ -335,6 +340,9 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
), ),
contractAddress, contractAddress,
); );
} catch (_) {
rethrow;
}
_sendFunction = _deployedContract.function('transfer'); _sendFunction = _deployedContract.function('transfer');

View file

@ -19,6 +19,8 @@ import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/show_loading.dart'; import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/dialogs/basic_dialog.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart';
import 'package:stackwallet/widgets/wallet_info_row/wallet_info_row.dart'; import 'package:stackwallet/widgets/wallet_info_row/wallet_info_row.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
@ -37,7 +39,7 @@ class SimpleWalletCard extends ConsumerWidget {
final bool popPrevious; final bool popPrevious;
final NavigatorState? desktopNavigatorState; final NavigatorState? desktopNavigatorState;
Future<void> _loadTokenWallet( Future<bool> _loadTokenWallet(
BuildContext context, BuildContext context,
WidgetRef ref, WidgetRef ref,
Manager manager, Manager manager,
@ -52,7 +54,31 @@ class SimpleWalletCard extends ConsumerWidget {
), ),
); );
try {
await ref.read(tokenServiceProvider)!.initialize(); await ref.read(tokenServiceProvider)!.initialize();
return true;
} catch (_) {
await showDialog<void>(
barrierDismissible: false,
context: context,
builder: (context) => BasicDialog(
title: "Failed to load token data",
desktopHeight: double.infinity,
desktopWidth: 450,
rightButton: PrimaryButton(
label: "OK",
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pop();
if (desktopNavigatorState == null) {
Navigator.of(context).pop();
}
},
),
),
);
return false;
}
} }
void _openWallet(BuildContext context, WidgetRef ref) async { void _openWallet(BuildContext context, WidgetRef ref) async {
@ -91,13 +117,21 @@ class SimpleWalletCard extends ConsumerWidget {
final contract = final contract =
ref.read(mainDBProvider).getEthContractSync(contractAddress!)!; ref.read(mainDBProvider).getEthContractSync(contractAddress!)!;
await showLoading<void>( final success = await showLoading<bool>(
whileFuture: _loadTokenWallet(context, ref, manager, contract), whileFuture: _loadTokenWallet(
context: context, desktopNavigatorState?.context ?? context,
ref,
manager,
contract),
context: desktopNavigatorState?.context ?? context,
opaqueBG: true, opaqueBG: true,
message: "Loading ${contract.name}", message: "Loading ${contract.name}",
); );
if (!success) {
return;
}
if (desktopNavigatorState == null) { if (desktopNavigatorState == null) {
// pop loading // pop loading
nav.pop(); nav.pop();