Skip to content

Commit

Permalink
Fix up hotspot location uses, reuse hpl key serialization (#367)
Browse files Browse the repository at this point in the history
* entity key encoding reuse from hpl

keyserialization fixups

* Fix is_none not working for hotspot info

* impl FromStr for HotspotLocation

drop skip_serializing_none on enum
  • Loading branch information
madninja committed May 30, 2024
1 parent 74f0204 commit c74bad3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
12 changes: 4 additions & 8 deletions helium-lib/src/entity_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ impl AsEntityKey for helium_crypto::PublicKey {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum EntityKeyEncoding {
String,
UTF8,
}
pub use helium_anchor_gen::helium_entity_manager::KeySerialization;

pub fn from_string(str: String, encoding: EntityKeyEncoding) -> Result<Vec<u8>> {
pub fn from_string(str: String, encoding: KeySerialization) -> Result<Vec<u8>> {
let entity_key = match encoding {
EntityKeyEncoding::String => str.as_entity_key(),
EntityKeyEncoding::UTF8 => bs58::decode(str).into_vec().map_err(DecodeError::from)?,
KeySerialization::UTF8 => str.as_entity_key(),
KeySerialization::B58 => bs58::decode(str).into_vec().map_err(DecodeError::from)?,
};
Ok(entity_key)
}
15 changes: 13 additions & 2 deletions helium-lib/src/hotspot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,12 +672,12 @@ impl TryFrom<asset::AssetPage> for HotspotPage {
}

#[derive(Debug, Serialize, Clone)]
#[skip_serializing_none]
pub struct Hotspot {
pub key: helium_crypto::PublicKey,
pub name: String,
#[serde(with = "serde_pubkey")]
pub owner: Pubkey,
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<HashMap<SubDao, HotspotInfo>>,
}

Expand Down Expand Up @@ -743,6 +743,13 @@ impl TryFrom<u64> for HotspotLocation {
}
}

impl FromStr for HotspotLocation {
type Err = h3o::error::InvalidCellIndex;
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
s.parse::<h3o::CellIndex>().map(Into::into)
}
}

impl HotspotLocation {
pub fn from_maybe<T: TryInto<HotspotLocation>>(value: Option<T>) -> Option<Self> {
value.and_then(|v| TryInto::try_into(v).ok())
Expand All @@ -751,22 +758,26 @@ impl HotspotLocation {

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "lowercase", untagged)]
#[skip_serializing_none]
pub enum HotspotInfo {
Iot {
#[serde(skip_serializing_if = "Option::is_none")]
asset: Option<String>,
mode: HotspotMode,
gain: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
elevation: Option<i32>,
#[serde(flatten)]
#[serde(skip_serializing_if = "Option::is_none")]
location: Option<HotspotLocation>,
#[serde(skip_serializing_if = "is_zero")]
location_asserts: u16,
},
Mobile {
#[serde(skip_serializing_if = "Option::is_none")]
asset: Option<String>,
mode: HotspotMode,
#[serde(flatten)]
#[serde(skip_serializing_if = "Option::is_none")]
location: Option<HotspotLocation>,
#[serde(skip_serializing_if = "is_zero")]
location_asserts: u16,
Expand Down
6 changes: 3 additions & 3 deletions helium-lib/src/reward.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
asset,
dao::SubDao,
entity_key::{self, AsEntityKey, EntityKeyEncoding},
entity_key::{self, AsEntityKey, KeySerialization},
keypair::{Keypair, Pubkey, PublicKey},
programs::SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
result::{DecodeError, Error, Result},
Expand Down Expand Up @@ -60,7 +60,7 @@ pub async fn claim<C: Clone + Deref<Target = impl Signer>, E>(
settings: &Settings,
subdao: &SubDao,
entity_key_string: &str,
entity_key_encoding: EntityKeyEncoding,
entity_key_encoding: KeySerialization,
keypair: C,
) -> Result<solana_sdk::transaction::Transaction>
where
Expand Down Expand Up @@ -214,7 +214,7 @@ pub async fn pending(
settings: &Settings,
subdao: &SubDao,
entity_key_strings: &[String],
entity_key_encoding: EntityKeyEncoding,
entity_key_encoding: KeySerialization,
) -> Result<HashMap<String, OracleReward>> {
fn for_entity_key(
bulk_rewards: &HashMap<String, Vec<OracleReward>>,
Expand Down
4 changes: 2 additions & 2 deletions helium-wallet/src/cmd/hotspots/rewards.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cmd::*;
use helium_lib::{dao::SubDao, entity_key::EntityKeyEncoding, reward};
use helium_lib::{dao::SubDao, entity_key::KeySerialization, reward};

#[derive(Debug, Clone, clap::Args)]
pub struct Cmd {
Expand Down Expand Up @@ -46,7 +46,7 @@ impl PendingCmd {
&settings,
&self.subdao,
&entity_key_strings,
EntityKeyEncoding::UTF8,
KeySerialization::B58,
)
.await?;

Expand Down

0 comments on commit c74bad3

Please sign in to comment.