From 2ca12e41de740c578476d2b8ebe9b0cfa1287012 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Thu, 1 Aug 2024 13:18:56 +0300 Subject: [PATCH] farm supply for week fix. incl claimBoosted aggr --- dex/farm-with-locked-rewards/src/lib.rs | 6 ++ dex/farm/src/lib.rs | 5 ++ dex/farm/tests/total_farm_position_test.rs | 58 +++++++++++++++++++ .../src/claim_only_boosted_staking_rewards.rs | 8 +++ .../src/claim_stake_farm_rewards.rs | 4 +- 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 91144b387..84d7dc7f4 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -237,7 +237,13 @@ pub trait Farm: ); } + let mut storage_cache = StorageCache::new(self); + NoMintWrapper::::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, diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 08d447830..211b05e02 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -228,10 +228,15 @@ pub trait Farm: ); } + let mut storage_cache = StorageCache::new(self); + Wrapper::::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 diff --git a/dex/farm/tests/total_farm_position_test.rs b/dex/farm/tests/total_farm_position_test.rs index b7ca1be7b..e0819d411 100644 --- a/dex/farm/tests/total_farm_position_test.rs +++ b/dex/farm/tests/total_farm_position_test.rs @@ -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), + ); +} diff --git a/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs b/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs index a1843a1f6..79d433dfe 100644 --- a/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs +++ b/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs @@ -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!(); @@ -38,10 +41,15 @@ pub trait ClaimOnlyBoostedStakingRewardsModule: ); } + let mut storage_cache = StorageCache::new(self); + FarmStakingWrapper::::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 diff --git a/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs b/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs index 14aa23da7..e2ba49fe4 100644 --- a/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs +++ b/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs @@ -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(