Merge pull request #554 from cypherstack/ui-fixes

fix: Handle sent to self transactions when sent to a change address
This commit is contained in:
julian-CStack 2023-05-25 12:25:23 -06:00 committed by GitHub
commit 990cc7cfa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 62 deletions

View file

@ -6,7 +6,7 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:stackwallet/pages/wallet_view/wallet_view.dart';
import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/services/coins/firo/firo_wallet.dart';
import 'package:stackwallet/themes/coin_icon_provider.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
@ -24,13 +24,11 @@ class FavoriteCard extends ConsumerStatefulWidget {
required this.walletId,
required this.width,
required this.height,
required this.managerProvider,
}) : super(key: key);
final String walletId;
final double width;
final double height;
final ChangeNotifierProvider<Manager> managerProvider;
@override
ConsumerState<FavoriteCard> createState() => _FavoriteCardState();
@ -38,15 +36,10 @@ class FavoriteCard extends ConsumerStatefulWidget {
class _FavoriteCardState extends ConsumerState<FavoriteCard> {
late final String walletId;
late final ChangeNotifierProvider<Manager> managerProvider;
Amount _cachedBalance = Amount.zero;
Amount _cachedFiatValue = Amount.zero;
@override
void initState() {
walletId = widget.walletId;
managerProvider = widget.managerProvider;
super.initState();
}
@ -55,9 +48,13 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
@override
Widget build(BuildContext context) {
final coin = ref.watch(managerProvider.select((value) => value.coin));
final coin = ref.watch(
walletsChangeNotifierProvider
.select((value) => value.getManager(walletId).coin),
);
final externalCalls = ref.watch(
prefsChangeNotifierProvider.select((value) => value.externalCalls));
prefsChangeNotifierProvider.select((value) => value.externalCalls),
);
return ConditionalParent(
condition: Util.isDesktop,
@ -109,7 +106,10 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
child: GestureDetector(
onTap: () async {
if (coin == Coin.monero || coin == Coin.wownero) {
await ref.read(managerProvider).initializeExisting();
await ref
.read(walletsChangeNotifierProvider)
.getManager(walletId)
.initializeExisting();
}
if (mounted) {
if (Util.isDesktop) {
@ -122,7 +122,9 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
WalletView.routeName,
arguments: Tuple2(
walletId,
managerProvider,
ref
.read(walletsChangeNotifierProvider)
.getManagerProvider(walletId),
),
);
}
@ -205,8 +207,12 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
children: [
Expanded(
child: Text(
ref.watch(managerProvider
.select((value) => value.walletName)),
ref.watch(
walletsChangeNotifierProvider.select(
(value) =>
value.getManager(walletId).walletName,
),
),
style: STextStyles.itemSubtitle12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
@ -225,41 +231,54 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
],
),
),
FutureBuilder(
future: Future(() => ref.watch(managerProvider
.select((value) => value.balance.total))),
builder: (builderContext, AsyncSnapshot<Amount> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
if (snapshot.data != null) {
_cachedBalance = snapshot.data!;
if (externalCalls && _cachedBalance > Amount.zero) {
_cachedFiatValue = (_cachedBalance.decimal *
ref
.watch(
priceAnd24hChangeNotifierProvider
.select(
(value) => value.getPrice(coin),
),
)
.item1)
.toAmount(fractionDigits: 2);
}
}
Builder(
builder: (context) {
final balance = ref.watch(
walletsChangeNotifierProvider.select(
(value) => value.getManager(walletId).balance,
),
);
Amount total = balance.total;
if (coin == Coin.firo || coin == Coin.firoTestNet) {
final balancePrivate = ref.watch(
walletsChangeNotifierProvider.select(
(value) => (value.getManager(walletId).wallet
as FiroWallet)
.balancePrivate,
),
);
total += balancePrivate.total;
}
Amount fiatTotal = Amount.zero;
if (externalCalls && total > Amount.zero) {
fiatTotal = (total.decimal *
ref
.watch(
priceAnd24hChangeNotifierProvider.select(
(value) => value.getPrice(coin),
),
)
.item1)
.toAmount(fractionDigits: 2);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
"${_cachedBalance.localizedStringAsFixed(
"${total.localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),
localeServiceChangeNotifierProvider.select(
(value) => value.locale,
),
),
decimalPlaces: ref.watch(managerProvider
.select((value) => value.coin.decimals)),
decimalPlaces: coin.decimals,
)} ${coin.ticker}",
style: STextStyles.titleBold12(context).copyWith(
fontSize: 16,
@ -275,15 +294,17 @@ class _FavoriteCardState extends ConsumerState<FavoriteCard> {
),
if (externalCalls)
Text(
"${_cachedFiatValue.localizedStringAsFixed(
"${fiatTotal.localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),
localeServiceChangeNotifierProvider.select(
(value) => value.locale,
),
),
decimalPlaces: 2,
)} ${ref.watch(
prefsChangeNotifierProvider
.select((value) => value.currency),
prefsChangeNotifierProvider.select(
(value) => value.currency,
),
)}",
style:
STextStyles.itemSubtitle12(context).copyWith(

View file

@ -211,7 +211,6 @@ class _FavoriteWalletsState extends ConsumerState<FavoriteWallets> {
child: FavoriteCard(
key: Key("favCard_$walletId"),
walletId: walletId!,
managerProvider: managerProvider!,
width: cardWidth,
height: cardHeight,
),
@ -219,7 +218,6 @@ class _FavoriteWalletsState extends ConsumerState<FavoriteWallets> {
: FavoriteCard(
key: Key("favCard_$walletId"),
walletId: walletId!,
managerProvider: managerProvider!,
width: cardWidth,
height: cardHeight,
)

View file

@ -74,7 +74,6 @@ class DesktopFavoriteWallets extends ConsumerWidget {
key: Key(walletName),
width: cardWidth,
height: cardHeight,
managerProvider: managerProvider,
);
})
],

View file

@ -148,19 +148,31 @@ mixin ElectrumXParsing {
type = TransactionType.outgoing;
amount = amountSentFromWallet - changeAmount - fee;
final possible =
outputAddresses.difference(myChangeReceivedOnAddresses).first;
// non wallet addresses found in tx outputs
final nonWalletOutAddresses = outputAddresses.difference(
myChangeReceivedOnAddresses,
);
if (transactionAddress.value != possible) {
transactionAddress = Address(
walletId: walletId,
value: possible,
derivationIndex: -1,
derivationPath: null,
subType: AddressSubType.nonWallet,
type: AddressType.nonWallet,
publicKey: [],
);
if (nonWalletOutAddresses.isNotEmpty) {
final possible = nonWalletOutAddresses.first;
if (transactionAddress.value != possible) {
transactionAddress = Address(
walletId: walletId,
value: possible,
derivationIndex: -1,
derivationPath: null,
subType: AddressSubType.nonWallet,
type: AddressType.nonWallet,
publicKey: [],
);
}
} else {
// some other type of tx where the receiving address is
// one of my change addresses
type = TransactionType.sentToSelf;
amount = changeAmount;
}
} else {
// incoming tx

View file

@ -4,8 +4,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/coins/firo/firo_wallet.dart';
import 'package:stackwallet/themes/coin_icon_provider.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/text_styles.dart';
@ -34,6 +36,28 @@ class _ManagedFavoriteCardState extends ConsumerState<ManagedFavorite> {
final isDesktop = Util.isDesktop;
final balance = ref.watch(
walletsChangeNotifierProvider.select(
(value) => value.getManager(widget.walletId).balance,
),
);
Amount total = balance.total;
if (manager.coin == Coin.firo || manager.coin == Coin.firoTestNet) {
final balancePrivate = ref.watch(
walletsChangeNotifierProvider.select(
(value) => (value
.getManager(
widget.walletId,
)
.wallet as FiroWallet)
.balancePrivate,
),
);
total += balancePrivate.total;
}
return RoundedWhiteContainer(
padding: EdgeInsets.all(isDesktop ? 0 : 4.0),
child: RawMaterialButton(
@ -107,7 +131,7 @@ class _ManagedFavoriteCardState extends ConsumerState<ManagedFavorite> {
),
Expanded(
child: Text(
"${manager.balance.total.localizedStringAsFixed(
"${total.localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider.select(
(value) => value.locale,
@ -150,7 +174,7 @@ class _ManagedFavoriteCardState extends ConsumerState<ManagedFavorite> {
height: 2,
),
Text(
"${manager.balance.total.localizedStringAsFixed(
"${total.localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider.select(
(value) => value.locale,