helium_api/models/
hotspot.rs

1use super::Dbi;
2use crate::{Error, Result};
3use chrono::{DateTime, Utc};
4
5use serde::{de, Deserialize, Serialize};
6use std::{fmt, str::FromStr};
7
8use super::Geocode;
9
10#[derive(Clone, Serialize, Deserialize, Debug)]
11pub struct Hotspot {
12    /// The address of the hotspots. This is the public key in base58
13    /// check-encoding of the hotspot.
14    pub address: String,
15    /// The hotspot owner wallet address
16    pub owner: String,
17    /// The "animal" name of the hotspot. The name can be `None` for
18    /// some API endpoints.
19    pub name: Option<String>,
20    /// The block height when the hotspot was added to the blockchain
21    pub added_height: Option<u64>,
22    /// The last asserted latitude of the hotspot
23    pub lat: Option<f64>,
24    /// The last asserted longitude of the hotspot
25    pub lng: Option<f64>,
26    /// The h3 index based on the lat/lon of the hotspot is used for
27    /// PoC challenges.
28    pub location: Option<String>,
29    /// The mode in which the hotspots was added to the network.
30    pub mode: HotspotStakingMode,
31    /// The elevation (in meters) above or belowo sea level
32    pub elevation: Option<i32>,
33    /// The gain (in dbi) above or belowo sea level
34    #[serde(deserialize_with = "Dbi::deserialize_option")]
35    pub gain: Option<Dbi>,
36    /// The geocode information for the hotspot location
37    pub geocode: Geocode,
38    /// The current nonce for the hotspot
39    pub nonce: u64,
40    /// The speculative nonce for the hotspot. This field is only meaningful
41    /// when a single hotspot is requested
42    #[serde(default)]
43    pub speculative_nonce: u64,
44    /// The current reward scale for the hotspot
45    pub reward_scale: Option<f64>,
46    /// The current status for the hotspot
47    pub status: Status,
48}
49
50#[derive(Clone, Serialize, Deserialize, Debug)]
51pub struct Status {
52    /// The timestamp of hotspot status, when it last sends gossip
53    pub timestamp: Option<DateTime<Utc>>,
54    /// The online status of the hotspot
55    pub status: Option<String>,
56    /// The IP addresses to listen to hotspot.
57    pub listen_addrs: Option<Vec<String>>,
58    /// The height of the hotspot when gossip is sent out
59    pub height: Option<u64>,
60}
61
62#[derive(Clone, Serialize, Debug)]
63pub enum HotspotStakingMode {
64    Full,
65    Light,
66    DataOnly,
67}
68
69impl fmt::Display for HotspotStakingMode {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::DataOnly => f.write_str("dataonly"),
73            Self::Full => f.write_str("full"),
74            Self::Light => f.write_str("light"),
75        }
76    }
77}
78
79impl<'de> Deserialize<'de> for HotspotStakingMode {
80    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
81    where
82        D: de::Deserializer<'de>,
83    {
84        struct HotspotStakingModeVisitor;
85
86        impl<'de> de::Visitor<'de> for HotspotStakingModeVisitor {
87            type Value = HotspotStakingMode;
88
89            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90                formatter.write_str("full, light, dataonly")
91            }
92
93            fn visit_str<E>(self, value: &str) -> std::result::Result<HotspotStakingMode, E>
94            where
95                E: de::Error,
96            {
97                match HotspotStakingMode::from_str(value) {
98                    Ok(v) => Ok(v),
99                    Err(_) => Err(de::Error::custom("invalid staking mode")),
100                }
101            }
102        }
103
104        deserializer.deserialize_str(HotspotStakingModeVisitor)
105    }
106}
107impl FromStr for HotspotStakingMode {
108    type Err = Error;
109
110    fn from_str(s: &str) -> Result<Self> {
111        match s.to_lowercase().as_ref() {
112            "light" => Ok(Self::Light),
113            "full" => Ok(Self::Full),
114            "dataonly" => Ok(Self::DataOnly),
115            _ => Err(Error::value(s.into())),
116        }
117    }
118}