Skip to content

Commit

Permalink
add rewards aggregation in merge endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Aug 2, 2024
1 parent 55de050 commit a907b89
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);
Expand Down Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);
Expand Down
7 changes: 7 additions & 0 deletions dex/farm/src/base_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);
Expand Down Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);
Expand Down
4 changes: 4 additions & 0 deletions farm-staking/farm-staking/src/custom_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let reward_capacity_mapper = self.reward_capacity();
Expand Down Expand Up @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);
self.produce_rewards_enabled().set(false);
}
Expand All @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);
self.per_block_reward_amount().set(&per_block_amount);
}
Expand All @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);
self.max_annual_percentage_rewards().set(&max_apr);
}
Expand Down
8 changes: 8 additions & 0 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);
Expand Down
8 changes: 6 additions & 2 deletions farm-staking/farm-staking/src/unbond_farm.rs
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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();
Expand All @@ -48,6 +49,9 @@ pub trait UnbondFarmModule:
"Unbond period not over"
);

FarmStakingWrapper::<Self>::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();
Expand Down

0 comments on commit a907b89

Please sign in to comment.