Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Farm concentrated liq (part 3) #843

Open
wants to merge 10 commits into
base: feat/concentrated-liq
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ pub trait FarmContract {
fn create_enter_farm_initial_attributes(
sc: &Self::FarmSc,
caller: ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
farming_token_amount: BigUint<<Self::FarmSc as ContractBase>::Api>,
farming_token_payment: EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
current_reward_per_share: BigUint<<Self::FarmSc as ContractBase>::Api>,
) -> Self::AttributesType {
let current_epoch = sc.blockchain().get_block_epoch();
let attributes = FarmTokenAttributes {
reward_per_share: current_reward_per_share,
entering_epoch: current_epoch,
compounded_reward: BigUint::zero(),
current_farm_amount: farming_token_amount,
current_farm_amount: farming_token_payment.amount,
original_owner: caller,
};

Expand Down
2 changes: 1 addition & 1 deletion common/modules/farm/farm_base_impl/src/enter_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub trait BaseEnterFarmModule:
let base_attributes = FC::create_enter_farm_initial_attributes(
self,
caller,
enter_farm_context.farming_token_payment.amount.clone(),
enter_farm_context.farming_token_payment.clone(),
storage_cache.reward_per_share.clone(),
);
let new_farm_token = self.merge_and_create_token(
Expand Down
3 changes: 3 additions & 0 deletions dex/farm-concentrated-liq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ path = "../../energy-integration/common-modules/weekly-rewards-splitting"
[dependencies.energy-query]
path = "../../energy-integration/common-modules/energy-query"

[dependencies.math]
path = "../../common/modules/math"

[dependencies.multiversx-sc]
version = "=0.46.1"
features = ["esdt-token-payment-legacy-decode"]
Expand Down
86 changes: 71 additions & 15 deletions dex/farm-concentrated-liq/src/base_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ multiversx_sc::derive_imports!();
use core::marker::PhantomData;

use common_errors::ERROR_ZERO_AMOUNT;
use common_structs::FarmTokenAttributes;
use common_structs::FarmToken;
use contexts::storage_cache::StorageCache;

use farm_base_impl::base_traits_impl::{DefaultFarmWrapper, FarmContract};
use farm_base_impl::base_traits_impl::FarmContract;
use fixed_supply_token::FixedSupplyToken;

use crate::exit_penalty;
use crate::{custom_token_attributes::FarmTokenConcentratedLiqAttributes, exit_penalty};

pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1;

Expand Down Expand Up @@ -193,7 +193,7 @@ pub trait BaseFunctionsModule:
}

fn claim_only_boosted_payment(&self, caller: &ManagedAddress) -> BigUint {
let reward = Wrapper::<Self>::calculate_boosted_rewards(self, caller);
let reward = FarmConcentratedLiqWrapper::<Self>::calculate_boosted_rewards(self, caller);
if reward > 0 {
self.reward_reserve().update(|reserve| *reserve -= &reward);
}
Expand Down Expand Up @@ -227,15 +227,15 @@ pub trait BaseFunctionsModule:
}
}

pub struct Wrapper<
pub struct FarmConcentratedLiqWrapper<
T: BaseFunctionsModule
+ farm_boosted_yields::FarmBoostedYieldsModule
+ crate::exit_penalty::ExitPenaltyModule,
> {
_phantom: PhantomData<T>,
}

impl<T> Wrapper<T>
impl<T> FarmConcentratedLiqWrapper<T>
where
T: BaseFunctionsModule
+ farm_boosted_yields::FarmBoostedYieldsModule
Expand All @@ -252,14 +252,14 @@ where
}
}

impl<T> FarmContract for Wrapper<T>
impl<T> FarmContract for FarmConcentratedLiqWrapper<T>
where
T: BaseFunctionsModule
+ farm_boosted_yields::FarmBoostedYieldsModule
+ crate::exit_penalty::ExitPenaltyModule,
{
type FarmSc = T;
type AttributesType = FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api>;
type AttributesType = FarmTokenConcentratedLiqAttributes<<Self::FarmSc as ContractBase>::Api>;

fn generate_aggregated_rewards(
sc: &Self::FarmSc,
Expand Down Expand Up @@ -289,13 +289,14 @@ where
token_attributes: &Self::AttributesType,
storage_cache: &StorageCache<Self::FarmSc>,
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
let base_farm_reward = DefaultFarmWrapper::<T>::calculate_rewards(
sc,
caller,
farm_token_amount,
token_attributes,
storage_cache,
);
let token_rps = token_attributes.get_reward_per_share();
let base_farm_reward = if storage_cache.reward_per_share <= token_rps {
BigUint::zero()
} else {
let rps_diff = &storage_cache.reward_per_share - &token_rps;
farm_token_amount * &rps_diff / &storage_cache.division_safety_constant
};

let boosted_yield_rewards = Self::calculate_boosted_rewards(sc, caller);

base_farm_reward + boosted_yield_rewards
Expand Down Expand Up @@ -335,4 +336,59 @@ where
&storage_cache.reward_token_id,
);
}

fn create_enter_farm_initial_attributes(
sc: &Self::FarmSc,
caller: ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
farming_token_payment: EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
current_reward_per_share: BigUint<<Self::FarmSc as ContractBase>::Api>,
) -> Self::AttributesType {
let current_epoch = sc.blockchain().get_block_epoch();
FarmTokenConcentratedLiqAttributes {
reward_per_share: current_reward_per_share,
entering_epoch: current_epoch,
compounded_reward: BigUint::zero(),
current_farm_amount: farming_token_payment.amount,
original_owner: caller,
lp_token_nonce: farming_token_payment.token_nonce,
}
}

fn create_claim_rewards_initial_attributes(
_sc: &Self::FarmSc,
caller: ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
first_token_attributes: Self::AttributesType,
current_reward_per_share: BigUint<<Self::FarmSc as ContractBase>::Api>,
) -> Self::AttributesType {
let current_farm_amount = first_token_attributes.get_total_supply();
FarmTokenConcentratedLiqAttributes {
reward_per_share: current_reward_per_share,
entering_epoch: first_token_attributes.entering_epoch,
compounded_reward: first_token_attributes.compounded_reward,
current_farm_amount,
original_owner: caller,
lp_token_nonce: first_token_attributes.lp_token_nonce,
}
}

fn create_compound_rewards_initial_attributes(
sc: &Self::FarmSc,
caller: ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
first_token_attributes: Self::AttributesType,
current_reward_per_share: BigUint<<Self::FarmSc as ContractBase>::Api>,
reward: &BigUint<<Self::FarmSc as ContractBase>::Api>,
) -> Self::AttributesType {
let current_epoch = sc.blockchain().get_block_epoch();
let new_pos_compounded_reward = first_token_attributes.compounded_reward + reward;
let new_pos_current_farm_amount = first_token_attributes.current_farm_amount + reward;

FarmTokenConcentratedLiqAttributes {
reward_per_share: current_reward_per_share,
entering_epoch: current_epoch,
compounded_reward: new_pos_compounded_reward,
current_farm_amount: new_pos_current_farm_amount,
original_owner: caller,
lp_token_nonce: first_token_attributes.lp_token_nonce,
}
}
}
Loading
Loading