Skip to content

Commit

Permalink
fix ElectrumEstimateFee
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Oct 10, 2024
1 parent d42a8be commit 9bfdd2f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ void main() async {
/// get network fee esmtimate (fee per kilobyte)
final networkEstimate = await provider.request(ElectrumEstimateFee());

/// the daemon does not have enough information to make an estimate
if (networkEstimate == null) {
return;
}

/// Convert kilobytes to bytes, multiply by the transaction size, and the result yields the transaction fees.
final fee =
BigInt.from(transactionSize) * (networkEstimate ~/ BigInt.from(1000));
Expand Down
5 changes: 5 additions & 0 deletions example/lib/global/transfer_to_8_account_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ void main() async {
/// get network fee esmtimate (kb/s)
final networkEstimate = await provider.request(ElectrumEstimateFee());

/// the daemon does not have enough information to make an estimate
if (networkEstimate == null) {
return;
}

/// kb to bytes and mul with transaction size and now we have fee
final fee =
BigInt.from(estimateSize) * (networkEstimate ~/ BigInt.from(1000));
Expand Down
8 changes: 5 additions & 3 deletions lib/src/provider/electrum_methods/methods/estimate_fee.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:bitcoin_base/src/utils/btc_utils.dart';

/// Return the estimated transaction fee per kilobyte for a transaction to be confirmed within a certain number of blocks.
/// https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html
class ElectrumEstimateFee extends ElectrumRequest<BigInt, dynamic> {
class ElectrumEstimateFee extends ElectrumRequest<BigInt?, dynamic> {
ElectrumEstimateFee({this.numberOfBlock = 2});

/// The number of blocks to target for confirmation.
Expand All @@ -20,7 +20,9 @@ class ElectrumEstimateFee extends ElectrumRequest<BigInt, dynamic> {

/// The estimated transaction fee in Bigint(satoshi)
@override
BigInt onResonse(result) {
return BtcUtils.toSatoshi(result.toString()).abs();
BigInt? onResonse(result) {
final fee = BtcUtils.toSatoshi(result.toString());
if (fee.isNegative) return null;
return fee;
}
}

0 comments on commit 9bfdd2f

Please sign in to comment.