From 8082b2e389264cd163f137266c521d226fd9fd7a 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 sweep method --- lib/bloc/fee_options/fee_options_bloc.dart | 26 +++++++++++-------- .../reverse_swap_confirmation_page.dart | 4 ++- .../sweep_confirmation_page.dart | 8 ++++-- pubspec.lock | 8 ------ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/bloc/fee_options/fee_options_bloc.dart b/lib/bloc/fee_options/fee_options_bloc.dart index 41835c861..c283744e4 100644 --- a/lib/bloc/fee_options/fee_options_bloc.dart +++ b/lib/bloc/fee_options/fee_options_bloc.dart @@ -33,7 +33,7 @@ class FeeOptionsBloc extends Cubit { } /// Fetches the current recommended fees - Future> fetchFeeOptions() async { + Future> fetchFeeOptions(String address) async { RecommendedFees recommendedFees; try { recommendedFees = await _breezLib.recommendedFees(); @@ -41,8 +41,7 @@ class FeeOptionsBloc 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.severe("fetchFeeOptions error", e); emit(FeeOptionsState(error: extractExceptionMessage(e, getSystemAppLocalizations()))); @@ -61,24 +60,27 @@ class FeeOptionsBloc 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, ), ]; @@ -86,9 +88,11 @@ class FeeOptionsBloc 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 satsPerVbyte) async { + final response = await _breezLib.prepareSweep( + address: address, + satsPerVbyte: satsPerVbyte, + ); + return response.sweepTxFeeSat; } } diff --git a/lib/routes/withdraw/reverse_swap/confirmation_page/reverse_swap_confirmation_page.dart b/lib/routes/withdraw/reverse_swap/confirmation_page/reverse_swap_confirmation_page.dart index 2e712888a..8daafd797 100644 --- a/lib/routes/withdraw/reverse_swap/confirmation_page/reverse_swap_confirmation_page.dart +++ b/lib/routes/withdraw/reverse_swap/confirmation_page/reverse_swap_confirmation_page.dart @@ -34,7 +34,9 @@ class _ReverseSwapConfirmationPageState extends State().fetchFeeOptions(); + _fetchFeeOptionsFuture = context.read().fetchFeeOptions( + widget.onchainRecipientAddress, + ); _fetchFeeOptionsFuture.then((feeOptions) { setState(() { affordableFees = feeOptions.where((f) => f.isAffordable(widget.amountSat)).toList(); diff --git a/lib/routes/withdraw/sweep/confirmation_page/sweep_confirmation_page.dart b/lib/routes/withdraw/sweep/confirmation_page/sweep_confirmation_page.dart index 2466d0f27..5d5dee6c6 100644 --- a/lib/routes/withdraw/sweep/confirmation_page/sweep_confirmation_page.dart +++ b/lib/routes/withdraw/sweep/confirmation_page/sweep_confirmation_page.dart @@ -30,7 +30,7 @@ class _SweepConfirmationPageState extends State { @override void initState() { super.initState(); - _fetchFeeOptionsFuture = context.read().fetchFeeOptions(); + _fetchFeeOptionsFuture = context.read().fetchFeeOptions(widget.toAddress); _fetchFeeOptionsFuture.then((feeOptions) { setState(() { affordableFees = feeOptions.where((f) => f.isAffordable(widget.amountSat)).toList(); @@ -56,7 +56,11 @@ class _SweepConfirmationPageState extends State { ); } if (snapshot.connectionState != ConnectionState.done) { - return const Center(child: Loader()); + return const Center( + child: Loader( + color: Colors.white, + ), + ); } if (affordableFees.isNotEmpty) { diff --git a/pubspec.lock b/pubspec.lock index dd389e7b5..dfc303176 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -401,14 +401,6 @@ packages: url: "https://pub.dev" source: hosted version: "0.9.3+1" - fimber: - dependency: transitive - description: - name: fimber - sha256: "42fcfa33acd43556c1e7ebfc12c2b03893418bc04a07931368c3573e228af2f0" - url: "https://pub.dev" - source: hosted - version: "0.7.0" firebase_core: dependency: "direct main" description: