diff --git a/crates/btcio/src/rpc/mod.rs b/crates/btcio/src/rpc/mod.rs index 1e0a18047..768082242 100644 --- a/crates/btcio/src/rpc/mod.rs +++ b/crates/btcio/src/rpc/mod.rs @@ -1,6 +1,5 @@ pub mod client; pub mod error; pub mod traits; -pub mod types; pub use client::*; diff --git a/crates/btcio/src/rpc/types.rs b/crates/btcio/src/rpc/types.rs deleted file mode 100644 index 41dfb6497..000000000 --- a/crates/btcio/src/rpc/types.rs +++ /dev/null @@ -1,149 +0,0 @@ -#[cfg(test)] -use arbitrary::Arbitrary; -use bitcoin::BlockHash; -use serde::{de::Visitor, Deserialize, Deserializer, Serialize}; -use tracing::*; - -#[derive(Clone, Debug, Deserialize)] -#[cfg_attr(test, derive(Arbitrary))] -pub struct RPCTransactionInfo { - pub amount: f64, - pub fee: Option, - pub confirmations: u64, - pub generated: Option, - pub trusted: Option, - pub blockhash: Option, - pub blockheight: Option, - pub blockindex: Option, - pub blocktime: Option, - pub txid: String, - pub wtxid: String, - pub walletconflicts: Vec, - pub replaced_by_txid: Option, - pub replaces_txid: Option, - pub comment: Option, - pub to: Option, - pub time: u64, - pub timereceived: u64, - #[serde(rename = "bip125-replaceable")] - pub bip125_replaceable: String, - pub parent_descs: Option>, - pub hex: String, - // NOTE: "details", and "decoded" fields omitted as not used, add them when used -} - -impl RPCTransactionInfo { - pub fn block_height(&self) -> u64 { - if self.confirmations == 0 { - return 0; - } - self.blockheight.unwrap_or_else(|| { - warn!("Txn confirmed but did not obtain blockheight. Setting height to zero"); - 0 - }) - } -} - -#[derive(Clone, Deserialize)] -#[cfg_attr(test, derive(Arbitrary))] -pub struct RawUTXO { - pub txid: String, - pub vout: u32, - pub address: String, - #[serde(rename = "scriptPubKey")] - pub script_pub_key: String, - #[serde(deserialize_with = "deserialize_satoshis")] - pub amount: u64, // satoshis - pub confirmations: u64, - pub spendable: bool, - pub solvable: bool, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(test, derive(Arbitrary))] -pub struct RpcBlockchainInfo { - pub blocks: u64, - pub headers: u64, - bestblockhash: String, - pub initialblockdownload: bool, - pub warnings: String, -} - -impl RpcBlockchainInfo { - pub fn bestblockhash(&self) -> BlockHash { - self.bestblockhash - .parse::() - .expect("rpc: bad blockhash") - } -} - -fn deserialize_satoshis<'d, D>(deserializer: D) -> Result -where - D: Deserializer<'d>, -{ - struct SatVisitor; - - impl<'d> Visitor<'d> for SatVisitor { - type Value = u64; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(formatter, "a float representation of btc values expected") - } - - fn visit_f64(self, v: f64) -> Result - where - E: serde::de::Error, - { - let sats = (v * 100_000_000.0).round() as u64; - Ok(sats) - } - } - deserializer.deserialize_any(SatVisitor) -} - -#[cfg(test)] -mod test { - - use serde::Deserialize; - - use super::*; - - #[derive(Deserialize)] - struct TestStruct { - #[serde(deserialize_with = "deserialize_satoshis")] - value: u64, - } - - #[test] - fn test_deserialize_satoshis() { - // Valid cases - let json_data = r#"{"value": 0.000042}"#; - let result: TestStruct = serde_json::from_str(json_data).unwrap(); - assert_eq!(result.value, 4200); - - let json_data = r#"{"value": 1.23456789}"#; - let result: TestStruct = serde_json::from_str(json_data).unwrap(); - assert_eq!(result.value, 123456789); - - let json_data = r#"{"value": 123.0}"#; - let result: TestStruct = serde_json::from_str(json_data).unwrap(); - assert_eq!(result.value, 12300000000); - - let json_data = r#"{"value": 123.45}"#; - let result: TestStruct = serde_json::from_str(json_data).unwrap(); - assert_eq!(result.value, 12345000000); - - // Invalid cases - let json_data = r#"{"value": 123}"#; - let result: Result = serde_json::from_str(json_data); - assert!(result.is_err()); - - let json_data = r#"{"value": "abc"}"#; - let result: Result = serde_json::from_str(json_data); - assert!(result.is_err()); - - let json_data = r#"{"value": "123.456.78"}"#; - let result: Result = serde_json::from_str(json_data); - assert!(result.is_err()); - } -}