eth wallet token list fixes

This commit is contained in:
julian 2023-03-01 15:27:10 -06:00
parent c89ae56135
commit e732c4f1b7
3 changed files with 100 additions and 28 deletions

View file

@ -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/providers.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/transaction_notification_tracker.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.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/text_styles.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
class MyTokenSelectItem extends ConsumerWidget {
const MyTokenSelectItem(
{Key? key, required this.walletId, required this.token, required})
: super(key: key);
class MyTokenSelectItem extends ConsumerStatefulWidget {
const MyTokenSelectItem({
Key? key,
required this.walletId,
required this.token,
}) : super(key: key);
final String walletId;
final EthContractInfo token;
@override
Widget build(BuildContext context, WidgetRef ref) {
final balanceInDecimal = Format.satoshisToEthTokenAmount(
1111111111111, //token.balance,
token.decimals,
);
ConsumerState<MyTokenSelectItem> createState() => _MyTokenSelectItemState();
}
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(
padding: const EdgeInsets.all(0),
child: MaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
key: Key("walletListItemButtonKey_${token.symbol}"),
key: Key("walletListItemButtonKey_${widget.token.symbol}"),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 13),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
@ -45,29 +66,30 @@ class MyTokenSelectItem extends ConsumerWidget {
onPressed: () async {
ref.read(tokenServiceStateProvider.state).state =
EthereumTokenService(
token: token,
token: widget.token,
secureStore: ref.read(secureStoreProvider),
ethWallet: ref
.read(walletsChangeNotifierProvider)
.getManager(walletId)
.getManager(widget.walletId)
.wallet as EthereumWallet,
tracker: TransactionNotificationTracker(
walletId: walletId,
walletId: widget.walletId,
),
);
await showLoading<void>(
whileFuture: ref.read(tokenServiceProvider)!.initializeExisting(),
whileFuture: ref.read(tokenServiceProvider)!.initialize(),
context: context,
message: "Loading ${token.name}",
message: "Loading ${widget.token.name}",
);
if (mounted) {
await Navigator.of(context).pushNamed(
TokenView.routeName,
arguments: walletId,
arguments: widget.walletId,
);
}
},
child: Row(
children: [
SvgPicture.asset(
@ -87,12 +109,13 @@ class MyTokenSelectItem extends ConsumerWidget {
Row(
children: [
Text(
token.name,
widget.token.name,
style: STextStyles.titleBold12(context),
),
const Spacer(),
Text(
"$balanceInDecimal ${token.symbol}",
"${cachedBalance.getCachedBalance().getTotal()} "
"${widget.token.symbol}",
style: STextStyles.itemSubtitle(context),
),
],
@ -103,11 +126,23 @@ class MyTokenSelectItem extends ConsumerWidget {
Row(
children: [
Text(
token.symbol,
widget.token.symbol,
style: STextStyles.itemSubtitle(context),
),
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,
),
)}"),
],
),
],

View 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,
);
}
}
}

View file

@ -35,8 +35,6 @@ mixin EthTokenCache {
}
return TokenBalance.fromJson(
jsonString,
_token.contractAddress,
_token.decimals,
);
}