Skip to content

Commit

Permalink
even more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 30, 2024
1 parent 89abd34 commit 3636f8a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,11 @@ pub trait CustomRewardLogicModule:
current_week: Week,
min_timestamp: Timestamp,
claim_progress: &mut ClaimProgress<Self::Api>,
) -> Week {
) {
claim_progress.last_claim_timestamp = min_timestamp;

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

claim_progress.week - 1
} else {
claim_progress.week
}
}

Expand Down
16 changes: 8 additions & 8 deletions energy-integration/farm-boosted-yields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where
let min_timestamp = core::cmp::min(current_timestamp, week_timestamps.end);

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

return user_rewards;
}
Expand All @@ -135,14 +135,14 @@ where
if energy_amount < factors.min_energy_amount
|| self.user_farm_amount < factors.min_farm_amount
{
let _ = sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);
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() {
let _ = sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);
sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);

return user_rewards;
}
Expand All @@ -155,7 +155,7 @@ where

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

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

return user_rewards;
}

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

return user_rewards;
}

let prev_week = sc.advance_week_if_needed(current_week, min_timestamp, claim_progress);
sc.remaining_boosted_rewards_to_distribute(prev_week)
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
1 change: 1 addition & 0 deletions farm-staking/farm-staking/src/claim_stake_farm_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub trait ClaimStakeFarmRewardsModule:
opt_new_farming_amount: Option<BigUint>,
) -> ClaimRewardsResultType<Self::Api> {
self.migrate_old_farm_positions(&original_caller);

let payment = self.call_value().single_esdt();
let mut claim_result = self
.claim_rewards_base_no_farm_token_mint::<FarmStakingWrapper<Self>>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait CompoundStakeFarmRewardsModule:
fn compound_rewards(&self) -> EsdtTokenPayment {
let caller = self.blockchain().get_caller();
self.migrate_old_farm_positions(&caller);

let payments = self.get_non_empty_payments();
let compound_result =
self.compound_rewards_base::<FarmStakingWrapper<Self>>(caller.clone(), payments);
Expand Down
1 change: 1 addition & 0 deletions farm-staking/farm-staking/src/stake_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub trait StakeFarmModule:
) -> EnterFarmResultType<Self::Api> {
let caller = self.blockchain().get_caller();
self.migrate_old_farm_positions(&original_caller);

let boosted_rewards = self.claim_only_boosted_payment(&original_caller);
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);
Expand Down
103 changes: 77 additions & 26 deletions farm-staking/farm-staking/tests/farm_staking_energy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ use multiversx_sc_scenario::{

#[test]
fn farm_staking_with_energy_setup_test() {
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand All @@ -26,8 +29,12 @@ fn farm_staking_with_energy_setup_test() {
#[test]
fn farm_staking_boosted_rewards_no_energy_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -72,8 +79,12 @@ fn farm_staking_boosted_rewards_no_energy_test() {
#[test]
fn farm_staking_other_user_enter_negative_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();
let rand_user = fs_setup.b_mock.create_user_account(&rust_biguint!(0));
Expand Down Expand Up @@ -118,8 +129,12 @@ fn farm_staking_other_user_enter_negative_test() {
#[test]
fn farm_staking_boosted_rewards_with_energy_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();
let user_address2 = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -343,8 +358,12 @@ fn farm_staking_boosted_rewards_with_energy_test() {
#[test]
fn farm_staking_partial_position_handling_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -505,8 +524,12 @@ fn farm_staking_partial_position_handling_test() {
#[test]
fn farm_staking_claim_boosted_rewards_for_user_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -619,8 +642,12 @@ fn farm_staking_claim_boosted_rewards_for_user_test() {
#[test]
fn farm_staking_full_position_boosted_rewards_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -734,8 +761,12 @@ fn farm_staking_full_position_boosted_rewards_test() {
#[test]
fn position_owner_change_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let first_user = fs_setup.user_address.clone();
let second_user = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -1026,8 +1057,12 @@ fn position_owner_change_test() {
#[test]
fn farm_staking_farm_position_migration_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user = fs_setup.user_address.clone();

Expand Down Expand Up @@ -1120,8 +1155,12 @@ fn farm_staking_farm_position_migration_test() {
#[test]
fn boosted_rewards_config_change_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let first_user = fs_setup.user_address.clone();
let second_user = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -1430,8 +1469,12 @@ fn boosted_rewards_config_change_test() {
#[test]
fn claim_only_boosted_rewards_per_week_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down Expand Up @@ -1525,8 +1568,12 @@ fn claim_only_boosted_rewards_per_week_test() {
#[test]
fn claim_rewards_per_week_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down Expand Up @@ -1619,8 +1666,12 @@ fn claim_rewards_per_week_test() {
#[test]
fn claim_boosted_rewards_with_zero_position_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down

0 comments on commit 3636f8a

Please sign in to comment.