Skip to content

Commit

Permalink
Plug prepare sweep method
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar111190 committed Oct 6, 2023
1 parent 8180e13 commit cdba2be
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
26 changes: 15 additions & 11 deletions lib/bloc/fee_options/fee_options_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ class FeeOptionsBloc extends Cubit<FeeOptionsState> {
}

/// Fetches the current recommended fees
Future<List<FeeOption>> fetchFeeOptions() async {
Future<List<FeeOption>> fetchFeeOptions(String address) async {
RecommendedFees recommendedFees;
try {
recommendedFees = await _breezLib.recommendedFees();
_log.v(
"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(FeeOptionsState(error: extractExceptionMessage(e, getSystemAppLocalizations())));
Expand All @@ -61,34 +60,39 @@ class FeeOptionsBloc extends Cubit<FeeOptionsState> {
return utxos;
}

List<FeeOption> _constructFeeOptionList(int utxos, RecommendedFees recommendedFees) {
Future<List<FeeOption>> _constructFeeOptionList(
String address,
RecommendedFees recommendedFees,
) async {
final List<FeeOption> 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,
),
];
emit(state.copyWith(feeOptions: feeOptions));
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<int> _calculateTransactionFee(String address, int satsPerVbyte) async {
final response = await _breezLib.prepareSweep(
address: address,
satsPerVbyte: satsPerVbyte,
);
return response.sweepTxFeeSat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class _ReverseSwapConfirmationPageState extends State<ReverseSwapConfirmationPag
@override
void initState() {
super.initState();
_fetchFeeOptionsFuture = context.read<FeeOptionsBloc>().fetchFeeOptions();
_fetchFeeOptionsFuture = context.read<FeeOptionsBloc>().fetchFeeOptions(
widget.onchainRecipientAddress,
);
_fetchFeeOptionsFuture.then((feeOptions) {
setState(() {
affordableFees = feeOptions.where((f) => f.isAffordable(widget.amountSat)).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _SweepConfirmationPageState extends State<SweepConfirmationPage> {
@override
void initState() {
super.initState();
_fetchFeeOptionsFuture = context.read<FeeOptionsBloc>().fetchFeeOptions();
_fetchFeeOptionsFuture = context.read<FeeOptionsBloc>().fetchFeeOptions(widget.toAddress);
_fetchFeeOptionsFuture.then((feeOptions) {
setState(() {
affordableFees = feeOptions.where((f) => f.isAffordable(widget.amountSat)).toList();
Expand All @@ -56,7 +56,11 @@ class _SweepConfirmationPageState extends State<SweepConfirmationPage> {
);
}
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: Loader());
return const Center(
child: Loader(
color: Colors.white,
),
);
}

if (affordableFees.isNotEmpty) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ packages:
path: "../breez-sdk/libs/sdk-flutter"
relative: true
source: path
version: "0.2.3"
version: "0.2.5"
breez_translations:
dependency: "direct main"
description:
Expand Down

0 comments on commit cdba2be

Please sign in to comment.