From 9bfdd2fba2a4cd4d87ea53069ad35a13f239efd0 Mon Sep 17 00:00:00 2001 From: Mohsen <56779182+mrtnetwork@users.noreply.github.com> Date: Thu, 10 Oct 2024 09:18:49 +0330 Subject: [PATCH] fix ElectrumEstimateFee --- .../transfer_from_7_account_to_6_accout_example.dart | 5 +++++ example/lib/global/transfer_to_8_account_example.dart | 5 +++++ .../provider/electrum_methods/methods/estimate_fee.dart | 8 +++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/example/lib/global/transfer_from_7_account_to_6_accout_example.dart b/example/lib/global/transfer_from_7_account_to_6_accout_example.dart index a441543..295d906 100644 --- a/example/lib/global/transfer_from_7_account_to_6_accout_example.dart +++ b/example/lib/global/transfer_from_7_account_to_6_accout_example.dart @@ -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)); diff --git a/example/lib/global/transfer_to_8_account_example.dart b/example/lib/global/transfer_to_8_account_example.dart index 90f21f7..6830feb 100644 --- a/example/lib/global/transfer_to_8_account_example.dart +++ b/example/lib/global/transfer_to_8_account_example.dart @@ -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)); diff --git a/lib/src/provider/electrum_methods/methods/estimate_fee.dart b/lib/src/provider/electrum_methods/methods/estimate_fee.dart index 9e3b2f5..859dfd8 100644 --- a/lib/src/provider/electrum_methods/methods/estimate_fee.dart +++ b/lib/src/provider/electrum_methods/methods/estimate_fee.dart @@ -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 { +class ElectrumEstimateFee extends ElectrumRequest { ElectrumEstimateFee({this.numberOfBlock = 2}); /// The number of blocks to target for confirmation. @@ -20,7 +20,9 @@ class ElectrumEstimateFee extends ElectrumRequest { /// 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; } }