|
1 | 1 | //! Responses |
2 | 2 |
|
3 | 3 | use std::cmp::Ordering; |
4 | | -use std::collections::BTreeSet; |
| 4 | +use std::collections::{BTreeSet, HashMap}; |
5 | 5 |
|
6 | 6 | use bitcoin::address::{Address, NetworkUnchecked}; |
7 | 7 | use bitcoin::{Amount, BlockHash, FeeRate, TxMerkleNode, Weight}; |
@@ -32,32 +32,42 @@ impl<T> MempoolResponse<T> { |
32 | 32 | } |
33 | 33 |
|
34 | 34 | /// Prices |
35 | | -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 35 | +#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Deserialize)] |
36 | 36 | pub struct Prices { |
37 | 37 | /// Timestamp |
38 | 38 | #[serde(rename = "time")] |
39 | 39 | pub timestamp: u64, |
40 | 40 | /// USD |
41 | 41 | #[serde(rename = "USD")] |
42 | | - pub usd: u32, |
| 42 | + pub usd: f64, |
43 | 43 | /// EUR |
44 | 44 | #[serde(rename = "EUR")] |
45 | | - pub eur: u32, |
| 45 | + pub eur: Option<f64>, |
46 | 46 | /// GBP |
47 | 47 | #[serde(rename = "GBP")] |
48 | | - pub gbp: u32, |
| 48 | + pub gbp: Option<f64>, |
49 | 49 | /// CAD |
50 | 50 | #[serde(rename = "CAD")] |
51 | | - pub cad: u32, |
| 51 | + pub cad: Option<f64>, |
52 | 52 | /// CHF |
53 | 53 | #[serde(rename = "CHF")] |
54 | | - pub chf: u32, |
| 54 | + pub chf: Option<f64>, |
55 | 55 | /// AUD |
56 | 56 | #[serde(rename = "AUD")] |
57 | | - pub aud: u32, |
| 57 | + pub aud: Option<f64>, |
58 | 58 | /// JPY |
59 | 59 | #[serde(rename = "JPY")] |
60 | | - pub jpy: u32, |
| 60 | + pub jpy: Option<f64>, |
| 61 | +} |
| 62 | + |
| 63 | +/// Historical price |
| 64 | +#[derive(Debug, Clone, PartialEq, Deserialize)] |
| 65 | +pub struct HistoricalPrice { |
| 66 | + /// Prices |
| 67 | + pub prices: Vec<Prices>, |
| 68 | + /// Exchange rates |
| 69 | + #[serde(rename = "exchangeRates")] |
| 70 | + pub rates: HashMap<String, f64>, |
61 | 71 | } |
62 | 72 |
|
63 | 73 | /// Bitcoin difficulty adjustment information |
@@ -881,4 +891,51 @@ mod tests { |
881 | 891 | }; |
882 | 892 | assert_eq!(stats.avg_fee_rate(), FeeRate::from_sat_per_vb_unchecked(10)); |
883 | 893 | } |
| 894 | + |
| 895 | + #[test] |
| 896 | + fn test_historical_price_deserialization() { |
| 897 | + let json = r#"{ |
| 898 | + "prices": [ |
| 899 | + { |
| 900 | + "time": 1499904000, |
| 901 | + "EUR": 1964, |
| 902 | + "USD": 2254.9 |
| 903 | + } |
| 904 | + ], |
| 905 | + "exchangeRates": { |
| 906 | + "USDEUR": 0.92, |
| 907 | + "USDGBP": 0.78, |
| 908 | + "USDCAD": 1.36, |
| 909 | + "USDCHF": 0.89, |
| 910 | + "USDAUD": 1.53, |
| 911 | + "USDJPY": 149.48 |
| 912 | + } |
| 913 | +} |
| 914 | +"#; |
| 915 | + |
| 916 | + let historical_price: HistoricalPrice = serde_json::from_str(json).unwrap(); |
| 917 | + assert_eq!( |
| 918 | + historical_price, |
| 919 | + HistoricalPrice { |
| 920 | + prices: vec![Prices { |
| 921 | + timestamp: 1499904000, |
| 922 | + usd: 2254.9, |
| 923 | + eur: Some(1964.0), |
| 924 | + gbp: None, |
| 925 | + cad: None, |
| 926 | + chf: None, |
| 927 | + aud: None, |
| 928 | + jpy: None, |
| 929 | + }], |
| 930 | + rates: HashMap::from([ |
| 931 | + ("USDEUR".to_string(), 0.92), |
| 932 | + ("USDGBP".to_string(), 0.78), |
| 933 | + ("USDCAD".to_string(), 1.36), |
| 934 | + ("USDCHF".to_string(), 0.89), |
| 935 | + ("USDAUD".to_string(), 1.53), |
| 936 | + ("USDJPY".to_string(), 149.48), |
| 937 | + ]), |
| 938 | + } |
| 939 | + ) |
| 940 | + } |
884 | 941 | } |
0 commit comments