| 1 | import 'package:cake_wallet/entities/bitcoin_amount_display_mode.dart'; |
| 2 | import 'package:cake_wallet/src/screens/wallet_connect/utils/string_parsing.dart'; |
| 3 | import 'package:cw_core/amount/money.dart'; |
| 4 | import 'package:cw_core/crypto_amount_format.dart'; |
| 5 | import 'package:cw_core/crypto_currency.dart'; |
| 6 | import 'package:cw_core/currency.dart'; |
| 7 | import 'package:cw_core/utils/print_verbose.dart'; |
| 8 | |
| 9 | class AmountParsingProxy { |
| 10 | final BitcoinAmountDisplayMode displayMode; |
| 11 | |
| 12 | const AmountParsingProxy(this.displayMode); |
| 13 | |
| 14 | /// [getCryptoInputAmount] turns the input [amount] into the canonical representation of [cryptoCurrency] |
| 15 | String getCanonicalCryptoAmount(String amount, CryptoCurrency cryptoCurrency) { |
| 16 | if (useSatoshi(cryptoCurrency) && amount.isNotEmpty) { |
| 17 | return cryptoCurrency.formatAmount(BigInt.tryParse(amount) ?? BigInt.zero); |
| 18 | } |
| 19 | |
| 20 | return amount; |
| 21 | } |
| 22 | |
| 23 | /// [getCryptoOutputAmount] turns the input [amount] into the preferred representation of [cryptoCurrency] |
| 24 | String getDisplayCryptoAmount(String amount, CryptoCurrency cryptoCurrency) { |
| 25 | try { |
| 26 | if (useSatoshi(cryptoCurrency) && amount.isNotEmpty) { |
| 27 | return cryptoCurrency |
| 28 | .parseAmount(amount.withMaxDecimals(cryptoCurrency.decimals)) |
| 29 | .toStringWithPrecision(useBaseUnit: true); |
| 30 | } |
| 31 | |
| 32 | return amount.withMaxDecimals(cryptoCurrency.decimals); |
| 33 | } catch (_) { |
| 34 | printV( |
| 35 | "failed to parse amount $amount for currency ${cryptoCurrency.title}, falling back to showing unparsed"); |
| 36 | return amount; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | String getDisplayCryptoString(int amount, CryptoCurrency cryptoCurrency) => |
| 41 | asDisplayString(Money.fromInt(amount, cryptoCurrency)); |
| 42 | |
| 43 | /// [getDisplayCryptoString] turns the input [amount] into the preferred representation of [cryptoCurrency] |
| 44 | /// |
| 45 | /// if [displayMode] is [BitcoinAmountDisplayMode.satoshi] it returns 1 BTC as 100000000 |
| 46 | /// if [displayMode] is [BitcoinAmountDisplayMode.bitcoin] it returns 1 BTC as 1 |
| 47 | /// hardcoding fractionalDigits to 8 as it's a display string, so no need to show more than that |
| 48 | String asDisplayString(Money amount) => |
| 49 | amount.toStringWithPrecision(useBaseUnit: useSatoshi(amount.currency), fractionalDigits: 8); |
| 50 | |
| 51 | /// [asDisplayStringWithSymbol] turns the input [amount] into the preferred representation of |
| 52 | /// [cryptoCurrency] following the symbol of the unit |
| 53 | /// |
| 54 | /// if [displayMode] is [BitcoinAmountDisplayMode.satoshi] it returns 1 BTC as 100000000 sats |
| 55 | /// if [displayMode] is [BitcoinAmountDisplayMode.bitcoin] it returns 1 BTC as 1 BTC |
| 56 | /// hardcoding fractionalDigits to 8 as it's a display string, so no need to show more than that |
| 57 | String asDisplayStringWithSymbol(Money amount) => |
| 58 | amount.toStringWithSymbol(useBaseUnit: useSatoshi(amount.currency), fractionalDigits: 8); |
| 59 | |
| 60 | /// [parseCryptoString] turns the the display representation [string] into `Money` with [cryptoCurrency] |
| 61 | Money parseCryptoString(String amount, CryptoCurrency cryptoCurrency) => |
| 62 | Money.parse(amount, cryptoCurrency, isBaseUnit: useSatoshi(cryptoCurrency)); |
| 63 | |
| 64 | /// [tryParseCryptoString] tries to turn the display representation [string] into `Money` with [cryptoCurrency] |
| 65 | Money? tryParseCryptoString(String amount, CryptoCurrency cryptoCurrency) => |
| 66 | Money.tryParse(amount, cryptoCurrency, isBaseUnit: useSatoshi(cryptoCurrency)); |
| 67 | |
| 68 | /// [getCryptoSymbol] returns the correct Symbol related to the presentation |
| 69 | String getCryptoSymbol(CryptoCurrency cryptoCurrency) => |
| 70 | useSatoshi(cryptoCurrency) ? "sats" : cryptoCurrency.title.safeSubString(0, 8); |
| 71 | |
| 72 | bool useSatoshi(Currency cryptoCurrency) => |
| 73 | ([CryptoCurrency.btc, CryptoCurrency.btcln].contains(cryptoCurrency) && |
| 74 | displayMode == BitcoinAmountDisplayMode.satoshi) || |
| 75 | (CryptoCurrency.btcln == cryptoCurrency && |
| 76 | displayMode == BitcoinAmountDisplayMode.satoshiForLightning); |
| 77 | } |