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 9, 2023
1 parent 213ed2c commit 8082b2e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 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.fine(
"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())));
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
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8082b2e

Please sign in to comment.