Skip to content

Commit

Permalink
clarify scalar values a bit more and update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Aug 2, 2024
1 parent 5b6093e commit 088b109
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
14 changes: 9 additions & 5 deletions contracts/distribution/dao-rewards-distributor/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn get_voting_power_at_block(
}

/// returns underlying scalar value for a given duration.
/// if the duration is in blocks, returns the block height.
/// if the duration is in time, returns the time in seconds.
/// if the duration is a block height, return the number of blocks.
/// if the duration is a time, return the number of seconds.
pub fn get_duration_scalar(duration: &Duration) -> u64 {
match duration {
Duration::Height(h) => *h,
Expand Down Expand Up @@ -73,9 +73,13 @@ pub(crate) fn scale_factor() -> Uint256 {
Uint256::from(10u8).pow(39)
}

/// Calculate the duration from start to end. If the end is at or before the
/// start, return 0. The first argument is end, and the second is start.
pub fn get_exp_diff(end: &Expiration, start: &Expiration) -> StdResult<u64> {
/// Calculate the duration scalar value from start to end. If the end is at or
/// before the start, return 0. The first argument is end, and the second is
/// start. If start and end are block heights, this returns the number of
/// blocks. If they are times, this returns the number of seconds. If both are
/// never, this returns 0. If start and end have different units, it errors as
/// that should not be possible.
pub fn get_exp_diff_scalar(end: &Expiration, start: &Expiration) -> StdResult<u64> {
match (end, start) {
(Expiration::AtHeight(end), Expiration::AtHeight(start)) => {
if end > start {
Expand Down
10 changes: 5 additions & 5 deletions contracts/distribution/dao-rewards-distributor/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use cosmwasm_std::{Addr, BlockInfo, Deps, DepsMut, Env, StdResult, Uint128, Uint

use crate::{
helpers::{
get_duration_scalar, get_exp_diff, get_prev_block_total_vp, get_voting_power_at_block,
scale_factor,
get_duration_scalar, get_exp_diff_scalar, get_prev_block_total_vp,
get_voting_power_at_block, scale_factor,
},
state::{DistributionState, EmissionRate, UserRewardState, DISTRIBUTIONS, USER_REWARDS},
ContractError,
Expand Down Expand Up @@ -98,15 +98,15 @@ pub fn get_active_total_earned_puvp(
// get the duration from the last time rewards were updated to the
// last time rewards were distributed. this will be 0 if the rewards
// were updated at or after the last time rewards were distributed.
let new_reward_distribution_duration: Uint128 = get_exp_diff(
let new_reward_distribution_duration_scalar: Uint128 = get_exp_diff_scalar(
&last_time_rewards_distributed,
&distribution.active_epoch.last_updated_total_earned_puvp,
)?
.into();

// no need to query total voting power and do math if distribution
// is already up to date.
if new_reward_distribution_duration.is_zero() {
if new_reward_distribution_duration_scalar.is_zero() {
return Ok(curr);
}

Expand All @@ -118,7 +118,7 @@ pub fn get_active_total_earned_puvp(
} else {
// count intervals of the rewards emission that have passed
// since the last update which need to be distributed
let complete_distribution_periods = new_reward_distribution_duration
let complete_distribution_periods = new_reward_distribution_duration_scalar
.checked_div(get_duration_scalar(&duration).into())?;

// It is impossible for this to overflow as total rewards can
Expand Down
15 changes: 6 additions & 9 deletions contracts/distribution/dao-rewards-distributor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cw_utils::Duration;
use std::{cmp::min, collections::HashMap};

use crate::{
helpers::{get_duration_scalar, get_exp_diff, get_prev_block_total_vp, scale_factor},
helpers::{get_duration_scalar, get_exp_diff_scalar, get_prev_block_total_vp, scale_factor},
rewards::get_active_total_earned_puvp,
ContractError,
};
Expand Down Expand Up @@ -70,7 +70,7 @@ impl EmissionRate {
EmissionRate::Linear {
amount, duration, ..
} => {
if *amount == Uint128::zero() {
if amount.is_zero() {
return Err(ContractError::InvalidEmissionRateFieldZero {
field: "amount".to_string(),
});
Expand Down Expand Up @@ -236,16 +236,13 @@ impl DistributionState {
EmissionRate::Linear {
amount, duration, ..
} => {
let epoch_duration =
get_exp_diff(&self.active_epoch.ends_at, &self.active_epoch.started_at)?;
let epoch_duration_scalar =
get_exp_diff_scalar(&self.active_epoch.ends_at, &self.active_epoch.started_at)?;

let emission_rate_duration_scalar = match duration {
Duration::Height(h) => h,
Duration::Time(t) => t,
};
let emission_rate_duration_scalar = get_duration_scalar(&duration);

amount
.checked_multiply_ratio(epoch_duration, emission_rate_duration_scalar)
.checked_multiply_ratio(epoch_duration_scalar, emission_rate_duration_scalar)
.map_err(|e| StdError::generic_err(e.to_string()))
}
}
Expand Down

0 comments on commit 088b109

Please sign in to comment.