From b0f2235c98529f579fdae122b4ffe4c5129900e8 Mon Sep 17 00:00:00 2001 From: Ademar Alves de Oliveira Date: Wed, 23 Aug 2023 10:37:47 -0300 Subject: [PATCH] Plug prepare withdraw method --- lib/bloc/withdraw/withdraw_funds_bloc.dart | 34 ++++++++++++------- .../withdraw_funds_confirmation_page.dart | 8 +++-- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/bloc/withdraw/withdraw_funds_bloc.dart b/lib/bloc/withdraw/withdraw_funds_bloc.dart index 66bf1e2ff..1f3f91fd3 100644 --- a/lib/bloc/withdraw/withdraw_funds_bloc.dart +++ b/lib/bloc/withdraw/withdraw_funds_bloc.dart @@ -23,12 +23,16 @@ class WithdrawFundsBloc extends Cubit { }) async { _log.v("Sweep to address $toAddress using $feeRateSatsPerVbyte fee vByte"); await _breezLib.sweep( - toAddress: toAddress, - feeRateSatsPerVbyte: feeRateSatsPerVbyte, + req: SweepRequest( + toAddress: toAddress, + fee: SweepFee_Perkw( + satPerKw: feeRateSatsPerVbyte, + ), + ), ); } - Future> fetchFeeOptions() async { + Future> fetchFeeOptions(String address) async { RecommendedFees recommendedFees; try { recommendedFees = await _breezLib.recommendedFees(); @@ -36,8 +40,7 @@ class WithdrawFundsBloc extends Cubit { "fetchFeeOptions recommendedFees:\nfastestFee: ${recommendedFees.fastestFee}," "\nhalfHourFee: ${recommendedFees.halfHourFee},\nhourFee: ${recommendedFees.hourFee}.", ); - final utxos = await _retrieveUTXOS(); - return _constructFeeOptionList(utxos, recommendedFees); + return await _constructFeeOptionList(address, recommendedFees); } catch (e) { _log.e("fetchFeeOptions error", ex: e); emit(WithdrawFundsState(errorMessage: extractExceptionMessage(e, getSystemAppLocalizations()))); @@ -56,24 +59,27 @@ class WithdrawFundsBloc extends Cubit { return utxos; } - List _constructFeeOptionList(int utxos, RecommendedFees recommendedFees) { + Future> _constructFeeOptionList( + String address, + RecommendedFees recommendedFees, + ) async { final List feeOptions = [ FeeOption( processingSpeed: ProcessingSpeed.economy, waitingTime: const Duration(minutes: 60), - fee: _calculateTransactionFee(utxos, recommendedFees.hourFee), + fee: await _calculateTransactionFee(address, recommendedFees.hourFee), feeVByte: recommendedFees.hourFee, ), FeeOption( processingSpeed: ProcessingSpeed.regular, waitingTime: const Duration(minutes: 30), - fee: _calculateTransactionFee(utxos, recommendedFees.halfHourFee), + fee: await _calculateTransactionFee(address, recommendedFees.halfHourFee), feeVByte: recommendedFees.halfHourFee, ), FeeOption( processingSpeed: ProcessingSpeed.priority, waitingTime: const Duration(minutes: 10), - fee: _calculateTransactionFee(utxos, recommendedFees.fastestFee), + fee: await _calculateTransactionFee(address, recommendedFees.fastestFee), feeVByte: recommendedFees.fastestFee, ), ]; @@ -81,9 +87,11 @@ class WithdrawFundsBloc extends Cubit { return feeOptions; } - int _calculateTransactionFee(int inputs, int feeRateSatsPerVbyte) { - // based on https://bitcoin.stackexchange.com/a/3011 - final transactionSize = (inputs * 148) + (2 * 34) + 10; - return transactionSize * feeRateSatsPerVbyte; + Future _calculateTransactionFee(String address, int feeRateSatsPerVbyte) async { + final response = await _breezLib.prepareWithdraw( + address: address, + feeRateSatsPerVbyte: feeRateSatsPerVbyte, + ); + return response.feeSat; } } diff --git a/lib/routes/withdraw_funds/confirmation_page/withdraw_funds_confirmation_page.dart b/lib/routes/withdraw_funds/confirmation_page/withdraw_funds_confirmation_page.dart index 90625301d..51d932abd 100644 --- a/lib/routes/withdraw_funds/confirmation_page/withdraw_funds_confirmation_page.dart +++ b/lib/routes/withdraw_funds/confirmation_page/withdraw_funds_confirmation_page.dart @@ -31,7 +31,7 @@ class _WithdrawFundsConfirmationPageState extends State().fetchFeeOptions(); + _fetchFeeOptionsFuture = context.read().fetchFeeOptions(widget.toAddress); _fetchFeeOptionsFuture.then((feeOptions) { setState(() { affordableFees = feeOptions.where((f) => f.isAffordable(widget.amount)).toList(); @@ -57,7 +57,11 @@ class _WithdrawFundsConfirmationPageState extends State