From c65d8c43bcabe730c565e8603c4f03c10b34c356 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 5 Mar 2024 09:51:20 -0800 Subject: [PATCH] monero.com fixes --- cw_bitcoin/lib/bitcoin_amount_format.dart | 12 +++---- cw_lightning/lib/lightning_balance.dart | 2 +- .../lib/lightning_receive_page_option.dart | 19 ------------ lib/entities/main_actions.dart | 4 +-- lib/lightning/cw_lightning.dart | 31 +++++++++++++++++++ .../receive/lightning_invoice_page.dart | 31 ++++++++----------- .../receive/lightning_receive_page.dart | 27 ++++++---------- .../receive/widgets/lightning_input_form.dart | 8 +++-- .../send/lightning_send_confirm_page.dart | 4 +-- .../lightning_invoice_page_view_model.dart | 1 - tool/configure.dart | 8 ++++- 11 files changed, 76 insertions(+), 71 deletions(-) delete mode 100644 cw_lightning/lib/lightning_receive_page_option.dart diff --git a/cw_bitcoin/lib/bitcoin_amount_format.dart b/cw_bitcoin/lib/bitcoin_amount_format.dart index a65443849..7069c8396 100644 --- a/cw_bitcoin/lib/bitcoin_amount_format.dart +++ b/cw_bitcoin/lib/bitcoin_amount_format.dart @@ -8,8 +8,8 @@ final bitcoinAmountFormat = NumberFormat() ..maximumFractionDigits = bitcoinAmountLength ..minimumFractionDigits = 1; -String bitcoinAmountToString({required int amount}) => bitcoinAmountFormat.format( - cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider)); +String bitcoinAmountToString({required int amount}) => + bitcoinAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider)); double bitcoinAmountToDouble({required int amount}) => cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider); @@ -27,11 +27,7 @@ int stringDoubleToBitcoinAmount(String amount) { } String bitcoinAmountToLightningString({required int amount}) { - String formattedAmount = bitcoinAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: lightningAmountDivider)); + String formattedAmount = bitcoinAmountFormat + .format(cryptoAmountToDouble(amount: amount, divider: lightningAmountDivider)); return formattedAmount.substring(0, formattedAmount.length - 2); } - -String satsToLightningString(int sats) { - String formattedAmount = bitcoinAmountFormat.format(sats); - return formattedAmount.substring(0, formattedAmount.length - 2); -} \ No newline at end of file diff --git a/cw_lightning/lib/lightning_balance.dart b/cw_lightning/lib/lightning_balance.dart index c07d5e651..308675d7b 100644 --- a/cw_lightning/lib/lightning_balance.dart +++ b/cw_lightning/lib/lightning_balance.dart @@ -36,7 +36,7 @@ class LightningBalance extends ElectrumBalance { @override String get formattedUnAvailableBalance { - final frozenFormatted = bitcoinAmountToString(amount: frozen); + final frozenFormatted = bitcoinAmountToLightningString(amount: frozen); return frozenFormatted == '0.0' ? '' : frozenFormatted; } diff --git a/cw_lightning/lib/lightning_receive_page_option.dart b/cw_lightning/lib/lightning_receive_page_option.dart deleted file mode 100644 index fd2917a14..000000000 --- a/cw_lightning/lib/lightning_receive_page_option.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:cw_core/receive_page_option.dart'; - -class LightningReceivePageOption implements ReceivePageOption { - static const lightningOnchain = LightningReceivePageOption._('lightningOnchain'); - static const lightningInvoice = LightningReceivePageOption._('lightningInvoice'); - - const LightningReceivePageOption._(this.value); - - final String value; - - String toString() { - return value; - } - - static const all = [ - LightningReceivePageOption.lightningInvoice, - LightningReceivePageOption.lightningOnchain - ]; -} diff --git a/lib/entities/main_actions.dart b/lib/entities/main_actions.dart index 3152f7f4f..57ba57583 100644 --- a/lib/entities/main_actions.dart +++ b/lib/entities/main_actions.dart @@ -1,10 +1,10 @@ import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; import 'package:cw_core/wallet_type.dart'; -import 'package:cw_lightning/lightning_receive_page_option.dart'; import 'package:flutter/material.dart'; class MainActions { @@ -60,7 +60,7 @@ class MainActions { Navigator.pushNamed( context, Routes.lightningInvoice, - arguments: [LightningReceivePageOption.lightningInvoice], + arguments: [lightning!.getOptionInvoice()], ); return; } diff --git a/lib/lightning/cw_lightning.dart b/lib/lightning/cw_lightning.dart index 323ee08e3..5cb5e71f6 100644 --- a/lib/lightning/cw_lightning.dart +++ b/lib/lightning/cw_lightning.dart @@ -65,4 +65,35 @@ class CWLightning extends Lightning { @override List getLightningReceivePageOptions() => LightningReceivePageOption.all; + + @override + ReceivePageOption getOptionInvoice() => LightningReceivePageOption.lightningInvoice; + + @override + ReceivePageOption getOptionOnchain() => LightningReceivePageOption.lightningOnchain; + + String satsToLightningString(int sats) { + const bitcoinAmountLength = 8; + const bitcoinAmountDivider = 100000000; + const lightningAmountDivider = 1; + final bitcoinAmountFormat = NumberFormat() + ..maximumFractionDigits = bitcoinAmountLength + ..minimumFractionDigits = 1; + + String formattedAmount = bitcoinAmountFormat.format(sats); + return formattedAmount.substring(0, formattedAmount.length - 2); + } + + String bitcoinAmountToLightningString({required int amount}) { + final bitcoinAmountFormat = NumberFormat() + ..maximumFractionDigits = bitcoinAmountLength + ..minimumFractionDigits = 1; + String formattedAmount = bitcoinAmountFormat + .format(cryptoAmountToDouble(amount: amount, divider: 1)); + return formattedAmount.substring(0, formattedAmount.length - 2); + } + + int bitcoinAmountToLightningAmount({required int amount}) { + return amount * 100000000; + } } diff --git a/lib/src/screens/receive/lightning_invoice_page.dart b/lib/src/screens/receive/lightning_invoice_page.dart index e23cdcb9a..fe9e7e24f 100644 --- a/lib/src/screens/receive/lightning_invoice_page.dart +++ b/lib/src/screens/receive/lightning_invoice_page.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/bitcoin/bitcoin.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; import 'package:cake_wallet/src/screens/receive/widgets/lightning_input_form.dart'; import 'package:cake_wallet/src/screens/receive/widgets/qr_image.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; @@ -16,9 +17,8 @@ import 'package:cake_wallet/utils/show_bar.dart'; import 'package:cake_wallet/view_model/dashboard/receive_option_view_model.dart'; import 'package:cake_wallet/view_model/lightning_invoice_page_view_model.dart'; import 'package:cake_wallet/view_model/lightning_view_model.dart'; -import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/receive_page_option.dart'; -import 'package:cw_lightning/lightning_receive_page_option.dart'; +import 'package:cw_core/lightning_receive_page_option.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -175,14 +175,15 @@ class LightningInvoicePage extends BasePage { int balance = (snapshot.data as List)[2]; if (balance == 0) { int min = (snapshot.data as List)[0]; - finalText = - S.of(context).lightning_invoice_min(satsToLightningString(min)); + finalText = S + .of(context) + .lightning_invoice_min(lightning!.satsToLightningString(min)); } else { int min = (snapshot.data as List)[0]; int max = (snapshot.data as List)[1]; finalText = S.of(context).lightning_invoice_min_max( - satsToLightningString(min), - satsToLightningString(max), + lightning!.satsToLightningString(min), + lightning!.satsToLightningString(max), ); } @@ -232,18 +233,12 @@ class LightningInvoicePage extends BasePage { } reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) async { - switch (option) { - case LightningReceivePageOption.lightningInvoice: - break; - case LightningReceivePageOption.lightningOnchain: - Navigator.popAndPushNamed( - context, - Routes.lightningReceiveOnchain, - arguments: [LightningReceivePageOption.lightningOnchain], - ); - break; - default: - break; + if (option == lightning!.getOptionOnchain()) { + Navigator.popAndPushNamed( + context, + Routes.lightningReceiveOnchain, + arguments: [LightningReceivePageOption.lightningOnchain], + ); } }); diff --git a/lib/src/screens/receive/lightning_receive_page.dart b/lib/src/screens/receive/lightning_receive_page.dart index af64753c8..0af4bbca1 100644 --- a/lib/src/screens/receive/lightning_receive_page.dart +++ b/lib/src/screens/receive/lightning_receive_page.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/entities/qr_view_data.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/src/screens/dashboard/widgets/present_receive_option_picker.dart'; import 'package:cake_wallet/src/screens/receive/widgets/qr_image.dart'; @@ -9,9 +10,7 @@ import 'package:cake_wallet/utils/brightness_util.dart'; import 'package:cake_wallet/utils/show_bar.dart'; import 'package:cake_wallet/view_model/dashboard/receive_option_view_model.dart'; import 'package:cake_wallet/view_model/lightning_view_model.dart'; -import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/receive_page_option.dart'; -import 'package:cw_lightning/lightning_receive_page_option.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; @@ -214,9 +213,9 @@ class LightningReceiveOnchainPage extends BasePage { return Expanded( child: Text( S.of(context).lightning_receive_limits( - satsToLightningString(min), - satsToLightningString(max), - satsToLightningString(fee), + lightning!.satsToLightningString(min), + lightning!.satsToLightningString(max), + lightning!.satsToLightningString(fee), ), maxLines: 10, style: TextStyle( @@ -240,18 +239,12 @@ class LightningReceiveOnchainPage extends BasePage { } reaction((_) => receiveOptionViewModel.selectedReceiveOption, (ReceivePageOption option) async { - switch (option) { - case LightningReceivePageOption.lightningInvoice: - Navigator.popAndPushNamed( - context, - Routes.lightningInvoice, - arguments: [LightningReceivePageOption.lightningInvoice], - ); - break; - case LightningReceivePageOption.lightningOnchain: - break; - default: - break; + if (option == lightning!.getOptionInvoice()) { + Navigator.popAndPushNamed( + context, + Routes.lightningInvoice, + arguments: [lightning!.getOptionInvoice()], + ); } }); diff --git a/lib/src/screens/receive/widgets/lightning_input_form.dart b/lib/src/screens/receive/widgets/lightning_input_form.dart index 7efdd8bc5..59727d629 100644 --- a/lib/src/screens/receive/widgets/lightning_input_form.dart +++ b/lib/src/screens/receive/widgets/lightning_input_form.dart @@ -1,13 +1,14 @@ import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; import 'package:cake_wallet/src/screens/receive/widgets/anonpay_currency_input_field.dart'; import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; import 'package:cake_wallet/themes/extensions/exchange_page_theme.dart'; import 'package:cake_wallet/typography.dart'; import 'package:cake_wallet/view_model/lightning_invoice_page_view_model.dart'; -import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; class LightningInvoiceForm extends StatelessWidget { LightningInvoiceForm({ @@ -45,7 +46,10 @@ class LightningInvoiceForm extends StatelessWidget { controller: amountController, focusNode: depositAmountFocus, maxAmount: '', - minAmount: (lightningInvoicePageViewModel.minimum != null) ? satsToLightningString(lightningInvoicePageViewModel.minimum!.round()) : '...', + minAmount: (lightningInvoicePageViewModel.minimum != null) + ? lightning! + .satsToLightningString(lightningInvoicePageViewModel.minimum!.round()) + : '...', selectedCurrency: CryptoCurrency.btcln, ); }), diff --git a/lib/src/screens/send/lightning_send_confirm_page.dart b/lib/src/screens/send/lightning_send_confirm_page.dart index bd3c4b19e..b00ee94f9 100644 --- a/lib/src/screens/send/lightning_send_confirm_page.dart +++ b/lib/src/screens/send/lightning_send_confirm_page.dart @@ -1,5 +1,6 @@ import 'package:breez_sdk/breez_sdk.dart'; import 'package:breez_sdk/bridge_generated.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; @@ -8,7 +9,6 @@ import 'package:cake_wallet/themes/extensions/keyboard_theme.dart'; import 'package:cake_wallet/themes/theme_base.dart'; import 'package:cake_wallet/utils/responsive_layout_util.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; -import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -164,7 +164,7 @@ class LightningSendConfirmPage extends BasePage { .textFieldBorderTopPanelColor, suffixIcon: SizedBox(width: 36), initialValue: - "sats: ${bitcoinAmountToLightningString(amount: (invoice.amountMsat ?? 0) ~/ 1000)}", + "sats: ${lightning!.bitcoinAmountToLightningString(amount: (invoice.amountMsat ?? 0) ~/ 1000)}", placeholderTextStyle: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, diff --git a/lib/view_model/lightning_invoice_page_view_model.dart b/lib/view_model/lightning_invoice_page_view_model.dart index 7cb8cf139..8978310e1 100644 --- a/lib/view_model/lightning_invoice_page_view_model.dart +++ b/lib/view_model/lightning_invoice_page_view_model.dart @@ -2,7 +2,6 @@ import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/entities/fiat_currency.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/view_model/lightning_view_model.dart'; -import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/currency.dart'; import 'package:cw_core/receive_page_option.dart'; diff --git a/tool/configure.dart b/tool/configure.dart index 8395ca4c4..91cdfb94c 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -727,6 +727,8 @@ import 'package:cw_core/transaction_priority.dart'; import 'package:cw_core/output_info.dart'; import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/wallet_service.dart'; +import 'package:cw_core/receive_page_option.dart'; +import 'package:cw_core/crypto_amount_format.dart'; import 'package:cake_wallet/view_model/send/output.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:hive/hive.dart'; @@ -742,7 +744,6 @@ import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart'; import 'package:cw_lightning/lightning_wallet_service.dart'; -import 'package:cw_lightning/lightning_receive_page_option.dart'; """; const lightningCwPart = "part 'cw_lightning.dart';"; const lightningContent = """ @@ -767,6 +768,11 @@ abstract class Lightning { WalletService createLightningWalletService( Box walletInfoSource, Box unspentCoinSource); List getLightningReceivePageOptions(); + String satsToLightningString(int sats); + LightningReceivePageOption getOptionInvoice(); + LightningReceivePageOption getOptionOnchain(); + String bitcoinAmountToLightningString({required int amount}); + int bitcoinAmountToLightningAmount({required int amount}); } """;