Skip to content

Commit

Permalink
change impl... again
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 31, 2024
1 parent a9b52d7 commit 27a7b86
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ pub trait WeeklyRewardsSplittingTraitsModule {
}
}

claim_progress.advance_week();

user_rewards
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use week_timekeeping::{Week, EPOCHS_IN_WEEK};
pub struct ClaimProgress<M: ManagedTypeApi> {
pub energy: Energy<M>,
pub week: Week,
pub last_claim_timestamp: Timestamp,
pub enter_timestamp: Timestamp,
}

impl<M: ManagedTypeApi> TopDecode for ClaimProgress<M> {
Expand All @@ -32,7 +32,7 @@ impl<M: ManagedTypeApi> TopDecode for ClaimProgress<M> {
let mut input_nested = input.into_nested_buffer();
let energy = Energy::dep_decode(&mut input_nested)?;
let week = Week::dep_decode(&mut input_nested)?;
let last_claim_timestamp = if !input_nested.is_depleted() {
let enter_timestamp = if !input_nested.is_depleted() {
Timestamp::dep_decode(&mut input_nested)?
} else {
0
Expand All @@ -45,7 +45,7 @@ impl<M: ManagedTypeApi> TopDecode for ClaimProgress<M> {
Result::Ok(ClaimProgress {
energy,
week,
last_claim_timestamp,
enter_timestamp,
})
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ pub trait WeeklyRewardsSplittingModule:
ClaimProgress {
energy: current_user_energy.clone(),
week: current_week,
last_claim_timestamp: self.blockchain().get_block_timestamp(),
enter_timestamp: self.blockchain().get_block_timestamp(),
}
};

Expand All @@ -107,14 +107,13 @@ pub trait WeeklyRewardsSplittingModule:
opt_progress_for_energy_update,
);

let mut all_rewards = ManagedVec::new();

let total_weeks_to_claim = current_week - claim_progress.week;
if total_weeks_to_claim > USER_MAX_CLAIM_WEEKS {
let extra_weeks = total_weeks_to_claim - USER_MAX_CLAIM_WEEKS;
claim_progress.advance_multiple_weeks(extra_weeks);
}

let mut all_rewards = ManagedVec::new();
let weeks_to_claim = core::cmp::min(total_weeks_to_claim, USER_MAX_CLAIM_WEEKS);
for _ in 0..weeks_to_claim {
let rewards_for_week = self.claim_single(wrapper, &mut claim_progress);
Expand Down Expand Up @@ -148,8 +147,11 @@ pub trait WeeklyRewardsSplittingModule:
claim_progress: &mut ClaimProgress<Self::Api>,
) -> PaymentsVec<Self::Api> {
let total_energy = self.total_energy_for_week(claim_progress.week).get();
let user_rewards = wrapper.get_user_rewards_for_week(self, claim_progress, &total_energy);

claim_progress.advance_week();

wrapper.get_user_rewards_for_week(self, claim_progress, &total_energy)
user_rewards
}

#[view(getLastActiveWeekForUser)]
Expand Down Expand Up @@ -212,7 +214,7 @@ mod tests {
ClaimProgress {
energy: Energy::new(BigInt::<DebugApi>::zero(), 10, managed_biguint!(20)),
week: 2,
last_claim_timestamp: 0
enter_timestamp: 0,
}
);
}
Expand All @@ -224,7 +226,7 @@ mod tests {
let new_progress = ClaimProgress {
energy: Energy::new(BigInt::<DebugApi>::zero(), 10, managed_biguint!(20)),
week: 2,
last_claim_timestamp: 5,
enter_timestamp: 0,
};
let mut new_progress_encoded = ManagedBuffer::<DebugApi>::new();
let _ = new_progress.top_encode(&mut new_progress_encoded);
Expand All @@ -234,7 +236,7 @@ mod tests {
ClaimProgress {
energy: Energy::new(BigInt::<DebugApi>::zero(), 10, managed_biguint!(20)),
week: 2,
last_claim_timestamp: 5
enter_timestamp: 0,
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub trait UpdateClaimProgressEnergyModule:
let current_user_energy = self.get_energy_entry(caller);

let progress_mapper = self.current_claim_progress(caller);
let (last_claim_timestamp, opt_progress_for_update) = if !progress_mapper.is_empty() {
let (enter_timestamp, opt_progress_for_update) = if !progress_mapper.is_empty() {
let progress = progress_mapper.get();

(progress.last_claim_timestamp, Some(progress))
(progress.enter_timestamp, Some(progress))
} else {
(self.blockchain().get_block_epoch(), None)
(self.blockchain().get_block_timestamp(), None)
};
self.update_user_energy_for_current_week(
caller,
Expand All @@ -52,7 +52,7 @@ pub trait UpdateClaimProgressEnergyModule:
progress_mapper.set(&ClaimProgress {
week: current_week,
energy: current_user_energy,
last_claim_timestamp,
enter_timestamp,
});
} else {
progress_mapper.clear();
Expand Down
52 changes: 8 additions & 44 deletions energy-integration/farm-boosted-yields/src/custom_reward_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,47 +138,25 @@ pub trait CustomRewardLogicModule:
&self,
user_reward: BigUint,
week_timestamps: &WeekTimestamps,
claim_progress: &mut ClaimProgress<Self::Api>,
claim_progress: &ClaimProgress<Self::Api>,
) -> BigUint {
let current_timestamp = self.blockchain().get_block_timestamp();
let min_timestamp = core::cmp::min(current_timestamp, week_timestamps.end);
if !(claim_progress.last_claim_timestamp >= week_timestamps.start
&& claim_progress.last_claim_timestamp < week_timestamps.end)
if !(claim_progress.enter_timestamp >= week_timestamps.start
&& claim_progress.enter_timestamp < week_timestamps.end)
{
claim_progress.last_claim_timestamp = min_timestamp;

return user_reward;
}

// last claim - 25% of week, current time - 90% of week => give 65% of rewards
// Using u128 just for extra safety. It's not technically needed.
let last_claim_timestamp_percent_of_week = linear_interpolation::<Self::Api, _>(
// Example: user entered at 25% of week, so give only 75% of rewards
let enter_timestamp_percent_of_week = linear_interpolation::<Self::Api, _>(
week_timestamps.start as u128,
week_timestamps.end as u128,
claim_progress.last_claim_timestamp as u128,
claim_progress.enter_timestamp as u128,
0,
MAX_PERCENT as u128,
);
let current_timestamp_percent_of_week = if min_timestamp != week_timestamps.end {
linear_interpolation::<Self::Api, _>(
week_timestamps.start as u128,
week_timestamps.end as u128,
min_timestamp as u128,
0,
MAX_PERCENT as u128,
)
} else {
MAX_PERCENT as u128 // do less math
};
let percent_leftover = MAX_PERCENT as u128 - enter_timestamp_percent_of_week;

claim_progress.last_claim_timestamp = min_timestamp;

if last_claim_timestamp_percent_of_week >= current_timestamp_percent_of_week {
return user_reward;
}

let percent_diff = current_timestamp_percent_of_week - last_claim_timestamp_percent_of_week;
user_reward * BigUint::from(percent_diff) / MAX_PERCENT
user_reward * BigUint::from(percent_leftover) / MAX_PERCENT
}

fn get_week_start_and_end_timestamp(&self, week: Week) -> WeekTimestamps {
Expand All @@ -201,20 +179,6 @@ pub trait CustomRewardLogicModule:
}
}

/// Returns the previous week
fn advance_week_if_needed(
&self,
current_week: Week,
min_timestamp: Timestamp,
claim_progress: &mut ClaimProgress<Self::Api>,
) {
claim_progress.last_claim_timestamp = min_timestamp;

if claim_progress.week != current_week - 1 {
claim_progress.advance_week();
}
}

#[inline]
fn update_start_of_epoch_timestamp(&self) {
let _ = self.get_start_of_epoch_timestamp();
Expand Down
20 changes: 1 addition & 19 deletions energy-integration/farm-boosted-yields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,7 @@ where
let mut user_rewards = ManagedVec::new();
let energy_amount = claim_progress.energy.get_energy_amount();
let farm_supply_for_week = sc.farm_supply_for_week(claim_progress.week).get();

let current_week = sc.get_current_week();
let week_timestamps = sc.get_week_start_and_end_timestamp(claim_progress.week + 1);
let current_timestamp = sc.blockchain().get_block_timestamp();
let min_timestamp = core::cmp::min(current_timestamp, week_timestamps.end);

if total_energy == &0 || farm_supply_for_week == 0 {
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

Expand All @@ -135,15 +127,11 @@ where
if energy_amount < factors.min_energy_amount
|| self.user_farm_amount < factors.min_farm_amount
{
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

let total_rewards = self.collect_and_get_rewards_for_week(sc, claim_progress.week);
if total_rewards.is_empty() {
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

Expand All @@ -155,8 +143,6 @@ where

let weekly_reward = total_rewards.get(0);
if weekly_reward.amount == 0 {
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

Expand All @@ -169,22 +155,18 @@ where
total_energy,
});
if user_reward == 0 {
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

let week_timestamps = sc.get_week_start_and_end_timestamp(claim_progress.week + 1);
let new_user_reward =
sc.limit_boosted_rewards_by_claim_time(user_reward, &week_timestamps, claim_progress);
if new_user_reward == 0 {
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}

sc.remaining_boosted_rewards_to_distribute(claim_progress.week)
.update(|amount| *amount -= &new_user_reward);
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

user_rewards.push(EsdtTokenPayment::new(
weekly_reward.token_identifier,
Expand Down
22 changes: 11 additions & 11 deletions energy-integration/fees-collector/tests/fees_collector_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn claim_first_week_test() {
ClaimProgress {
energy: first_user_energy,
week: 1,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
assert_eq!(
Expand All @@ -100,7 +100,7 @@ fn claim_first_week_test() {
ClaimProgress {
energy: second_user_energy,
week: 1,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -149,7 +149,7 @@ fn claim_first_week_test() {
ClaimProgress {
energy: first_user_energy,
week: 1,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
assert_eq!(
Expand All @@ -158,7 +158,7 @@ fn claim_first_week_test() {
ClaimProgress {
energy: second_user_energy,
week: 1,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -267,7 +267,7 @@ fn claim_after_dex_inactive_test() {
ClaimProgress {
energy: first_user_energy,
week: 4,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -391,7 +391,7 @@ fn claim_second_week_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -445,7 +445,7 @@ fn claim_second_week_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -598,7 +598,7 @@ fn claim_for_other_user_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -678,7 +678,7 @@ fn claim_inactive_week_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -1234,7 +1234,7 @@ fn claim_locked_rewards_with_energy_update_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down Expand Up @@ -1314,7 +1314,7 @@ fn claim_locked_rewards_with_energy_update_test() {
ClaimProgress {
energy: first_user_energy,
week: 2,
last_claim_timestamp: INIT_TIMESTAMP
enter_timestamp: INIT_TIMESTAMP
}
);
})
Expand Down

0 comments on commit 27a7b86

Please sign in to comment.