Skip to content

Commit

Permalink
refactor(client)!: relay_fee result should be strongly typed
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Feb 9, 2025
1 parent 83747b1 commit eb9003d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Borrow;
use std::convert::TryInto;

use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin::{block, Script, Transaction, Txid};
use bitcoin::{block, Amount, Script, Transaction, Txid};

use crate::batch::Batch;
use crate::types::*;
Expand Down Expand Up @@ -97,8 +97,8 @@ pub trait ElectrumApi {
/// Estimates the fee required in **Bitcoin per kilobyte** to confirm a transaction in `number` blocks.
fn estimate_fee(&self, number: usize) -> Result<f64, Error>;

/// Returns the minimum accepted fee by the server's node in **Bitcoin, not Satoshi**.
fn relay_fee(&self) -> Result<f64, Error>;
/// Returns the minimum accepted fee by the server's node.
fn relay_fee(&self) -> Result<Amount, Error>;

/// Subscribes to notifications for activity on a specific *scriptPubKey*.
///
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{borrow::Borrow, sync::RwLock};

use log::{info, warn};

use bitcoin::{Script, Txid};
use bitcoin::{Amount, Script, Txid};

use crate::api::ElectrumApi;
use crate::batch::Batch;
Expand Down Expand Up @@ -212,7 +212,7 @@ impl ElectrumApi for Client {
}

#[inline]
fn relay_fee(&self) -> Result<f64, Error> {
fn relay_fee(&self) -> Result<Amount, Error> {
impl_inner_call!(self, relay_fee)
}

Expand Down
9 changes: 5 additions & 4 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::{debug, error, info, trace, warn};

use bitcoin::consensus::encode::deserialize;
use bitcoin::hex::{DisplayHex, FromHex};
use bitcoin::{Script, Txid};
use bitcoin::{Amount, Script, Txid};

#[cfg(feature = "use-openssl")]
use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};
Expand Down Expand Up @@ -879,17 +879,18 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
.ok_or_else(|| Error::InvalidResponse(result.clone()))
}

fn relay_fee(&self) -> Result<f64, Error> {
fn relay_fee(&self) -> Result<Amount, Error> {
let req = Request::new_id(
self.last_id.fetch_add(1, Ordering::SeqCst),
"blockchain.relayfee",
vec![],
);
let result = self.call(req)?;

result
let amount_float = result
.as_f64()
.ok_or_else(|| Error::InvalidResponse(result.clone()))
.ok_or_else(|| Error::InvalidResponse(result.clone()))?;
Amount::from_btc(amount_float).map_err(|_| Error::InvalidResponse(result.clone()))
}

fn script_subscribe(&self, script: &Script) -> Result<Option<ScriptStatus>, Error> {
Expand Down

0 comments on commit eb9003d

Please sign in to comment.