Skip to content

Commit

Permalink
carry across tweaks from mobile pr
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 6, 2024
1 parent 7b405f3 commit 21ab8c7
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 84 deletions.
65 changes: 41 additions & 24 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ sqlx = { git = "https://github.com/launchbadge/sqlx.git", rev = "42dd78fe931df65
# Patching for beacon must point directly to the crate, it will not look in the
# repo for sibling crates.
#
[patch.'https://github.com/helium/proto']
helium-proto = { path = "../../proto" }
# [patch.'https://github.com/helium/proto']
# helium-proto = { path = "../../proto" }
# beacon = { path = "../../proto" }
6 changes: 4 additions & 2 deletions iot_config/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub enum ClientError {
Verification(#[from] file_store::Error),
#[error("error resolving region params: {0}")]
UndefinedRegionParams(String),
#[error("error {0}")]
AnyhowError(#[from] anyhow::Error),
#[error("Invalid SubDaoRewardInfo proto response {0}")]
InvalidSubDaoRewardInfoProto(
#[from] crate::sub_dao_epoch_reward_info::SubDaoRewardInfoParseError,
),
}

#[async_trait::async_trait]
Expand Down
19 changes: 6 additions & 13 deletions iot_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub mod sub_dao_epoch_reward_info;
pub mod sub_dao_service;

pub use admin_service::AdminService;
use anyhow::anyhow;
use chrono::{DateTime, Duration, TimeZone, Utc};
use chrono::{DateTime, Duration, Utc};
pub use client::{Client, Settings as ClientSettings};
pub use gateway_service::GatewayService;
pub use org_service::OrgService;
Expand Down Expand Up @@ -71,18 +70,12 @@ pub struct EpochPeriod {
pub period: Range<DateTime<Utc>>,
}

impl TryFrom<u64> for EpochPeriod {
type Error = anyhow::Error;

fn try_from(next_reward_epoch: u64) -> anyhow::Result<Self> {
let start_time = Utc
.timestamp_opt(0, 0)
.single()
.ok_or_else(|| anyhow!("Failed to get Unix epoch start time"))?
+ Duration::days(next_reward_epoch as i64);
impl From<u64> for EpochPeriod {
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);
Ok(EpochPeriod {
EpochPeriod {
period: start_time..end_time,
})
}
}
}
22 changes: 13 additions & 9 deletions iot_config/src/sub_dao_epoch_reward_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::Range;

#[derive(Clone, Debug)]
pub struct ResolvedSubDaoEpochRewardInfo {
pub epoch: u64,
pub epoch_day: u64,
pub epoch_address: String,
pub sub_dao_address: String,
pub epoch_period: Range<DateTime<Utc>>,
Expand All @@ -25,30 +25,34 @@ pub struct RawSubDaoEpochRewardInfo {
rewards_issued_at: DateTime<Utc>,
}

impl TryFrom<RawSubDaoEpochRewardInfo> for SubDaoEpochRewardInfoProto {
type Error = anyhow::Error;
#[derive(thiserror::Error, Debug)]
pub enum SubDaoRewardInfoParseError {
#[error("file_store: {0}")]
FileStore(#[from] file_store::Error),
}

fn try_from(info: RawSubDaoEpochRewardInfo) -> Result<Self, Self::Error> {
Ok(Self {
impl From<RawSubDaoEpochRewardInfo> for SubDaoEpochRewardInfoProto {
fn from(info: RawSubDaoEpochRewardInfo) -> Self {
Self {
epoch: info.epoch,
epoch_address: info.epoch_address,
sub_dao_address: info.sub_dao_address,
rewards_issued: info.rewards_issued,
delegation_rewards_issued: info.delegation_rewards_issued,
rewards_issued_at: info.rewards_issued_at.encode_timestamp(),
})
}
}
}

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

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

Ok(Self {
epoch: info.epoch,
epoch_day: info.epoch,
epoch_address: info.epoch_address,
sub_dao_address: info.sub_dao_address,
epoch_period: epoch_period.period,
Expand Down
4 changes: 1 addition & 3 deletions iot_config/src/sub_dao_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ impl sub_dao::sub_dao_server::SubDao for SubDaoService {
Err(Status::not_found(epoch.to_string()))
},
|info| {
let info = info.try_into().map_err(|_| {
Status::internal("error serializing sub_dao epoch reward info")
})?;
let info = info.into();
let mut res = SubDaoEpochRewardInfoResV1 {
info: Some(info),
timestamp: Utc::now().encode_timestamp(),
Expand Down
Loading

0 comments on commit 21ab8c7

Please sign in to comment.