mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
Add more guards to amounts being zero or infinity (#3612)
* Add more guards to amounts being zero or infinity * Add more guards to amounts being zero or infinity
This commit is contained in:
@@ -8,8 +8,9 @@ String calculateFiatAmount({double? price, String? cryptoAmount, bool raw = fals
|
||||
cryptoAmount = cryptoAmount.sanitized();
|
||||
|
||||
final _amount = double.tryParse(cryptoAmount);
|
||||
if (_amount == null || _amount.isNaN) return '0.00';
|
||||
if (_amount == null || !_amount.isFinite) return '0.00';
|
||||
final _result = price * _amount;
|
||||
if (!_result.isFinite) return '0.00';
|
||||
final result = _result < 0 ? _result * -1 : _result;
|
||||
|
||||
if (result == 0.0) {
|
||||
|
||||
@@ -7,6 +7,10 @@ String calculateFiatAmountRaw({required double cryptoAmount, double? price}) {
|
||||
|
||||
final result = price * cryptoAmount;
|
||||
|
||||
if (!result.isFinite) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
if (result == 0.0) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
@@ -214,7 +214,7 @@ class SendTransactionDetails extends StatelessWidget {
|
||||
sendViewModel.balance, sendViewModel.selectedCryptoCurrency) ??
|
||||
zero;
|
||||
|
||||
return sendViewModel.selectedCryptoCurrency.tryParseAmount(o.cryptoAmount) ?? zero;
|
||||
return o.cryptoAmountMoney;
|
||||
}, sendViewModel.selectedCryptoCurrency))
|
||||
: sendViewModel.amountParsingProxy.asDisplayString(transaction.amount);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import 'package:cake_wallet/src/widgets/new_list_row/new_list_section.dart';
|
||||
import 'package:cake_wallet/view_model/exchange/exchange_trade_view_model.dart';
|
||||
import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart';
|
||||
import 'package:cake_wallet/view_model/send/send_view_model_state.dart';
|
||||
import 'package:cw_core/amount/amount_sanitizer.dart';
|
||||
import 'package:cw_core/amount/money.dart';
|
||||
import 'package:cw_core/crypto_amount_format.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
@@ -176,9 +177,11 @@ class SwapTransactionDetails extends StatelessWidget {
|
||||
iconPath: exchangeViewModel.depositCurrency.iconPath ?? "",
|
||||
badgeIconPath: _resolveChainBadgePath(exchangeViewModel.depositCurrency),
|
||||
trailingText: exchangeViewModel.amountParsingProxy
|
||||
.asDisplayStringWithSymbol(exchangeViewModel.depositCurrency
|
||||
.tryParseAmount(exchangeTradeViewModel.trade.amount) ??
|
||||
Money.zero(exchangeViewModel.depositCurrency)),
|
||||
.asDisplayStringWithSymbol(
|
||||
exchangeViewModel.depositCurrency.tryParseAmount(
|
||||
exchangeTradeViewModel.trade.amount.sanitized(),
|
||||
) ??
|
||||
Money.zero(exchangeViewModel.depositCurrency)),
|
||||
),
|
||||
if (exchangeTradeViewModel.sendViewModel.pendingTransaction != null)
|
||||
ListItemRegularRow(
|
||||
|
||||
@@ -338,10 +338,21 @@ abstract class OutputBase with Store {
|
||||
? CryptoCurrency.btc
|
||||
: cryptoCurrencyHandler();
|
||||
|
||||
final price = _fiatConversationStore.prices[cryptoCurrency];
|
||||
if (price == null || !price.isFinite || price <= 0) {
|
||||
printV('_updateCryptoAmount: invalid fiat rate $price for $cryptoCurrency');
|
||||
cryptoAmount = '';
|
||||
return;
|
||||
}
|
||||
|
||||
final decimals = min(20, cryptoCurrencyHandler().decimals);
|
||||
final crypto = (double.parse(fiatAmount.replaceAll(',', '.')) /
|
||||
_fiatConversationStore.prices[cryptoCurrency]!)
|
||||
.toStringAsFixed(decimals);
|
||||
final rawCrypto = double.parse(fiatAmount.replaceAll(',', '.')) / price;
|
||||
if (!rawCrypto.isFinite) {
|
||||
cryptoAmount = '';
|
||||
return;
|
||||
}
|
||||
|
||||
final crypto = rawCrypto.toStringAsFixed(decimals);
|
||||
|
||||
if (cryptoAmount != crypto) cryptoAmount = crypto;
|
||||
} catch (e) {
|
||||
|
||||
@@ -54,6 +54,7 @@ import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_mod
|
||||
import 'package:cake_wallet/wownero/wownero.dart';
|
||||
import 'package:cake_wallet/zano/zano.dart';
|
||||
import 'package:cake_wallet/zcash/zcash.dart';
|
||||
import 'package:cw_core/amount/amount_sanitizer.dart';
|
||||
import 'package:cw_core/amount/money.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/currency_for_wallet_type.dart';
|
||||
@@ -882,7 +883,11 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
final fee = actualFee > 0 ? actualFee : 0.0005;
|
||||
|
||||
final fromCurrency = trade.from ?? CryptoCurrency.sol;
|
||||
final amount = Money.tryParse(trade.amount, fromCurrency) ?? Money.zero(fromCurrency);
|
||||
final amount = Money.tryParse(
|
||||
trade.amount.sanitized(),
|
||||
fromCurrency,
|
||||
strictParsing: false,
|
||||
) ?? Money.zero(fromCurrency);
|
||||
|
||||
pendingTransaction = await solana!.signAndPrepareJupiterSwapTransaction(
|
||||
wallet,
|
||||
@@ -920,8 +925,15 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
final bool isTradeTx = trade != null && provider != null;
|
||||
|
||||
if (isTradeTx) {
|
||||
final tradeAmountDouble = double.tryParse(trade.amount) ?? 0.0;
|
||||
if (tradeAmountDouble <= 0) throw Exception('Trade amount must be greater than 0');
|
||||
final tradeAmountMoney = Money.tryParse(
|
||||
trade.amount.sanitized(),
|
||||
trade.from ?? selectedCryptoCurrency,
|
||||
strictParsing: false,
|
||||
);
|
||||
if (tradeAmountMoney == null || tradeAmountMoney.sign <= 0) {
|
||||
throw Exception('Trade amount must be greater than 0');
|
||||
}
|
||||
final tradeAmountDouble = double.tryParse(tradeAmountMoney.toString()) ?? 0.0;
|
||||
|
||||
if (trade.isSendAll == true) {
|
||||
if (provider is NearIntentsExchangeProvider) {
|
||||
|
||||
Reference in New Issue
Block a user