diff --git a/energy-integration/common-modules/weekly-rewards-splitting/src/base_impl.rs b/energy-integration/common-modules/weekly-rewards-splitting/src/base_impl.rs index d0edff85a..35cf01435 100644 --- a/energy-integration/common-modules/weekly-rewards-splitting/src/base_impl.rs +++ b/energy-integration/common-modules/weekly-rewards-splitting/src/base_impl.rs @@ -73,8 +73,6 @@ pub trait WeeklyRewardsSplittingTraitsModule { } } - claim_progress.advance_week(); - user_rewards } diff --git a/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs b/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs index cf869d7cb..52c71d4dc 100644 --- a/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs +++ b/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs @@ -21,7 +21,7 @@ use week_timekeeping::{Week, EPOCHS_IN_WEEK}; pub struct ClaimProgress { pub energy: Energy, pub week: Week, - pub last_claim_timestamp: Timestamp, + pub enter_timestamp: Timestamp, } impl TopDecode for ClaimProgress { @@ -32,7 +32,7 @@ impl TopDecode for ClaimProgress { 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 @@ -45,7 +45,7 @@ impl TopDecode for ClaimProgress { Result::Ok(ClaimProgress { energy, week, - last_claim_timestamp, + enter_timestamp, }) } } @@ -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(), } }; @@ -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); @@ -148,8 +147,11 @@ pub trait WeeklyRewardsSplittingModule: claim_progress: &mut ClaimProgress, ) -> PaymentsVec { 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)] @@ -212,7 +214,7 @@ mod tests { ClaimProgress { energy: Energy::new(BigInt::::zero(), 10, managed_biguint!(20)), week: 2, - last_claim_timestamp: 0 + enter_timestamp: 0, } ); } @@ -224,7 +226,7 @@ mod tests { let new_progress = ClaimProgress { energy: Energy::new(BigInt::::zero(), 10, managed_biguint!(20)), week: 2, - last_claim_timestamp: 5, + enter_timestamp: 0, }; let mut new_progress_encoded = ManagedBuffer::::new(); let _ = new_progress.top_encode(&mut new_progress_encoded); @@ -234,7 +236,7 @@ mod tests { ClaimProgress { energy: Energy::new(BigInt::::zero(), 10, managed_biguint!(20)), week: 2, - last_claim_timestamp: 5 + enter_timestamp: 0, } ); } diff --git a/energy-integration/common-modules/weekly-rewards-splitting/src/update_claim_progress_energy.rs b/energy-integration/common-modules/weekly-rewards-splitting/src/update_claim_progress_energy.rs index fa02680eb..df7a64ba6 100644 --- a/energy-integration/common-modules/weekly-rewards-splitting/src/update_claim_progress_energy.rs +++ b/energy-integration/common-modules/weekly-rewards-splitting/src/update_claim_progress_energy.rs @@ -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, @@ -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(); diff --git a/energy-integration/farm-boosted-yields/src/custom_reward_logic.rs b/energy-integration/farm-boosted-yields/src/custom_reward_logic.rs index 206fa025c..8af5ba985 100644 --- a/energy-integration/farm-boosted-yields/src/custom_reward_logic.rs +++ b/energy-integration/farm-boosted-yields/src/custom_reward_logic.rs @@ -138,47 +138,25 @@ pub trait CustomRewardLogicModule: &self, user_reward: BigUint, week_timestamps: &WeekTimestamps, - claim_progress: &mut ClaimProgress, + claim_progress: &ClaimProgress, ) -> 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::( + // Example: user entered at 25% of week, so give only 75% of rewards + let enter_timestamp_percent_of_week = linear_interpolation::( 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::( - 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 { @@ -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, - ) { - 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(); diff --git a/energy-integration/farm-boosted-yields/src/lib.rs b/energy-integration/farm-boosted-yields/src/lib.rs index 50ea913cd..10725e790 100644 --- a/energy-integration/farm-boosted-yields/src/lib.rs +++ b/energy-integration/farm-boosted-yields/src/lib.rs @@ -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; } @@ -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; } @@ -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; } @@ -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, diff --git a/energy-integration/fees-collector/tests/fees_collector_rust_test.rs b/energy-integration/fees-collector/tests/fees_collector_rust_test.rs index dbe3b0be6..08d4d9165 100644 --- a/energy-integration/fees-collector/tests/fees_collector_rust_test.rs +++ b/energy-integration/fees-collector/tests/fees_collector_rust_test.rs @@ -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!( @@ -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 } ); }) @@ -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!( @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); }) @@ -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 } ); })