diff --git a/farm-staking/farm-staking/src/custom_rewards.rs b/farm-staking/farm-staking/src/custom_rewards.rs index 6df6eb187..f62ec0cb7 100644 --- a/farm-staking/farm-staking/src/custom_rewards.rs +++ b/farm-staking/farm-staking/src/custom_rewards.rs @@ -48,10 +48,16 @@ pub trait CustomRewardsModule: #[endpoint(withdrawRewards)] fn withdraw_rewards(&self, withdraw_amount: BigUint) { self.require_caller_has_admin_permissions(); - - let remaining_uncolected_rewards = self.reward_capacity().get() - self.accumulated_rewards().get(); - require!(withdraw_amount < remaining_uncolected_rewards, WITHDRAW_AMOUNT_TOO_HIGH); - self.reward_capacity().update(|rewards| { + + let mut storage_cache = StorageCache::new(self); + FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); + + let reward_capacity_mapper = self.reward_capacity(); + let accumulated_rewards_mapper = self.accumulated_rewards(); + let remaining_rewards = reward_capacity_mapper.get() - accumulated_rewards_mapper.get(); + require!(withdraw_amount <= remaining_rewards, WITHDRAW_AMOUNT_TOO_HIGH); + + reward_capacity_mapper.update(|rewards| { require!( *rewards >= withdraw_amount, "Not enough rewards to withdraw" diff --git a/farm-staking/farm-staking/tests/farm_staking_test.rs b/farm-staking/farm-staking/tests/farm_staking_test.rs index dec70ca8a..a8a6abb3a 100644 --- a/farm-staking/farm-staking/tests/farm_staking_test.rs +++ b/farm-staking/farm-staking/tests/farm_staking_test.rs @@ -249,7 +249,7 @@ fn test_withdraw_rewards() { } #[test] -fn test_admin_cannot_withdraw_users_rewards() { +fn test_withdraw_after_produced_rewards() { DebugApi::dummy(); let mut farm_setup = FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj); diff --git a/farm-staking/farm-staking/wasm/Cargo.lock b/farm-staking/farm-staking/wasm/Cargo.lock index dce82eb7c..88d4b1bba 100644 --- a/farm-staking/farm-staking/wasm/Cargo.lock +++ b/farm-staking/farm-staking/wasm/Cargo.lock @@ -411,6 +411,7 @@ dependencies = [ "permissions_module", "simple-lock", "token_send", + "utils", ] [[package]] diff --git a/farm-staking/farm-staking/wasm/src/lib.rs b/farm-staking/farm-staking/wasm/src/lib.rs index c05cf46e5..02de33846 100644 --- a/farm-staking/farm-staking/wasm/src/lib.rs +++ b/farm-staking/farm-staking/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 70 +// Endpoints: 71 // Async Callback: 1 -// Total number of exported functions: 72 +// Total number of exported functions: 73 #![no_std] #![allow(internal_features)] @@ -24,6 +24,7 @@ multiversx_sc_wasm_adapter::endpoints! { mergeFarmTokens => merge_farm_tokens_endpoint calculateRewardsForGivenPosition => calculate_rewards_for_given_position topUpRewards => top_up_rewards + withdrawRewards => withdraw_rewards endProduceRewards => end_produce_rewards setPerBlockRewardAmount => set_per_block_rewards setMaxApr => set_max_apr