Skip to content

Commit

Permalink
Aggregate rewards in withdraw_rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinCarabas committed Feb 6, 2024
1 parent c439675 commit 4792a42
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
14 changes: 10 additions & 4 deletions farm-staking/farm-staking/src/custom_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>::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"
Expand Down
2 changes: 1 addition & 1 deletion farm-staking/farm-staking/tests/farm_staking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions farm-staking/farm-staking/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions farm-staking/farm-staking/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down

0 comments on commit 4792a42

Please sign in to comment.