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

Remove farm penalty code #966

Merged
merged 26 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
39 changes: 28 additions & 11 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 @@ -29,6 +29,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
16 changes: 0 additions & 16 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,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>,
Expand Down
18 changes: 9 additions & 9 deletions common/modules/permissions_module/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ impl TopDecode for Permissions {

impl TypeAbiFrom<Self> for Permissions {}

impl TypeAbi for Permissions {
type Unmanaged = Self;
impl TypeAbi for Permissions {
type Unmanaged = Self;

fn type_name() -> multiversx_sc::abi::TypeName {
core::any::type_name::<u32>().into()
}
fn type_name() -> multiversx_sc::abi::TypeName {
core::any::type_name::<u32>().into()
}

fn type_name_rust() -> multiversx_sc::abi::TypeName {
core::any::type_name::<u32>().into()
}
}
fn type_name_rust() -> multiversx_sc::abi::TypeName {
core::any::type_name::<u32>().into()
}
}
69 changes: 29 additions & 40 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ use fixed_supply_token::FixedSupplyToken;

use farm::{
base_functions::{BaseFunctionsModule, ClaimRewardsResultType, DoubleMultiPayment, Wrapper},
exit_penalty::{
DEFAULT_BURN_GAS_LIMIT, DEFAULT_MINUMUM_FARMING_EPOCHS, DEFAULT_PENALTY_PERCENT,
},
EnterFarmResultType, ExitFarmWithPartialPosResultType, MAX_PERCENT,
};
use farm_base_impl::base_traits_impl::FarmContract;
Expand All @@ -31,7 +28,6 @@ pub trait Farm:
+ events::EventsModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ farm::base_functions::BaseFunctionsModule
+ farm::exit_penalty::ExitPenaltyModule
+ farm_base_impl::base_farm_init::BaseFarmInitModule
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule
+ farm_base_impl::enter_farm::BaseEnterFarmModule
Expand All @@ -40,6 +36,7 @@ pub trait Farm:
+ farm_base_impl::exit_farm::BaseExitFarmModule
+ farm_boosted_yields::FarmBoostedYieldsModule
+ farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule
+ farm_boosted_yields::custom_reward_logic::CustomRewardLogicModule
+ week_timekeeping::WeekTimekeepingModule
+ weekly_rewards_splitting::WeeklyRewardsSplittingModule
+ weekly_rewards_splitting::events::WeeklyRewardsSplittingEventsModule
Expand All @@ -54,7 +51,6 @@ pub trait Farm:
reward_token_id: TokenIdentifier,
farming_token_id: TokenIdentifier,
division_safety_constant: BigUint,
pair_contract_address: ManagedAddress,
owner: ManagedAddress,
admins: MultiValueEncoded<ManagedAddress>,
) {
Expand All @@ -66,28 +62,26 @@ 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);
self.first_week_start_epoch().set(current_epoch);

// Farm position migration code
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);
}

#[upgrade]
fn upgrade(&self) {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set_if_empty(current_epoch);
fn upgrade(&self, timestamp_oracle_address: ManagedAddress) {
if self.first_week_start_epoch().is_empty() {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set(current_epoch);
}

// Farm position migration code
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);

self.set_timestamp_oracle_address(timestamp_oracle_address);
}

#[payable("*")]
Expand All @@ -98,8 +92,8 @@ pub trait Farm:
) -> EnterFarmResultType<Self::Api> {
let caller = self.blockchain().get_caller();
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

self.migrate_old_farm_positions(&orig_caller);

let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
let boosted_rewards_payment = self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
Expand All @@ -110,9 +104,10 @@ pub trait Farm:

let new_farm_token = self.enter_farm::<NoMintWrapper<Self>>(orig_caller.clone());
self.send_payment_non_zero(&caller, &new_farm_token);

self.update_energy_and_progress(&orig_caller);

self.update_start_of_epoch_timestamp();

(new_farm_token, boosted_rewards_payment).into()
}

Expand All @@ -124,11 +119,9 @@ pub trait Farm:
) -> ClaimRewardsResultType<Self::Api> {
let caller = self.blockchain().get_caller();
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

self.migrate_old_farm_positions(&orig_caller);

let claim_rewards_result = self.claim_rewards::<NoMintWrapper<Self>>(orig_caller.clone());

self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token);

let rewards_payment = claim_rewards_result.rewards;
Expand All @@ -139,6 +132,8 @@ pub trait Farm:
orig_caller,
);

self.update_start_of_epoch_timestamp();

(claim_rewards_result.new_farm_token, locked_rewards_payment).into()
}

Expand All @@ -152,9 +147,7 @@ pub trait Farm:
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

let payment = self.call_value().single_esdt();

let migrated_amount = self.migrate_old_farm_positions(&orig_caller);

let exit_farm_result = self.exit_farm::<NoMintWrapper<Self>>(orig_caller.clone(), payment);

self.decrease_old_farm_positions(migrated_amount, &orig_caller);
Expand All @@ -171,6 +164,8 @@ pub trait Farm:

self.clear_user_energy_if_needed(&orig_caller);

self.update_start_of_epoch_timestamp();

(exit_farm_result.farming_tokens, locked_rewards_payment).into()
}

Expand All @@ -187,15 +182,17 @@ pub trait Farm:
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);

let merged_farm_token = self.merge_and_update_farm_tokens(orig_caller.clone());

self.send_payment_non_zero(&caller, &merged_farm_token);

let locked_rewards_payment = self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
caller,
orig_caller,
);

self.update_start_of_epoch_timestamp();

(merged_farm_token, locked_rewards_payment).into()
}

Expand Down Expand Up @@ -236,9 +233,10 @@ pub trait Farm:
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

let boosted_rewards = self.claim_only_boosted_payment(user);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

// Don't need to call update here too, the internal functions call it already

self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
Expand All @@ -251,18 +249,24 @@ pub trait Farm:
fn start_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.start_produce_rewards();

self.update_start_of_epoch_timestamp();
}

#[endpoint(endProduceRewards)]
fn end_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.end_produce_rewards::<NoMintWrapper<Self>>();

self.update_start_of_epoch_timestamp();
}

#[endpoint(setPerBlockRewardAmount)]
fn set_per_block_rewards_endpoint(&self, per_block_amount: BigUint) {
self.require_caller_has_admin_permissions();
self.set_per_block_rewards::<NoMintWrapper<Self>>(per_block_amount);

self.update_start_of_epoch_timestamp();
}

#[endpoint(setBoostedYieldsRewardsPercentage)]
Expand All @@ -274,6 +278,8 @@ pub trait Farm:
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);

self.update_start_of_epoch_timestamp();
}

#[view(calculateRewardsForGivenPosition)]
Expand Down Expand Up @@ -363,21 +369,4 @@ where
storage_cache,
)
}

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> {
Wrapper::<T>::get_exit_penalty(sc, total_exit_amount, token_attributes)
}

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>,
) {
Wrapper::<T>::apply_penalty(sc, total_exit_amount, token_attributes, storage_cache)
}
}
Loading
Loading