Skip to content

Commit

Permalink
Merge pull request #979 from multiversx/merge-with-farm-remove-penalty
Browse files Browse the repository at this point in the history
Merge with farm remove penalty
  • Loading branch information
dorin-iancu authored Dec 2, 2024
2 parents 17b9601 + a26137a commit 95c3f31
Show file tree
Hide file tree
Showing 144 changed files with 2,906 additions and 2,080 deletions.
47 changes: 32 additions & 15 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ members = [
"energy-integration/fees-collector/meta",
"energy-integration/governance-v2",
"energy-integration/governance-v2/meta",
"energy-integration/timestamp-oracle",
"energy-integration/timestamp-oracle/meta",

"farm-staking/farm-staking",
"farm-staking/farm-staking/meta",
Expand Down
1 change: 1 addition & 0 deletions common/common_structs/src/alias_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type Nonce = u64;
pub type Epoch = u64;
pub type Week = usize;
pub type Percent = u64;
pub type Timestamp = u64;
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type UnlockPeriod<M> = UnlockSchedule<M>;
pub type OldLockedTokenAttributes<M> = LockedAssetTokenAttributesEx<M>;
Expand Down
7 changes: 7 additions & 0 deletions common/common_structs/src/farm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub trait FarmToken<M: ManagedTypeApi> {
fn get_compounded_rewards(&self) -> BigUint<M>;

fn get_initial_farming_tokens(&self) -> BigUint<M>;

fn get_original_owner(&self) -> ManagedAddress<M>;
}

impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
Expand All @@ -97,4 +99,9 @@ impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
fn get_initial_farming_tokens(&self) -> BigUint<M> {
&self.current_farm_amount - &self.compounded_reward
}

#[inline]
fn get_original_owner(&self) -> ManagedAddress<M> {
self.original_owner.clone()
}
}
57 changes: 56 additions & 1 deletion common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::Nonce;
use common_structs::{FarmToken, Nonce, PaymentsVec};
use pausable::State;

pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10;
Expand Down Expand Up @@ -44,6 +44,61 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
.set(migration_farm_token_nonce);
}

fn check_and_update_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
&self,
user: &ManagedAddress,
farm_positions: &PaymentsVec<Self::Api>,
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
) {
for farm_position in farm_positions {
farm_token_mapper.require_same_token(&farm_position.token_identifier);

if self.is_old_farm_position(farm_position.token_nonce) {
continue;
}

let token_attributes: T =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

if &token_attributes.get_original_owner() != user {
self.decrease_user_farm_position::<T>(&farm_position, farm_token_mapper);
self.increase_user_farm_position(user, &farm_position.amount);
}
}
}

#[inline]
fn increase_user_farm_position(
&self,
user: &ManagedAddress,
increase_farm_position_amount: &BigUint,
) {
self.user_total_farm_position(user)
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
}

fn decrease_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
&self,
farm_position: &EsdtTokenPayment,
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
) {
if self.is_old_farm_position(farm_position.token_nonce) {
return;
}

let token_attributes: T = farm_token_mapper.get_token_attributes(farm_position.token_nonce);
let user_total_farm_position_mapper =
self.user_total_farm_position(&token_attributes.get_original_owner());
let mut user_total_farm_position = user_total_farm_position_mapper.get();

if user_total_farm_position > farm_position.amount {
user_total_farm_position -= &farm_position.amount;
user_total_farm_position_mapper.set(user_total_farm_position);
} else {
user_total_farm_position_mapper.clear();
}
}

#[view(getFarmingTokenId)]
#[storage_mapper("farming_token_id")]
fn farming_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
Expand Down
75 changes: 0 additions & 75 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use common_structs::{FarmToken, FarmTokenAttributes, Nonce};
use config::ConfigModule;
use contexts::storage_cache::StorageCache;
use core::marker::PhantomData;
use farm_token::FarmTokenModule;
use fixed_supply_token::FixedSupplyToken;
use mergeable::Mergeable;
use multiversx_sc_modules::transfer_role_proxy::PaymentsVec;
use rewards::RewardsModule;

pub trait AllBaseFarmImplTraits:
Expand Down Expand Up @@ -184,79 +182,6 @@ pub trait FarmContract {

new_attributes.into()
}

fn get_exit_penalty(
_sc: &Self::FarmSc,
_total_exit_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
_token_attributes: &Self::AttributesType,
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
BigUint::zero()
}

fn apply_penalty(
_sc: &Self::FarmSc,
_total_exit_amount: &mut BigUint<<Self::FarmSc as ContractBase>::Api>,
_token_attributes: &Self::AttributesType,
_storage_cache: &StorageCache<Self::FarmSc>,
) {
}

fn check_and_update_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
farm_positions: &PaymentsVec<<Self::FarmSc as ContractBase>::Api>,
) {
let farm_token_mapper = sc.farm_token();
for farm_position in farm_positions {
farm_token_mapper.require_same_token(&farm_position.token_identifier);

if sc.is_old_farm_position(farm_position.token_nonce) {
continue;
}

let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

if &token_attributes.original_owner != user {
Self::decrease_user_farm_position(sc, &farm_position);
Self::increase_user_farm_position(sc, user, &farm_position.amount);
}
}
}

#[inline]
fn increase_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
increase_farm_position_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
) {
sc.user_total_farm_position(user)
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
}

fn decrease_user_farm_position(
sc: &Self::FarmSc,
farm_position: &EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
) {
if sc.is_old_farm_position(farm_position.token_nonce) {
return;
}

let farm_token_mapper = sc.farm_token();
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

let user_total_farm_position_mapper =
sc.user_total_farm_position(&token_attributes.original_owner);
let mut user_total_farm_position = user_total_farm_position_mapper.get();

if user_total_farm_position > farm_position.amount {
user_total_farm_position -= &farm_position.amount;
user_total_farm_position_mapper.set(user_total_farm_position);
} else {
user_total_farm_position_mapper.clear();
}
}
}

pub struct DefaultFarmWrapper<T>
Expand Down
6 changes: 5 additions & 1 deletion common/modules/farm/farm_base_impl/src/claim_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub trait BaseClaimRewardsModule:
);
storage_cache.reward_reserve -= &reward;

FC::check_and_update_user_farm_position(self, &caller, &payments);
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&payments,
&self.farm_token(),
);

let farm_token_mapper = self.farm_token();
let base_attributes = FC::create_claim_rewards_initial_attributes(
Expand Down
8 changes: 6 additions & 2 deletions common/modules/farm/farm_base_impl/src/compound_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ pub trait BaseCompoundRewardsModule:
storage_cache.reward_reserve -= &reward;
storage_cache.farm_token_supply += &reward;

FC::check_and_update_user_farm_position(self, &caller, &payments);
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&payments,
&self.farm_token(),
);

let farm_token_mapper = self.farm_token();
let base_attributes = FC::create_compound_rewards_initial_attributes(
Expand All @@ -86,7 +90,7 @@ pub trait BaseCompoundRewardsModule:
&farm_token_mapper,
);

FC::increase_user_farm_position(self, &caller, &reward);
self.increase_user_farm_position(&caller, &reward);

let first_farm_token = &compound_rewards_context.first_farm_token.payment;
farm_token_mapper.nft_burn(first_farm_token.token_nonce, &first_farm_token.amount);
Expand Down
10 changes: 3 additions & 7 deletions common/modules/farm/farm_base_impl/src/enter_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ pub trait BaseEnterFarmModule:
);

// The order is important - first check and update, then increase position
FC::check_and_update_user_farm_position(
self,
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&enter_farm_context.additional_farm_tokens,
&self.farm_token(),
);
FC::increase_user_farm_position(
self,
&caller,
&enter_farm_context.farming_token_payment.amount,
);
self.increase_user_farm_position(&caller, &enter_farm_context.farming_token_payment.amount);

FC::generate_aggregated_rewards(self, &mut storage_cache);

Expand Down
Loading

0 comments on commit 95c3f31

Please sign in to comment.