diff --git a/energy-integration/fees-collector/src/claim.rs b/energy-integration/fees-collector/src/claim.rs index 1b5884410..a4cd5320b 100644 --- a/energy-integration/fees-collector/src/claim.rs +++ b/energy-integration/fees-collector/src/claim.rs @@ -23,6 +23,7 @@ pub trait ClaimModule: + utils::UtilsModule + sc_whitelist_module::SCWhitelistModule + multiversx_sc_modules::only_admin::OnlyAdminModule + + crate::redistribute_rewards::RedistributeRewardsModule { #[endpoint(claimRewards)] fn claim_rewards_endpoint( diff --git a/energy-integration/fees-collector/src/fees_accumulation.rs b/energy-integration/fees-collector/src/fees_accumulation.rs index f15177c44..87865e96f 100644 --- a/energy-integration/fees-collector/src/fees_accumulation.rs +++ b/energy-integration/fees-collector/src/fees_accumulation.rs @@ -1,9 +1,7 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); -use common_types::PaymentsVec; use week_timekeeping::Week; -use weekly_rewards_splitting::USER_MAX_CLAIM_WEEKS; #[multiversx_sc::module] pub trait FeesAccumulationModule: @@ -49,74 +47,6 @@ pub trait FeesAccumulationModule: self.emit_deposit_swap_fees_event(&caller, current_week, &payment); } - #[only_admin] - #[endpoint(redistributeRewards)] - fn redistribute_rewards(&self, start_week: Week, end_week: Week) { - let collect_rewards_offset = USER_MAX_CLAIM_WEEKS + 1; - let current_week = self.get_current_week(); - require!( - current_week > collect_rewards_offset, - "Current week must be higher than the week offset" - ); - require!(start_week <= end_week, "Invalid week numbers"); - require!( - end_week <= current_week - collect_rewards_offset, - "Invalid end week" - ); - - let all_tokens = self.all_tokens().get(); - let mut all_rewards = PaymentsVec::new(); - for token_id in &all_tokens { - all_rewards.push(EsdtTokenPayment::new(token_id, 0, BigUint::zero())); - } - - self.accumulate_remaining_rewards(&mut all_rewards, &all_tokens, start_week, end_week); - - for reward_entry in &all_rewards { - if reward_entry.amount == 0 { - continue; - } - - self.accumulated_fees(current_week, &reward_entry.token_identifier) - .update(|acc_fees| *acc_fees += reward_entry.amount); - } - } - - fn accumulate_remaining_rewards( - &self, - all_rewards: &mut PaymentsVec, - all_tokens: &ManagedVec, - start_week: Week, - end_week: Week, - ) { - for week in start_week..=end_week { - self.accumulate_remaining_rewards_single_week(all_rewards, all_tokens, week); - } - } - - fn accumulate_remaining_rewards_single_week( - &self, - all_rewards: &mut PaymentsVec, - all_tokens: &ManagedVec, - week: Week, - ) { - let remaining_rewards = self.remaining_rewards(week).take(); - for rem_rew_entry in &remaining_rewards { - if rem_rew_entry.amount == 0 { - continue; - } - - let opt_index = all_tokens.find(&rem_rew_entry.token_identifier); - if opt_index.is_none() { - continue; - } - - let index = unsafe { opt_index.unwrap_unchecked() }; - let mut rew_entry = all_rewards.get_mut(index); - rew_entry.amount += rem_rew_entry.amount; - } - } - fn get_and_clear_accumulated_fees( &self, week: Week, @@ -133,8 +63,4 @@ pub trait FeesAccumulationModule: #[view(getAccumulatedFees)] #[storage_mapper("accumulatedFees")] fn accumulated_fees(&self, week: Week, token: &TokenIdentifier) -> SingleValueMapper; - - #[view(getRemainingRewards)] - #[storage_mapper("remainingRewards")] - fn remaining_rewards(&self, week: Week) -> SingleValueMapper>; } diff --git a/energy-integration/fees-collector/src/lib.rs b/energy-integration/fees-collector/src/lib.rs index ee9bc0a82..2ee36602c 100644 --- a/energy-integration/fees-collector/src/lib.rs +++ b/energy-integration/fees-collector/src/lib.rs @@ -7,6 +7,7 @@ pub mod claim; pub mod config; pub mod events; pub mod fees_accumulation; +pub mod redistribute_rewards; #[multiversx_sc::contract] pub trait FeesCollector: @@ -27,6 +28,7 @@ pub trait FeesCollector: + sc_whitelist_module::SCWhitelistModule + multiversx_sc_modules::only_admin::OnlyAdminModule + claim::ClaimModule + + redistribute_rewards::RedistributeRewardsModule { #[init] fn init( diff --git a/energy-integration/fees-collector/src/redistribute_rewards.rs b/energy-integration/fees-collector/src/redistribute_rewards.rs new file mode 100644 index 000000000..28259de17 --- /dev/null +++ b/energy-integration/fees-collector/src/redistribute_rewards.rs @@ -0,0 +1,75 @@ +use common_types::{PaymentsVec, Week}; +use weekly_rewards_splitting::USER_MAX_CLAIM_WEEKS; + +multiversx_sc::imports!(); + +#[multiversx_sc::module] +pub trait RedistributeRewardsModule: + crate::fees_accumulation::FeesAccumulationModule + + crate::config::ConfigModule + + crate::events::FeesCollectorEventsModule + + week_timekeeping::WeekTimekeepingModule + + multiversx_sc_modules::only_admin::OnlyAdminModule +{ + #[only_admin] + #[endpoint(redistributeRewards)] + fn redistribute_rewards(&self, start_week: Week, end_week: Week) { + let collect_rewards_offset = USER_MAX_CLAIM_WEEKS + 1; + let current_week = self.get_current_week(); + require!( + current_week > collect_rewards_offset, + "Current week must be higher than the week offset" + ); + require!(start_week <= end_week, "Invalid week numbers"); + require!( + end_week <= current_week - collect_rewards_offset, + "Invalid end week" + ); + + let all_tokens = self.all_tokens().get(); + let mut all_rewards = PaymentsVec::new(); + for token_id in &all_tokens { + all_rewards.push(EsdtTokenPayment::new(token_id, 0, BigUint::zero())); + } + + for week in start_week..=end_week { + self.accumulate_remaining_rewards_single_week(&mut all_rewards, &all_tokens, week); + } + + for reward_entry in &all_rewards { + if reward_entry.amount == 0 { + continue; + } + + self.accumulated_fees(current_week, &reward_entry.token_identifier) + .update(|acc_fees| *acc_fees += reward_entry.amount); + } + } + + fn accumulate_remaining_rewards_single_week( + &self, + all_rewards: &mut PaymentsVec, + all_tokens: &ManagedVec, + week: Week, + ) { + let remaining_rewards = self.remaining_rewards(week).take(); + for rem_rew_entry in &remaining_rewards { + if rem_rew_entry.amount == 0 { + continue; + } + + let opt_index = all_tokens.find(&rem_rew_entry.token_identifier); + if opt_index.is_none() { + continue; + } + + let index = unsafe { opt_index.unwrap_unchecked() }; + let mut rew_entry = all_rewards.get_mut(index); + rew_entry.amount += rem_rew_entry.amount; + } + } + + #[view(getRemainingRewards)] + #[storage_mapper("remainingRewards")] + fn remaining_rewards(&self, week: Week) -> SingleValueMapper>; +} diff --git a/energy-integration/fees-collector/wasm/src/lib.rs b/energy-integration/fees-collector/wasm/src/lib.rs index 941bfb1fa..4aab5f1d3 100644 --- a/energy-integration/fees-collector/wasm/src/lib.rs +++ b/energy-integration/fees-collector/wasm/src/lib.rs @@ -37,9 +37,7 @@ multiversx_sc_wasm_adapter::endpoints! { updateEnergyForUser => update_energy_for_user getCurrentClaimProgress => current_claim_progress depositSwapFees => deposit_swap_fees - redistributeRewards => redistribute_rewards getAccumulatedFees => accumulated_fees - getRemainingRewards => remaining_rewards setLockedTokensPerBlock => set_locked_tokens_per_block getLastLockedTokensAddWeek => last_locked_token_add_week getLockedTokensPerBlock => locked_tokens_per_block @@ -63,6 +61,8 @@ multiversx_sc_wasm_adapter::endpoints! { getAdmins => admins claimRewards => claim_rewards_endpoint claimBoostedRewards => claim_boosted_rewards + redistributeRewards => redistribute_rewards + getRemainingRewards => remaining_rewards ) }