Skip to content

Commit

Permalink
require bool channelCreationPossible on validatePayment
Browse files Browse the repository at this point in the history
  • Loading branch information
ubbabeck committed Aug 21, 2023
1 parent 9447116 commit 0dfa941
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
10 changes: 4 additions & 6 deletions lib/bloc/account/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ class AccountBloc extends Cubit<AccountState> 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;
Expand All @@ -231,11 +231,9 @@ class AccountBloc extends Cubit<AccountState> 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)) {
Expand Down
21 changes: 10 additions & 11 deletions lib/routes/create_invoice/create_invoice_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class CreateInvoicePageState extends State<CreateInvoicePage> {
final _amountController = TextEditingController();
final _amountFocusNode = FocusNode();
var _doneAction = KeyboardDoneAction();
bool channelCreationPossible = false;

@override
void initState() {
Expand All @@ -66,12 +65,6 @@ class CreateInvoicePageState extends State<CreateInvoicePage> {
_amountController.text = (data.maxWithdrawable ~/ 1000).toString();
_descriptionController.text = data.defaultDescription;
}

final lspState = context.read<LSPBloc>().state;
if (lspState != null) {
channelCreationPossible = lspState.isChannelOpeningAvailiable;
}

super.initState();
}

Expand Down Expand Up @@ -131,13 +124,17 @@ class CreateInvoicePageState extends State<CreateInvoicePage> {
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(
Expand Down Expand Up @@ -259,14 +256,16 @@ class CreateInvoicePageState extends State<CreateInvoicePage> {
return PaymentValidator(
validatePayment: _validatePayment,
currency: context.read<CurrencyBloc>().state.bitcoinCurrency,
channelCreationPossible: context.read<LSPBloc>().state?.isChannelOpeningAvailiable ?? false,
channelMinimumFee: channelMinimumFee,
texts: context.texts(),
).validateIncoming(amount);
}

void _validatePayment(
int amount,
bool outgoing, {
bool outgoing,
bool channelCreationPossible, {
int? channelMinimumFee,
}) {
final data = widget.requestData;
Expand All @@ -281,8 +280,8 @@ class CreateInvoicePageState extends State<CreateInvoicePage> {
return context.read<AccountBloc>().validatePayment(
amount,
outgoing,
channelCreationPossible,
channelMinimumFee: channelMinimumFee,
channelCreationPossible: channelCreationPossible,
);
}
}
8 changes: 5 additions & 3 deletions lib/routes/lnurl/payment/lnurl_payment_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,7 +209,7 @@ class LNURLPaymentPageState extends State<LNURLPaymentPage> {
String? validatePayment(int amount) {
final texts = context.texts();
final accBloc = context.read<AccountBloc>();
final lspInfo = context.read<LSPBloc>().state?.lspInfo;
final lspState = context.read<LSPBloc>().state;
final currencyState = context.read<CurrencyBloc>().state;

final maxSendable = widget.requestData.maxSendable ~/ 1000;
Expand All @@ -222,13 +223,14 @@ class LNURLPaymentPageState extends State<LNURLPaymentPage> {
}

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);
Expand Down
3 changes: 3 additions & 0 deletions lib/routes/spontaneous_payment/spontaneous_payment_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -116,6 +117,8 @@ class SpontaneousPaymentPageState extends State<SpontaneousPaymentPage> {
validatorFn: PaymentValidator(
validatePayment: accBloc.validatePayment,
currency: currencyState.bitcoinCurrency,
channelCreationPossible:
context.read<LSPBloc>().state?.isChannelOpeningAvailiable ?? false,
texts: context.texts(),
).validateOutgoing,
style: theme.FieldTextStyle.textStyle),
Expand Down
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -28,7 +30,8 @@ class WithdrawAmountTextFormField extends AmountFormField {
return PaymentValidator(
currency: bitcoinCurrency,
texts: context.texts(),
validatePayment: (amount, outgoing, {channelMinimumFee}) {
channelCreationPossible: context.read<LSPBloc>().state?.isChannelOpeningAvailiable ?? false,
validatePayment: (amount, outgoing, channelCreationPossible, {channelMinimumFee}) {
_log.v("Validating $amount $policy");
if (amount < policy.minValue) {
throw PaymentBelowLimitError(policy.minValue);
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/payment_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/payment_dialogs/payment_request_info_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,6 +180,7 @@ class PaymentRequestInfoDialogState extends State<PaymentRequestInfoDialog> {
controller: _invoiceAmountController,
validatorFn: PaymentValidator(
validatePayment: context.read<AccountBloc>().validatePayment,
channelCreationPossible: context.read<LSPBloc>().state?.isChannelOpeningAvailiable ?? false,
currency: currencyState.bitcoinCurrency,
texts: context.texts(),
).validateOutgoing,
Expand Down Expand Up @@ -254,6 +256,7 @@ class PaymentRequestInfoDialogState extends State<PaymentRequestInfoDialog> {
final validationError = PaymentValidator(
validatePayment: context.read<AccountBloc>().validatePayment,
currency: currencyState.bitcoinCurrency,
channelCreationPossible: context.read<LSPBloc>().state?.isChannelOpeningAvailiable ?? false,
texts: context.texts(),
).validateOutgoing(
amountToPay(currencyState),
Expand Down

0 comments on commit 0dfa941

Please sign in to comment.