Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Feb 12, 2024
1 parent abad7ad commit 39d6bda
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 38 deletions.
21 changes: 10 additions & 11 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,18 @@ pub trait FarmContract {
) -> BigUint<<Self::FarmSc as ContractBase>::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(
Expand Down
61 changes: 38 additions & 23 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -57,6 +58,7 @@ pub trait Farm:
division_safety_constant: BigUint,
pair_contract_address: ManagedAddress,
owner: ManagedAddress,
first_week_start_epoch: Epoch,
admins: MultiValueEncoded<ManagedAddress>,
) {
self.base_farm_init(
Expand All @@ -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]
Expand Down Expand Up @@ -120,6 +122,13 @@ pub trait Farm:
&self,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> ClaimRewardsResultType<Self::Api> {
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);

Expand Down Expand Up @@ -209,10 +218,14 @@ pub trait Farm:
}

#[endpoint(claimBoostedRewards)]
fn claim_boosted_rewards(
&self,
opt_user: OptionalValue<ManagedAddress>,
) -> EsdtTokenPayment<Self::Api> {
fn claim_boosted_rewards(&self, opt_user: OptionalValue<ManagedAddress>) -> 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,
Expand Down Expand Up @@ -313,15 +326,17 @@ where
storage_cache: &mut StorageCache<Self::FarmSc>,
) {
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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ where
division_safety_constant,
pair_address,
managed_address!(&owner),
0,
MultiValueEncoded::new(),
);

Expand Down
19 changes: 15 additions & 4 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ pub trait Farm:
&self,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> ClaimRewardsResultType<Self::Api> {
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);

Expand Down Expand Up @@ -199,10 +206,14 @@ pub trait Farm:
}

#[endpoint(claimBoostedRewards)]
fn claim_boosted_rewards(
&self,
opt_user: OptionalValue<ManagedAddress>,
) -> EsdtTokenPayment<Self::Api> {
fn claim_boosted_rewards(&self, opt_user: OptionalValue<ManagedAddress>) -> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ where
division_safety_constant,
pair_address,
ManagedAddress::<DebugApi>::zero(),
0,
MultiValueEncoded::new(),
);

Expand Down
1 change: 1 addition & 0 deletions locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ where
division_safety_constant,
pair_address,
managed_address!(owner),
1,
MultiValueEncoded::new(),
);

Expand Down

0 comments on commit 39d6bda

Please sign in to comment.