[wallet] Add a type convert fee units, add Wallet::estimate_fee()
This commit is contained in:
@@ -11,6 +11,7 @@ use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
||||
use super::*;
|
||||
use crate::database::{BatchDatabase, DatabaseUtils};
|
||||
use crate::error::Error;
|
||||
use crate::FeeRate;
|
||||
|
||||
pub struct ElectrumBlockchain(Option<Client>);
|
||||
|
||||
@@ -77,6 +78,15 @@ impl OnlineBlockchain for ElectrumBlockchain {
|
||||
.block_headers_subscribe()
|
||||
.map(|data| data.height)?)
|
||||
}
|
||||
|
||||
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
||||
Ok(FeeRate::from_btc_per_kvb(
|
||||
self.0
|
||||
.as_ref()
|
||||
.ok_or(Error::OfflineClient)?
|
||||
.estimate_fee(target)? as f32,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl ElectrumLikeSync for Client {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||
|
||||
@@ -18,6 +18,7 @@ use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
||||
use super::*;
|
||||
use crate::database::{BatchDatabase, DatabaseUtils};
|
||||
use crate::error::Error;
|
||||
use crate::FeeRate;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UrlClient {
|
||||
@@ -99,6 +100,27 @@ impl OnlineBlockchain for EsploraBlockchain {
|
||||
.ok_or(Error::OfflineClient)?
|
||||
._get_height())?)
|
||||
}
|
||||
|
||||
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
||||
let estimates = await_or_block!(self
|
||||
.0
|
||||
.as_ref()
|
||||
.ok_or(Error::OfflineClient)?
|
||||
._get_fee_estimates())?;
|
||||
|
||||
let fee_val = estimates
|
||||
.into_iter()
|
||||
.map(|(k, v)| Ok::<_, std::num::ParseIntError>((k.parse::<usize>()?, v)))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| Error::Generic(e.to_string()))?
|
||||
.into_iter()
|
||||
.take_while(|(k, _)| k <= &target)
|
||||
.map(|(_, v)| v)
|
||||
.last()
|
||||
.unwrap_or(1.0);
|
||||
|
||||
Ok(FeeRate::from_sat_per_vb(fee_val as f32))
|
||||
}
|
||||
}
|
||||
|
||||
impl UrlClient {
|
||||
@@ -232,6 +254,17 @@ impl UrlClient {
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn _get_fee_estimates(&self) -> Result<HashMap<String, f64>, EsploraError> {
|
||||
Ok(self
|
||||
.client
|
||||
.get(&format!("{}/api/fee-estimates", self.url,))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<HashMap<String, f64>>()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
|
||||
@@ -5,6 +5,7 @@ use bitcoin::{Transaction, Txid};
|
||||
|
||||
use crate::database::{BatchDatabase, DatabaseUtils};
|
||||
use crate::error::Error;
|
||||
use crate::FeeRate;
|
||||
|
||||
pub mod utils;
|
||||
|
||||
@@ -64,6 +65,7 @@ pub trait OnlineBlockchain: Blockchain {
|
||||
fn broadcast(&self, tx: &Transaction) -> Result<(), Error>;
|
||||
|
||||
fn get_height(&self) -> Result<usize, Error>;
|
||||
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error>;
|
||||
}
|
||||
|
||||
pub type ProgressData = (f32, Option<String>);
|
||||
|
||||
Reference in New Issue
Block a user