From a907b89b64f9efdeebebb8a5144c9a9b5fc4cd0e Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Fri, 2 Aug 2024 11:34:22 +0300 Subject: [PATCH 1/2] add rewards aggregation in merge endpoints --- dex/farm-with-locked-rewards/src/lib.rs | 2 ++ dex/farm/src/base_functions.rs | 7 +++++++ dex/farm/src/lib.rs | 2 ++ .../src/claim_only_boosted_staking_rewards.rs | 1 + farm-staking/farm-staking/src/custom_rewards.rs | 4 ++++ farm-staking/farm-staking/src/lib.rs | 8 ++++++++ farm-staking/farm-staking/src/unbond_farm.rs | 8 ++++++-- 7 files changed, 30 insertions(+), 2 deletions(-) diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index e03cb80fe..972ddbbd7 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -227,6 +227,7 @@ pub trait Farm: } let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); NoMintWrapper::::generate_aggregated_rewards(self, &mut storage_cache); let boosted_rewards = self.claim_only_boosted_payment(user); @@ -265,6 +266,7 @@ pub trait Farm: require!(percentage <= MAX_PERCENT, "Invalid percentage"); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); NoMintWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.boosted_yields_rewards_percentage().set(percentage); diff --git a/dex/farm/src/base_functions.rs b/dex/farm/src/base_functions.rs index d6455a2b7..dad01984e 100644 --- a/dex/farm/src/base_functions.rs +++ b/dex/farm/src/base_functions.rs @@ -192,6 +192,13 @@ pub trait BaseFunctionsModule: FC::check_and_update_user_farm_position(self, orig_caller, &payments); + let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); + + FC::generate_aggregated_rewards(self, &mut storage_cache); + + self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply); + self.merge_from_payments_and_burn(payments, &token_mapper) } diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 211b05e02..aeabcaaa0 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -229,6 +229,7 @@ pub trait Farm: } let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); Wrapper::::generate_aggregated_rewards(self, &mut storage_cache); let boosted_rewards = self.claim_only_boosted_payment(user); @@ -266,6 +267,7 @@ pub trait Farm: require!(percentage <= MAX_PERCENT, "Invalid percentage"); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); Wrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.boosted_yields_rewards_percentage().set(percentage); 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 79d433dfe..4969f3ddb 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 @@ -42,6 +42,7 @@ pub trait ClaimOnlyBoostedStakingRewardsModule: } let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); let boosted_rewards = self.claim_only_boosted_payment(user); diff --git a/farm-staking/farm-staking/src/custom_rewards.rs b/farm-staking/farm-staking/src/custom_rewards.rs index 4d07dd5a4..024b8c5e2 100644 --- a/farm-staking/farm-staking/src/custom_rewards.rs +++ b/farm-staking/farm-staking/src/custom_rewards.rs @@ -51,6 +51,7 @@ pub trait CustomRewardsModule: self.require_caller_has_admin_permissions(); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); let reward_capacity_mapper = self.reward_capacity(); @@ -80,6 +81,7 @@ pub trait CustomRewardsModule: self.require_caller_has_admin_permissions(); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.produce_rewards_enabled().set(false); } @@ -90,6 +92,7 @@ pub trait CustomRewardsModule: require!(per_block_amount != 0, "Amount cannot be zero"); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.per_block_reward_amount().set(&per_block_amount); } @@ -100,6 +103,7 @@ pub trait CustomRewardsModule: require!(max_apr != 0, "Max APR cannot be zero"); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.max_annual_percentage_rewards().set(&max_apr); } diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index f783c7831..9cf4b1228 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -144,6 +144,13 @@ pub trait FarmStaking: FC::check_and_update_user_farm_position(self, orig_caller, &payments); + let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); + + FC::generate_aggregated_rewards(self, &mut storage_cache); + + self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply); + self.merge_from_payments_and_burn(payments, &token_mapper) } @@ -153,6 +160,7 @@ pub trait FarmStaking: require!(percentage <= MAX_PERCENT, "Invalid percentage"); let mut storage_cache = StorageCache::new(self); + self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.boosted_yields_rewards_percentage().set(percentage); diff --git a/farm-staking/farm-staking/src/unbond_farm.rs b/farm-staking/farm-staking/src/unbond_farm.rs index 7e85a2a2c..ec2044897 100644 --- a/farm-staking/farm-staking/src/unbond_farm.rs +++ b/farm-staking/farm-staking/src/unbond_farm.rs @@ -1,8 +1,9 @@ multiversx_sc::imports!(); use contexts::storage_cache::StorageCache; +use farm_base_impl::base_traits_impl::FarmContract; -use crate::token_attributes::UnbondSftAttributes; +use crate::{base_impl_wrapper::FarmStakingWrapper, token_attributes::UnbondSftAttributes}; #[multiversx_sc::module] pub trait UnbondFarmModule: @@ -32,7 +33,7 @@ pub trait UnbondFarmModule: #[payable("*")] #[endpoint(unbondFarm)] fn unbond_farm(&self) -> EsdtTokenPayment { - let storage_cache = StorageCache::new(self); + let mut storage_cache = StorageCache::new(self); self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); let farm_token_mapper = self.farm_token(); @@ -48,6 +49,9 @@ pub trait UnbondFarmModule: "Unbond period not over" ); + FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); + self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply); + farm_token_mapper.nft_burn(payment.token_nonce, &payment.amount); let caller = self.blockchain().get_caller(); From 3464f856e0ef3cbf573ed71e1848b00fc5245f15 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Fri, 2 Aug 2024 12:02:57 +0300 Subject: [PATCH 2/2] remove validation checks from farm staking SC --- .../farm-staking/src/claim_only_boosted_staking_rewards.rs | 1 - farm-staking/farm-staking/src/custom_rewards.rs | 4 ---- 2 files changed, 5 deletions(-) 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 4969f3ddb..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 @@ -42,7 +42,6 @@ pub trait ClaimOnlyBoostedStakingRewardsModule: } let mut storage_cache = StorageCache::new(self); - self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); let boosted_rewards = self.claim_only_boosted_payment(user); diff --git a/farm-staking/farm-staking/src/custom_rewards.rs b/farm-staking/farm-staking/src/custom_rewards.rs index 024b8c5e2..4d07dd5a4 100644 --- a/farm-staking/farm-staking/src/custom_rewards.rs +++ b/farm-staking/farm-staking/src/custom_rewards.rs @@ -51,7 +51,6 @@ pub trait CustomRewardsModule: self.require_caller_has_admin_permissions(); let mut storage_cache = StorageCache::new(self); - self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); let reward_capacity_mapper = self.reward_capacity(); @@ -81,7 +80,6 @@ pub trait CustomRewardsModule: self.require_caller_has_admin_permissions(); let mut storage_cache = StorageCache::new(self); - self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.produce_rewards_enabled().set(false); } @@ -92,7 +90,6 @@ pub trait CustomRewardsModule: require!(per_block_amount != 0, "Amount cannot be zero"); let mut storage_cache = StorageCache::new(self); - self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.per_block_reward_amount().set(&per_block_amount); } @@ -103,7 +100,6 @@ pub trait CustomRewardsModule: require!(max_apr != 0, "Max APR cannot be zero"); let mut storage_cache = StorageCache::new(self); - self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.max_annual_percentage_rewards().set(&max_apr); }