Skip to content

Commit

Permalink
Add hnt claiming support
Browse files Browse the repository at this point in the history
* Adds support for claiming HNT rewards for hotspots (or any rewardable asset).
* removes the need for a separate recipient init check when constructing a claim transaction

**Note** This is a breaking change for the cli wallet. the subdao (mobile/iot) parameter for any reward related commands (hotspots/assets) is now a long argument and not providing it assumes that HNT rewards are being asked for or claimed
  • Loading branch information
madninja committed Jan 5, 2025
1 parent 0d6e1b5 commit 339b244
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 171 deletions.
97 changes: 51 additions & 46 deletions helium-lib/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@ use crate::{
use chrono::Timelike;
use sha2::{Digest, Sha256};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
pub trait RewardableDao {
fn token(&self) -> Token;
fn lazy_distributor_key(&self) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[b"lazy_distributor", self.token().mint().as_ref()],
&lazy_distributor::id(),
);
key
}
fn receipient_key_from_kta(&self, kta: &helium_entity_manager::KeyToAssetV0) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[
b"recipient",
self.lazy_distributor_key().as_ref(),
kta.asset.as_ref(),
],
&lazy_distributor::id(),
);
key
}
}

#[derive(
Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize, Default,
)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "lowercase")]
pub enum Dao {
#[default]
Hnt,
}

Expand Down Expand Up @@ -116,6 +141,12 @@ impl Dao {
}
}

impl RewardableDao for Dao {
fn token(&self) -> Token {
Token::Hnt
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "lowercase")]
Expand All @@ -140,32 +171,11 @@ impl SubDao {
}

pub fn key(&self) -> Pubkey {
let mint = self.mint();
let (subdao_key, _) =
Pubkey::find_program_address(&[b"sub_dao", mint.as_ref()], &helium_sub_daos::id());
subdao_key
}

pub fn mint(&self) -> &Pubkey {
match self {
Self::Iot => Token::Iot.mint(),
Self::Mobile => Token::Mobile.mint(),
}
}

pub fn token(&self) -> Token {
match self {
Self::Iot => Token::Iot,
Self::Mobile => Token::Mobile,
}
}

pub fn lazy_distributor(&self) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[b"lazy_distributor", self.mint().as_ref()],
&lazy_distributor::id(),
let (subdao_key, _) = Pubkey::find_program_address(
&[b"sub_dao", self.token().mint().as_ref()],
&helium_sub_daos::id(),
);
key
subdao_key
}

pub fn delegated_dc_key(&self, router_key: &str) -> Pubkey {
Expand Down Expand Up @@ -211,26 +221,6 @@ impl SubDao {
key
}

pub fn lazy_distributor_key(&self) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[b"lazy_distributor", self.mint().as_ref()],
&lazy_distributor::id(),
);
key
}

pub fn receipient_key_from_kta(&self, kta: &helium_entity_manager::KeyToAssetV0) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[
b"recipient",
self.lazy_distributor_key().as_ref(),
kta.asset.as_ref(),
],
&lazy_distributor::id(),
);
key
}

pub fn config_key(&self) -> Pubkey {
let prefix = match self {
Self::Iot => "iot_config",
Expand Down Expand Up @@ -258,3 +248,18 @@ impl SubDao {
key
}
}

impl RewardableDao for SubDao {
fn token(&self) -> Token {
match self {
Self::Iot => Token::Iot,
Self::Mobile => Token::Mobile,
}
}
}

impl RewardableDao for Option<SubDao> {
fn token(&self) -> Token {
self.map(|subdao| subdao.token()).unwrap_or(Token::Hnt)
}
}
Loading

0 comments on commit 339b244

Please sign in to comment.