switch to safeParse

This commit is contained in:
Robert Malikowski
2026-08-21 14:14:32 +02:00
parent 5a15d5644c
commit d3a2966706
2 changed files with 10 additions and 4 deletions
+8
View File
@@ -25,6 +25,14 @@ class Money implements Comparable<Money> {
return Money(amount, currency);
}
factory Money.safeParse(source, Currency currency, {bool isBaseUnit = false}) {
final amount = isBaseUnit
? BigInt.parse(source.toString())
: parseFixed(source.toString().withDecimals(currency.decimals), currency.decimals);
return Money(amount, currency);
}
/// Parse the [source] and turn it into [Money] if possible
///
/// As [parse] except that this method returns `null` if the input is not
+2 -4
View File
@@ -171,17 +171,15 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
return maxAmount.toStringAsFixed(2);
}
Money amountForQuote(Quote quote) => Money.parse(
Money amountForQuote(Quote quote) => Money.safeParse(
(double.parse(fiatAmount) / quote.rate).toStringAsFixed(cryptoCurrency.decimals),
cryptoCurrency);
Money fiatAmountForQuote(Quote quote) {
return Money.parse(
Money fiatAmountForQuote(Quote quote) => Money.safeParse(
(fiatConversionStore.prices[cryptoCurrency]! *
double.parse(amountForQuote(quote).toString()))
.toStringAsFixed(2),
fiatCurrency);
}
// based on usd values, should have roughly equal worth (was done with ai though so it's subject to correction)
static final Map<FiatCurrency, List<String>> _defaultAmountsMap = {