Skip to content

Commit

Permalink
remove feature + test
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Nov 15, 2024
1 parent 696428e commit f3f689a
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 83 deletions.
7 changes: 1 addition & 6 deletions energy-integration/fees-collector/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ where
let total_rewards = self.collect_and_get_rewards_for_week(sc, week);
let remaining_rewards_mapper = sc.remaining_rewards(week);
let mut remaining_rewards = remaining_rewards_mapper.get();
let mut update_storage = false;

for (i, weekly_reward) in total_rewards.iter().enumerate() {
let reward_amount = weekly_reward.amount * energy_amount / total_energy;
if reward_amount == 0 {
Expand All @@ -157,7 +155,6 @@ where

let mut rem_rew_entry = remaining_rewards.get_mut(i);
rem_rew_entry.amount -= &reward_amount;
update_storage = true;

user_rewards.push(EsdtTokenPayment::new(
weekly_reward.token_identifier,
Expand All @@ -166,9 +163,7 @@ where
));
}

if update_storage {
remaining_rewards_mapper.set(remaining_rewards);
}
remaining_rewards_mapper.set(remaining_rewards);

user_rewards
}
Expand Down
73 changes: 1 addition & 72 deletions energy-integration/fees-collector/src/redistribute_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,22 @@ use weekly_rewards_splitting::USER_MAX_CLAIM_WEEKS;

multiversx_sc::imports!();

const INITIAL_REW_DIST: bool = true;
const INITIAL_REW_NOT_DIST: bool = false;
static INVALID_OFFSET_ERR_MSG: &[u8] = b"Current week must be higher than the week offset";

#[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
+ crate::additional_locked_tokens::AdditionalLockedTokensModule
{
#[only_admin]
#[endpoint(redistributeInitialRewards)]
fn redistribute_initial_rewards(&self) {
require!(
self.redistributed_initial_rewards().get() == INITIAL_REW_NOT_DIST,
"Initial rewards already distributed"
);

let collect_rewards_offset = USER_MAX_CLAIM_WEEKS + 1;
let current_week = self.get_current_week();
require!(
current_week > collect_rewards_offset,
INVALID_OFFSET_ERR_MSG
);

let all_rem_rewards = self.redist_initial_rew(current_week, collect_rewards_offset);
for reward_entry in &all_rem_rewards {
self.accumulated_fees(current_week, &reward_entry.token)
.update(|acc_fees| *acc_fees += reward_entry.amount);
}

self.redistributed_initial_rewards().set(INITIAL_REW_DIST);
}

#[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,
INVALID_OFFSET_ERR_MSG
"Current week must be higher than the week offset"
);
require!(start_week <= end_week, "Invalid week numbers");
require!(
Expand Down Expand Up @@ -98,48 +69,6 @@ pub trait RedistributeRewardsModule:
}
}

fn redist_initial_rew(
&self,
current_week: Week,
collect_rewards_offset: Week,
) -> ManagedVec<TokenAmountPair<Self::Api>> {
let all_tokens = self.all_tokens().get();
let locked_token_id = self.locked_token_id().get();
let start_week = current_week - collect_rewards_offset;
let end_week = current_week;

let mut all_rem_rewards = ManagedVec::new();
for token_id in &all_tokens {
if token_id == locked_token_id {
continue;
}

let mut token_balance = self
.blockchain()
.get_sc_balance(&EgldOrEsdtTokenIdentifier::esdt(token_id.clone()), 0);
if token_balance == 0 {
continue;
}

for week in start_week..=end_week {
let to_dist_week = self.accumulated_fees(week, &token_id).get();
token_balance -= to_dist_week;
}

if token_balance == 0 {
continue;
}

all_rem_rewards.push(TokenAmountPair::new(token_id, token_balance));
}

all_rem_rewards
}

#[view(wereInitialRewardsRedistributed)]
#[storage_mapper("redistributedInitialRewards")]
fn redistributed_initial_rewards(&self) -> SingleValueMapper<bool>;

#[view(getRemainingRewards)]
#[storage_mapper("remainingRewards")]
fn remaining_rewards(&self, week: Week) -> SingleValueMapper<PaymentsVec<Self::Api>>;
Expand Down
194 changes: 194 additions & 0 deletions energy-integration/fees-collector/tests/fees_collector_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod fees_collector_test_setup;
use energy_query::Energy;
use fees_collector::additional_locked_tokens::{AdditionalLockedTokensModule, BLOCKS_IN_WEEK};
use fees_collector::fees_accumulation::FeesAccumulationModule;
use fees_collector::redistribute_rewards::RedistributeRewardsModule;
use fees_collector_test_setup::*;
use multiversx_sc::types::{BigInt, EsdtTokenPayment, ManagedVec};
use multiversx_sc_scenario::{
Expand Down Expand Up @@ -1419,3 +1420,196 @@ fn additional_locked_tokens_test() {
})
.assert_ok();
}

#[test]
fn redistribute_rewards_test() {
let rust_zero = rust_biguint!(0);
let mut fc_setup =
FeesCollectorSetup::new(fees_collector::contract_obj, energy_factory::contract_obj);

let first_user = fc_setup.b_mock.create_user_account(&rust_zero);
let second_user = fc_setup.b_mock.create_user_account(&rust_zero);
let third_user = fc_setup.b_mock.create_user_account(&rust_zero);

fc_setup.set_energy(&first_user, 50, 3_000);
fc_setup.set_energy(&second_user, 50, 9_000);
fc_setup.set_energy(&third_user, 1, 1);

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.claim(&first_user).assert_ok();
fc_setup.claim(&second_user).assert_ok();
fc_setup.claim(&third_user).assert_ok();

// advance to week 2 (inactive week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

fc_setup
.b_mock
.execute_query(&fc_setup.fc_wrapper, |sc| {
let mut expected_total_rewards = ManagedVec::new();
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(FIRST_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE / 10),
));
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(SECOND_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE / 20),
));
assert_eq!(expected_total_rewards, sc.total_rewards_for_week(1).get());
})
.assert_ok();

// advance to week 3 (inactive week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 4 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 5 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 6 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 7 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 8 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 9 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// advance to week 10 (active week)
fc_setup.advance_week();

fc_setup
.deposit(FIRST_TOKEN_ID, USER_BALANCE / 10)
.assert_ok();
fc_setup
.deposit(SECOND_TOKEN_ID, USER_BALANCE / 20)
.assert_ok();

fc_setup.set_energy(&third_user, 1, 1);
fc_setup.claim(&third_user).assert_ok();

// redist rewards
fc_setup
.b_mock
.execute_tx(
&fc_setup.owner_address,
&fc_setup.fc_wrapper,
&rust_zero,
|sc| {
sc.redistribute_rewards(1, 5);

// Rewards were put in current_week storage (i.e. 10)

let first_token_balance = sc
.accumulated_fees(10, &managed_token_id!(FIRST_TOKEN_ID))
.get();
let second_token_balance = sc
.accumulated_fees(10, &managed_token_id!(SECOND_TOKEN_ID))
.get();

// i.e. 6 weeks worth of rewards minus what the third user claimed
assert_eq!(
first_token_balance,
managed_biguint!(599_952_417_140_485_515u64)
);
assert_eq!(
second_token_balance,
managed_biguint!(299_976_208_570_242_758)
);
},
)
.assert_ok();
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ where

b_mock
.execute_tx(&owner_address, &fc_wrapper, &rust_zero, |sc| {
let mut admins = MultiValueEncoded::new();
admins.push(managed_address!(&owner_address));

sc.init(
managed_token_id!(LOCKED_TOKEN_ID),
managed_address!(energy_factory_wrapper.address_ref()),
MultiValueEncoded::new(),
admins,
);

let _ = sc
Expand Down
6 changes: 2 additions & 4 deletions energy-integration/fees-collector/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 45
// Endpoints: 43
// Async Callback (empty): 1
// Total number of exported functions: 48
// Total number of exported functions: 46

#![no_std]

Expand Down Expand Up @@ -61,9 +61,7 @@ multiversx_sc_wasm_adapter::endpoints! {
getAdmins => admins
claimRewards => claim_rewards_endpoint
claimBoostedRewards => claim_boosted_rewards
redistributeInitialRewards => redistribute_initial_rewards
redistributeRewards => redistribute_rewards
wereInitialRewardsRedistributed => redistributed_initial_rewards
getRemainingRewards => remaining_rewards
)
}
Expand Down

0 comments on commit f3f689a

Please sign in to comment.