-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace fee rate estimator with mempool.space client
WIP: the blocking version currently does not work. Looks like some runtime issue. Signed-off-by: Philipp Hoenisch <[email protected]>
- Loading branch information
Showing
6 changed files
with
196 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "mempool" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
reqwest = { version = "0.11", features = ["json", "blocking"] } | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = { version = "1" } | ||
|
||
[dev-dependencies] | ||
tokio = { version = "1", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use anyhow::Result; | ||
use serde::Deserialize; | ||
|
||
const MEMPOOL_FEE_RATE_URL_MAINNET: &str = "https://mempool.space"; | ||
const MEMPOOL_FEE_RATE_URL_SIGNET: &str = "https://mempool.space/signet"; | ||
const MEMPOOL_FEE_RATE_URL_TESTNET: &str = "https://mempool.space/testnet"; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct FeeRate { | ||
pub fastest_fee: usize, | ||
pub half_hour_fee: usize, | ||
pub hour_fee: usize, | ||
pub economy_fee: usize, | ||
pub minimum_fee: usize, | ||
} | ||
|
||
impl FeeRate { | ||
fn local_fee_rate() -> Self { | ||
FeeRate { | ||
// we on purpose have different values to see an effect for clients asking for different | ||
// priorities | ||
fastest_fee: 5, | ||
half_hour_fee: 4, | ||
hour_fee: 3, | ||
economy_fee: 2, | ||
minimum_fee: 1, | ||
} | ||
} | ||
} | ||
|
||
#[derive(PartialEq)] | ||
pub enum Network { | ||
Mainnet, | ||
Signet, | ||
Testnet, | ||
/// We assume a local regtest setup and will not perform any request to mempool.space | ||
Local, | ||
} | ||
|
||
pub struct MempoolFeeRateEstimator { | ||
url: String, | ||
network: Network, | ||
} | ||
|
||
impl MempoolFeeRateEstimator { | ||
pub fn new(network: Network) -> Self { | ||
let url = match network { | ||
Network::Mainnet => MEMPOOL_FEE_RATE_URL_MAINNET, | ||
Network::Signet => MEMPOOL_FEE_RATE_URL_SIGNET, | ||
Network::Testnet => MEMPOOL_FEE_RATE_URL_TESTNET, | ||
Network::Local => "http://thereisnosuchthingasabitcoinmempool.com", | ||
} | ||
.to_string(); | ||
|
||
Self { url, network } | ||
} | ||
|
||
pub async fn fetch_fee(&self) -> Result<FeeRate> { | ||
if Network::Local == self.network { | ||
return Ok(FeeRate::local_fee_rate()); | ||
} | ||
let client = reqwest::Client::new(); | ||
let url = format!("{}/api/v1/fees/recommended", self.url); | ||
let response = client.get(url).send().await?; | ||
let fee_rate = response.json().await?; | ||
Ok(fee_rate) | ||
} | ||
|
||
/// Fetches the latest fee rate from mempool.space | ||
/// | ||
/// Note: if self.network is Network::Local we will not perform a request to mempool.space but | ||
/// return static values | ||
pub fn fetch_fee_sync(&self) -> Result<FeeRate> { | ||
if Network::Local == self.network { | ||
return Ok(FeeRate::local_fee_rate()); | ||
} | ||
let url = format!("{}/api/v1/fees/recommended", self.url); | ||
let response = reqwest::blocking::get(url)?; | ||
let fee_rate = response.json()?; | ||
Ok(fee_rate) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
// we keep this test running on CI even though it connects to the internet. This allows us to | ||
// be notified if the API ever changes | ||
#[tokio::test] | ||
pub async fn test_fetching_fee_rate_from_mempool() { | ||
let mempool = MempoolFeeRateEstimator::new(Network::Testnet); | ||
let _testnet_fee_rate = mempool.fetch_fee().await.unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Mainnet); | ||
let _testnet_fee_rate = mempool.fetch_fee().await.unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Signet); | ||
let _testnet_fee_rate = mempool.fetch_fee().await.unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Local); | ||
let _testnet_fee_rate = mempool.fetch_fee().await.unwrap(); | ||
} | ||
|
||
// we keep this test running on CI even though it connects to the internet. This allows us to | ||
// be notified if the API ever changes | ||
#[test] | ||
pub fn test_fetching_fee_rate_from_mempool_sync() { | ||
let mempool = MempoolFeeRateEstimator::new(Network::Testnet); | ||
let _testnet_fee_rate = mempool.fetch_fee_sync().unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Mainnet); | ||
let _testnet_fee_rate = mempool.fetch_fee_sync().unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Signet); | ||
let _testnet_fee_rate = mempool.fetch_fee_sync().unwrap(); | ||
let mempool = MempoolFeeRateEstimator::new(Network::Local); | ||
let _testnet_fee_rate = mempool.fetch_fee_sync().unwrap(); | ||
} | ||
} |