Skip to content

Commit

Permalink
Compare payment amount against maxPayableSat from onchainPaymentLimits
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jul 16, 2024
1 parent 71cda9e commit 63ca0c7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
7 changes: 4 additions & 3 deletions lib/bloc/reverse_swap/reverse_swap_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ class ReverseSwapBloc extends Cubit<ReverseSwapState> {
try {
OnchainPaymentLimitsResponse paymentLimits = await _breezSDK.onchainPaymentLimits();
_log.info(
"Current maximum ${paymentLimits.maxSat} and "
"minimum ${paymentLimits.minSat} payment limits for onchain payments",
"Current minimum ${paymentLimits.minSat} and "
"maximum ${paymentLimits.maxSat} payment limits, in sats, for onchain payments."
"\nMaximum amount this node can send with the current channels and the current local balance: ${paymentLimits.maxPayableSat} (sats).",
);
return paymentLimits;
} catch (e) {
_log.severe("fetchOnchainPaymentLimits error", e);
_log.severe("onchainPaymentLimits error", e);
rethrow;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class _RedeemFundsPageState extends State<RedeemFundsPage> {
widget.walletBalanceSat,
widget.walletBalanceSat,
),
balanceSat: widget.walletBalanceSat,
maxPayableSat: widget.walletBalanceSat,
);
},
),
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/withdraw/reverse_swap/reverse_swap_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class _ReverseSwapFormState extends State<ReverseSwapForm> {
bitcoinCurrency: widget.bitcoinCurrency,
controller: widget.amountController,
withdrawMaxValue: widget.withdrawMaxValue,
balanceSat: widget.paymentLimits.maxSat,
maxPayableSat: widget.paymentLimits.maxPayableSat,
policy: WithdrawFundsPolicy(
WithdrawKind.withdraw_funds,
widget.paymentLimits.minSat,
Expand All @@ -105,7 +105,7 @@ class _ReverseSwapFormState extends State<ReverseSwapForm> {
setState(() {
widget.onChanged(value);
if (widget.withdrawMaxValue) {
_setAmount(widget.paymentLimits.maxSat);
_setAmount(widget.paymentLimits.maxPayableSat);
} else {
widget.amountController.text = "";
}
Expand Down
15 changes: 8 additions & 7 deletions lib/routes/withdraw/reverse_swap/reverse_swap_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ final _log = Logger("ReverseSwapPage");

class ReverseSwapPage extends StatefulWidget {
final BitcoinAddressData? btcAddressData;

const ReverseSwapPage({super.key, required this.btcAddressData});

@override
State<ReverseSwapPage> createState() => _ReverseSwapPageState();
}

class _ReverseSwapPageState extends State<ReverseSwapPage> {
Future<OnchainPaymentLimitsResponse>? _revSwapOptionsFuture;
Future<OnchainPaymentLimitsResponse>? _onchainPaymentLimitsFuture;

@override
void initState() {
super.initState();
_fetchReverseSwapPairInfo();
_fetchOnchainPaymentLimits();
}

void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_fetchReverseSwapPairInfo();
_fetchOnchainPaymentLimits();
}
}

Future _fetchReverseSwapPairInfo() async {
_log.info("Fetching reverse swap pair info");
Future _fetchOnchainPaymentLimits() async {
_log.info("Fetching onchain payment limits");
final revSwapBloc = context.read<ReverseSwapBloc>();
setState(() {
_revSwapOptionsFuture = revSwapBloc.onchainPaymentLimits();
_onchainPaymentLimitsFuture = revSwapBloc.onchainPaymentLimits();
});
}

Expand All @@ -54,7 +55,7 @@ class _ReverseSwapPageState extends State<ReverseSwapPage> {
title: Text(texts.reverse_swap_title),
),
body: FutureBuilder<OnchainPaymentLimitsResponse>(
future: _revSwapOptionsFuture,
future: _onchainPaymentLimitsFuture,
builder: (BuildContext context, AsyncSnapshot<OnchainPaymentLimitsResponse> snapshot) {
if (snapshot.hasError) {
return Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WithdrawFundsAmountTextFormField extends AmountFormField {
required TextEditingController super.controller,
required bool withdrawMaxValue,
required WithdrawFundsPolicy policy,
required int balanceSat,
required int maxPayableSat,
}) : super(
texts: context.texts(),
readOnly: policy.withdrawKind == WithdrawKind.unexpected_funds || withdrawMaxValue,
Expand All @@ -36,7 +36,7 @@ class WithdrawFundsAmountTextFormField extends AmountFormField {
if (amountSat > policy.maxValue) {
throw PaymentExceededLimitError(policy.maxValue);
}
if (amountSat > balanceSat) {
if (amountSat > maxPayableSat) {
throw const InsufficientLocalBalanceError();
}
},
Expand Down

0 comments on commit 63ca0c7

Please sign in to comment.