Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions src/custom_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,6 @@ where
Ok(weight)
}

pub fn amount_from_btc<'de, D>(deserializer: D) -> Result<bitcoin::Amount, D::Error>
where
D: Deserializer<'de>,
{
let btc = f64::deserialize(deserializer)?;
bitcoin::Amount::from_btc(btc).map_err(serde::de::Error::custom)
}

pub fn amount_from_sats<'de, D>(deserializer: D) -> Result<bitcoin::Amount, D::Error>
where
D: Deserializer<'de>,
{
let sats = u64::deserialize(deserializer)?;
Ok(bitcoin::Amount::from_sat(sats))
}

pub fn amount_from_maybe_negative_sats<'de, D>(deserializer: D) -> Result<bitcoin::Amount, D::Error>
where
D: Deserializer<'de>,
{
let sats = i64::deserialize(deserializer)?.unsigned_abs();
Ok(bitcoin::Amount::from_sat(sats))
}

pub fn all_inputs_confirmed_bool_from_height<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
Expand Down
4 changes: 2 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ impl Request for RelayFee {
/// A request for the confirmed and unconfirmed balance of a specific script hash.
///
/// This corresponds to the `"blockchain.scripthash.get_balance"` Electrum RPC method. It returns
/// both the confirmed balance (from mined transactions) and unconfirmed balance (from mempool
/// transactions) for the provided script hash.
/// the confirmed balance (from mined transactions) and the unconfirmed balance (from mempool transactions) for the
/// provided script hash. The unconfirmed value is signed and can be negative if confirmed outputs are spent.
///
/// See: <https://electrum-protocol.readthedocs.io/en/latest/protocol-methods.html#blockchain-scripthash-get-balance>
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
18 changes: 10 additions & 8 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::HashMap;
use bitcoin::{
absolute,
hashes::{Hash, HashEngine},
Amount, BlockHash,
Amount, BlockHash, SignedAmount,
};

use crate::DoubleSHA;
Expand Down Expand Up @@ -105,20 +105,22 @@ pub struct HeadersSubscribeResp {
#[serde(transparent)]
pub struct RelayFeeResp {
/// The minimum fee amount that the server will accept for relaying transactions.
#[serde(deserialize_with = "crate::custom_serde::amount_from_btc")]
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub fee: Amount,
}

/// Response to the `"blockchain.scripthash.get_balance"` method.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GetBalanceResp {
/// The confirmed balance in satoshis.
#[serde(deserialize_with = "crate::custom_serde::amount_from_sats")]
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub confirmed: Amount,

/// The unconfirmed balance in satoshis (may be negative).
#[serde(deserialize_with = "crate::custom_serde::amount_from_maybe_negative_sats")]
pub unconfirmed: Amount,
/// The unconfirmed balance in satoshis.
///
/// Can be negative when confirmed outputs are spent in the mempool.
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub unconfirmed: SignedAmount,
}

#[derive(Debug, Clone, serde::Deserialize)]
Expand Down Expand Up @@ -177,7 +179,7 @@ pub struct MempoolTx {
pub txid: bitcoin::Txid,

/// The fee paid by the transaction in satoshis.
#[serde(deserialize_with = "crate::custom_serde::amount_from_sats")]
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fee: bitcoin::Amount,

/// Whether all inputs are confirmed.
Expand All @@ -202,7 +204,7 @@ pub struct Utxo {
pub txid: bitcoin::Txid,

/// The value of the UTXO in satoshis.
#[serde(deserialize_with = "crate::custom_serde::amount_from_sats")]
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub value: bitcoin::Amount,
}

Expand Down