diff --git a/src/custom_serde.rs b/src/custom_serde.rs index 722fffb..0fbddbc 100644 --- a/src/custom_serde.rs +++ b/src/custom_serde.rs @@ -72,30 +72,6 @@ where Ok(weight) } -pub fn amount_from_btc<'de, D>(deserializer: D) -> Result -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 -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 -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 where D: Deserializer<'de>, diff --git a/src/request.rs b/src/request.rs index 9973529..ef2b86c 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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: #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/src/response.rs b/src/response.rs index 7510ec4..ac228ae 100644 --- a/src/response.rs +++ b/src/response.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use bitcoin::{ absolute, hashes::{Hash, HashEngine}, - Amount, BlockHash, + Amount, BlockHash, SignedAmount, }; use crate::DoubleSHA; @@ -105,7 +105,7 @@ 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, } @@ -113,12 +113,14 @@ pub struct RelayFeeResp { #[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)] @@ -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. @@ -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, }