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 Sep 18, 2023
1 parent ae9b50e commit b21d7d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
32 changes: 19 additions & 13 deletions lib/bloc/withdraw/withdraw_funds_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ class WithdrawFundsBloc extends Cubit<WithdrawFundsState> {
}) async {
_log.v("Sweep to address $toAddress using $feeRateSatsPerVbyte fee vByte");
await _breezLib.sweep(
toAddress: toAddress,
feeRateSatsPerVbyte: feeRateSatsPerVbyte,
req: SweepRequest(
toAddress: toAddress,
satsPerVbyte: feeRateSatsPerVbyte,
),
);
}

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(WithdrawFundsState(errorMessage: extractExceptionMessage(e, getSystemAppLocalizations())));
Expand All @@ -56,34 +57,39 @@ class WithdrawFundsBloc extends Cubit<WithdrawFundsState> {
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(WithdrawFundsState(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.feeSat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _WithdrawFundsConfirmationPageState extends State<WithdrawFundsConfirmatio
@override
void initState() {
super.initState();
_fetchFeeOptionsFuture = context.read<WithdrawFundsBloc>().fetchFeeOptions();
_fetchFeeOptionsFuture = context.read<WithdrawFundsBloc>().fetchFeeOptions(widget.toAddress);
_fetchFeeOptionsFuture.then((feeOptions) {
setState(() {
affordableFees = feeOptions.where((f) => f.isAffordable(widget.amount)).toList();
Expand All @@ -57,7 +57,11 @@ class _WithdrawFundsConfirmationPageState extends State<WithdrawFundsConfirmatio
);
}
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: Loader());
return const Center(
child: Loader(
color: Colors.white,
),
);
}

if (affordableFees.isNotEmpty) {
Expand Down

0 comments on commit b21d7d0

Please sign in to comment.