Skip to content

Commit

Permalink
refactor price handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 2, 2024
1 parent 95dd14c commit 69e61c6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 61 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mobile_verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
h3o = { workspace = true, features = ["geo"] }
helium-lib = { workspace = true }
hextree = { workspace = true }
http-serde = { workspace = true }
clap = { workspace = true }
Expand Down
35 changes: 18 additions & 17 deletions mobile_verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ pub mod telemetry;
pub use settings::Settings;

use async_trait::async_trait;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use std::error::Error;

pub const MOBILE_SUB_DAO_ONCHAIN_ADDRESS: &str = "39Lw1RH6zt8AJvKn3BTxmUDofzduCM2J3kSaGDZ8L7Sk";
Expand Down Expand Up @@ -102,21 +100,24 @@ impl IsAuthorized for mobile_config::client::AuthorizationClient {
}
}

pub struct PriceConverter;

impl PriceConverter {
pub fn hnt_bones_to_pricer_format(hnt_bone_price: Decimal) -> u64 {
(hnt_bone_price * dec!(1_0000_0000) * dec!(1_0000_0000))
.to_u64()
.unwrap_or_default()
}

// Hnt prices are supplied from pricer in 10^8
pub fn pricer_format_to_hnt_bones(hnt_price: u64) -> Decimal {
Decimal::from(hnt_price) / dec!(1_0000_0000) / dec!(1_0000_0000)
}
#[derive(Clone, Debug)]
pub struct HntPrice {
pub hnt_price_in_bones: u64,
pub hnt_price: Decimal,
pub price_per_hnt_bone: Decimal,
pub decimals: u8,
}

pub fn pricer_format_to_hnt(hnt_price: u64) -> Decimal {
Decimal::from(hnt_price) / dec!(1_0000_0000)
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));
Self {
hnt_price_in_bones,
hnt_price,
price_per_hnt_bone,
decimals,
}
}
}
47 changes: 24 additions & 23 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +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,
PriceConverter,
subscriber_verified_mapping_event::VerifiedSubscriberVerifiedMappingEventShares, HntPrice,
};
use chrono::{DateTime, Utc};
use coverage_point_calculator::{OracleBoostingStatus, SPBoostedRewardEligibility};
Expand Down Expand Up @@ -58,7 +57,7 @@ pub struct TransferRewards {
reward_scale: Decimal,
rewards: HashMap<PublicKeyBinary, TransferReward>,
reward_sum: Decimal,
hnt_bone_price: Decimal,
hnt_price: HntPrice,
}

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

pub async fn from_transfer_sessions(
hnt_bone_price: Decimal,
hnt_price: HntPrice,
transfer_sessions: HotspotMap,
reward_shares: &DataTransferAndPocAllocatedRewardBuckets,
) -> Self {
Expand All @@ -103,8 +102,10 @@ impl TransferRewards {
.into_iter()
// Calculate rewards per hotspot
.map(|(pub_key, rewardable)| {
let bones =
dc_to_hnt_bones(Decimal::from(rewardable.rewardable_dc), hnt_bone_price);
let bones = dc_to_hnt_bones(
Decimal::from(rewardable.rewardable_dc),
hnt_price.price_per_hnt_bone,
);
reward_sum += bones;
(
pub_key,
Expand All @@ -129,7 +130,7 @@ impl TransferRewards {
reward_scale,
rewards,
reward_sum: reward_sum * reward_scale,
hnt_bone_price,
hnt_price,
}
}

Expand All @@ -144,7 +145,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 = PriceConverter::hnt_bones_to_pricer_format(self.hnt_bone_price);
let price = self.hnt_price.hnt_price_in_bones;

rewards
.into_iter()
Expand Down Expand Up @@ -956,8 +957,11 @@ mod test {
let reward_shares =
DataTransferAndPocAllocatedRewardBuckets::new(rewards_info.epoch_emissions);

// todo: rebalance the tests to use a normalised hnt price
let hnt_price = HntPrice::new(10000000000000000, 8);

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

assert_eq!(data_transfer_rewards.reward(&owner), dec!(0.00002));
Expand Down Expand Up @@ -1004,11 +1008,14 @@ mod test {
// set our rewards info
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);

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

let data_transfer_rewards = TransferRewards::from_transfer_sessions(
dec!(1.0),
hnt_price,
aggregated_data_transfer_sessions,
&reward_shares,
)
Expand Down Expand Up @@ -2420,20 +2427,14 @@ mod test {
#[test]
fn test_price_conversion() {
let hnt_dollar_price = dec!(1.0);
let hnt_dollar_bone_price = dec!(0.00000001);
let hnt_price_from_pricer = 100000000_u64;
let hnt_dollar_bone_price = dec!(0.00000001);

assert_eq!(
hnt_dollar_bone_price,
PriceConverter::pricer_format_to_hnt_bones(hnt_price_from_pricer)
);
assert_eq!(
hnt_price_from_pricer,
PriceConverter::hnt_bones_to_pricer_format(hnt_dollar_bone_price)
);
assert_eq!(
hnt_dollar_price,
PriceConverter::pricer_format_to_hnt(hnt_price_from_pricer)
);
let pricer_decimals = 8;
let hnt_price = HntPrice::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);
}
}
23 changes: 12 additions & 11 deletions mobile_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
service_provider::{self, ServiceProviderDCSessions, ServiceProviderPromotions},
sp_boosted_rewards_bans, speedtests,
speedtests_average::SpeedtestAverages,
subscriber_location, subscriber_verified_mapping_event, telemetry, PriceConverter, Settings,
subscriber_location, subscriber_verified_mapping_event, telemetry, HntPrice, Settings,
MOBILE_SUB_DAO_ONCHAIN_ADDRESS,
};
use anyhow::bail;
Expand All @@ -24,6 +24,7 @@ use file_store::{
use futures_util::TryFutureExt;

use self::boosted_hex_eligibility::BoostedHexEligibility;
use helium_lib::token::Token;
use helium_proto::{
reward_manifest::RewardData::MobileRewardData,
services::poc_mobile::{
Expand Down Expand Up @@ -264,19 +265,19 @@ where
next_reward_epoch
))?;

let hnt_price = self
let pricer_hnt_price = self
.price_tracker
.price(&helium_proto::BlockchainTokenTypeV1::Hnt)
.await?;

let hnt_bone_price = PriceConverter::pricer_format_to_hnt_bones(hnt_price);
let hnt_price = HntPrice::new(pricer_hnt_price, Token::Hnt.decimals());

tracing::info!(
"Rewarding for epoch {} period: {} to {} with hnt bone price: {}",
reward_info.epoch,
reward_info.epoch_period.start,
reward_info.epoch_period.end,
hnt_bone_price
hnt_price.price_per_hnt_bone
);

// process rewards for poc and data transfer
Expand All @@ -286,7 +287,7 @@ where
&self.mobile_rewards,
&self.speedtest_averages,
&reward_info,
hnt_bone_price,
hnt_price.clone(),
)
.await?;

Expand All @@ -308,7 +309,7 @@ where
sp_promotions.clone(),
&self.mobile_rewards,
&reward_info,
hnt_bone_price,
hnt_price.price_per_hnt_bone,
)
.await?;

Expand Down Expand Up @@ -356,7 +357,7 @@ where
written_files,
reward_data: Some(MobileRewardData(reward_data)),
epoch: reward_info.epoch,
price: hnt_price,
price: hnt_price.hnt_price_in_bones,
},
[],
)
Expand Down Expand Up @@ -394,13 +395,13 @@ pub async fn reward_poc_and_dc(
mobile_rewards: &FileSinkClient<proto::MobileRewardShare>,
speedtest_avg_sink: &FileSinkClient<proto::SpeedtestAvg>,
reward_info: &ResolvedSubDaoEpochRewardInfo,
hnt_bone_price: Decimal,
hnt_price: HntPrice,
) -> anyhow::Result<CalculatedPocRewardShares> {
let mut reward_shares =
DataTransferAndPocAllocatedRewardBuckets::new(reward_info.epoch_emissions);

let transfer_rewards = TransferRewards::from_transfer_sessions(
hnt_bone_price,
hnt_price,
data_session::aggregate_hotspot_data_sessions_to_dc(pool, &reward_info.epoch_period)
.await?,
&reward_shares,
Expand Down Expand Up @@ -641,7 +642,7 @@ pub async fn reward_service_providers(
sp_promotions: ServiceProviderPromotions,
mobile_rewards: &FileSinkClient<proto::MobileRewardShare>,
reward_info: &ResolvedSubDaoEpochRewardInfo,
mobile_bone_price: Decimal,
hnt_bone_price: Decimal,
) -> anyhow::Result<()> {
use service_provider::ServiceProviderRewardInfos;

Expand All @@ -651,7 +652,7 @@ pub async fn reward_service_providers(
dc_sessions,
sp_promotions,
total_sp_rewards,
mobile_bone_price,
hnt_bone_price,
reward_info.clone(),
);

Expand Down
Loading

0 comments on commit 69e61c6

Please sign in to comment.