mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-23 11:59:30 +00:00
eth wallet token list fixes
This commit is contained in:
parent
c89ae56135
commit
e732c4f1b7
3 changed files with 100 additions and 28 deletions
|
@ -6,36 +6,57 @@ import 'package:stackwallet/pages/token_view/token_view.dart';
|
||||||
import 'package:stackwallet/providers/global/secure_store_provider.dart';
|
import 'package:stackwallet/providers/global/secure_store_provider.dart';
|
||||||
import 'package:stackwallet/providers/providers.dart';
|
import 'package:stackwallet/providers/providers.dart';
|
||||||
import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart';
|
import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart';
|
||||||
|
import 'package:stackwallet/services/ethereum/cached_eth_token_balance.dart';
|
||||||
import 'package:stackwallet/services/ethereum/ethereum_token_service.dart';
|
import 'package:stackwallet/services/ethereum/ethereum_token_service.dart';
|
||||||
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
||||||
import 'package:stackwallet/utilities/assets.dart';
|
import 'package:stackwallet/utilities/assets.dart';
|
||||||
import 'package:stackwallet/utilities/constants.dart';
|
import 'package:stackwallet/utilities/constants.dart';
|
||||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||||
import 'package:stackwallet/utilities/format.dart';
|
|
||||||
import 'package:stackwallet/utilities/show_loading.dart';
|
import 'package:stackwallet/utilities/show_loading.dart';
|
||||||
import 'package:stackwallet/utilities/text_styles.dart';
|
import 'package:stackwallet/utilities/text_styles.dart';
|
||||||
import 'package:stackwallet/widgets/rounded_white_container.dart';
|
import 'package:stackwallet/widgets/rounded_white_container.dart';
|
||||||
|
|
||||||
class MyTokenSelectItem extends ConsumerWidget {
|
class MyTokenSelectItem extends ConsumerStatefulWidget {
|
||||||
const MyTokenSelectItem(
|
const MyTokenSelectItem({
|
||||||
{Key? key, required this.walletId, required this.token, required})
|
Key? key,
|
||||||
: super(key: key);
|
required this.walletId,
|
||||||
|
required this.token,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
final String walletId;
|
final String walletId;
|
||||||
final EthContractInfo token;
|
final EthContractInfo token;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
ConsumerState<MyTokenSelectItem> createState() => _MyTokenSelectItemState();
|
||||||
final balanceInDecimal = Format.satoshisToEthTokenAmount(
|
}
|
||||||
1111111111111, //token.balance,
|
|
||||||
token.decimals,
|
|
||||||
);
|
|
||||||
|
|
||||||
|
class _MyTokenSelectItemState extends ConsumerState<MyTokenSelectItem> {
|
||||||
|
late final CachedEthTokenBalance cachedBalance;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
cachedBalance = CachedEthTokenBalance(widget.walletId, widget.token);
|
||||||
|
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
|
final address = await ref
|
||||||
|
.read(walletsChangeNotifierProvider)
|
||||||
|
.getManager(widget.walletId)
|
||||||
|
.currentReceivingAddress;
|
||||||
|
await cachedBalance.fetchAndUpdateCachedBalance(address);
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
return RoundedWhiteContainer(
|
return RoundedWhiteContainer(
|
||||||
padding: const EdgeInsets.all(0),
|
padding: const EdgeInsets.all(0),
|
||||||
child: MaterialButton(
|
child: MaterialButton(
|
||||||
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
|
key: Key("walletListItemButtonKey_${widget.token.symbol}"),
|
||||||
key: Key("walletListItemButtonKey_${token.symbol}"),
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 13),
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 13),
|
||||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
|
@ -45,29 +66,30 @@ class MyTokenSelectItem extends ConsumerWidget {
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
ref.read(tokenServiceStateProvider.state).state =
|
ref.read(tokenServiceStateProvider.state).state =
|
||||||
EthereumTokenService(
|
EthereumTokenService(
|
||||||
token: token,
|
token: widget.token,
|
||||||
secureStore: ref.read(secureStoreProvider),
|
secureStore: ref.read(secureStoreProvider),
|
||||||
ethWallet: ref
|
ethWallet: ref
|
||||||
.read(walletsChangeNotifierProvider)
|
.read(walletsChangeNotifierProvider)
|
||||||
.getManager(walletId)
|
.getManager(widget.walletId)
|
||||||
.wallet as EthereumWallet,
|
.wallet as EthereumWallet,
|
||||||
tracker: TransactionNotificationTracker(
|
tracker: TransactionNotificationTracker(
|
||||||
walletId: walletId,
|
walletId: widget.walletId,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
await showLoading<void>(
|
await showLoading<void>(
|
||||||
whileFuture: ref.read(tokenServiceProvider)!.initializeExisting(),
|
whileFuture: ref.read(tokenServiceProvider)!.initialize(),
|
||||||
context: context,
|
context: context,
|
||||||
message: "Loading ${token.name}",
|
message: "Loading ${widget.token.name}",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
await Navigator.of(context).pushNamed(
|
await Navigator.of(context).pushNamed(
|
||||||
TokenView.routeName,
|
TokenView.routeName,
|
||||||
arguments: walletId,
|
arguments: widget.walletId,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
SvgPicture.asset(
|
SvgPicture.asset(
|
||||||
|
@ -87,12 +109,13 @@ class MyTokenSelectItem extends ConsumerWidget {
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
token.name,
|
widget.token.name,
|
||||||
style: STextStyles.titleBold12(context),
|
style: STextStyles.titleBold12(context),
|
||||||
),
|
),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
Text(
|
Text(
|
||||||
"$balanceInDecimal ${token.symbol}",
|
"${cachedBalance.getCachedBalance().getTotal()} "
|
||||||
|
"${widget.token.symbol}",
|
||||||
style: STextStyles.itemSubtitle(context),
|
style: STextStyles.itemSubtitle(context),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -103,11 +126,23 @@ class MyTokenSelectItem extends ConsumerWidget {
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
token.symbol,
|
widget.token.symbol,
|
||||||
style: STextStyles.itemSubtitle(context),
|
style: STextStyles.itemSubtitle(context),
|
||||||
),
|
),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
const Text("0 USD"),
|
Text("${ref.watch(
|
||||||
|
priceAnd24hChangeNotifierProvider.select(
|
||||||
|
(value) => value
|
||||||
|
.getTokenPrice(widget.token.contractAddress)
|
||||||
|
.item1
|
||||||
|
.toStringAsFixed(2),
|
||||||
|
),
|
||||||
|
)} "
|
||||||
|
"${ref.watch(
|
||||||
|
prefsChangeNotifierProvider.select(
|
||||||
|
(value) => value.currency,
|
||||||
|
),
|
||||||
|
)}"),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
39
lib/services/ethereum/cached_eth_token_balance.dart
Normal file
39
lib/services/ethereum/cached_eth_token_balance.dart
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import 'package:stackwallet/models/ethereum/eth_token.dart';
|
||||||
|
import 'package:stackwallet/models/token_balance.dart';
|
||||||
|
import 'package:stackwallet/services/ethereum/ethereum_api.dart';
|
||||||
|
import 'package:stackwallet/services/mixins/eth_token_cache.dart';
|
||||||
|
import 'package:stackwallet/utilities/logger.dart';
|
||||||
|
|
||||||
|
class CachedEthTokenBalance with EthTokenCache {
|
||||||
|
final String walletId;
|
||||||
|
final EthContractInfo token;
|
||||||
|
|
||||||
|
CachedEthTokenBalance(this.walletId, this.token) {
|
||||||
|
initCache(walletId, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> fetchAndUpdateCachedBalance(String address) async {
|
||||||
|
final response = await EthereumAPI.getWalletTokenBalance(
|
||||||
|
address: address,
|
||||||
|
contractAddress: token.contractAddress,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.value != null) {
|
||||||
|
await updateCachedBalance(
|
||||||
|
TokenBalance(
|
||||||
|
contractAddress: token.contractAddress,
|
||||||
|
decimalPlaces: token.decimals,
|
||||||
|
total: response.value!,
|
||||||
|
spendable: response.value!,
|
||||||
|
blockedTotal: 0,
|
||||||
|
pendingSpendable: 0,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Logging.instance.log(
|
||||||
|
"CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}",
|
||||||
|
level: LogLevel.Warning,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,8 +35,6 @@ mixin EthTokenCache {
|
||||||
}
|
}
|
||||||
return TokenBalance.fromJson(
|
return TokenBalance.fromJson(
|
||||||
jsonString,
|
jsonString,
|
||||||
_token.contractAddress,
|
|
||||||
_token.decimals,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue