diff --git a/lib/bloc/account/account_bloc.dart b/lib/bloc/account/account_bloc.dart index 0ddd22ba6..8875b5247 100644 --- a/lib/bloc/account/account_bloc.dart +++ b/lib/bloc/account/account_bloc.dart @@ -219,9 +219,9 @@ class AccountBloc extends Cubit with HydratedMixin { // constraints. void validatePayment( int amount, - bool outgoing, { + bool outgoing, + bool channelCreationPossible, { int? channelMinimumFee, - bool? channelCreationPossible, }) { _log.v("validatePayment: $amount, $outgoing, $channelMinimumFee"); var accState = state; @@ -231,11 +231,9 @@ class AccountBloc extends Cubit with HydratedMixin { } if (!outgoing) { - if (channelCreationPossible != null && !channelCreationPossible && accState.maxInboundLiquidity == 0) { + if (!channelCreationPossible && accState.maxInboundLiquidity == 0) { throw NoChannelCreationZeroLiqudityError(); - } else if (channelCreationPossible != null && - !channelCreationPossible && - accState.maxInboundLiquidity < amount) { + } else if (!channelCreationPossible && accState.maxInboundLiquidity < amount) { throw PaymentExcededLiqudityChannelCreationNotPossibleError(accState.maxInboundLiquidity); } else if (channelMinimumFee != null && (amount > accState.maxInboundLiquidity && amount <= channelMinimumFee)) { diff --git a/lib/routes/create_invoice/create_invoice_page.dart b/lib/routes/create_invoice/create_invoice_page.dart index 28bf19cfa..eed855a8a 100644 --- a/lib/routes/create_invoice/create_invoice_page.dart +++ b/lib/routes/create_invoice/create_invoice_page.dart @@ -55,7 +55,6 @@ class CreateInvoicePageState extends State { final _amountController = TextEditingController(); final _amountFocusNode = FocusNode(); var _doneAction = KeyboardDoneAction(); - bool channelCreationPossible = false; @override void initState() { @@ -66,12 +65,6 @@ class CreateInvoicePageState extends State { _amountController.text = (data.maxWithdrawable ~/ 1000).toString(); _descriptionController.text = data.defaultDescription; } - - final lspState = context.read().state; - if (lspState != null) { - channelCreationPossible = lspState.isChannelOpeningAvailiable; - } - super.initState(); } @@ -131,13 +124,17 @@ class CreateInvoicePageState extends State { builder: (context, accountState, currencyState, lspState) { return ReceivableBTCBox( onTap: () { - if (!channelCreationPossible && accountState.maxInboundLiquidity > 0) { + if (lspState != null && + !lspState.isChannelOpeningAvailiable && + accountState.maxInboundLiquidity > 0) { _amountController.text = currencyState.bitcoinCurrency.format( accountState.maxInboundLiquidity, includeDisplayName: false, userInput: true, ); - } else if (!channelCreationPossible && accountState.maxInboundLiquidity == 0) { + } else if (lspState != null && + !lspState.isChannelOpeningAvailiable && + accountState.maxInboundLiquidity == 0) { // do nothing } else { _amountController.text = currencyState.bitcoinCurrency.format( @@ -259,6 +256,7 @@ class CreateInvoicePageState extends State { return PaymentValidator( validatePayment: _validatePayment, currency: context.read().state.bitcoinCurrency, + channelCreationPossible: context.read().state?.isChannelOpeningAvailiable ?? false, channelMinimumFee: channelMinimumFee, texts: context.texts(), ).validateIncoming(amount); @@ -266,7 +264,8 @@ class CreateInvoicePageState extends State { void _validatePayment( int amount, - bool outgoing, { + bool outgoing, + bool channelCreationPossible, { int? channelMinimumFee, }) { final data = widget.requestData; @@ -281,8 +280,8 @@ class CreateInvoicePageState extends State { return context.read().validatePayment( amount, outgoing, + channelCreationPossible, channelMinimumFee: channelMinimumFee, - channelCreationPossible: channelCreationPossible, ); } } diff --git a/lib/routes/lnurl/payment/lnurl_payment_page.dart b/lib/routes/lnurl/payment/lnurl_payment_page.dart index 31526a1bb..705c0dbb7 100644 --- a/lib/routes/lnurl/payment/lnurl_payment_page.dart +++ b/lib/routes/lnurl/payment/lnurl_payment_page.dart @@ -5,6 +5,7 @@ import 'package:breez_translations/breez_translations_locales.dart'; import 'package:c_breez/bloc/account/account_bloc.dart'; import 'package:c_breez/bloc/currency/currency_bloc.dart'; import 'package:c_breez/bloc/lsp/lsp_bloc.dart'; +import 'package:c_breez/bloc/lsp/lsp_state.dart'; import 'package:c_breez/routes/lnurl/payment/lnurl_payment_info.dart'; import 'package:c_breez/routes/lnurl/widgets/lnurl_metadata.dart'; import 'package:c_breez/theme/theme_provider.dart' as theme; @@ -208,7 +209,7 @@ class LNURLPaymentPageState extends State { String? validatePayment(int amount) { final texts = context.texts(); final accBloc = context.read(); - final lspInfo = context.read().state?.lspInfo; + final lspState = context.read().state; final currencyState = context.read().state; final maxSendable = widget.requestData.maxSendable ~/ 1000; @@ -222,13 +223,14 @@ class LNURLPaymentPageState extends State { } int? channelMinimumFee; - if (lspInfo != null) { - channelMinimumFee = lspInfo.openingFeeParamsList.values.first.minMsat ~/ 1000; + if (lspState != null && lspState.lspInfo != null) { + channelMinimumFee = lspState.lspInfo!.openingFeeParamsList.values.first.minMsat ~/ 1000; } return PaymentValidator( validatePayment: accBloc.validatePayment, currency: currencyState.bitcoinCurrency, + channelCreationPossible: lspState?.isChannelOpeningAvailiable ?? false, channelMinimumFee: channelMinimumFee, texts: context.texts(), ).validateIncoming(amount); diff --git a/lib/routes/spontaneous_payment/spontaneous_payment_page.dart b/lib/routes/spontaneous_payment/spontaneous_payment_page.dart index 06383fe47..5dd52cf39 100644 --- a/lib/routes/spontaneous_payment/spontaneous_payment_page.dart +++ b/lib/routes/spontaneous_payment/spontaneous_payment_page.dart @@ -6,6 +6,7 @@ import 'package:c_breez/bloc/account/account_bloc.dart'; import 'package:c_breez/bloc/account/account_state.dart'; import 'package:c_breez/bloc/currency/currency_bloc.dart'; import 'package:c_breez/bloc/currency/currency_state.dart'; +import 'package:c_breez/bloc/lsp/lsp_bloc.dart'; import 'package:c_breez/theme/theme_provider.dart' as theme; import 'package:c_breez/utils/fiat_conversion.dart'; import 'package:c_breez/utils/min_font_size.dart'; @@ -116,6 +117,8 @@ class SpontaneousPaymentPageState extends State { validatorFn: PaymentValidator( validatePayment: accBloc.validatePayment, currency: currencyState.bitcoinCurrency, + channelCreationPossible: + context.read().state?.isChannelOpeningAvailiable ?? false, texts: context.texts(), ).validateOutgoing, style: theme.FieldTextStyle.textStyle), diff --git a/lib/routes/withdraw_funds/widgets/withdraw_amount_text_form_field.dart b/lib/routes/withdraw_funds/widgets/withdraw_amount_text_form_field.dart index 3a4513953..2c2c3c60e 100644 --- a/lib/routes/withdraw_funds/widgets/withdraw_amount_text_form_field.dart +++ b/lib/routes/withdraw_funds/widgets/withdraw_amount_text_form_field.dart @@ -1,11 +1,13 @@ import 'package:breez_translations/breez_translations_locales.dart'; import 'package:c_breez/bloc/account/payment_error.dart'; +import 'package:c_breez/bloc/lsp/lsp_bloc.dart'; import 'package:c_breez/models/currency.dart'; import 'package:c_breez/routes/withdraw_funds/withdraw_funds_address_page.dart'; import 'package:c_breez/utils/payment_validator.dart'; import 'package:c_breez/widgets/amount_form_field/amount_form_field.dart'; import 'package:fimber/fimber.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; final _log = FimberLog("WithdrawAmountTextFormField"); @@ -28,7 +30,8 @@ class WithdrawAmountTextFormField extends AmountFormField { return PaymentValidator( currency: bitcoinCurrency, texts: context.texts(), - validatePayment: (amount, outgoing, {channelMinimumFee}) { + channelCreationPossible: context.read().state?.isChannelOpeningAvailiable ?? false, + validatePayment: (amount, outgoing, channelCreationPossible, {channelMinimumFee}) { _log.v("Validating $amount $policy"); if (amount < policy.minValue) { throw PaymentBelowLimitError(policy.minValue); diff --git a/lib/utils/payment_validator.dart b/lib/utils/payment_validator.dart index 9e6253145..0741e7a1f 100644 --- a/lib/utils/payment_validator.dart +++ b/lib/utils/payment_validator.dart @@ -10,15 +10,18 @@ class PaymentValidator { final BitcoinCurrency currency; final void Function( int amount, - bool outgoing, { + bool outgoing, + bool channelCreationPossible, { int? channelMinimumFee, }) validatePayment; + final bool channelCreationPossible; final int? channelMinimumFee; final BreezTranslations texts; const PaymentValidator({ required this.validatePayment, required this.currency, + required this.channelCreationPossible, this.channelMinimumFee, required this.texts, }); @@ -34,7 +37,7 @@ class PaymentValidator { String? _validate(int amount, bool outgoing) { _log.v("Validating for $amount and $outgoing"); try { - validatePayment(amount, outgoing, channelMinimumFee: channelMinimumFee); + validatePayment(amount, outgoing, channelCreationPossible, channelMinimumFee: channelMinimumFee); } on PaymentExceededLimitError catch (e) { _log.v("Got PaymentExceededLimitError", ex: e); return texts.invoice_payment_validator_error_payment_exceeded_limit( diff --git a/lib/widgets/payment_dialogs/payment_request_info_dialog.dart b/lib/widgets/payment_dialogs/payment_request_info_dialog.dart index 691cb8f66..420b6b840 100644 --- a/lib/widgets/payment_dialogs/payment_request_info_dialog.dart +++ b/lib/widgets/payment_dialogs/payment_request_info_dialog.dart @@ -4,6 +4,7 @@ import 'package:c_breez/bloc/account/account_bloc.dart'; import 'package:c_breez/bloc/account/account_state.dart'; import 'package:c_breez/bloc/currency/currency_bloc.dart'; import 'package:c_breez/bloc/currency/currency_state.dart'; +import 'package:c_breez/bloc/lsp/lsp_bloc.dart'; import 'package:c_breez/models/currency.dart'; import 'package:c_breez/models/invoice.dart'; import 'package:c_breez/theme/theme_provider.dart' as theme; @@ -179,6 +180,7 @@ class PaymentRequestInfoDialogState extends State { controller: _invoiceAmountController, validatorFn: PaymentValidator( validatePayment: context.read().validatePayment, + channelCreationPossible: context.read().state?.isChannelOpeningAvailiable ?? false, currency: currencyState.bitcoinCurrency, texts: context.texts(), ).validateOutgoing, @@ -254,6 +256,7 @@ class PaymentRequestInfoDialogState extends State { final validationError = PaymentValidator( validatePayment: context.read().validatePayment, currency: currencyState.bitcoinCurrency, + channelCreationPossible: context.read().state?.isChannelOpeningAvailiable ?? false, texts: context.texts(), ).validateOutgoing( amountToPay(currencyState),