4 use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
5 use lightning::util::logger::{Logger, Record};
6 use lightning_block_sync::http::HttpEndpoint;
7 use lightning_block_sync::rpc::RpcClient;
11 pub struct BitcoindFeeEstimator {
12 bitcoind_rpc_client: Mutex<RpcClient>,
15 impl BitcoindFeeEstimator {
16 fn new(host: String, port: u16, path: Option<String>, rpc_user: String, rpc_password: String) ->
19 let mut http_endpoint = HttpEndpoint::for_host(host).with_port(port);
20 if let Some(p) = path {
21 http_endpoint = http_endpoint.with_path(p);
23 let rpc_credentials = base64::encode(format!("{}:{}", rpc_user, rpc_password));
24 let bitcoind_rpc_client = RpcClient::new(&rpc_credentials, http_endpoint)?;
26 bitcoind_rpc_client: Mutex::new(bitcoind_rpc_client)
31 impl FeeEstimator for BitcoindFeeEstimator {
32 fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
33 let mut rpc_client_guard = self.bitcoind_rpc_client.lock().unwrap();
34 match confirmation_target {
35 ConfirmationTarget::Background => {
36 let conf_target = serde_json::json!(144);
37 let estimate_mode = serde_json::json!("ECONOMICAL");
38 let resp = rpc_client_guard.call_method("estimatesmartfee",
39 &vec![conf_target, estimate_mode]).unwrap();
40 resp["feerate"].as_u64().unwrap() as u32
42 ConfirmationTarget::Normal => {
43 let conf_target = serde_json::json!(18);
44 let estimate_mode = serde_json::json!("ECONOMICAL");
45 let resp = rpc_client_guard.call_method("estimatesmartfee",
46 &vec![conf_target, estimate_mode]).unwrap();
47 resp["feerate"].as_u64().unwrap() as u32
49 ConfirmationTarget::HighPriority => {
50 let conf_target = serde_json::json!(6);
51 let estimate_mode = serde_json::json!("CONSERVATIVE");
52 let resp = rpc_client_guard.call_method("estimatesmartfee",
53 &vec![conf_target, estimate_mode]).unwrap();
54 resp["feerate"].as_u64().unwrap() as u32
61 let bitcoind_host = "127.0.0.1".to_string();
62 let bitcoind_port = 18443;
63 let rpc_user = "polaruser".to_string();
64 let rpc_password = "polarpass".to_string();
65 let fee_estimator = BitcoindFeeEstimator::new(bitcoind_host, bitcoind_port, None, rpc_user, rpc_password).unwrap();
66 let normal_fee = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
67 println!("VMW: {}", normal_fee);