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/theme/stack_colors.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/rounded_white_container.dart';
@ -36,6 +38,36 @@ class _MyTokenSelectItemState extends ConsumerState<MyTokenSelectItem> {
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 {
ref.read(tokenServiceStateProvider.state).state = EthTokenWallet(
token: widget.token,
@ -49,13 +81,17 @@ class _MyTokenSelectItemState extends ConsumerState<MyTokenSelectItem> {
),
);
await showLoading<void>(
whileFuture: ref.read(tokenServiceProvider)!.initialize(),
final success = await showLoading<bool>(
whileFuture: _loadTokenWallet(context, ref),
context: context,
isDesktop: isDesktop,
message: "Loading ${widget.token.name}",
);
if (!success) {
return;
}
if (mounted) {
await Navigator.of(context).pushNamed(
isDesktop ? DesktopTokenView.routeName : TokenView.routeName,

View file

@ -253,13 +253,17 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
);
_credentials = web3dart.EthPrivateKey.fromHex(privateKey);
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
try {
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
} catch (_) {
rethrow;
}
try {
_sendFunction = _deployedContract.function('transfer');
@ -328,13 +332,17 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
//====================================================================
}
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
try {
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
} catch (_) {
rethrow;
}
_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/util.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/wallet_info_row/wallet_info_row.dart';
import 'package:tuple/tuple.dart';
@ -37,7 +39,7 @@ class SimpleWalletCard extends ConsumerWidget {
final bool popPrevious;
final NavigatorState? desktopNavigatorState;
Future<void> _loadTokenWallet(
Future<bool> _loadTokenWallet(
BuildContext context,
WidgetRef ref,
Manager manager,
@ -52,7 +54,31 @@ class SimpleWalletCard extends ConsumerWidget {
),
);
await ref.read(tokenServiceProvider)!.initialize();
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();
Navigator.of(context).pop();
if (desktopNavigatorState == null) {
Navigator.of(context).pop();
}
},
),
),
);
return false;
}
}
void _openWallet(BuildContext context, WidgetRef ref) async {
@ -91,13 +117,21 @@ class SimpleWalletCard extends ConsumerWidget {
final contract =
ref.read(mainDBProvider).getEthContractSync(contractAddress!)!;
await showLoading<void>(
whileFuture: _loadTokenWallet(context, ref, manager, contract),
context: context,
final success = await showLoading<bool>(
whileFuture: _loadTokenWallet(
desktopNavigatorState?.context ?? context,
ref,
manager,
contract),
context: desktopNavigatorState?.context ?? context,
opaqueBG: true,
message: "Loading ${contract.name}",
);
if (!success) {
return;
}
if (desktopNavigatorState == null) {
// pop loading
nav.pop();