From d3a29667067c804781444936a94f9c8674ddbc65 Mon Sep 17 00:00:00 2001 From: Robert Malikowski Date: Fri, 21 Aug 2026 14:14:32 +0200 Subject: [PATCH] switch to safeParse --- cw_core/lib/amount/money.dart | 8 ++++++++ lib/view_model/buy/buy_sell_view_model.dart | 6 ++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cw_core/lib/amount/money.dart b/cw_core/lib/amount/money.dart index de5483e47..9ac551da6 100644 --- a/cw_core/lib/amount/money.dart +++ b/cw_core/lib/amount/money.dart @@ -25,6 +25,14 @@ class Money implements Comparable { 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 diff --git a/lib/view_model/buy/buy_sell_view_model.dart b/lib/view_model/buy/buy_sell_view_model.dart index 7f6ffd8f8..185516493 100644 --- a/lib/view_model/buy/buy_sell_view_model.dart +++ b/lib/view_model/buy/buy_sell_view_model.dart @@ -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> _defaultAmountsMap = {