subcoin_rpc_bitcoind/mining.rs
1//! Bitcoin Core compatible mining/fee estimation RPC methods.
2//!
3//! Implements: estimatesmartfee
4
5use crate::error::Error;
6use crate::types::EstimateSmartFee;
7use jsonrpsee::proc_macros::rpc;
8
9/// Bitcoin Core compatible mining RPC API.
10#[rpc(client, server)]
11pub trait MiningApi {
12 /// Estimates the approximate fee per kilobyte needed for a transaction
13 /// to begin confirmation within conf_target blocks.
14 ///
15 /// Returns estimate fee rate in BTC/kvB.
16 #[method(name = "estimatesmartfee", blocking)]
17 fn estimate_smart_fee(
18 &self,
19 conf_target: u32,
20 estimate_mode: Option<String>,
21 ) -> Result<EstimateSmartFee, Error>;
22}
23
24/// Bitcoin Core compatible mining RPC implementation.
25pub struct MiningRpc {
26 // In the future, this could hold a reference to mempool for fee estimation
27}
28
29impl MiningRpc {
30 /// Creates a new instance of [`MiningRpc`].
31 pub fn new() -> Self {
32 Self {}
33 }
34}
35
36impl Default for MiningRpc {
37 fn default() -> Self {
38 Self::new()
39 }
40}
41
42#[async_trait::async_trait]
43impl MiningApiServer for MiningRpc {
44 fn estimate_smart_fee(
45 &self,
46 conf_target: u32,
47 _estimate_mode: Option<String>,
48 ) -> Result<EstimateSmartFee, Error> {
49 // For now, return a simple fallback fee rate.
50 // TODO: Implement proper fee estimation based on mempool analysis.
51 //
52 // electrs handles -32603 error gracefully, so we can return an error
53 // or a simple estimate.
54
55 // Return a conservative estimate of 1 sat/vB = 0.00001 BTC/kvB
56 // This is a minimum fee rate that should work for most cases.
57 let feerate = Some(0.00001);
58
59 Ok(EstimateSmartFee {
60 feerate,
61 errors: None,
62 blocks: conf_target,
63 })
64 }
65}