Skip to content

Commit

Permalink
farm supply for week fix. incl claimBoosted aggr
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Aug 1, 2024
1 parent 6cd702c commit 2ca12e4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
6 changes: 6 additions & 0 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ pub trait Farm:
);
}

let mut storage_cache = StorageCache::new(self);
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
Expand Down
5 changes: 5 additions & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ pub trait Farm:
);
}

let mut storage_cache = StorageCache::new(self);
Wrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

self.send_payment_non_zero(user, &boosted_rewards_payment);

boosted_rewards_payment
Expand Down
58 changes: 58 additions & 0 deletions dex/farm/tests/total_farm_position_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,61 @@ fn total_farm_position_through_simple_lock_test() {
&rust_biguint!(first_received_reward_amt),
);
}

#[test]
fn claim_only_boosted_rewards_per_week_test() {
DebugApi::dummy();
let mut farm_setup = MultiUserFarmSetup::new(
farm::contract_obj,
energy_factory_mock::contract_obj,
energy_update::contract_obj,
);

farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
farm_setup.set_boosted_yields_factors();
farm_setup.b_mock.set_block_epoch(2);

let temp_user = farm_setup.third_user.clone();

// first user enter farm
let farm_in_amount = 100_000_000;
let first_user = farm_setup.first_user.clone();
farm_setup.set_user_energy(&first_user, 1_000, 2, 1);
farm_setup.enter_farm(&first_user, farm_in_amount);

farm_setup.check_farm_token_supply(farm_in_amount);

farm_setup.b_mock.set_block_nonce(10);
farm_setup.b_mock.set_block_epoch(6);
farm_setup.set_user_energy(&first_user, 1_000, 6, 1);
farm_setup.set_user_energy(&temp_user, 1, 6, 1);
farm_setup.enter_farm(&temp_user, 1);
farm_setup.exit_farm(&temp_user, 2, 1);

// advance 1 week
farm_setup.set_user_energy(&first_user, 1_000, 13, 1);
farm_setup.b_mock.set_block_nonce(20);
farm_setup.b_mock.set_block_epoch(13);

let boosted_rewards = 2_500;
let second_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user);

assert_eq!(second_week_received_reward_amt, boosted_rewards);

// advance 1 week
farm_setup.set_user_energy(&first_user, 1_000, 15, 1);
farm_setup.b_mock.set_block_nonce(30);
farm_setup.b_mock.set_block_epoch(15);
let third_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user);

// Should be equal to half base generated rewards + full boosted generated rewards
assert_eq!(third_week_received_reward_amt, boosted_rewards);

farm_setup.b_mock.check_esdt_balance(
&first_user,
REWARD_TOKEN_ID,
&rust_biguint!(boosted_rewards * 2),
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use contexts::storage_cache::StorageCache;
use farm_base_impl::base_traits_impl::FarmContract;

use crate::base_impl_wrapper::FarmStakingWrapper;

multiversx_sc::imports!();
Expand Down Expand Up @@ -38,10 +41,15 @@ pub trait ClaimOnlyBoostedStakingRewardsModule:
);
}

let mut storage_cache = StorageCache::new(self);
FarmStakingWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

self.send_payment_non_zero(user, &boosted_rewards_payment);

boosted_rewards_payment
Expand Down
4 changes: 2 additions & 2 deletions farm-staking/farm-staking/src/claim_stake_farm_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ pub trait ClaimStakeFarmRewardsModule:

virtual_farm_token.payment.amount = new_amount.clone();
virtual_farm_token.attributes.current_farm_amount = new_amount;

self.set_farm_supply_for_current_week(&claim_result.storage_cache.farm_token_supply);
}

self.set_farm_supply_for_current_week(&claim_result.storage_cache.farm_token_supply);

self.update_energy_and_progress(&original_caller);

let new_farm_token_nonce = self.send().esdt_nft_create_compact(
Expand Down

0 comments on commit 2ca12e4

Please sign in to comment.