diff --git a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs index 70f4513b8..d86c3c685 100644 --- a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs +++ b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs @@ -63,19 +63,18 @@ pub trait FarmContract { ) -> BigUint<::Api> { let current_block_nonce = sc.blockchain().get_block_nonce(); let last_reward_nonce = sc.last_reward_block_nonce().get(); - if current_block_nonce > last_reward_nonce { - let to_mint = - Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce); - if to_mint != 0 { - Self::mint_rewards(sc, token_id, &to_mint); - } - - sc.last_reward_block_nonce().set(current_block_nonce); + if current_block_nonce <= last_reward_nonce { + return BigUint::zero(); + } - to_mint - } else { - BigUint::zero() + let to_mint = Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce); + if to_mint != 0 { + Self::mint_rewards(sc, token_id, &to_mint); } + + sc.last_reward_block_nonce().set(current_block_nonce); + + to_mint } fn generate_aggregated_rewards( diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 473fa77d2..8952f697c 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -8,6 +8,7 @@ multiversx_sc::derive_imports!(); use common_structs::FarmTokenAttributes; use contexts::storage_cache::StorageCache; use core::marker::PhantomData; +use week_timekeeping::Epoch; use farm::{ base_functions::{BaseFunctionsModule, ClaimRewardsResultType, DoubleMultiPayment, Wrapper}, @@ -57,6 +58,7 @@ pub trait Farm: division_safety_constant: BigUint, pair_contract_address: ManagedAddress, owner: ManagedAddress, + first_week_start_epoch: Epoch, admins: MultiValueEncoded, ) { self.base_farm_init( @@ -67,18 +69,18 @@ pub trait Farm: admins, ); - self.penalty_percent().set_if_empty(DEFAULT_PENALTY_PERCENT); - self.minimum_farming_epochs() - .set_if_empty(DEFAULT_MINUMUM_FARMING_EPOCHS); - self.burn_gas_limit().set_if_empty(DEFAULT_BURN_GAS_LIMIT); - self.pair_contract_address().set(&pair_contract_address); - let current_epoch = self.blockchain().get_block_epoch(); - self.first_week_start_epoch().set_if_empty(current_epoch); + require!( + first_week_start_epoch >= current_epoch, + "Invalid start epoch" + ); + self.first_week_start_epoch().set(first_week_start_epoch); - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); + self.penalty_percent().set(DEFAULT_PENALTY_PERCENT); + self.minimum_farming_epochs() + .set(DEFAULT_MINUMUM_FARMING_EPOCHS); + self.burn_gas_limit().set(DEFAULT_BURN_GAS_LIMIT); + self.pair_contract_address().set(&pair_contract_address); } #[endpoint] @@ -120,6 +122,13 @@ pub trait Farm: &self, opt_orig_caller: OptionalValue, ) -> ClaimRewardsResultType { + let current_epoch = self.blockchain().get_block_epoch(); + let first_week_start_epoch = self.first_week_start_epoch().get(); + require!( + first_week_start_epoch <= current_epoch, + "Cannot claim rewards yet" + ); + let caller = self.blockchain().get_caller(); let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); @@ -209,10 +218,14 @@ pub trait Farm: } #[endpoint(claimBoostedRewards)] - fn claim_boosted_rewards( - &self, - opt_user: OptionalValue, - ) -> EsdtTokenPayment { + fn claim_boosted_rewards(&self, opt_user: OptionalValue) -> EsdtTokenPayment { + let current_epoch = self.blockchain().get_block_epoch(); + let first_week_start_epoch = self.first_week_start_epoch().get(); + require!( + first_week_start_epoch <= current_epoch, + "Cannot claim rewards yet" + ); + let caller = self.blockchain().get_caller(); let user = match &opt_user { OptionalValue::Some(user) => user, @@ -313,15 +326,17 @@ where storage_cache: &mut StorageCache, ) { let total_reward = Self::mint_per_block_rewards(sc, &storage_cache.reward_token_id); - if total_reward > 0u64 { - storage_cache.reward_reserve += &total_reward; - let split_rewards = sc.take_reward_slice(total_reward); - - if storage_cache.farm_token_supply != 0u64 { - let increase = (&split_rewards.base_farm * &storage_cache.division_safety_constant) - / &storage_cache.farm_token_supply; - storage_cache.reward_per_share += &increase; - } + if total_reward == 0u64 { + return; + } + + storage_cache.reward_reserve += &total_reward; + let split_rewards = sc.take_reward_slice(total_reward); + + if storage_cache.farm_token_supply != 0u64 { + let increase = (&split_rewards.base_farm * &storage_cache.division_safety_constant) + / &storage_cache.farm_token_supply; + storage_cache.reward_per_share += &increase; } } diff --git a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs index f92abc5c7..5f0d032d1 100644 --- a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs +++ b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs @@ -139,6 +139,7 @@ where division_safety_constant, pair_address, managed_address!(&owner), + 0, MultiValueEncoded::new(), ); diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 8f1a3c23f..cefcf1d31 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -119,6 +119,13 @@ pub trait Farm: &self, opt_orig_caller: OptionalValue, ) -> ClaimRewardsResultType { + let current_epoch = self.blockchain().get_block_epoch(); + let first_week_start_epoch = self.first_week_start_epoch().get(); + require!( + first_week_start_epoch <= current_epoch, + "Cannot claim rewards yet" + ); + let caller = self.blockchain().get_caller(); let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); @@ -199,10 +206,14 @@ pub trait Farm: } #[endpoint(claimBoostedRewards)] - fn claim_boosted_rewards( - &self, - opt_user: OptionalValue, - ) -> EsdtTokenPayment { + fn claim_boosted_rewards(&self, opt_user: OptionalValue) -> EsdtTokenPayment { + let current_epoch = self.blockchain().get_block_epoch(); + let first_week_start_epoch = self.first_week_start_epoch().get(); + require!( + first_week_start_epoch <= current_epoch, + "Cannot claim rewards yet" + ); + let caller = self.blockchain().get_caller(); let user = match &opt_user { OptionalValue::Some(user) => user, diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs index 327897bfb..9d1da7b8d 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs @@ -276,6 +276,7 @@ where division_safety_constant, pair_address, ManagedAddress::::zero(), + 0, MultiValueEncoded::new(), ); diff --git a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs index 2ab0326d4..a82f050da 100644 --- a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs +++ b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs @@ -296,6 +296,7 @@ where division_safety_constant, pair_address, managed_address!(owner), + 1, MultiValueEncoded::new(), );