From 042c7f14b3c02f804f5a29283aa1e7de368a1659 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 20 Feb 2024 12:14:00 +0200 Subject: [PATCH 1/4] farm concentrated liq - copy farm --- Cargo.lock | 41 ++ Cargo.toml | 2 + dex/farm-concentrated-liq/Cargo.toml | 91 +++ dex/farm-concentrated-liq/meta/Cargo.toml | 13 + dex/farm-concentrated-liq/meta/src/main.rs | 3 + dex/farm-concentrated-liq/multiversx.json | 3 + .../src/base_functions.rs | 370 ++++++++++++ dex/farm-concentrated-liq/src/exit_penalty.rs | 75 +++ dex/farm-concentrated-liq/src/lib.rs | 265 +++++++++ dex/farm-concentrated-liq/wasm/Cargo.lock | 538 ++++++++++++++++++ dex/farm-concentrated-liq/wasm/Cargo.toml | 31 + dex/farm-concentrated-liq/wasm/src/lib.rs | 91 +++ 12 files changed, 1523 insertions(+) create mode 100644 dex/farm-concentrated-liq/Cargo.toml create mode 100644 dex/farm-concentrated-liq/meta/Cargo.toml create mode 100644 dex/farm-concentrated-liq/meta/src/main.rs create mode 100644 dex/farm-concentrated-liq/multiversx.json create mode 100644 dex/farm-concentrated-liq/src/base_functions.rs create mode 100644 dex/farm-concentrated-liq/src/exit_penalty.rs create mode 100644 dex/farm-concentrated-liq/src/lib.rs create mode 100644 dex/farm-concentrated-liq/wasm/Cargo.lock create mode 100644 dex/farm-concentrated-liq/wasm/Cargo.toml create mode 100644 dex/farm-concentrated-liq/wasm/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index fe863b981..dc1da0bf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -777,6 +777,47 @@ dependencies = [ "weekly-rewards-splitting", ] +[[package]] +name = "farm-concentrated-liq" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "contexts", + "energy-factory-mock", + "energy-query", + "energy-update", + "events", + "farm-boosted-yields", + "farm_base_impl", + "farm_token", + "fixed-supply-token", + "mergeable", + "multiversx-sc", + "multiversx-sc-modules", + "multiversx-sc-scenario", + "num-bigint", + "pair", + "pausable", + "permissions_module", + "rewards", + "sc_whitelist_module", + "simple-lock", + "token_send", + "utils", + "week-timekeeping", + "weekly-rewards-splitting", +] + +[[package]] +name = "farm-concentrated-liq-abi" +version = "0.0.0" +dependencies = [ + "farm-concentrated-liq", + "multiversx-sc-meta", +] + [[package]] name = "farm-staking" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index dbdd0d3ec..fc023520e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ members = [ "dex", "dex/farm", "dex/farm/meta", + "dex/farm-concentrated-liq", + "dex/farm-concentrated-liq/meta", "dex/farm-with-locked-rewards", "dex/farm-with-locked-rewards/meta", "dex/pair", diff --git a/dex/farm-concentrated-liq/Cargo.toml b/dex/farm-concentrated-liq/Cargo.toml new file mode 100644 index 000000000..8a73b96fa --- /dev/null +++ b/dex/farm-concentrated-liq/Cargo.toml @@ -0,0 +1,91 @@ +[package] +name = "farm-concentrated-liq" +version = "0.0.0" +authors = ["MultiversX "] +edition = "2021" +publish = false + +[lib] +path = "src/lib.rs" + +[dependencies.farm_base_impl] +path = "../../common/modules/farm/farm_base_impl" + +[dependencies.config] +path = "../../common/modules/farm/config" + +[dependencies.farm_token] +path = "../../common/modules/farm/farm_token" + +[dependencies.rewards] +path = "../../common/modules/farm/rewards" + +[dependencies.events] +path = "../../common/modules/farm/events" + +[dependencies.contexts] +path = "../../common/modules/farm/contexts" + +[dependencies.token_send] +path = "../../common/modules/token_send" + +[dependencies.utils] +path = "../../common/modules/utils" + +[dependencies.pausable] +path = "../../common/modules/pausable" + +[dependencies.permissions_module] +path = "../../common/modules/permissions_module" + +[dependencies.sc_whitelist_module] +path = "../../common/modules/sc_whitelist_module" + +[dependencies.pair] +path = "../pair" + +[dependencies.common_structs] +path = "../../common/common_structs" + +[dependencies.common_errors] +path = "../../common/common_errors" + +[dependencies.mergeable] +path = "../../common/traits/mergeable" + +[dependencies.fixed-supply-token] +path = "../../common/traits/fixed-supply-token" + +[dependencies.farm-boosted-yields] +path = "../../energy-integration/farm-boosted-yields" + +[dependencies.week-timekeeping] +path = "../../energy-integration/common-modules/week-timekeeping" + +[dependencies.weekly-rewards-splitting] +path = "../../energy-integration/common-modules/weekly-rewards-splitting" + +[dependencies.energy-query] +path = "../../energy-integration/common-modules/energy-query" + +[dependencies.multiversx-sc] +version = "=0.46.1" +features = ["esdt-token-payment-legacy-decode"] + +[dependencies.multiversx-sc-modules] +version = "=0.46.1" + +[dev-dependencies] +num-bigint = "0.4.2" + +[dev-dependencies.energy-update] +path = "../../energy-integration/energy-update" + +[dev-dependencies.multiversx-sc-scenario] +version = "=0.46.1" + +[dev-dependencies.energy-factory-mock] +path = "../../energy-integration/energy-factory-mock" + +[dev-dependencies.simple-lock] +path = "../../locked-asset/simple-lock" diff --git a/dex/farm-concentrated-liq/meta/Cargo.toml b/dex/farm-concentrated-liq/meta/Cargo.toml new file mode 100644 index 000000000..526fdda08 --- /dev/null +++ b/dex/farm-concentrated-liq/meta/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "farm-concentrated-liq-abi" +version = "0.0.0" +authors = ["MultiversX "] +edition = "2021" +publish = false + +[dependencies.farm-concentrated-liq] +path = ".." + +[dependencies.multiversx-sc-meta] +version = "0.46.1" +default-features = false diff --git a/dex/farm-concentrated-liq/meta/src/main.rs b/dex/farm-concentrated-liq/meta/src/main.rs new file mode 100644 index 000000000..731b7cad7 --- /dev/null +++ b/dex/farm-concentrated-liq/meta/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + multiversx_sc_meta::cli_main::(); +} diff --git a/dex/farm-concentrated-liq/multiversx.json b/dex/farm-concentrated-liq/multiversx.json new file mode 100644 index 000000000..736553962 --- /dev/null +++ b/dex/farm-concentrated-liq/multiversx.json @@ -0,0 +1,3 @@ +{ + "language": "rust" +} \ No newline at end of file diff --git a/dex/farm-concentrated-liq/src/base_functions.rs b/dex/farm-concentrated-liq/src/base_functions.rs new file mode 100644 index 000000000..968e3381d --- /dev/null +++ b/dex/farm-concentrated-liq/src/base_functions.rs @@ -0,0 +1,370 @@ +#![allow(clippy::too_many_arguments)] +#![allow(clippy::from_over_into)] + +multiversx_sc::imports!(); +multiversx_sc::derive_imports!(); + +use core::marker::PhantomData; + +use common_errors::ERROR_ZERO_AMOUNT; +use common_structs::FarmTokenAttributes; +use contexts::storage_cache::StorageCache; + +use farm_base_impl::base_traits_impl::{DefaultFarmWrapper, FarmContract}; +use fixed_supply_token::FixedSupplyToken; + +use crate::exit_penalty; + +pub type DoubleMultiPayment = MultiValue2, EsdtTokenPayment>; +pub type ClaimRewardsResultType = DoubleMultiPayment; +pub type ExitFarmResultType = DoubleMultiPayment; + +pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1; + +pub struct ClaimRewardsResultWrapper { + pub new_farm_token: EsdtTokenPayment, + pub rewards: EsdtTokenPayment, +} + +pub struct ExitFarmResultWrapper { + pub farming_tokens: EsdtTokenPayment, + pub rewards: EsdtTokenPayment, +} + +impl Into> for ClaimRewardsResultWrapper { + fn into(self) -> ClaimRewardsResultType { + (self.new_farm_token, self.rewards).into() + } +} + +impl Into> for ExitFarmResultWrapper { + fn into(self) -> ExitFarmResultType { + (self.farming_tokens, self.rewards).into() + } +} + +#[multiversx_sc::module] +pub trait BaseFunctionsModule: + rewards::RewardsModule + + config::ConfigModule + + token_send::TokenSendModule + + farm_token::FarmTokenModule + + pausable::PausableModule + + permissions_module::PermissionsModule + + events::EventsModule + + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + + exit_penalty::ExitPenaltyModule + + farm_base_impl::base_farm_init::BaseFarmInitModule + + farm_base_impl::base_farm_validation::BaseFarmValidationModule + + farm_base_impl::enter_farm::BaseEnterFarmModule + + farm_base_impl::claim_rewards::BaseClaimRewardsModule + + farm_base_impl::compound_rewards::BaseCompoundRewardsModule + + farm_base_impl::exit_farm::BaseExitFarmModule + + utils::UtilsModule + + farm_boosted_yields::FarmBoostedYieldsModule + + farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule + + week_timekeeping::WeekTimekeepingModule + + weekly_rewards_splitting::WeeklyRewardsSplittingModule + + weekly_rewards_splitting::events::WeeklyRewardsSplittingEventsModule + + weekly_rewards_splitting::global_info::WeeklyRewardsGlobalInfo + + weekly_rewards_splitting::locked_token_buckets::WeeklyRewardsLockedTokenBucketsModule + + weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule + + energy_query::EnergyQueryModule +{ + fn enter_farm>( + &self, + caller: ManagedAddress, + ) -> EsdtTokenPayment { + let payments = self.call_value().all_esdt_transfers().clone_value(); + let base_enter_farm_result = self.enter_farm_base::(caller.clone(), payments); + + self.set_farm_supply_for_current_week( + &base_enter_farm_result.storage_cache.farm_token_supply, + ); + + self.emit_enter_farm_event( + &caller, + base_enter_farm_result.context.farming_token_payment, + base_enter_farm_result.new_farm_token.clone(), + base_enter_farm_result.created_with_merge, + base_enter_farm_result.storage_cache, + ); + + base_enter_farm_result.new_farm_token.payment + } + + fn claim_rewards>( + &self, + caller: ManagedAddress, + ) -> ClaimRewardsResultWrapper { + let payments = self.call_value().all_esdt_transfers().clone_value(); + let base_claim_rewards_result = self.claim_rewards_base::(caller.clone(), payments); + + let output_farm_token_payment = base_claim_rewards_result.new_farm_token.payment.clone(); + let rewards_payment = base_claim_rewards_result.rewards; + + self.set_farm_supply_for_current_week( + &base_claim_rewards_result.storage_cache.farm_token_supply, + ); + + self.emit_claim_rewards_event( + &caller, + base_claim_rewards_result.context, + base_claim_rewards_result.new_farm_token, + rewards_payment.clone(), + base_claim_rewards_result.created_with_merge, + base_claim_rewards_result.storage_cache, + ); + + ClaimRewardsResultWrapper { + new_farm_token: output_farm_token_payment, + rewards: rewards_payment, + } + } + + fn compound_rewards>( + &self, + caller: ManagedAddress, + ) -> EsdtTokenPayment { + let payments = self.call_value().all_esdt_transfers().clone_value(); + let base_compound_rewards_result = + self.compound_rewards_base::(caller.clone(), payments); + + let output_farm_token_payment = base_compound_rewards_result.new_farm_token.payment.clone(); + + self.set_farm_supply_for_current_week( + &base_compound_rewards_result.storage_cache.farm_token_supply, + ); + + self.emit_compound_rewards_event( + &caller, + base_compound_rewards_result.context, + base_compound_rewards_result.new_farm_token, + base_compound_rewards_result.compounded_rewards, + base_compound_rewards_result.created_with_merge, + base_compound_rewards_result.storage_cache, + ); + + output_farm_token_payment + } + + fn exit_farm>( + &self, + caller: ManagedAddress, + payment: EsdtTokenPayment, + ) -> ExitFarmResultWrapper { + let base_exit_farm_result = self.exit_farm_base::(caller.clone(), payment); + + let mut farming_token_payment = base_exit_farm_result.farming_token_payment; + let reward_payment = base_exit_farm_result.reward_payment; + + self.set_farm_supply_for_current_week( + &base_exit_farm_result.storage_cache.farm_token_supply, + ); + + FC::apply_penalty( + self, + &mut farming_token_payment.amount, + &base_exit_farm_result.context.farm_token.attributes, + &base_exit_farm_result.storage_cache, + ); + + self.emit_exit_farm_event( + &caller, + base_exit_farm_result.context, + farming_token_payment.clone(), + reward_payment.clone(), + base_exit_farm_result.storage_cache, + ); + + ExitFarmResultWrapper { + farming_tokens: farming_token_payment, + rewards: reward_payment, + } + } + + fn merge_farm_tokens>(&self) -> EsdtTokenPayment { + let payments = self.get_non_empty_payments(); + let token_mapper = self.farm_token(); + token_mapper.require_all_same_token(&payments); + + let caller = self.blockchain().get_caller(); + FC::check_and_update_user_farm_position(self, &caller, &payments); + + let output_attributes: FC::AttributesType = + self.merge_from_payments_and_burn(payments, &token_mapper); + let new_token_amount = output_attributes.get_total_supply(); + token_mapper.nft_create(new_token_amount, &output_attributes) + } + + fn claim_only_boosted_payment(&self, caller: &ManagedAddress) -> BigUint { + let reward = Wrapper::::calculate_boosted_rewards(self, caller); + if reward > 0 { + self.reward_reserve().update(|reserve| *reserve -= &reward); + } + + reward + } + + fn migrate_old_farm_positions(&self, caller: &ManagedAddress) -> BigUint { + let payments = self.get_non_empty_payments(); + let farm_token_mapper = self.farm_token(); + let farm_token_id = farm_token_mapper.get_token_id(); + let mut migrated_amount = BigUint::zero(); + for farm_position in &payments { + if farm_position.token_identifier == farm_token_id + && self.is_old_farm_position(farm_position.token_nonce) + { + migrated_amount += farm_position.amount; + } + } + + if migrated_amount > 0 { + let mut user_total_farm_position = self.get_user_total_farm_position(caller); + user_total_farm_position.total_farm_position += &migrated_amount; + self.user_total_farm_position(caller) + .set(user_total_farm_position); + } + + migrated_amount + } + + fn decrease_old_farm_positions(&self, migrated_amount: BigUint, caller: &ManagedAddress) { + if migrated_amount == BigUint::zero() { + return; + } + self.user_total_farm_position(caller) + .update(|user_total_farm_position| { + user_total_farm_position.total_farm_position -= migrated_amount; + }); + } + + fn end_produce_rewards>(&self) { + let mut storage = StorageCache::new(self); + FC::generate_aggregated_rewards(self, &mut storage); + + self.produce_rewards_enabled().set(false); + } + + fn set_per_block_rewards>(&self, per_block_amount: BigUint) { + require!(per_block_amount != 0u64, ERROR_ZERO_AMOUNT); + + let mut storage = StorageCache::new(self); + FC::generate_aggregated_rewards(self, &mut storage); + + self.per_block_reward_amount().set(&per_block_amount); + } + + fn require_queried(&self) { + let caller = self.blockchain().get_caller(); + let sc_address = self.blockchain().get_sc_address(); + require!( + caller == sc_address, + "May only call this function through VM query" + ); + } +} + +pub struct Wrapper< + T: BaseFunctionsModule + + farm_boosted_yields::FarmBoostedYieldsModule + + crate::exit_penalty::ExitPenaltyModule, +> { + _phantom: PhantomData, +} + +impl Wrapper +where + T: BaseFunctionsModule + + farm_boosted_yields::FarmBoostedYieldsModule + + crate::exit_penalty::ExitPenaltyModule, +{ + pub fn calculate_boosted_rewards( + sc: &::FarmSc, + caller: &ManagedAddress<<::FarmSc as ContractBase>::Api>, + ) -> BigUint<<::FarmSc as ContractBase>::Api> { + let user_total_farm_position = sc.get_user_total_farm_position(caller); + let user_farm_position = user_total_farm_position.total_farm_position; + + sc.claim_boosted_yields_rewards(caller, user_farm_position) + } +} + +impl FarmContract for Wrapper +where + T: BaseFunctionsModule + + farm_boosted_yields::FarmBoostedYieldsModule + + crate::exit_penalty::ExitPenaltyModule, +{ + type FarmSc = T; + type AttributesType = FarmTokenAttributes<::Api>; + + fn generate_aggregated_rewards( + sc: &Self::FarmSc, + storage_cache: &mut StorageCache, + ) { + 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; + } + } + } + + fn calculate_rewards( + sc: &Self::FarmSc, + caller: &ManagedAddress<::Api>, + farm_token_amount: &BigUint<::Api>, + token_attributes: &Self::AttributesType, + storage_cache: &StorageCache, + ) -> BigUint<::Api> { + let base_farm_reward = DefaultFarmWrapper::::calculate_rewards( + sc, + caller, + farm_token_amount, + token_attributes, + storage_cache, + ); + let boosted_yield_rewards = Self::calculate_boosted_rewards(sc, caller); + + base_farm_reward + boosted_yield_rewards + } + + fn get_exit_penalty( + sc: &Self::FarmSc, + total_exit_amount: &BigUint<::Api>, + token_attributes: &Self::AttributesType, + ) -> BigUint<::Api> { + let current_epoch = sc.blockchain().get_block_epoch(); + let user_farming_epochs = current_epoch - token_attributes.entering_epoch; + let min_farming_epochs = sc.minimum_farming_epochs().get(); + if user_farming_epochs >= min_farming_epochs { + BigUint::zero() + } else { + total_exit_amount * sc.penalty_percent().get() / exit_penalty::MAX_PERCENT + } + } + + fn apply_penalty( + sc: &Self::FarmSc, + total_exit_amount: &mut BigUint<::Api>, + token_attributes: &Self::AttributesType, + storage_cache: &StorageCache, + ) { + let penalty_amount = Self::get_exit_penalty(sc, total_exit_amount, token_attributes); + if penalty_amount > 0 { + *total_exit_amount -= &penalty_amount; + + sc.burn_farming_tokens( + &penalty_amount, + &storage_cache.farming_token_id, + &storage_cache.reward_token_id, + ); + } + } +} diff --git a/dex/farm-concentrated-liq/src/exit_penalty.rs b/dex/farm-concentrated-liq/src/exit_penalty.rs new file mode 100644 index 000000000..e8905fade --- /dev/null +++ b/dex/farm-concentrated-liq/src/exit_penalty.rs @@ -0,0 +1,75 @@ +multiversx_sc::imports!(); + +use common_errors::ERROR_PARAMETERS; +use common_structs::Epoch; +use pair::pair_actions::remove_liq::ProxyTrait as _; + +pub const MAX_PERCENT: u64 = 10_000; +pub const DEFAULT_PENALTY_PERCENT: u64 = 100; +pub const DEFAULT_MINUMUM_FARMING_EPOCHS: u64 = 3; +pub const DEFAULT_BURN_GAS_LIMIT: u64 = 50_000_000; +pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10; +pub const MAX_MINIMUM_FARMING_EPOCHS: u64 = 30; + +#[multiversx_sc::module] +pub trait ExitPenaltyModule: permissions_module::PermissionsModule { + #[only_owner] + #[endpoint] + fn set_penalty_percent(&self, percent: u64) { + require!(percent < MAX_PERCENT, ERROR_PARAMETERS); + self.penalty_percent().set(percent); + } + + #[endpoint] + fn set_minimum_farming_epochs(&self, epochs: Epoch) { + self.require_caller_has_admin_permissions(); + require!(epochs <= MAX_MINIMUM_FARMING_EPOCHS, ERROR_PARAMETERS); + + self.minimum_farming_epochs().set(epochs); + } + + #[only_owner] + #[endpoint] + fn set_burn_gas_limit(&self, gas_limit: u64) { + self.burn_gas_limit().set(gas_limit); + } + + fn burn_farming_tokens( + &self, + farming_amount: &BigUint, + farming_token_id: &TokenIdentifier, + reward_token_id: &TokenIdentifier, + ) { + let pair_contract_address = self.pair_contract_address().get(); + if pair_contract_address.is_zero() { + self.send() + .esdt_local_burn(farming_token_id, 0, farming_amount); + } else { + let gas_limit = self.burn_gas_limit().get(); + self.pair_contract_proxy(pair_contract_address) + .remove_liquidity_and_burn_token(reward_token_id.clone()) + .with_esdt_transfer((farming_token_id.clone(), 0, farming_amount.clone())) + .with_gas_limit(gas_limit) + .transfer_execute(); + } + } + + #[proxy] + fn pair_contract_proxy(&self, to: ManagedAddress) -> pair::Proxy; + + #[view(getPenaltyPercent)] + #[storage_mapper("penalty_percent")] + fn penalty_percent(&self) -> SingleValueMapper; + + #[view(getMinimumFarmingEpoch)] + #[storage_mapper("minimum_farming_epochs")] + fn minimum_farming_epochs(&self) -> SingleValueMapper; + + #[view(getBurnGasLimit)] + #[storage_mapper("burn_gas_limit")] + fn burn_gas_limit(&self) -> SingleValueMapper; + + #[view(getPairContractManagedAddress)] + #[storage_mapper("pair_contract_address")] + fn pair_contract_address(&self) -> SingleValueMapper; +} diff --git a/dex/farm-concentrated-liq/src/lib.rs b/dex/farm-concentrated-liq/src/lib.rs new file mode 100644 index 000000000..48cf893f1 --- /dev/null +++ b/dex/farm-concentrated-liq/src/lib.rs @@ -0,0 +1,265 @@ +#![no_std] +#![allow(clippy::too_many_arguments)] +#![feature(exact_size_is_empty)] + +multiversx_sc::imports!(); +multiversx_sc::derive_imports!(); + +pub mod base_functions; +pub mod exit_penalty; + +use base_functions::{ClaimRewardsResultType, DoubleMultiPayment, Wrapper}; +use common_structs::FarmTokenAttributes; +use contexts::storage_cache::StorageCache; + +use exit_penalty::{ + DEFAULT_BURN_GAS_LIMIT, DEFAULT_MINUMUM_FARMING_EPOCHS, DEFAULT_PENALTY_PERCENT, +}; +use farm_base_impl::base_traits_impl::FarmContract; + +pub type EnterFarmResultType = DoubleMultiPayment; +pub type ExitFarmWithPartialPosResultType = DoubleMultiPayment; + +#[multiversx_sc::contract] +pub trait Farm: + rewards::RewardsModule + + config::ConfigModule + + token_send::TokenSendModule + + farm_token::FarmTokenModule + + pausable::PausableModule + + permissions_module::PermissionsModule + + sc_whitelist_module::SCWhitelistModule + + events::EventsModule + + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + + base_functions::BaseFunctionsModule + + exit_penalty::ExitPenaltyModule + + farm_base_impl::base_farm_init::BaseFarmInitModule + + farm_base_impl::base_farm_validation::BaseFarmValidationModule + + farm_base_impl::enter_farm::BaseEnterFarmModule + + farm_base_impl::claim_rewards::BaseClaimRewardsModule + + farm_base_impl::compound_rewards::BaseCompoundRewardsModule + + farm_base_impl::exit_farm::BaseExitFarmModule + + farm_boosted_yields::FarmBoostedYieldsModule + + farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule + + week_timekeeping::WeekTimekeepingModule + + weekly_rewards_splitting::WeeklyRewardsSplittingModule + + weekly_rewards_splitting::events::WeeklyRewardsSplittingEventsModule + + weekly_rewards_splitting::global_info::WeeklyRewardsGlobalInfo + + weekly_rewards_splitting::locked_token_buckets::WeeklyRewardsLockedTokenBucketsModule + + weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule + + energy_query::EnergyQueryModule + + utils::UtilsModule +{ + #[init] + fn init( + &self, + reward_token_id: TokenIdentifier, + farming_token_id: TokenIdentifier, + division_safety_constant: BigUint, + pair_contract_address: ManagedAddress, + owner: ManagedAddress, + admins: MultiValueEncoded, + ) { + self.base_farm_init( + reward_token_id, + farming_token_id, + division_safety_constant, + owner, + 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); + + // Farm position migration code + let farm_token_mapper = self.farm_token(); + self.try_set_farm_position_migration_nonce(farm_token_mapper); + } + + #[endpoint] + fn upgrade(&self) { + // Farm position migration code + let farm_token_mapper = self.farm_token(); + self.try_set_farm_position_migration_nonce(farm_token_mapper); + } + + #[payable("*")] + #[endpoint(enterFarm)] + fn enter_farm_endpoint( + &self, + opt_orig_caller: OptionalValue, + ) -> EnterFarmResultType { + 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 = + EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); + + let new_farm_token = self.enter_farm::>(orig_caller.clone()); + self.send_payment_non_zero(&caller, &new_farm_token); + self.send_payment_non_zero(&caller, &boosted_rewards_payment); + + self.update_energy_and_progress(&orig_caller); + + (new_farm_token, boosted_rewards_payment).into() + } + + #[payable("*")] + #[endpoint(claimRewards)] + fn claim_rewards_endpoint( + &self, + opt_orig_caller: OptionalValue, + ) -> ClaimRewardsResultType { + 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::>(orig_caller); + + self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token); + self.send_payment_non_zero(&caller, &claim_rewards_result.rewards); + + claim_rewards_result.into() + } + + #[payable("*")] + #[endpoint(compoundRewards)] + fn compound_rewards_endpoint( + &self, + opt_orig_caller: OptionalValue, + ) -> EsdtTokenPayment { + 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 output_farm_token_payment = self.compound_rewards::>(orig_caller.clone()); + + self.send_payment_non_zero(&caller, &output_farm_token_payment); + + self.update_energy_and_progress(&orig_caller); + + output_farm_token_payment + } + + #[payable("*")] + #[endpoint(exitFarm)] + fn exit_farm_endpoint( + &self, + opt_orig_caller: OptionalValue, + ) -> ExitFarmWithPartialPosResultType { + let caller = self.blockchain().get_caller(); + 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::>(orig_caller.clone(), payment); + + self.decrease_old_farm_positions(migrated_amount, &orig_caller); + + self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens); + self.send_payment_non_zero(&caller, &exit_farm_result.rewards); + + self.clear_user_energy_if_needed(&orig_caller); + + (exit_farm_result.farming_tokens, exit_farm_result.rewards).into() + } + + #[payable("*")] + #[endpoint(mergeFarmTokens)] + fn merge_farm_tokens_endpoint( + &self, + opt_orig_caller: OptionalValue, + ) -> DoubleMultiPayment { + 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 = + EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); + + let merged_farm_token = self.merge_farm_tokens::>(); + self.send_payment_non_zero(&caller, &merged_farm_token); + self.send_payment_non_zero(&caller, &boosted_rewards_payment); + + (merged_farm_token, boosted_rewards_payment).into() + } + + #[endpoint(claimBoostedRewards)] + fn claim_boosted_rewards( + &self, + opt_user: OptionalValue, + ) -> EsdtTokenPayment { + let caller = self.blockchain().get_caller(); + let user = match &opt_user { + OptionalValue::Some(user) => user, + OptionalValue::None => &caller, + }; + let user_total_farm_position = self.get_user_total_farm_position(user); + if user != &caller { + require!( + user_total_farm_position.allow_external_claim_boosted_rewards, + "Cannot claim rewards for this address" + ); + } + + let boosted_rewards = self.claim_only_boosted_payment(user); + let boosted_rewards_payment = + EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); + + self.send_payment_non_zero(user, &boosted_rewards_payment); + + boosted_rewards_payment + } + + #[endpoint(startProduceRewards)] + fn start_produce_rewards_endpoint(&self) { + self.require_caller_has_admin_permissions(); + self.start_produce_rewards(); + } + + #[endpoint(endProduceRewards)] + fn end_produce_rewards_endpoint(&self) { + self.require_caller_has_admin_permissions(); + self.end_produce_rewards::>(); + } + + #[endpoint(setPerBlockRewardAmount)] + fn set_per_block_rewards_endpoint(&self, per_block_amount: BigUint) { + self.require_caller_has_admin_permissions(); + self.set_per_block_rewards::>(per_block_amount); + } + + #[view(calculateRewardsForGivenPosition)] + fn calculate_rewards_for_given_position( + &self, + user: ManagedAddress, + farm_token_amount: BigUint, + attributes: FarmTokenAttributes, + ) -> BigUint { + self.require_queried(); + + let mut storage_cache = StorageCache::new(self); + Wrapper::::generate_aggregated_rewards(self, &mut storage_cache); + + Wrapper::::calculate_rewards( + self, + &user, + &farm_token_amount, + &attributes, + &storage_cache, + ) + } +} diff --git a/dex/farm-concentrated-liq/wasm/Cargo.lock b/dex/farm-concentrated-liq/wasm/Cargo.lock new file mode 100644 index 000000000..52aa29daa --- /dev/null +++ b/dex/farm-concentrated-liq/wasm/Cargo.lock @@ -0,0 +1,538 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "common-types" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "common_errors" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "common_structs" +version = "0.0.0" +dependencies = [ + "fixed-supply-token", + "math", + "mergeable", + "multiversx-sc", + "unwrappable", +] + +[[package]] +name = "config" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "multiversx-sc", + "pausable", + "permissions_module", + "token_send", +] + +[[package]] +name = "contexts" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "farm_token", + "multiversx-sc", + "multiversx-sc-modules", + "pausable", + "permissions_module", + "rewards", + "token_merge_helper", + "token_send", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "energy-factory" +version = "0.0.0" +dependencies = [ + "common_structs", + "legacy_token_decode_module", + "math", + "mergeable", + "multiversx-sc", + "multiversx-sc-modules", + "sc_whitelist_module", + "simple-lock", + "unwrappable", + "utils", +] + +[[package]] +name = "energy-query" +version = "0.0.0" +dependencies = [ + "energy-factory", + "multiversx-sc", +] + +[[package]] +name = "events" +version = "0.0.0" +dependencies = [ + "common_structs", + "contexts", + "multiversx-sc", +] + +[[package]] +name = "farm-boosted-yields" +version = "0.0.0" +dependencies = [ + "common-types", + "config", + "energy-query", + "multiversx-sc", + "pausable", + "permissions_module", + "week-timekeeping", + "weekly-rewards-splitting", +] + +[[package]] +name = "farm-concentrated-liq" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "contexts", + "energy-query", + "events", + "farm-boosted-yields", + "farm_base_impl", + "farm_token", + "fixed-supply-token", + "mergeable", + "multiversx-sc", + "multiversx-sc-modules", + "pair", + "pausable", + "permissions_module", + "rewards", + "sc_whitelist_module", + "token_send", + "utils", + "week-timekeeping", + "weekly-rewards-splitting", +] + +[[package]] +name = "farm-concentrated-liq-wasm" +version = "0.0.0" +dependencies = [ + "farm-concentrated-liq", + "multiversx-sc-wasm-adapter", +] + +[[package]] +name = "farm_base_impl" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "contexts", + "events", + "farm_token", + "fixed-supply-token", + "mergeable", + "multiversx-sc", + "multiversx-sc-modules", + "pausable", + "permissions_module", + "rewards", + "token_merge_helper", + "token_send", + "utils", +] + +[[package]] +name = "farm_token" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "multiversx-sc", + "multiversx-sc-modules", + "pausable", + "permissions_module", + "token_send", +] + +[[package]] +name = "fees-collector" +version = "0.0.0" +dependencies = [ + "common-types", + "common_errors", + "energy-factory", + "energy-query", + "locking_module", + "multiversx-sc", + "multiversx-sc-modules", + "sc_whitelist_module", + "simple-lock", + "utils", + "week-timekeeping", + "weekly-rewards-splitting", +] + +[[package]] +name = "fixed-supply-token" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "legacy_token_decode_module" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", + "utils", +] + +[[package]] +name = "locking_module" +version = "0.0.0" +dependencies = [ + "energy-factory", + "multiversx-sc", + "simple-lock", +] + +[[package]] +name = "math" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "mergeable" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c94b173dc5ff0e157f767275fe6b7a1b4d2ad343bef7b66cd22a6353e016b93" +dependencies = [ + "bitflags", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19908153158c03df4582af08f47c0eb39fb52a7dff4736b301a66acbbb9955d3" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3b03b43f9cad320992f54ed162de2ed63e3ec83ed01361e57ee9c1865fba5a2" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b78945957036c281ad6ee21bb5120dcefa2017688adf43ec94e3e7c982efb09" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-modules" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c63ffaba95e630ff75981e2f5f50da64f523219b52f484234c66f3adc248885f" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9579f40c00da56a5a68e010ff851fa48ac7b9c6a16ad4314795cb32d889d9e78" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "pair" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "fees-collector", + "itertools", + "multiversx-sc", + "pausable", + "permissions_module", + "simple-lock", + "token_send", + "utils", +] + +[[package]] +name = "pausable" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "permissions_module", +] + +[[package]] +name = "permissions_module" +version = "0.0.0" +dependencies = [ + "bitflags", + "common_errors", + "multiversx-sc", +] + +[[package]] +name = "proc-macro2" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "907a61bd0f64c2f29cd1cf1dc34d05176426a3f504a78010f08416ddb7b13708" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "rewards" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "farm_token", + "multiversx-sc", + "multiversx-sc-modules", + "pausable", + "permissions_module", + "token_send", +] + +[[package]] +name = "sc_whitelist_module" +version = "0.0.0" +dependencies = [ + "common_errors", + "multiversx-sc", +] + +[[package]] +name = "simple-lock" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", + "multiversx-sc-modules", +] + +[[package]] +name = "smallvec" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" + +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "token_merge_helper" +version = "0.0.0" +dependencies = [ + "common_errors", + "multiversx-sc", +] + +[[package]] +name = "token_send" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "multiversx-sc", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unwrappable" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "utils" +version = "0.0.0" +dependencies = [ + "common_structs", + "fixed-supply-token", + "mergeable", + "multiversx-sc", +] + +[[package]] +name = "week-timekeeping" +version = "0.0.0" +dependencies = [ + "common-types", + "multiversx-sc", +] + +[[package]] +name = "weekly-rewards-splitting" +version = "0.0.0" +dependencies = [ + "common-types", + "energy-query", + "math", + "multiversx-sc", + "unwrappable", + "week-timekeeping", +] diff --git a/dex/farm-concentrated-liq/wasm/Cargo.toml b/dex/farm-concentrated-liq/wasm/Cargo.toml new file mode 100644 index 000000000..9f9fc90d3 --- /dev/null +++ b/dex/farm-concentrated-liq/wasm/Cargo.toml @@ -0,0 +1,31 @@ +# Code generated by the multiversx-sc build system. DO NOT EDIT. + +# ########################################## +# ############## AUTO-GENERATED ############# +# ########################################## + +[package] +name = "farm-concentrated-liq-wasm" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] + +[profile.release] +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" +overflow-checks = false + +[dependencies.farm-concentrated-liq] +path = ".." + +[dependencies.multiversx-sc-wasm-adapter] +version = "=0.46.1" + +[workspace] +members = ["."] diff --git a/dex/farm-concentrated-liq/wasm/src/lib.rs b/dex/farm-concentrated-liq/wasm/src/lib.rs new file mode 100644 index 000000000..b4109c2b1 --- /dev/null +++ b/dex/farm-concentrated-liq/wasm/src/lib.rs @@ -0,0 +1,91 @@ +// Code generated by the multiversx-sc build system. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +// Init: 1 +// Endpoints: 65 +// Async Callback: 1 +// Total number of exported functions: 67 + +#![no_std] +#![allow(internal_features)] +#![feature(lang_items)] + +multiversx_sc_wasm_adapter::allocator!(); +multiversx_sc_wasm_adapter::panic_handler!(); + +multiversx_sc_wasm_adapter::endpoints! { + farm_concentrated_liq + ( + init => init + upgrade => upgrade + enterFarm => enter_farm_endpoint + claimRewards => claim_rewards_endpoint + compoundRewards => compound_rewards_endpoint + exitFarm => exit_farm_endpoint + mergeFarmTokens => merge_farm_tokens_endpoint + claimBoostedRewards => claim_boosted_rewards + startProduceRewards => start_produce_rewards_endpoint + endProduceRewards => end_produce_rewards_endpoint + setPerBlockRewardAmount => set_per_block_rewards_endpoint + calculateRewardsForGivenPosition => calculate_rewards_for_given_position + getRewardPerShare => reward_per_share + getRewardReserve => reward_reserve + allowExternalClaimBoostedRewards => allow_external_claim_boosted_rewards + getAllowExternalClaimRewards => get_allow_external_claim_rewards + getFarmingTokenId => farming_token_id + getRewardTokenId => reward_token_id + getPerBlockRewardAmount => per_block_reward_amount + getLastRewardBlockNonce => last_reward_block_nonce + getDivisionSafetyConstant => division_safety_constant + getUserTotalFarmPosition => user_total_farm_position + getFarmPositionMigrationNonce => farm_position_migration_nonce + registerFarmToken => register_farm_token + getFarmTokenId => farm_token + getFarmTokenSupply => farm_token_supply + addToPauseWhitelist => add_to_pause_whitelist + removeFromPauseWhitelist => remove_from_pause_whitelist + pause => pause + resume => resume + getState => state + addAdmin => add_admin_endpoint + removeAdmin => remove_admin_endpoint + updateOwnerOrAdmin => update_owner_or_admin_endpoint + getPermissions => permissions + addSCAddressToWhitelist => add_sc_address_to_whitelist + removeSCAddressFromWhitelist => remove_sc_address_from_whitelist + isSCAddressWhitelisted => is_sc_address_whitelisted + set_penalty_percent => set_penalty_percent + set_minimum_farming_epochs => set_minimum_farming_epochs + set_burn_gas_limit => set_burn_gas_limit + getPenaltyPercent => penalty_percent + getMinimumFarmingEpoch => minimum_farming_epochs + getBurnGasLimit => burn_gas_limit + getPairContractManagedAddress => pair_contract_address + setBoostedYieldsRewardsPercentage => set_boosted_yields_rewards_percentage + collectUndistributedBoostedRewards => collect_undistributed_boosted_rewards + getBoostedYieldsRewardsPercentage => boosted_yields_rewards_percentage + getAccumulatedRewardsForWeek => accumulated_rewards_for_week + getFarmSupplyForWeek => farm_supply_for_week + getRemainingBoostedRewardsToDistribute => remaining_boosted_rewards_to_distribute + getUndistributedBoostedRewards => undistributed_boosted_rewards + setBoostedYieldsFactors => set_boosted_yields_factors + getBoostedYieldsFactors => get_boosted_yields_factors + getCurrentWeek => get_current_week + getFirstWeekStartEpoch => first_week_start_epoch + getLastActiveWeekForUser => get_last_active_week_for_user_view + getUserEnergyForWeek => get_user_energy_for_week_view + getLastGlobalUpdateWeek => last_global_update_week + getTotalRewardsForWeek => total_rewards_for_week + getTotalEnergyForWeek => total_energy_for_week + getTotalLockedTokensForWeek => total_locked_tokens_for_week + updateEnergyForUser => update_energy_for_user + getCurrentClaimProgress => current_claim_progress + setEnergyFactoryAddress => set_energy_factory_address + getEnergyFactoryAddress => energy_factory_address + ) +} + +multiversx_sc_wasm_adapter::async_callback! { farm_concentrated_liq } From e08920491043ff25674f6b818d7b337a334be5be Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 20 Feb 2024 12:45:43 +0200 Subject: [PATCH 2/4] rip migration --- common/modules/farm/config/src/config.rs | 5 - .../farm/farm_base_impl/src/base_farm_init.rs | 6 +- .../farm_base_impl/src/base_traits_impl.rs | 9 - .../src/base_functions.rs | 33 --- dex/farm-concentrated-liq/src/lib.rs | 27 +-- dex/farm-with-locked-rewards/src/lib.rs | 12 -- dex/farm/src/base_functions.rs | 33 --- dex/farm/src/lib.rs | 15 -- dex/farm/tests/total_farm_position_test.rs | 198 ------------------ .../farm-staking/src/base_impl_wrapper.rs | 8 - .../src/claim_only_boosted_staking_rewards.rs | 33 --- .../src/claim_stake_farm_rewards.rs | 1 - .../src/compound_stake_farm_rewards.rs | 1 - farm-staking/farm-staking/src/lib.rs | 2 - farm-staking/farm-staking/src/stake_farm.rs | 1 - farm-staking/farm-staking/src/unstake_farm.rs | 4 - 16 files changed, 7 insertions(+), 381 deletions(-) diff --git a/common/modules/farm/config/src/config.rs b/common/modules/farm/config/src/config.rs index b712765e5..a5a3a3ce6 100644 --- a/common/modules/farm/config/src/config.rs +++ b/common/modules/farm/config/src/config.rs @@ -69,11 +69,6 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio } } - fn is_old_farm_position(&self, token_nonce: Nonce) -> bool { - let farm_position_migration_nonce = self.farm_position_migration_nonce().get(); - token_nonce > 0 && token_nonce < farm_position_migration_nonce - } - fn try_set_farm_position_migration_nonce( &self, farm_token_mapper: NonFungibleTokenMapper, diff --git a/common/modules/farm/farm_base_impl/src/base_farm_init.rs b/common/modules/farm/farm_base_impl/src/base_farm_init.rs index 700057a3e..1f1431abf 100644 --- a/common/modules/farm/farm_base_impl/src/base_farm_init.rs +++ b/common/modules/farm/farm_base_impl/src/base_farm_init.rs @@ -32,10 +32,10 @@ pub trait BaseFarmInitModule: self.state().set(State::Inactive); self.division_safety_constant() - .set_if_empty(&division_safety_constant); + .set(&division_safety_constant); - self.reward_token_id().set_if_empty(&reward_token_id); - self.farming_token_id().set_if_empty(&farming_token_id); + self.reward_token_id().set(&reward_token_id); + self.farming_token_id().set(&farming_token_id); if !owner.is_zero() { self.add_permissions(owner, Permissions::OWNER | Permissions::PAUSE); diff --git a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs index f2ccc293c..61dfc1fdd 100644 --- a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs +++ b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs @@ -198,10 +198,6 @@ pub trait FarmContract { 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<::Api> = farm_token_mapper.get_token_attributes(farm_position.token_nonce); @@ -212,7 +208,6 @@ pub trait FarmContract { } } - #[inline] fn increase_user_farm_position( sc: &Self::FarmSc, user: &ManagedAddress<::Api>, @@ -228,10 +223,6 @@ pub trait FarmContract { sc: &Self::FarmSc, farm_position: &EsdtTokenPayment<::Api>, ) { - if sc.is_old_farm_position(farm_position.token_nonce) { - return; - } - let farm_token_mapper = sc.farm_token(); let token_attributes: FarmTokenAttributes<::Api> = farm_token_mapper.get_token_attributes(farm_position.token_nonce); diff --git a/dex/farm-concentrated-liq/src/base_functions.rs b/dex/farm-concentrated-liq/src/base_functions.rs index 968e3381d..536de7d7a 100644 --- a/dex/farm-concentrated-liq/src/base_functions.rs +++ b/dex/farm-concentrated-liq/src/base_functions.rs @@ -206,39 +206,6 @@ pub trait BaseFunctionsModule: reward } - fn migrate_old_farm_positions(&self, caller: &ManagedAddress) -> BigUint { - let payments = self.get_non_empty_payments(); - let farm_token_mapper = self.farm_token(); - let farm_token_id = farm_token_mapper.get_token_id(); - let mut migrated_amount = BigUint::zero(); - for farm_position in &payments { - if farm_position.token_identifier == farm_token_id - && self.is_old_farm_position(farm_position.token_nonce) - { - migrated_amount += farm_position.amount; - } - } - - if migrated_amount > 0 { - let mut user_total_farm_position = self.get_user_total_farm_position(caller); - user_total_farm_position.total_farm_position += &migrated_amount; - self.user_total_farm_position(caller) - .set(user_total_farm_position); - } - - migrated_amount - } - - fn decrease_old_farm_positions(&self, migrated_amount: BigUint, caller: &ManagedAddress) { - if migrated_amount == BigUint::zero() { - return; - } - self.user_total_farm_position(caller) - .update(|user_total_farm_position| { - user_total_farm_position.total_farm_position -= migrated_amount; - }); - } - fn end_produce_rewards>(&self) { let mut storage = StorageCache::new(self); FC::generate_aggregated_rewards(self, &mut storage); diff --git a/dex/farm-concentrated-liq/src/lib.rs b/dex/farm-concentrated-liq/src/lib.rs index 48cf893f1..9492b9fa0 100644 --- a/dex/farm-concentrated-liq/src/lib.rs +++ b/dex/farm-concentrated-liq/src/lib.rs @@ -68,18 +68,14 @@ pub trait Farm: admins, ); - self.penalty_percent().set_if_empty(DEFAULT_PENALTY_PERCENT); + self.penalty_percent().set(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); + .set(DEFAULT_MINUMUM_FARMING_EPOCHS); + self.burn_gas_limit().set(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); - - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); + self.first_week_start_epoch().set(current_epoch); } #[endpoint] @@ -97,8 +93,6 @@ pub trait Farm: ) -> EnterFarmResultType { 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 = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); @@ -120,9 +114,6 @@ pub trait Farm: ) -> ClaimRewardsResultType { 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::>(orig_caller); self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token); @@ -139,9 +130,6 @@ pub trait Farm: ) -> EsdtTokenPayment { 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 output_farm_token_payment = self.compound_rewards::>(orig_caller.clone()); self.send_payment_non_zero(&caller, &output_farm_token_payment); @@ -161,13 +149,8 @@ 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::>(orig_caller.clone(), payment); - self.decrease_old_farm_positions(migrated_amount, &orig_caller); - self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens); self.send_payment_non_zero(&caller, &exit_farm_result.rewards); @@ -184,8 +167,6 @@ pub trait Farm: ) -> DoubleMultiPayment { 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 = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 473fa77d2..23b3bc130 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -96,8 +96,6 @@ pub trait Farm: ) -> EnterFarmResultType { 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(), @@ -122,9 +120,6 @@ pub trait Farm: ) -> ClaimRewardsResultType { 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 payments = self.call_value().all_esdt_transfers().clone_value(); let base_claim_rewards_result = self.claim_rewards_base::>(orig_caller.clone(), payments); @@ -161,13 +156,8 @@ 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::>(orig_caller.clone(), payment); - self.decrease_old_farm_positions(migrated_amount, &orig_caller); - let rewards = exit_farm_result.rewards; self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens); @@ -191,8 +181,6 @@ pub trait Farm: ) -> DoubleMultiPayment { 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 merged_farm_token = self.merge_farm_tokens::>(); diff --git a/dex/farm/src/base_functions.rs b/dex/farm/src/base_functions.rs index 968e3381d..536de7d7a 100644 --- a/dex/farm/src/base_functions.rs +++ b/dex/farm/src/base_functions.rs @@ -206,39 +206,6 @@ pub trait BaseFunctionsModule: reward } - fn migrate_old_farm_positions(&self, caller: &ManagedAddress) -> BigUint { - let payments = self.get_non_empty_payments(); - let farm_token_mapper = self.farm_token(); - let farm_token_id = farm_token_mapper.get_token_id(); - let mut migrated_amount = BigUint::zero(); - for farm_position in &payments { - if farm_position.token_identifier == farm_token_id - && self.is_old_farm_position(farm_position.token_nonce) - { - migrated_amount += farm_position.amount; - } - } - - if migrated_amount > 0 { - let mut user_total_farm_position = self.get_user_total_farm_position(caller); - user_total_farm_position.total_farm_position += &migrated_amount; - self.user_total_farm_position(caller) - .set(user_total_farm_position); - } - - migrated_amount - } - - fn decrease_old_farm_positions(&self, migrated_amount: BigUint, caller: &ManagedAddress) { - if migrated_amount == BigUint::zero() { - return; - } - self.user_total_farm_position(caller) - .update(|user_total_farm_position| { - user_total_farm_position.total_farm_position -= migrated_amount; - }); - } - fn end_produce_rewards>(&self) { let mut storage = StorageCache::new(self); FC::generate_aggregated_rewards(self, &mut storage); diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 48cf893f1..91933f140 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -97,8 +97,6 @@ pub trait Farm: ) -> EnterFarmResultType { 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 = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); @@ -120,9 +118,6 @@ pub trait Farm: ) -> ClaimRewardsResultType { 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::>(orig_caller); self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token); @@ -139,9 +134,6 @@ pub trait Farm: ) -> EsdtTokenPayment { 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 output_farm_token_payment = self.compound_rewards::>(orig_caller.clone()); self.send_payment_non_zero(&caller, &output_farm_token_payment); @@ -161,13 +153,8 @@ 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::>(orig_caller.clone(), payment); - self.decrease_old_farm_positions(migrated_amount, &orig_caller); - self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens); self.send_payment_non_zero(&caller, &exit_farm_result.rewards); @@ -184,8 +171,6 @@ pub trait Farm: ) -> DoubleMultiPayment { 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 = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); diff --git a/dex/farm/tests/total_farm_position_test.rs b/dex/farm/tests/total_farm_position_test.rs index 26756ebaa..3e16ebcbe 100644 --- a/dex/farm/tests/total_farm_position_test.rs +++ b/dex/farm/tests/total_farm_position_test.rs @@ -3,7 +3,6 @@ mod farm_setup; use common_structs::FarmTokenAttributes; -use config::ConfigModule; use farm_setup::multi_user_farm_setup::{MultiUserFarmSetup, BOOSTED_YIELDS_PERCENTAGE}; use multiversx_sc_scenario::{managed_address, managed_biguint, rust_biguint, DebugApi}; @@ -283,203 +282,6 @@ fn total_farm_position_claim_for_other_test() { ); } -#[test] -fn farm_total_position_migration_test() { - DebugApi::dummy(); - let mut farm_setup = MultiUserFarmSetup::new( - farm::contract_obj, - energy_factory_mock::contract_obj, - energy_update::contract_obj, - ); - - farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); - farm_setup.set_boosted_yields_factors(); - farm_setup.b_mock.set_block_epoch(2); - - let temp_user = farm_setup.third_user.clone(); - - // first user enter farm - let farm_in_amount = 50_000_000; - let first_user = farm_setup.first_user.clone(); - farm_setup.set_user_energy(&first_user, 1_000, 2, 1); - farm_setup.enter_farm(&first_user, farm_in_amount); - - // Remove current farm position from storage - farm_setup.set_user_total_farm_position(&first_user, 0); - farm_setup.check_user_total_farm_position(&first_user, 0); - - // User enters farm again - farm_setup.enter_farm(&first_user, farm_in_amount); - farm_setup.check_user_total_farm_position(&first_user, farm_in_amount); - - // Set farm position migration nonce - farm_setup - .b_mock - .execute_tx( - &farm_setup.owner, - &farm_setup.farm_wrapper, - &rust_biguint!(0), - |sc| { - sc.farm_position_migration_nonce().set(2); - }, - ) - .assert_ok(); - - farm_setup.check_farm_token_supply(farm_in_amount * 2); - - // users claim rewards to get their energy registered - let _ = farm_setup.claim_rewards(&first_user, 2, farm_in_amount); - - // advance blocks - 10 blocks - 10 * 1_000 = 10_000 total rewards - // 7_500 base farm, 2_500 boosted yields - farm_setup.b_mock.set_block_nonce(10); - - // random tx on end of week 1, to cummulate rewards - farm_setup.b_mock.set_block_epoch(6); - farm_setup.set_user_energy(&first_user, 1_000, 6, 1); - farm_setup.set_user_energy(&temp_user, 1, 6, 1); - farm_setup.enter_farm(&temp_user, 1); - farm_setup.exit_farm(&temp_user, 4, 1); - - // advance 1 week - farm_setup.b_mock.set_block_epoch(10); - farm_setup.set_user_energy(&first_user, 1_000, 10, 1); - - let total_farm_tokens = farm_in_amount * 2; - - // first user claim with half total position - let first_base_farm_amt = farm_in_amount * 7_500 / total_farm_tokens; - - let first_boosted_amt = 2_000; // claim boosted with only half total position - not full rewards - let first_total_rewards = first_base_farm_amt + first_boosted_amt; - - let first_received_reward_amt = farm_setup.claim_rewards(&first_user, 3, farm_in_amount); - - // Should be equal to half base generated rewards + partial boosted generated rewards - assert_eq!(first_received_reward_amt, first_total_rewards); - - farm_setup - .b_mock - .check_nft_balance::>( - &first_user, - FARM_TOKEN_ID, - 5, - &rust_biguint!(farm_in_amount), - None, - ); - - farm_setup.b_mock.check_esdt_balance( - &first_user, - REWARD_TOKEN_ID, - &rust_biguint!(first_received_reward_amt), - ); - - // advance 10 more blocks - 10_000 more total rewards - // 7_500 base farm, 2_500 boosted yields - farm_setup.b_mock.set_block_nonce(20); - - // random tx on end of week 2, to cummulate rewards - farm_setup.b_mock.set_block_epoch(13); - farm_setup.set_user_energy(&first_user, 1_000, 13, 1); - farm_setup.set_user_energy(&temp_user, 1, 13, 1); - farm_setup.enter_farm(&temp_user, 1); - farm_setup.exit_farm(&temp_user, 6, 1); - - // advance 1 week - farm_setup.b_mock.set_block_epoch(20); - farm_setup.set_user_energy(&first_user, 1_000, 20, 1); - - // user claims with old position - should migrate his entire position - let second_received_reward_amt = farm_setup.claim_rewards(&first_user, 1, farm_in_amount); - - farm_setup.check_user_total_farm_position(&first_user, farm_in_amount * 2); - - let second_base_farm_amt = (farm_in_amount * 7_500 / total_farm_tokens) * 2; // user claims with initial position (2 weeks worth of rewards) - let second_boosted_amt = 2_500; // claim boosted with entire total position - receives full rewards - let second_total_rewards = second_base_farm_amt + second_boosted_amt; - assert_eq!(second_received_reward_amt, second_total_rewards); -} - -#[test] -fn farm_total_position_exit_migration_test() { - DebugApi::dummy(); - let mut farm_setup = MultiUserFarmSetup::new( - farm::contract_obj, - energy_factory_mock::contract_obj, - energy_update::contract_obj, - ); - - farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); - farm_setup.set_boosted_yields_factors(); - farm_setup.b_mock.set_block_epoch(2); - - let temp_user = farm_setup.third_user.clone(); - - // first user enter farm - let farm_in_amount = 50_000_000; - let first_user = farm_setup.first_user.clone(); - farm_setup.set_user_energy(&first_user, 1_000, 2, 1); - farm_setup.enter_farm(&first_user, farm_in_amount); - - // Remove current farm position from storage - farm_setup.set_user_total_farm_position(&first_user, 0); - farm_setup.check_user_total_farm_position(&first_user, 0); - - // User enters farm again - farm_setup.enter_farm(&first_user, farm_in_amount); - farm_setup.check_user_total_farm_position(&first_user, farm_in_amount); - - // Set farm position migration nonce - farm_setup - .b_mock - .execute_tx( - &farm_setup.owner, - &farm_setup.farm_wrapper, - &rust_biguint!(0), - |sc| { - sc.farm_position_migration_nonce().set(2); - }, - ) - .assert_ok(); - - farm_setup.check_farm_token_supply(farm_in_amount * 2); - - // users claim rewards to get their energy registered - let _ = farm_setup.claim_rewards(&first_user, 2, farm_in_amount); - - // advance blocks - 10 blocks - 10 * 1_000 = 10_000 total rewards - // 7_500 base farm, 2_500 boosted yields - farm_setup.b_mock.set_block_nonce(10); - - // random tx on end of week 1, to cummulate rewards - farm_setup.b_mock.set_block_epoch(6); - farm_setup.set_user_energy(&first_user, 1_000, 6, 1); - farm_setup.set_user_energy(&temp_user, 1, 6, 1); - farm_setup.enter_farm(&temp_user, 1); - farm_setup.exit_farm(&temp_user, 4, 1); - - // advance 1 week - farm_setup.b_mock.set_block_epoch(10); - farm_setup.set_user_energy(&first_user, 1_000, 10, 1); - - // first user exist farm with old position - farm_setup.exit_farm(&first_user, 1, farm_in_amount); - - // user farm position should be unchanged - farm_setup.check_user_total_farm_position(&first_user, farm_in_amount); - - // User should receive half base rewards and full boosted rewards - let total_farm_tokens = farm_in_amount * 2; - let first_base_farm_amt = farm_in_amount * 7_500 / total_farm_tokens; - let first_boosted_amt = 2_500; - let first_total_rewards = first_base_farm_amt + first_boosted_amt; - farm_setup.b_mock.check_esdt_balance( - &first_user, - REWARD_TOKEN_ID, - &rust_biguint!(first_total_rewards), - ); -} - #[test] fn no_boosted_rewards_penalty_for_no_energy_test() { DebugApi::dummy(); diff --git a/farm-staking/farm-staking/src/base_impl_wrapper.rs b/farm-staking/farm-staking/src/base_impl_wrapper.rs index 2b671677f..a29a64549 100644 --- a/farm-staking/farm-staking/src/base_impl_wrapper.rs +++ b/farm-staking/farm-staking/src/base_impl_wrapper.rs @@ -192,10 +192,6 @@ where ) { let farm_token_mapper = sc.farm_token(); for farm_position in farm_positions { - if sc.is_old_farm_position(farm_position.token_nonce) { - continue; - } - farm_token_mapper.require_same_token(&farm_position.token_identifier); let token_attributes: StakingFarmTokenAttributes<::Api> = @@ -224,10 +220,6 @@ where sc: &Self::FarmSc, farm_position: &EsdtTokenPayment<::Api>, ) { - if sc.is_old_farm_position(farm_position.token_nonce) { - return; - } - let farm_token_mapper = sc.farm_token(); let token_attributes: StakingFarmTokenAttributes<::Api> = farm_token_mapper.get_token_attributes(farm_position.token_nonce); diff --git a/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs b/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs index 71e7ab9e3..f1138d6c0 100644 --- a/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs +++ b/farm-staking/farm-staking/src/claim_only_boosted_staking_rewards.rs @@ -49,39 +49,6 @@ pub trait ClaimOnlyBoostedStakingRewardsModule: boosted_rewards_payment } - fn migrate_old_farm_positions(&self, caller: &ManagedAddress) -> BigUint { - let payments = self.call_value().all_esdt_transfers().clone_value(); - let farm_token_mapper = self.farm_token(); - let farm_token_id = farm_token_mapper.get_token_id(); - let mut migrated_amount = BigUint::zero(); - for farm_position in &payments { - if farm_position.token_identifier == farm_token_id - && self.is_old_farm_position(farm_position.token_nonce) - { - migrated_amount += farm_position.amount; - } - } - - if migrated_amount > 0 { - let mut user_total_farm_position = self.get_user_total_farm_position(caller); - user_total_farm_position.total_farm_position += &migrated_amount; - self.user_total_farm_position(caller) - .set(user_total_farm_position); - } - - migrated_amount - } - - fn decrease_old_farm_positions(&self, migrated_amount: BigUint, caller: &ManagedAddress) { - if migrated_amount == BigUint::zero() { - return; - } - self.user_total_farm_position(caller) - .update(|user_total_farm_position| { - user_total_farm_position.total_farm_position -= migrated_amount; - }); - } - // Cannot import the one from farm, as the Wrapper struct has different dependencies fn claim_only_boosted_payment(&self, caller: &ManagedAddress) -> BigUint { let reward = FarmStakingWrapper::::calculate_boosted_rewards(self, caller); diff --git a/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs b/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs index e3d0b1dc4..56ba824a0 100644 --- a/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs +++ b/farm-staking/farm-staking/src/claim_stake_farm_rewards.rs @@ -61,7 +61,6 @@ pub trait ClaimStakeFarmRewardsModule: original_caller: ManagedAddress, opt_new_farming_amount: Option, ) -> ClaimRewardsResultType { - self.migrate_old_farm_positions(&original_caller); let payment = self.call_value().single_esdt(); let mut claim_result = self .claim_rewards_base_no_farm_token_mint::>( diff --git a/farm-staking/farm-staking/src/compound_stake_farm_rewards.rs b/farm-staking/farm-staking/src/compound_stake_farm_rewards.rs index a7d83d574..35beed24f 100644 --- a/farm-staking/farm-staking/src/compound_stake_farm_rewards.rs +++ b/farm-staking/farm-staking/src/compound_stake_farm_rewards.rs @@ -33,7 +33,6 @@ pub trait CompoundStakeFarmRewardsModule: #[endpoint(compoundRewards)] fn compound_rewards(&self) -> EsdtTokenPayment { let caller = self.blockchain().get_caller(); - self.migrate_old_farm_positions(&caller); let payments = self.get_non_empty_payments(); let compound_result = self.compound_rewards_base::>(caller.clone(), payments); diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index ac1f23727..301cc3eb9 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -105,8 +105,6 @@ pub trait FarmStaking: #[endpoint(mergeFarmTokens)] fn merge_farm_tokens_endpoint(&self) -> DoubleMultiPayment { let caller = self.blockchain().get_caller(); - self.migrate_old_farm_positions(&caller); - let boosted_rewards = self.claim_only_boosted_payment(&caller); let boosted_rewards_payment = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); diff --git a/farm-staking/farm-staking/src/stake_farm.rs b/farm-staking/farm-staking/src/stake_farm.rs index 7ab3dea1c..9fc180761 100644 --- a/farm-staking/farm-staking/src/stake_farm.rs +++ b/farm-staking/farm-staking/src/stake_farm.rs @@ -72,7 +72,6 @@ pub trait StakeFarmModule: payments: PaymentsVec, ) -> EnterFarmResultType { let caller = self.blockchain().get_caller(); - self.migrate_old_farm_positions(&original_caller); let boosted_rewards = self.claim_only_boosted_payment(&original_caller); let boosted_rewards_payment = EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); diff --git a/farm-staking/farm-staking/src/unstake_farm.rs b/farm-staking/farm-staking/src/unstake_farm.rs index e40642b4f..95df940ca 100644 --- a/farm-staking/farm-staking/src/unstake_farm.rs +++ b/farm-staking/farm-staking/src/unstake_farm.rs @@ -72,13 +72,9 @@ pub trait UnstakeFarmModule: payment: EsdtTokenPayment, opt_unbond_amount: Option, ) -> ExitFarmWithPartialPosResultType { - let migrated_amount = self.migrate_old_farm_positions(&original_caller); - let exit_result = self.exit_farm_base::>(original_caller.clone(), payment); - self.decrease_old_farm_positions(migrated_amount, &original_caller); - let unbond_token_amount = opt_unbond_amount.unwrap_or(exit_result.farming_token_payment.amount); let farm_token_id = exit_result.storage_cache.farm_token_id.clone(); From a4fe8ad3b2773022f8c2b50e41e6f693257dcfd4 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 20 Feb 2024 12:52:31 +0200 Subject: [PATCH 3/4] more migration remove --- common/modules/farm/config/src/config.rs | 26 ------------------------ dex/farm-concentrated-liq/src/lib.rs | 6 +----- dex/farm-with-locked-rewards/src/lib.rs | 18 +++++----------- dex/farm/src/lib.rs | 18 +++++----------- farm-staking/farm-staking/src/lib.rs | 16 ++++----------- 5 files changed, 15 insertions(+), 69 deletions(-) diff --git a/common/modules/farm/config/src/config.rs b/common/modules/farm/config/src/config.rs index a5a3a3ce6..eb13c27c0 100644 --- a/common/modules/farm/config/src/config.rs +++ b/common/modules/farm/config/src/config.rs @@ -69,28 +69,6 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio } } - fn try_set_farm_position_migration_nonce( - &self, - farm_token_mapper: NonFungibleTokenMapper, - ) { - if !self.farm_position_migration_nonce().is_empty() { - return; - } - - let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() { - let token_identifier = farm_token_mapper.get_token_id_ref(); - let current_nonce = self - .blockchain() - .get_current_esdt_nft_nonce(&self.blockchain().get_sc_address(), token_identifier); - current_nonce + DEFAULT_FARM_POSITION_MIGRATION_NONCE - } else { - DEFAULT_FARM_POSITION_MIGRATION_NONCE - }; - - self.farm_position_migration_nonce() - .set(migration_farm_token_nonce); - } - #[view(getFarmingTokenId)] #[storage_mapper("farming_token_id")] fn farming_token_id(&self) -> SingleValueMapper; @@ -120,8 +98,4 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio &self, user: &ManagedAddress, ) -> SingleValueMapper>; - - #[view(getFarmPositionMigrationNonce)] - #[storage_mapper("farm_position_migration_nonce")] - fn farm_position_migration_nonce(&self) -> SingleValueMapper; } diff --git a/dex/farm-concentrated-liq/src/lib.rs b/dex/farm-concentrated-liq/src/lib.rs index 9492b9fa0..9da99566d 100644 --- a/dex/farm-concentrated-liq/src/lib.rs +++ b/dex/farm-concentrated-liq/src/lib.rs @@ -79,11 +79,7 @@ pub trait Farm: } #[endpoint] - fn upgrade(&self) { - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); - } + fn upgrade(&self) {} #[payable("*")] #[endpoint(enterFarm)] diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 23b3bc130..0597f9b90 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -67,26 +67,18 @@ pub trait Farm: admins, ); - self.penalty_percent().set_if_empty(DEFAULT_PENALTY_PERCENT); + self.penalty_percent().set(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); + .set(DEFAULT_MINUMUM_FARMING_EPOCHS); + self.burn_gas_limit().set(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); - - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); + self.first_week_start_epoch().set(current_epoch); } #[endpoint] - fn upgrade(&self) { - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); - } + fn upgrade(&self) {} #[payable("*")] #[endpoint(enterFarm)] diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 91933f140..9da99566d 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -68,26 +68,18 @@ pub trait Farm: admins, ); - self.penalty_percent().set_if_empty(DEFAULT_PENALTY_PERCENT); + self.penalty_percent().set(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); + .set(DEFAULT_MINUMUM_FARMING_EPOCHS); + self.burn_gas_limit().set(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); - - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); + self.first_week_start_epoch().set(current_epoch); } #[endpoint] - fn upgrade(&self) { - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); - } + fn upgrade(&self) {} #[payable("*")] #[endpoint(enterFarm)] diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index 301cc3eb9..10485cbd7 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -81,25 +81,17 @@ pub trait FarmStaking: ); require!(max_apr > 0u64, "Invalid max APR percentage"); - self.max_annual_percentage_rewards().set_if_empty(&max_apr); + self.max_annual_percentage_rewards().set(&max_apr); require!( min_unbond_epochs <= MAX_MIN_UNBOND_EPOCHS, "Invalid min unbond epochs" ); - self.min_unbond_epochs().set_if_empty(min_unbond_epochs); - - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); + self.min_unbond_epochs().set(min_unbond_epochs); } #[endpoint] - fn upgrade(&self) { - // Farm position migration code - let farm_token_mapper = self.farm_token(); - self.try_set_farm_position_migration_nonce(farm_token_mapper); - } + fn upgrade(&self) {} #[payable("*")] #[endpoint(mergeFarmTokens)] @@ -121,7 +113,7 @@ pub trait FarmStaking: (merged_farm_token, boosted_rewards_payment).into() } - + #[view(calculateRewardsForGivenPosition)] fn calculate_rewards_for_given_position( &self, From e5f62aeb61906fbebe3f1ed5b81b4356bc2cd5ce Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 20 Feb 2024 13:17:37 +0200 Subject: [PATCH 4/4] more cleanup --- .../farm_base_impl/src/base_traits_impl.rs | 49 +++++----- .../src/base_functions.rs | 91 ++++++++++--------- dex/farm-concentrated-liq/src/exit_penalty.rs | 30 +++--- dex/farm-concentrated-liq/src/lib.rs | 50 ++++++---- dex/farm-concentrated-liq/wasm/src/lib.rs | 11 +-- 5 files changed, 124 insertions(+), 107 deletions(-) diff --git a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs index 61dfc1fdd..ecf0f6e66 100644 --- a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs +++ b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs @@ -63,19 +63,18 @@ pub trait FarmContract { ) -> BigUint<::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( @@ -83,15 +82,19 @@ pub trait FarmContract { storage_cache: &mut StorageCache, ) { let total_reward = Self::mint_per_block_rewards(sc, &storage_cache.reward_token_id); - if total_reward > 0u64 { - storage_cache.reward_reserve += &total_reward; + if total_reward == 0u64 { + return; + } - if storage_cache.farm_token_supply != 0u64 { - let increase = (&total_reward * &storage_cache.division_safety_constant) - / &storage_cache.farm_token_supply; - storage_cache.reward_per_share += &increase; - } + storage_cache.reward_reserve += &total_reward; + + if storage_cache.farm_token_supply == 0u64 { + return; } + + let increase = (&total_reward * &storage_cache.division_safety_constant) + / &storage_cache.farm_token_supply; + storage_cache.reward_per_share += &increase; } fn calculate_rewards( @@ -102,12 +105,12 @@ pub trait FarmContract { storage_cache: &StorageCache, ) -> BigUint<::Api> { let token_rps = token_attributes.get_reward_per_share(); - if storage_cache.reward_per_share > token_rps { - let rps_diff = &storage_cache.reward_per_share - &token_rps; - farm_token_amount * &rps_diff / &storage_cache.division_safety_constant - } else { - BigUint::zero() + if storage_cache.reward_per_share <= token_rps { + return BigUint::zero(); } + + let rps_diff = &storage_cache.reward_per_share - &token_rps; + farm_token_amount * &rps_diff / &storage_cache.division_safety_constant } fn create_enter_farm_initial_attributes( diff --git a/dex/farm-concentrated-liq/src/base_functions.rs b/dex/farm-concentrated-liq/src/base_functions.rs index 536de7d7a..3e6620120 100644 --- a/dex/farm-concentrated-liq/src/base_functions.rs +++ b/dex/farm-concentrated-liq/src/base_functions.rs @@ -15,32 +15,24 @@ use fixed_supply_token::FixedSupplyToken; use crate::exit_penalty; -pub type DoubleMultiPayment = MultiValue2, EsdtTokenPayment>; -pub type ClaimRewardsResultType = DoubleMultiPayment; -pub type ExitFarmResultType = DoubleMultiPayment; - pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1; -pub struct ClaimRewardsResultWrapper { +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)] +pub struct CompoundRewardsResultType { pub new_farm_token: EsdtTokenPayment, - pub rewards: EsdtTokenPayment, + pub compounded_rewards: BigUint, } -pub struct ExitFarmResultWrapper { - pub farming_tokens: EsdtTokenPayment, +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)] +pub struct ClaimRewardsResultType { + pub new_farm_token: EsdtTokenPayment, pub rewards: EsdtTokenPayment, } -impl Into> for ClaimRewardsResultWrapper { - fn into(self) -> ClaimRewardsResultType { - (self.new_farm_token, self.rewards).into() - } -} - -impl Into> for ExitFarmResultWrapper { - fn into(self) -> ExitFarmResultType { - (self.farming_tokens, self.rewards).into() - } +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)] +pub struct ExitFarmResultType { + pub farming_tokens: EsdtTokenPayment, + pub rewards: EsdtTokenPayment, } #[multiversx_sc::module] @@ -96,7 +88,7 @@ pub trait BaseFunctionsModule: fn claim_rewards>( &self, caller: ManagedAddress, - ) -> ClaimRewardsResultWrapper { + ) -> ClaimRewardsResultType { let payments = self.call_value().all_esdt_transfers().clone_value(); let base_claim_rewards_result = self.claim_rewards_base::(caller.clone(), payments); @@ -116,7 +108,7 @@ pub trait BaseFunctionsModule: base_claim_rewards_result.storage_cache, ); - ClaimRewardsResultWrapper { + ClaimRewardsResultType { new_farm_token: output_farm_token_payment, rewards: rewards_payment, } @@ -125,7 +117,7 @@ pub trait BaseFunctionsModule: fn compound_rewards>( &self, caller: ManagedAddress, - ) -> EsdtTokenPayment { + ) -> CompoundRewardsResultType { let payments = self.call_value().all_esdt_transfers().clone_value(); let base_compound_rewards_result = self.compound_rewards_base::(caller.clone(), payments); @@ -140,19 +132,22 @@ pub trait BaseFunctionsModule: &caller, base_compound_rewards_result.context, base_compound_rewards_result.new_farm_token, - base_compound_rewards_result.compounded_rewards, + base_compound_rewards_result.compounded_rewards.clone(), base_compound_rewards_result.created_with_merge, base_compound_rewards_result.storage_cache, ); - output_farm_token_payment + CompoundRewardsResultType { + new_farm_token: output_farm_token_payment, + compounded_rewards: base_compound_rewards_result.compounded_rewards, + } } fn exit_farm>( &self, caller: ManagedAddress, payment: EsdtTokenPayment, - ) -> ExitFarmResultWrapper { + ) -> ExitFarmResultType { let base_exit_farm_result = self.exit_farm_base::(caller.clone(), payment); let mut farming_token_payment = base_exit_farm_result.farming_token_payment; @@ -177,7 +172,7 @@ pub trait BaseFunctionsModule: base_exit_farm_result.storage_cache, ); - ExitFarmResultWrapper { + ExitFarmResultType { farming_tokens: farming_token_payment, rewards: reward_payment, } @@ -271,16 +266,20 @@ where storage_cache: &mut StorageCache, ) { 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 { + return; } + + let increase = (&split_rewards.base_farm * &storage_cache.division_safety_constant) + / &storage_cache.farm_token_supply; + storage_cache.reward_per_share += &increase; } fn calculate_rewards( @@ -311,10 +310,10 @@ where let user_farming_epochs = current_epoch - token_attributes.entering_epoch; let min_farming_epochs = sc.minimum_farming_epochs().get(); if user_farming_epochs >= min_farming_epochs { - BigUint::zero() - } else { - total_exit_amount * sc.penalty_percent().get() / exit_penalty::MAX_PERCENT + return BigUint::zero(); } + + total_exit_amount * sc.penalty_percent().get() / exit_penalty::MAX_PERCENT } fn apply_penalty( @@ -324,14 +323,16 @@ where storage_cache: &StorageCache, ) { let penalty_amount = Self::get_exit_penalty(sc, total_exit_amount, token_attributes); - if penalty_amount > 0 { - *total_exit_amount -= &penalty_amount; - - sc.burn_farming_tokens( - &penalty_amount, - &storage_cache.farming_token_id, - &storage_cache.reward_token_id, - ); + if penalty_amount == 0 { + return; } + + *total_exit_amount -= &penalty_amount; + + sc.burn_farming_tokens( + &penalty_amount, + &storage_cache.farming_token_id, + &storage_cache.reward_token_id, + ); } } diff --git a/dex/farm-concentrated-liq/src/exit_penalty.rs b/dex/farm-concentrated-liq/src/exit_penalty.rs index e8905fade..f7d96b94f 100644 --- a/dex/farm-concentrated-liq/src/exit_penalty.rs +++ b/dex/farm-concentrated-liq/src/exit_penalty.rs @@ -14,13 +14,13 @@ pub const MAX_MINIMUM_FARMING_EPOCHS: u64 = 30; #[multiversx_sc::module] pub trait ExitPenaltyModule: permissions_module::PermissionsModule { #[only_owner] - #[endpoint] + #[endpoint(setPenaltyPercent)] fn set_penalty_percent(&self, percent: u64) { require!(percent < MAX_PERCENT, ERROR_PARAMETERS); self.penalty_percent().set(percent); } - #[endpoint] + #[endpoint(setMinimumFarmingEpochs)] fn set_minimum_farming_epochs(&self, epochs: Epoch) { self.require_caller_has_admin_permissions(); require!(epochs <= MAX_MINIMUM_FARMING_EPOCHS, ERROR_PARAMETERS); @@ -29,7 +29,7 @@ pub trait ExitPenaltyModule: permissions_module::PermissionsModule { } #[only_owner] - #[endpoint] + #[endpoint(setBurnGasLimit)] fn set_burn_gas_limit(&self, gas_limit: u64) { self.burn_gas_limit().set(gas_limit); } @@ -44,32 +44,34 @@ pub trait ExitPenaltyModule: permissions_module::PermissionsModule { if pair_contract_address.is_zero() { self.send() .esdt_local_burn(farming_token_id, 0, farming_amount); - } else { - let gas_limit = self.burn_gas_limit().get(); - self.pair_contract_proxy(pair_contract_address) - .remove_liquidity_and_burn_token(reward_token_id.clone()) - .with_esdt_transfer((farming_token_id.clone(), 0, farming_amount.clone())) - .with_gas_limit(gas_limit) - .transfer_execute(); + + return; } + + let gas_limit = self.burn_gas_limit().get(); + self.pair_contract_proxy(pair_contract_address) + .remove_liquidity_and_burn_token(reward_token_id.clone()) + .with_esdt_transfer((farming_token_id.clone(), 0, farming_amount.clone())) + .with_gas_limit(gas_limit) + .transfer_execute(); } #[proxy] fn pair_contract_proxy(&self, to: ManagedAddress) -> pair::Proxy; #[view(getPenaltyPercent)] - #[storage_mapper("penalty_percent")] + #[storage_mapper("penaltyPercent")] fn penalty_percent(&self) -> SingleValueMapper; #[view(getMinimumFarmingEpoch)] - #[storage_mapper("minimum_farming_epochs")] + #[storage_mapper("minimumFarmingEpochs")] fn minimum_farming_epochs(&self) -> SingleValueMapper; #[view(getBurnGasLimit)] - #[storage_mapper("burn_gas_limit")] + #[storage_mapper("burnGasLimit")] fn burn_gas_limit(&self) -> SingleValueMapper; #[view(getPairContractManagedAddress)] - #[storage_mapper("pair_contract_address")] + #[storage_mapper("pairContractAddress")] fn pair_contract_address(&self) -> SingleValueMapper; } diff --git a/dex/farm-concentrated-liq/src/lib.rs b/dex/farm-concentrated-liq/src/lib.rs index 9da99566d..3aedbdfdc 100644 --- a/dex/farm-concentrated-liq/src/lib.rs +++ b/dex/farm-concentrated-liq/src/lib.rs @@ -1,6 +1,4 @@ #![no_std] -#![allow(clippy::too_many_arguments)] -#![feature(exact_size_is_empty)] multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -8,7 +6,9 @@ multiversx_sc::derive_imports!(); pub mod base_functions; pub mod exit_penalty; -use base_functions::{ClaimRewardsResultType, DoubleMultiPayment, Wrapper}; +use base_functions::{ + ClaimRewardsResultType, CompoundRewardsResultType, ExitFarmResultType, Wrapper, +}; use common_structs::FarmTokenAttributes; use contexts::storage_cache::StorageCache; @@ -17,8 +17,17 @@ use exit_penalty::{ }; use farm_base_impl::base_traits_impl::FarmContract; -pub type EnterFarmResultType = DoubleMultiPayment; -pub type ExitFarmWithPartialPosResultType = DoubleMultiPayment; +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)] +pub struct EnterFarmResultType { + pub new_farm_token: EsdtTokenPayment, + pub boosted_rewards: EsdtTokenPayment, +} + +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)] +pub struct MergeTokensResultType { + pub new_farm_token: EsdtTokenPayment, + pub boosted_rewards: EsdtTokenPayment, +} #[multiversx_sc::contract] pub trait Farm: @@ -99,7 +108,10 @@ pub trait Farm: self.update_energy_and_progress(&orig_caller); - (new_farm_token, boosted_rewards_payment).into() + EnterFarmResultType { + new_farm_token, + boosted_rewards: boosted_rewards_payment, + } } #[payable("*")] @@ -115,7 +127,7 @@ pub trait Farm: self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token); self.send_payment_non_zero(&caller, &claim_rewards_result.rewards); - claim_rewards_result.into() + claim_rewards_result } #[payable("*")] @@ -123,16 +135,16 @@ pub trait Farm: fn compound_rewards_endpoint( &self, opt_orig_caller: OptionalValue, - ) -> EsdtTokenPayment { + ) -> CompoundRewardsResultType { let caller = self.blockchain().get_caller(); let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); - let output_farm_token_payment = self.compound_rewards::>(orig_caller.clone()); + let compund_result = self.compound_rewards::>(orig_caller.clone()); - self.send_payment_non_zero(&caller, &output_farm_token_payment); + self.send_payment_non_zero(&caller, &compund_result.new_farm_token); self.update_energy_and_progress(&orig_caller); - output_farm_token_payment + compund_result } #[payable("*")] @@ -140,7 +152,7 @@ pub trait Farm: fn exit_farm_endpoint( &self, opt_orig_caller: OptionalValue, - ) -> ExitFarmWithPartialPosResultType { + ) -> ExitFarmResultType { let caller = self.blockchain().get_caller(); let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); @@ -152,7 +164,7 @@ pub trait Farm: self.clear_user_energy_if_needed(&orig_caller); - (exit_farm_result.farming_tokens, exit_farm_result.rewards).into() + exit_farm_result } #[payable("*")] @@ -160,7 +172,7 @@ pub trait Farm: fn merge_farm_tokens_endpoint( &self, opt_orig_caller: OptionalValue, - ) -> DoubleMultiPayment { + ) -> MergeTokensResultType { let caller = self.blockchain().get_caller(); let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); @@ -171,14 +183,14 @@ pub trait Farm: self.send_payment_non_zero(&caller, &merged_farm_token); self.send_payment_non_zero(&caller, &boosted_rewards_payment); - (merged_farm_token, boosted_rewards_payment).into() + MergeTokensResultType { + new_farm_token: merged_farm_token, + boosted_rewards: boosted_rewards_payment, + } } #[endpoint(claimBoostedRewards)] - fn claim_boosted_rewards( - &self, - opt_user: OptionalValue, - ) -> EsdtTokenPayment { + fn claim_boosted_rewards(&self, opt_user: OptionalValue) -> EsdtTokenPayment { let caller = self.blockchain().get_caller(); let user = match &opt_user { OptionalValue::Some(user) => user, diff --git a/dex/farm-concentrated-liq/wasm/src/lib.rs b/dex/farm-concentrated-liq/wasm/src/lib.rs index b4109c2b1..26e4f4663 100644 --- a/dex/farm-concentrated-liq/wasm/src/lib.rs +++ b/dex/farm-concentrated-liq/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 65 +// Endpoints: 64 // Async Callback: 1 -// Total number of exported functions: 67 +// Total number of exported functions: 66 #![no_std] #![allow(internal_features)] @@ -41,7 +41,6 @@ multiversx_sc_wasm_adapter::endpoints! { getLastRewardBlockNonce => last_reward_block_nonce getDivisionSafetyConstant => division_safety_constant getUserTotalFarmPosition => user_total_farm_position - getFarmPositionMigrationNonce => farm_position_migration_nonce registerFarmToken => register_farm_token getFarmTokenId => farm_token getFarmTokenSupply => farm_token_supply @@ -57,9 +56,9 @@ multiversx_sc_wasm_adapter::endpoints! { addSCAddressToWhitelist => add_sc_address_to_whitelist removeSCAddressFromWhitelist => remove_sc_address_from_whitelist isSCAddressWhitelisted => is_sc_address_whitelisted - set_penalty_percent => set_penalty_percent - set_minimum_farming_epochs => set_minimum_farming_epochs - set_burn_gas_limit => set_burn_gas_limit + setPenaltyPercent => set_penalty_percent + setMinimumFarmingEpochs => set_minimum_farming_epochs + setBurnGasLimit => set_burn_gas_limit getPenaltyPercent => penalty_percent getMinimumFarmingEpoch => minimum_farming_epochs getBurnGasLimit => burn_gas_limit