Skip to content

Commit

Permalink
rename some things
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 12, 2024
1 parent e8643f6 commit 203dffc
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 127 deletions.
11 changes: 4 additions & 7 deletions mobile_config/src/client/sub_dao_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{call_with_retry, ClientError, Settings};
use crate::sub_dao_epoch_reward_info::ResolvedSubDaoEpochRewardInfo;
use crate::sub_dao_epoch_reward_info::EpochRewardInfo;
use file_store::traits::MsgVerify;
use helium_crypto::{Keypair, PublicKey, Sign};
use helium_proto::{
Expand Down Expand Up @@ -36,7 +36,7 @@ pub trait SubDaoEpochRewardInfoResolver: Clone + Send + Sync + 'static {
&self,
sub_dao: &str,
epoch: u64,
) -> Result<Option<ResolvedSubDaoEpochRewardInfo>, Self::Error>;
) -> Result<Option<EpochRewardInfo>, Self::Error>;
}

#[async_trait::async_trait]
Expand All @@ -47,7 +47,7 @@ impl SubDaoEpochRewardInfoResolver for SubDaoClient {
&self,
sub_dao: &str,
epoch: u64,
) -> Result<Option<ResolvedSubDaoEpochRewardInfo>, Self::Error> {
) -> Result<Option<EpochRewardInfo>, Self::Error> {
let mut request = SubDaoEpochRewardInfoReqV1 {
sub_dao_address: sub_dao.into(),
epoch,
Expand All @@ -64,10 +64,7 @@ impl SubDaoEpochRewardInfoResolver for SubDaoClient {
Ok(info_res) => {
let response = info_res.into_inner();
response.verify(&self.config_pubkey)?;
response
.info
.map(ResolvedSubDaoEpochRewardInfo::try_from)
.transpose()?
response.info.map(EpochRewardInfo::try_from).transpose()?
}
Err(status) if status.code() == tonic::Code::NotFound => None,
Err(status) => Err(status)?,
Expand Down
6 changes: 3 additions & 3 deletions mobile_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ impl std::fmt::Display for KeyRole {
}
}

pub struct EpochPeriod {
pub struct EpochInfo {
pub period: Range<DateTime<Utc>>,
}

impl From<u64> for EpochPeriod {
impl From<u64> for EpochInfo {
fn from(next_reward_epoch: u64) -> Self {
let start_time = DateTime::<Utc>::UNIX_EPOCH + Duration::days(next_reward_epoch as i64);
let end_time = start_time + Duration::days(1);
EpochPeriod {
EpochInfo {
period: start_time..end_time,
}
}
Expand Down
8 changes: 4 additions & 4 deletions mobile_config/src/sub_dao_epoch_reward_info.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::EpochPeriod;
use crate::EpochInfo;
use chrono::{DateTime, Utc};
use file_store::traits::{TimestampDecode, TimestampEncode};
use helium_proto::services::sub_dao::SubDaoEpochRewardInfo as SubDaoEpochRewardInfoProto;
use rust_decimal::prelude::*;
use std::ops::Range;

#[derive(Clone, Debug)]
pub struct ResolvedSubDaoEpochRewardInfo {
pub struct EpochRewardInfo {
pub epoch_day: u64,
pub epoch_address: String,
pub sub_dao_address: String,
Expand Down Expand Up @@ -44,11 +44,11 @@ impl From<RawSubDaoEpochRewardInfo> for SubDaoEpochRewardInfoProto {
}
}

impl TryFrom<SubDaoEpochRewardInfoProto> for ResolvedSubDaoEpochRewardInfo {
impl TryFrom<SubDaoEpochRewardInfoProto> for EpochRewardInfo {
type Error = SubDaoRewardInfoParseError;

fn try_from(info: SubDaoEpochRewardInfoProto) -> Result<Self, Self::Error> {
let epoch_period: EpochPeriod = info.epoch.into();
let epoch_period: EpochInfo = info.epoch.into();
let epoch_rewards = Decimal::from(info.rewards_issued + info.delegation_rewards_issued);

Ok(Self {
Expand Down
24 changes: 12 additions & 12 deletions mobile_verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,22 @@ impl IsAuthorized for mobile_config::client::AuthorizationClient {
}

#[derive(Clone, Debug)]
pub struct HntPrice {
pub hnt_price_in_bones: u64,
pub hnt_price: Decimal,
pub price_per_hnt_bone: Decimal,
pub struct PriceInfo {
pub price_in_bones: u64,
pub price_per_token: Decimal,
pub price_per_bone: Decimal,
pub decimals: u8,
}

impl HntPrice {
pub fn new(hnt_price_in_bones: u64, decimals: u8) -> Self {
let hnt_price =
Decimal::from(hnt_price_in_bones) / Decimal::from(10_u64.pow(decimals as u32));
let price_per_hnt_bone = hnt_price / Decimal::from(10_u64.pow(decimals as u32));
impl PriceInfo {
pub fn new(price_in_bones: u64, decimals: u8) -> Self {
let price_per_token =
Decimal::from(price_in_bones) / Decimal::from(10_u64.pow(decimals as u32));
let price_per_bone = price_per_token / Decimal::from(10_u64.pow(decimals as u32));
Self {
hnt_price_in_bones,
hnt_price,
price_per_hnt_bone,
price_in_bones,
price_per_token,
price_per_bone,
decimals,
}
}
Expand Down
49 changes: 22 additions & 27 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
rewarder::boosted_hex_eligibility::BoostedHexEligibility, seniority::Seniority,
sp_boosted_rewards_bans::BannedRadios, speedtests_average::SpeedtestAverages,
subscriber_location::SubscriberValidatedLocations,
subscriber_verified_mapping_event::VerifiedSubscriberVerifiedMappingEventShares, HntPrice,
subscriber_verified_mapping_event::VerifiedSubscriberVerifiedMappingEventShares, PriceInfo,
};
use chrono::{DateTime, Utc};
use coverage_point_calculator::{OracleBoostingStatus, SPBoostedRewardEligibility};
Expand All @@ -13,9 +13,7 @@ use helium_crypto::PublicKeyBinary;
use helium_proto::services::{
poc_mobile as proto, poc_mobile::mobile_reward_share::Reward as ProtoReward,
};
use mobile_config::{
boosted_hex_info::BoostedHexes, sub_dao_epoch_reward_info::ResolvedSubDaoEpochRewardInfo,
};
use mobile_config::{boosted_hex_info::BoostedHexes, sub_dao_epoch_reward_info::EpochRewardInfo};
use radio_reward_v2::{RadioRewardV2Ext, ToProtoDecimal};
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;
Expand Down Expand Up @@ -57,7 +55,7 @@ pub struct TransferRewards {
reward_scale: Decimal,
rewards: HashMap<PublicKeyBinary, TransferReward>,
reward_sum: Decimal,
hnt_price: HntPrice,
price_info: PriceInfo,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -93,7 +91,7 @@ impl TransferRewards {
}

pub async fn from_transfer_sessions(
hnt_price: HntPrice,
price_info: PriceInfo,
transfer_sessions: HotspotMap,
reward_shares: &DataTransferAndPocAllocatedRewardBuckets,
) -> Self {
Expand All @@ -104,7 +102,7 @@ impl TransferRewards {
.map(|(pub_key, rewardable)| {
let bones = dc_to_hnt_bones(
Decimal::from(rewardable.rewardable_dc),
hnt_price.price_per_hnt_bone,
price_info.price_per_bone,
);
reward_sum += bones;
(
Expand All @@ -130,13 +128,13 @@ impl TransferRewards {
reward_scale,
rewards,
reward_sum: reward_sum * reward_scale,
hnt_price,
price_info,
}
}

pub fn into_rewards(
self,
reward_info: &'_ ResolvedSubDaoEpochRewardInfo,
reward_info: &'_ EpochRewardInfo,
) -> impl Iterator<Item = (u64, proto::MobileRewardShare)> + '_ {
let Self {
reward_scale,
Expand All @@ -145,7 +143,7 @@ impl TransferRewards {
} = self;
let start_period = reward_info.epoch_period.start.encode_timestamp();
let end_period = reward_info.epoch_period.end.encode_timestamp();
let price = self.hnt_price.hnt_price_in_bones;
let price = self.price_info.price_in_bones;

rewards
.into_iter()
Expand Down Expand Up @@ -802,12 +800,9 @@ mod test {
(hnt_value / DC_USD_PRICE).round_dp_with_strategy(0, RoundingStrategy::ToNegativeInfinity)
}

fn default_rewards_info(
total_emissions: u64,
epoch_duration: Duration,
) -> ResolvedSubDaoEpochRewardInfo {
fn default_rewards_info(total_emissions: u64, epoch_duration: Duration) -> EpochRewardInfo {
let now = Utc::now();
ResolvedSubDaoEpochRewardInfo {
EpochRewardInfo {
epoch_day: 1,
epoch_address: EPOCH_ADDRESS.into(),
sub_dao_address: SUB_DAO_ADDRESS.into(),
Expand Down Expand Up @@ -958,12 +953,12 @@ mod test {
DataTransferAndPocAllocatedRewardBuckets::new(rewards_info.epoch_emissions);

// todo: rebalance the tests to use a normalised hnt price
let hnt_price = HntPrice::new(10000000000000000, 8);
assert_eq!(hnt_price.hnt_price, dec!(100000000));
assert_eq!(hnt_price.price_per_hnt_bone, dec!(1));
let price_info = PriceInfo::new(10000000000000000, 8);
assert_eq!(price_info.price_per_token, dec!(100000000));
assert_eq!(price_info.price_per_bone, dec!(1));

let data_transfer_rewards =
TransferRewards::from_transfer_sessions(hnt_price, data_transfer_map, &reward_shares)
TransferRewards::from_transfer_sessions(price_info, data_transfer_map, &reward_shares)
.await;

assert_eq!(data_transfer_rewards.reward(&owner), dec!(0.00002));
Expand Down Expand Up @@ -1011,15 +1006,15 @@ mod test {
let rewards_info = default_rewards_info(82_191_780_821_917, Duration::hours(24));

// todo: rebalance the tests to use a normalised hnt price
let hnt_price = HntPrice::new(10000000000000000, 8);
assert_eq!(hnt_price.hnt_price, dec!(100000000));
assert_eq!(hnt_price.price_per_hnt_bone, dec!(1));
let price_info = PriceInfo::new(10000000000000000, 8);
assert_eq!(price_info.price_per_token, dec!(100000000));
assert_eq!(price_info.price_per_bone, dec!(1));

let reward_shares =
DataTransferAndPocAllocatedRewardBuckets::new(rewards_info.epoch_emissions);

let data_transfer_rewards = TransferRewards::from_transfer_sessions(
hnt_price,
price_info,
aggregated_data_transfer_sessions,
&reward_shares,
)
Expand Down Expand Up @@ -2435,10 +2430,10 @@ mod test {
let hnt_dollar_bone_price = dec!(0.00000001);

let pricer_decimals = 8;
let hnt_price = HntPrice::new(hnt_price_from_pricer, pricer_decimals);
let hnt_price = PriceInfo::new(hnt_price_from_pricer, pricer_decimals);

assert_eq!(hnt_dollar_bone_price, hnt_price.price_per_hnt_bone);
assert_eq!(hnt_price_from_pricer, hnt_price.hnt_price_in_bones);
assert_eq!(hnt_dollar_price, hnt_price.hnt_price);
assert_eq!(hnt_dollar_bone_price, hnt_price.price_per_bone);
assert_eq!(hnt_price_from_pricer, hnt_price.price_in_bones);
assert_eq!(hnt_dollar_price, hnt_price.price_per_token);
}
}
Loading

0 comments on commit 203dffc

Please sign in to comment.