Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Nov 15, 2024
1 parent 0555884 commit 0c41aa5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
1 change: 1 addition & 0 deletions energy-integration/fees-collector/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
74 changes: 0 additions & 74 deletions energy-integration/fees-collector/src/fees_accumulation.rs
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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<Self::Api>,
all_tokens: &ManagedVec<TokenIdentifier>,
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<Self::Api>,
all_tokens: &ManagedVec<TokenIdentifier>,
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,
Expand All @@ -133,8 +63,4 @@ pub trait FeesAccumulationModule:
#[view(getAccumulatedFees)]
#[storage_mapper("accumulatedFees")]
fn accumulated_fees(&self, week: Week, token: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[view(getRemainingRewards)]
#[storage_mapper("remainingRewards")]
fn remaining_rewards(&self, week: Week) -> SingleValueMapper<PaymentsVec<Self::Api>>;
}
2 changes: 2 additions & 0 deletions energy-integration/fees-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
75 changes: 75 additions & 0 deletions energy-integration/fees-collector/src/redistribute_rewards.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Api>,
all_tokens: &ManagedVec<TokenIdentifier>,
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<PaymentsVec<Self::Api>>;
}
4 changes: 2 additions & 2 deletions energy-integration/fees-collector/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
}

Expand Down

0 comments on commit 0c41aa5

Please sign in to comment.