diff --git a/lib/src/screens/send/widgets/send_card.dart b/lib/src/screens/send/widgets/send_card.dart index 4edae103c..db67ea2b0 100644 --- a/lib/src/screens/send/widgets/send_card.dart +++ b/lib/src/screens/send/widgets/send_card.dart @@ -560,7 +560,9 @@ class SendCardState extends State reaction((_) => output.fiatAmount, (String amount) { if (amount != fiatAmountController.text) { - fiatAmountController.text = amount; + final doubleAmount = double.tryParse(amount) ?? 0; + final doubleFee = double.tryParse(sendViewModel.estimatedFiatFee(sendViewModel.balance)) ?? 0; + fiatAmountController.text = doubleAmount - doubleFee > 0 ? "${doubleAmount - doubleFee}" : "0.00"; } }); diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index b615c00c4..58e780324 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/core/amount_converter.dart'; import 'package:cake_wallet/entities/balance_display_mode.dart'; import 'package:cake_wallet/entities/transaction_description.dart'; import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart'; @@ -83,10 +84,7 @@ abstract class SendViewModelBase with Store { String get pendingTransactionFiatAmount { try { if (pendingTransaction != null) { - final fiat = calculateFiatAmount( - price: _fiatConversationStore.prices[selectedCryptoCurrency], - cryptoAmount: pendingTransaction.amountFormatted); - return fiat; + return _calculateFiatAmount(pendingTransaction.amountFormatted); } else { return '0.00'; } @@ -99,10 +97,7 @@ abstract class SendViewModelBase with Store { String get pendingTransactionFeeFiatAmount { try { if (pendingTransaction != null) { - final fiat = calculateFiatAmount( - price: _fiatConversationStore.prices[selectedCryptoCurrency], - cryptoAmount: pendingTransaction.feeFormatted); - return fiat; + return _calculateFiatAmount(pendingTransaction.feeFormatted); } else { return '0.00'; } @@ -111,6 +106,12 @@ abstract class SendViewModelBase with Store { } } + String _calculateFiatAmount(String cryptoAmount) { + return calculateFiatAmount( + price: _fiatConversationStore.prices[selectedCryptoCurrency], + cryptoAmount: cryptoAmount); + } + FiatCurrency get fiat => _settingsStore.fiatCurrency; TransactionPriority get transactionPriority => @@ -159,6 +160,16 @@ abstract class SendViewModelBase with Store { bool get hasCurrecyChanger => walletType == WalletType.haven; + int estimatedFee(String amount) { + return _wallet.calculateEstimatedFee(_settingsStore.priority[_wallet.type], AmountConverter.amountStringToInt( + selectedCryptoCurrency, amount)); + } + + String estimatedFiatFee(String amount) { + return _calculateFiatAmount(AmountConverter.amountIntToString( + selectedCryptoCurrency, estimatedFee(amount))); + } + final WalletBase _wallet; final SettingsStore _settingsStore; final SendTemplateViewModel sendTemplateViewModel; @@ -254,6 +265,6 @@ abstract class SendViewModelBase with Store { return priority.toString(); } - bool _isEqualCurrency(String currency) => + bool _isEqualCurrency(String currency) => currency.toLowerCase() == _wallet.currency.title.toLowerCase(); }