From da8986847f3459091f6c37063df7a183e9643757 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Thu, 12 Oct 2023 13:45:10 +0300 Subject: [PATCH 1/6] farm with locked rewards references cleanup --- dex/farm-with-locked-rewards/src/lib.rs | 16 ++++++---------- dex/farm/src/lib.rs | 14 ++++---------- farm-staking/farm-staking/Cargo.toml | 1 + farm-staking/farm-staking/src/lib.rs | 7 ++----- farm-staking/farm-staking/src/stake_farm.rs | 7 ++----- farm-staking/farm-staking/wasm/src/lib.rs | 5 +++-- locked-asset/proxy_dex/src/farm_interactions.rs | 6 ++++-- locked-asset/proxy_dex/wasm/Cargo.lock | 1 + locked-asset/proxy_dex/wasm/src/lib.rs | 10 ++++++++-- 9 files changed, 31 insertions(+), 36 deletions(-) diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 6a13bc7ef..01ce11880 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -95,16 +95,12 @@ pub trait Farm: self.migrate_old_farm_positions(&orig_caller); let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); - let boosted_rewards_payment = if boosted_rewards > 0 { - self.send_to_lock_contract_non_zero( - self.reward_token_id().get(), - boosted_rewards, - caller.clone(), - orig_caller.clone(), - ) - } else { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) - }; + let boosted_rewards_payment = self.send_to_lock_contract_non_zero( + self.reward_token_id().get(), + boosted_rewards, + caller.clone(), + orig_caller.clone(), + ); let new_farm_token = self.enter_farm::>(orig_caller.clone()); self.send_payment_non_zero(&caller, &new_farm_token); diff --git a/dex/farm/src/lib.rs b/dex/farm/src/lib.rs index 97b280546..b29d7c8a5 100644 --- a/dex/farm/src/lib.rs +++ b/dex/farm/src/lib.rs @@ -96,11 +96,8 @@ pub trait Farm: self.migrate_old_farm_positions(&orig_caller); let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); - let boosted_rewards_payment = if boosted_rewards > 0 { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards) - } else { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) - }; + 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); @@ -186,11 +183,8 @@ pub trait Farm: self.migrate_old_farm_positions(&orig_caller); let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); - let boosted_rewards_payment = if boosted_rewards > 0 { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards) - } else { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) - }; + 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); diff --git a/farm-staking/farm-staking/Cargo.toml b/farm-staking/farm-staking/Cargo.toml index 9884e34ce..a8dc189e4 100644 --- a/farm-staking/farm-staking/Cargo.toml +++ b/farm-staking/farm-staking/Cargo.toml @@ -7,6 +7,7 @@ publish = false [lib] path = "src/lib.rs" + [dependencies.farm] path = "../../dex/farm" diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index e44b3b3df..32cbec0e1 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -104,11 +104,8 @@ pub trait FarmStaking: self.migrate_old_farm_positions(&caller); let boosted_rewards = self.claim_only_boosted_payment(&caller); - let boosted_rewards_payment = if boosted_rewards > 0 { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards) - } else { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) - }; + let boosted_rewards_payment = + EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); let payments = self.get_non_empty_payments(); let token_mapper = self.farm_token(); diff --git a/farm-staking/farm-staking/src/stake_farm.rs b/farm-staking/farm-staking/src/stake_farm.rs index bd5f6481a..7ab3dea1c 100644 --- a/farm-staking/farm-staking/src/stake_farm.rs +++ b/farm-staking/farm-staking/src/stake_farm.rs @@ -74,11 +74,8 @@ pub trait StakeFarmModule: 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 = if boosted_rewards > 0 { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards) - } else { - EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) - }; + let boosted_rewards_payment = + EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); let enter_result = self.enter_farm_base::>(original_caller.clone(), payments); diff --git a/farm-staking/farm-staking/wasm/src/lib.rs b/farm-staking/farm-staking/wasm/src/lib.rs index fedf1dba8..3c3946ede 100644 --- a/farm-staking/farm-staking/wasm/src/lib.rs +++ b/farm-staking/farm-staking/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 68 +// Endpoints: 69 // Async Callback: 1 -// Total number of exported functions: 70 +// Total number of exported functions: 71 #![no_std] @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { farm_staking ( init => init + upgrade => upgrade mergeFarmTokens => merge_farm_tokens_endpoint calculateRewardsForGivenPosition => calculate_rewards_for_given_position topUpRewards => top_up_rewards diff --git a/locked-asset/proxy_dex/src/farm_interactions.rs b/locked-asset/proxy_dex/src/farm_interactions.rs index e1620f1d4..dab9a7825 100644 --- a/locked-asset/proxy_dex/src/farm_interactions.rs +++ b/locked-asset/proxy_dex/src/farm_interactions.rs @@ -2,8 +2,9 @@ multiversx_sc::imports!(); use farm::{ base_functions::{ClaimRewardsResultType, ClaimRewardsResultWrapper}, - EnterFarmResultType, ExitFarmWithPartialPosResultType, ProxyTrait as _, + EnterFarmResultType, ExitFarmWithPartialPosResultType, }; +use farm_with_locked_rewards::ProxyTrait as _; pub struct EnterFarmResultWrapper { pub farm_token: EsdtTokenPayment, @@ -77,5 +78,6 @@ pub trait FarmInteractionsModule { } #[proxy] - fn farm_contract_proxy(&self, to: ManagedAddress) -> farm::Proxy; + fn farm_contract_proxy(&self, to: ManagedAddress) + -> farm_with_locked_rewards::Proxy; } diff --git a/locked-asset/proxy_dex/wasm/Cargo.lock b/locked-asset/proxy_dex/wasm/Cargo.lock index b40656523..4af1937e2 100644 --- a/locked-asset/proxy_dex/wasm/Cargo.lock +++ b/locked-asset/proxy_dex/wasm/Cargo.lock @@ -480,6 +480,7 @@ dependencies = [ "multiversx-sc", "multiversx-sc-modules", "pair", + "sc_whitelist_module", "simple-lock", "token_merge_helper", "token_send", diff --git a/locked-asset/proxy_dex/wasm/src/lib.rs b/locked-asset/proxy_dex/wasm/src/lib.rs index 096dff097..5ffdc8d79 100644 --- a/locked-asset/proxy_dex/wasm/src/lib.rs +++ b/locked-asset/proxy_dex/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 25 +// Endpoints: 31 // Async Callback: 1 -// Total number of exported functions: 27 +// Total number of exported functions: 33 #![no_std] @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { proxy_dex ( init => init + upgrade => upgrade registerProxyPair => register_proxy_pair setTransferRoleWrappedLpToken => set_transfer_role_wrapped_lp_token registerProxyFarm => register_proxy_farm @@ -40,13 +41,18 @@ multiversx_sc_wasm_adapter::endpoints! { getIntermediatedFarms => intermediated_farms addLiquidityProxy => add_liquidity_proxy removeLiquidityProxy => remove_liquidity_proxy + increaseProxyPairTokenEnergy => increase_proxy_pair_token_energy_endpoint enterFarmProxy => enter_farm_proxy_endpoint exitFarmProxy => exit_farm_proxy claimRewardsProxy => claim_rewards_proxy + increaseProxyFarmTokenEnergy => increase_proxy_farm_token_energy_endpoint mergeWrappedFarmTokens => merge_wrapped_farm_tokens_endpoint mergeWrappedLpTokens => merge_wrapped_lp_tokens_endpoint setEnergyFactoryAddress => set_energy_factory_address getEnergyFactoryAddress => energy_factory_address + addSCAddressToWhitelist => add_sc_address_to_whitelist + removeSCAddressFromWhitelist => remove_sc_address_from_whitelist + isSCAddressWhitelisted => is_sc_address_whitelisted ) } From 00b25dc2e1b3d0bd17ac7ca3282bf601c79d2ed9 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Thu, 12 Oct 2023 13:46:50 +0300 Subject: [PATCH 2/6] farm staking proxy lp tokens merge fix --- farm-staking/farm-staking-proxy/Cargo.toml | 3 ++ .../src/external_contracts_interactions.rs | 16 +++++++--- .../src/proxy_actions/stake.rs | 15 +++++---- .../farm-staking-proxy/src/result_types.rs | 6 ++-- .../farm-staking-proxy/wasm/Cargo.lock | 31 +++++++++++++++++++ .../farm-staking-proxy/wasm/src/lib.rs | 5 +-- 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/farm-staking/farm-staking-proxy/Cargo.toml b/farm-staking/farm-staking-proxy/Cargo.toml index 088edab13..95cc91c4a 100644 --- a/farm-staking/farm-staking-proxy/Cargo.toml +++ b/farm-staking/farm-staking-proxy/Cargo.toml @@ -21,6 +21,9 @@ version = "=0.43.3" [dependencies.farm] path = "../../dex/farm" +[dependencies.farm-with-locked-rewards] +path = "../../dex/farm-with-locked-rewards" + [dependencies.farm-staking] path = "../farm-staking" diff --git a/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs b/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs index 17d32d056..4e24d0597 100644 --- a/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs +++ b/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs @@ -1,13 +1,14 @@ multiversx_sc::imports!(); use farm::{ - base_functions::ClaimRewardsResultType, EnterFarmResultType, ExitFarmWithPartialPosResultType, - ProxyTrait as _, + base_functions::{ClaimRewardsResultType, DoubleMultiPayment}, + EnterFarmResultType, ExitFarmWithPartialPosResultType, }; use farm_staking::{ claim_stake_farm_rewards::ProxyTrait as _, stake_farm::ProxyTrait as _, unstake_farm::ProxyTrait as _, }; +use farm_with_locked_rewards::ProxyTrait as _; use pair::safe_price_view::ProxyTrait as _; use crate::result_types::*; @@ -68,9 +69,11 @@ pub trait ExternalContractsInteractionsModule: orig_caller: ManagedAddress, base_lp_token: EsdtTokenPayment, mut additional_lp_tokens: PaymentsVec, - ) -> EsdtTokenPayment { + ) -> DoubleMultiPayment { if additional_lp_tokens.is_empty() { - return base_lp_token; + let rewards_payment = + EsdtTokenPayment::new(base_lp_token.token_identifier.clone(), 0, BigUint::zero()); + return (base_lp_token, rewards_payment).into(); } additional_lp_tokens.push(base_lp_token); @@ -216,7 +219,10 @@ pub trait ExternalContractsInteractionsModule: fn staking_farm_proxy_obj(&self, sc_address: ManagedAddress) -> farm_staking::Proxy; #[proxy] - fn lp_farm_proxy_obj(&self, sc_address: ManagedAddress) -> farm::Proxy; + fn lp_farm_proxy_obj( + &self, + sc_address: ManagedAddress, + ) -> farm_with_locked_rewards::Proxy; #[proxy] fn pair_proxy_obj(&self, sc_address: ManagedAddress) -> pair::Proxy; diff --git a/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs b/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs index d39dd7f2c..a4cc65206 100644 --- a/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs +++ b/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs @@ -67,11 +67,13 @@ pub trait ProxyStakeModule: ); let received_staking_farm_token = staking_farm_enter_result.received_staking_farm_token; - let merged_lp_farm_tokens = self.merge_lp_farm_tokens( - orig_caller, - lp_farm_token_payment, - additional_lp_farm_tokens, - ); + let (merged_lp_farm_tokens, lp_farm_boosted_rewards) = self + .merge_lp_farm_tokens( + orig_caller, + lp_farm_token_payment, + additional_lp_farm_tokens, + ) + .into_tuple(); let new_attributes = DualYieldTokenAttributes { lp_farm_token_nonce: merged_lp_farm_tokens.token_nonce, @@ -83,7 +85,8 @@ pub trait ProxyStakeModule: self.create_dual_yield_tokens(&dual_yield_token_mapper, &new_attributes); let output_payments = StakeProxyResult { dual_yield_tokens: new_dual_yield_tokens, - boosted_rewards: staking_farm_enter_result.boosted_rewards, + staking_boosted_rewards: staking_farm_enter_result.boosted_rewards, + lp_farm_boosted_rewards, }; output_payments.send_and_return(self, &caller) diff --git a/farm-staking/farm-staking-proxy/src/result_types.rs b/farm-staking/farm-staking-proxy/src/result_types.rs index 65e0cf238..2130fd696 100644 --- a/farm-staking/farm-staking-proxy/src/result_types.rs +++ b/farm-staking/farm-staking-proxy/src/result_types.rs @@ -44,7 +44,8 @@ pub struct PairRemoveLiquidityResult { #[derive(TypeAbi, TopEncode, TopDecode)] pub struct StakeProxyResult { pub dual_yield_tokens: EsdtTokenPayment, - pub boosted_rewards: EsdtTokenPayment, + pub staking_boosted_rewards: EsdtTokenPayment, + pub lp_farm_boosted_rewards: EsdtTokenPayment, } impl StakeProxyResult { @@ -54,7 +55,8 @@ impl StakeProxyResult { to: &ManagedAddress, ) -> Self { sc.send_payment_non_zero(to, &self.dual_yield_tokens); - sc.send_payment_non_zero(to, &self.boosted_rewards); + sc.send_payment_non_zero(to, &self.staking_boosted_rewards); + sc.send_payment_non_zero(to, &self.lp_farm_boosted_rewards); self } diff --git a/farm-staking/farm-staking-proxy/wasm/Cargo.lock b/farm-staking/farm-staking-proxy/wasm/Cargo.lock index b39a7d277..a84b712c8 100644 --- a/farm-staking/farm-staking-proxy/wasm/Cargo.lock +++ b/farm-staking/farm-staking-proxy/wasm/Cargo.lock @@ -217,6 +217,7 @@ dependencies = [ "config", "farm", "farm-staking", + "farm-with-locked-rewards", "fixed-supply-token", "hex-literal", "mergeable", @@ -239,6 +240,36 @@ dependencies = [ "multiversx-sc-wasm-adapter", ] +[[package]] +name = "farm-with-locked-rewards" +version = "0.0.0" +dependencies = [ + "common_errors", + "common_structs", + "config", + "contexts", + "energy-factory", + "energy-query", + "events", + "farm", + "farm-boosted-yields", + "farm_base_impl", + "farm_token", + "fixed-supply-token", + "locking_module", + "mergeable", + "multiversx-sc", + "multiversx-sc-modules", + "pausable", + "permissions_module", + "rewards", + "sc_whitelist_module", + "token_send", + "utils", + "week-timekeeping", + "weekly-rewards-splitting", +] + [[package]] name = "farm_base_impl" version = "0.0.0" diff --git a/farm-staking/farm-staking-proxy/wasm/src/lib.rs b/farm-staking/farm-staking-proxy/wasm/src/lib.rs index 4223b260d..b5ff89409 100644 --- a/farm-staking/farm-staking-proxy/wasm/src/lib.rs +++ b/farm-staking/farm-staking-proxy/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 15 +// Endpoints: 16 // Async Callback: 1 -// Total number of exported functions: 17 +// Total number of exported functions: 18 #![no_std] @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { farm_staking_proxy ( init => init + upgrade => upgrade registerDualYieldToken => register_dual_yield_token getDualYieldTokenId => dual_yield_token getLpFarmAddress => lp_farm_address From 49fea1a9815873826cd89b706066e6e02dbb1fd1 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Fri, 13 Oct 2023 14:28:40 +0300 Subject: [PATCH 3/6] small optimizations --- dex/farm-with-locked-rewards/src/lib.rs | 3 ++- dex/farm-with-locked-rewards/wasm/src/lib.rs | 5 +++-- farm-staking/farm-staking-proxy/Cargo.toml | 3 +++ .../src/external_contracts_interactions.rs | 18 +++++++++--------- farm-staking/farm-staking-proxy/src/lib.rs | 5 +++++ .../src/proxy_actions/claim.rs | 1 + .../src/proxy_actions/stake.rs | 1 + .../src/proxy_actions/unstake.rs | 1 + .../mod.rs | 1 + .../farm-staking-proxy/wasm/Cargo.lock | 1 + .../farm-staking-proxy/wasm/src/lib.rs | 6 ++++-- locked-asset/energy-factory/elrond.json | 3 --- locked-asset/energy-factory/wasm/src/lib.rs | 6 ++++-- 13 files changed, 35 insertions(+), 19 deletions(-) delete mode 100644 locked-asset/energy-factory/elrond.json diff --git a/dex/farm-with-locked-rewards/src/lib.rs b/dex/farm-with-locked-rewards/src/lib.rs index 01ce11880..2a6b299a3 100644 --- a/dex/farm-with-locked-rewards/src/lib.rs +++ b/dex/farm-with-locked-rewards/src/lib.rs @@ -278,7 +278,8 @@ pub trait Farm: energy_address: ManagedAddress, ) -> EsdtTokenPayment { if amount == 0 { - return EsdtTokenPayment::new(token_id, 0, amount); + let locked_token_id = self.get_locked_token_id(); + return EsdtTokenPayment::new(locked_token_id, 0, amount); } self.lock_virtual(token_id, amount, destination_address, energy_address) diff --git a/dex/farm-with-locked-rewards/wasm/src/lib.rs b/dex/farm-with-locked-rewards/wasm/src/lib.rs index 64c73e3cc..9e0b6c29e 100644 --- a/dex/farm-with-locked-rewards/wasm/src/lib.rs +++ b/dex/farm-with-locked-rewards/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 66 +// Endpoints: 67 // Async Callback: 1 -// Total number of exported functions: 68 +// Total number of exported functions: 69 #![no_std] @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { farm_with_locked_rewards ( init => init + upgrade => upgrade enterFarm => enter_farm_endpoint claimRewards => claim_rewards_endpoint exitFarm => exit_farm_endpoint diff --git a/farm-staking/farm-staking-proxy/Cargo.toml b/farm-staking/farm-staking-proxy/Cargo.toml index 95cc91c4a..b9f7989f8 100644 --- a/farm-staking/farm-staking-proxy/Cargo.toml +++ b/farm-staking/farm-staking-proxy/Cargo.toml @@ -60,6 +60,9 @@ path = "../../common/traits/unwrappable" [dependencies.sc_whitelist_module] path = "../../common/modules/sc_whitelist_module" +[dependencies.energy-query] +path = "../../energy-integration/common-modules/energy-query" + [dev-dependencies] num-bigint = "0.4.2" diff --git a/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs b/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs index 4e24d0597..163ebb922 100644 --- a/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs +++ b/farm-staking/farm-staking-proxy/src/external_contracts_interactions.rs @@ -18,7 +18,7 @@ pub type SafePriceResult = MultiValue2, EsdtTokenPaym #[multiversx_sc::module] pub trait ExternalContractsInteractionsModule: - crate::lp_farm_token::LpFarmTokenModule + utils::UtilsModule + crate::lp_farm_token::LpFarmTokenModule + utils::UtilsModule + energy_query::EnergyQueryModule { // lp farm @@ -67,21 +67,21 @@ pub trait ExternalContractsInteractionsModule: fn merge_lp_farm_tokens( &self, orig_caller: ManagedAddress, - base_lp_token: EsdtTokenPayment, - mut additional_lp_tokens: PaymentsVec, + base_lp_farm_token: EsdtTokenPayment, + mut additional_lp_farm_tokens: PaymentsVec, ) -> DoubleMultiPayment { - if additional_lp_tokens.is_empty() { - let rewards_payment = - EsdtTokenPayment::new(base_lp_token.token_identifier.clone(), 0, BigUint::zero()); - return (base_lp_token, rewards_payment).into(); + if additional_lp_farm_tokens.is_empty() { + let locked_token_id = self.get_locked_token_id(); + let rewards_payment = EsdtTokenPayment::new(locked_token_id, 0, BigUint::zero()); + return (base_lp_farm_token, rewards_payment).into(); } - additional_lp_tokens.push(base_lp_token); + additional_lp_farm_tokens.push(base_lp_farm_token); let lp_farm_address = self.lp_farm_address().get(); self.lp_farm_proxy_obj(lp_farm_address) .merge_farm_tokens_endpoint(orig_caller) - .with_multi_token_transfer(additional_lp_tokens) + .with_multi_token_transfer(additional_lp_farm_tokens) .execute_on_dest_context() } diff --git a/farm-staking/farm-staking-proxy/src/lib.rs b/farm-staking/farm-staking-proxy/src/lib.rs index 24bd0d78d..bd8ef7013 100644 --- a/farm-staking/farm-staking-proxy/src/lib.rs +++ b/farm-staking/farm-staking-proxy/src/lib.rs @@ -16,6 +16,7 @@ pub trait FarmStakingProxy: + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + utils::UtilsModule + token_send::TokenSendModule + + energy_query::EnergyQueryModule + sc_whitelist_module::SCWhitelistModule + proxy_actions::stake::ProxyStakeModule + proxy_actions::claim::ProxyClaimModule @@ -24,6 +25,7 @@ pub trait FarmStakingProxy: #[init] fn init( &self, + energy_factory_address: ManagedAddress, lp_farm_address: ManagedAddress, staking_farm_address: ManagedAddress, pair_address: ManagedAddress, @@ -32,6 +34,7 @@ pub trait FarmStakingProxy: staking_farm_token_id: TokenIdentifier, lp_token_id: TokenIdentifier, ) { + self.require_sc_address(&energy_factory_address); self.require_sc_address(&lp_farm_address); self.require_sc_address(&staking_farm_address); self.require_sc_address(&pair_address); @@ -41,6 +44,8 @@ pub trait FarmStakingProxy: self.require_valid_token_id(&staking_farm_token_id); self.require_valid_token_id(&lp_token_id); + self.energy_factory_address() + .set_if_empty(&energy_factory_address); self.lp_farm_address().set_if_empty(&lp_farm_address); self.staking_farm_address() .set_if_empty(&staking_farm_address); diff --git a/farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs b/farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs index fe84fa65d..01df77c36 100644 --- a/farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs +++ b/farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs @@ -16,6 +16,7 @@ pub trait ProxyClaimModule: + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + utils::UtilsModule + token_send::TokenSendModule + + energy_query::EnergyQueryModule + sc_whitelist_module::SCWhitelistModule { #[payable("*")] diff --git a/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs b/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs index a4cc65206..b32f62b76 100644 --- a/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs +++ b/farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs @@ -10,6 +10,7 @@ pub trait ProxyStakeModule: + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + utils::UtilsModule + token_send::TokenSendModule + + energy_query::EnergyQueryModule + sc_whitelist_module::SCWhitelistModule { #[payable("*")] diff --git a/farm-staking/farm-staking-proxy/src/proxy_actions/unstake.rs b/farm-staking/farm-staking-proxy/src/proxy_actions/unstake.rs index 2faf021a3..3c46ffd3e 100644 --- a/farm-staking/farm-staking-proxy/src/proxy_actions/unstake.rs +++ b/farm-staking/farm-staking-proxy/src/proxy_actions/unstake.rs @@ -12,6 +12,7 @@ pub trait ProxyUnstakeModule: + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + utils::UtilsModule + token_send::TokenSendModule + + energy_query::EnergyQueryModule + sc_whitelist_module::SCWhitelistModule { #[payable("*")] diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs index e5d848e5d..2c67d2714 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs @@ -119,6 +119,7 @@ where b_mock .execute_tx(owner_addr, &proxy_wrapper, &rust_zero, |sc| { sc.init( + managed_address!(staking_farm_address), // TODO - replace with energy factory address when needed managed_address!(lp_farm_address), managed_address!(staking_farm_address), managed_address!(pair_address), diff --git a/farm-staking/farm-staking-proxy/wasm/Cargo.lock b/farm-staking/farm-staking-proxy/wasm/Cargo.lock index a84b712c8..381d25474 100644 --- a/farm-staking/farm-staking-proxy/wasm/Cargo.lock +++ b/farm-staking/farm-staking-proxy/wasm/Cargo.lock @@ -215,6 +215,7 @@ version = "0.0.0" dependencies = [ "common_structs", "config", + "energy-query", "farm", "farm-staking", "farm-with-locked-rewards", diff --git a/farm-staking/farm-staking-proxy/wasm/src/lib.rs b/farm-staking/farm-staking-proxy/wasm/src/lib.rs index b5ff89409..c233a2125 100644 --- a/farm-staking/farm-staking-proxy/wasm/src/lib.rs +++ b/farm-staking/farm-staking-proxy/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 16 +// Endpoints: 18 // Async Callback: 1 -// Total number of exported functions: 18 +// Total number of exported functions: 20 #![no_std] @@ -35,6 +35,8 @@ multiversx_sc_wasm_adapter::endpoints! { addSCAddressToWhitelist => add_sc_address_to_whitelist removeSCAddressFromWhitelist => remove_sc_address_from_whitelist isSCAddressWhitelisted => is_sc_address_whitelisted + setEnergyFactoryAddress => set_energy_factory_address + getEnergyFactoryAddress => energy_factory_address stakeFarmTokens => stake_farm_tokens claimDualYield => claim_dual_yield_endpoint unstakeFarmTokens => unstake_farm_tokens diff --git a/locked-asset/energy-factory/elrond.json b/locked-asset/energy-factory/elrond.json deleted file mode 100644 index 736553962..000000000 --- a/locked-asset/energy-factory/elrond.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "language": "rust" -} \ No newline at end of file diff --git a/locked-asset/energy-factory/wasm/src/lib.rs b/locked-asset/energy-factory/wasm/src/lib.rs index 213c94146..a116e6ce7 100644 --- a/locked-asset/energy-factory/wasm/src/lib.rs +++ b/locked-asset/energy-factory/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 32 +// Endpoints: 34 // Async Callback: 1 -// Total number of exported functions: 34 +// Total number of exported functions: 36 #![no_std] @@ -22,8 +22,10 @@ multiversx_sc_wasm_adapter::endpoints! { energy_factory ( init => init + upgrade => upgrade lockTokens => lock_tokens_endpoint unlockTokens => unlock_tokens_endpoint + extendLockPeriod => extend_lock_period issueLockedToken => issue_locked_token getLockedTokenId => locked_token getBaseAssetTokenId => base_asset_token_id From 69a57e33c149e4f57e61adf9677d4dd9d1bf9d77 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Fri, 13 Oct 2023 14:36:50 +0300 Subject: [PATCH 4/6] tests fixes --- .../tests/farm_with_locked_rewards_setup/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs index f8858aaf8..f92abc5c7 100644 --- a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs +++ b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_setup/mod.rs @@ -391,17 +391,13 @@ where assert_eq!(out_farm_token.token_nonce, expected_farm_token_nonce); assert_eq!(out_farm_token.amount, managed_biguint!(farm_token_amount)); + assert_eq!( + out_reward_token.token_identifier, + managed_token_id!(LOCKED_REWARD_TOKEN_ID) + ); if out_reward_token.amount > 0 { - assert_eq!( - out_reward_token.token_identifier, - managed_token_id!(LOCKED_REWARD_TOKEN_ID) - ); assert_eq!(out_reward_token.token_nonce, 1); } else { - assert_eq!( - out_reward_token.token_identifier, - managed_token_id!(REWARD_TOKEN_ID) - ); assert_eq!(out_reward_token.token_nonce, 0); } From c05ec53e0037bfd59fb46be73830a6eaf8a0b1d4 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Mon, 16 Oct 2023 01:38:32 +0300 Subject: [PATCH 5/6] farm staking proxy test setup updates --- farm-staking/farm-staking-proxy/Cargo.toml | 9 ++ .../farm-staking-proxy/tests/constants/mod.rs | 9 ++ .../tests/staking_farm_with_lp.rs | 40 ++++++--- .../mod.rs | 90 +++++++++++++++++-- .../mod.rs | 33 +++++-- .../tests/proxy_dex_test_setup/mod.rs | 6 +- 6 files changed, 162 insertions(+), 25 deletions(-) diff --git a/farm-staking/farm-staking-proxy/Cargo.toml b/farm-staking/farm-staking-proxy/Cargo.toml index b9f7989f8..64c132f8c 100644 --- a/farm-staking/farm-staking-proxy/Cargo.toml +++ b/farm-staking/farm-staking-proxy/Cargo.toml @@ -74,3 +74,12 @@ path = "../../common/modules/farm/farm_token" [dev-dependencies.farm-boosted-yields] path = "../../energy-integration/farm-boosted-yields" + +[dev-dependencies.energy-factory] +path = "../../locked-asset/energy-factory" + +[dev-dependencies.simple-lock] +path = "../../locked-asset/simple-lock" + +[dev-dependencies.locking_module] +path = "../../common/modules/locking_module" diff --git a/farm-staking/farm-staking-proxy/tests/constants/mod.rs b/farm-staking/farm-staking-proxy/tests/constants/mod.rs index 6e9bb9fd9..4ccf1ba79 100644 --- a/farm-staking/farm-staking-proxy/tests/constants/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/constants/mod.rs @@ -44,3 +44,12 @@ pub const MIN_FARM_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1; pub static PROXY_WASM_PATH: &str = "farm-staking-proxy/output/farm-staking-proxy"; pub static DUAL_YIELD_TOKEN_ID: &[u8] = b"DYIELD-abcdef"; + +// Energy factory constants + +pub const EPOCHS_IN_YEAR: u64 = 360; +pub static MEX_TOKEN_ID: &[u8] = b"MEX-123456"; +pub static LOCKED_TOKEN_ID: &[u8] = b"LOCKED-123456"; +pub static LEGACY_LOCKED_TOKEN_ID: &[u8] = b"LEGACY-123456"; +pub static LOCK_OPTIONS: &[u64] = &[EPOCHS_IN_YEAR, 5 * EPOCHS_IN_YEAR, 10 * EPOCHS_IN_YEAR]; // 1, 5 or 10 years +pub static PENALTY_PERCENTAGES: &[u64] = &[4_000, 6_000, 8_000]; diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs index c81d579ab..2589e1d2b 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs @@ -20,7 +20,8 @@ use staking_farm_with_lp_staking_contract_interactions::*; fn test_all_setup() { let _ = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -30,7 +31,8 @@ fn test_all_setup() { fn test_stake_farm_proxy() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -44,7 +46,8 @@ fn test_stake_farm_proxy() { fn test_claim_rewards_farm_proxy_full() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -71,7 +74,8 @@ fn test_claim_rewards_farm_proxy_full() { fn test_claim_rewards_farm_proxy_half() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -98,7 +102,8 @@ fn test_claim_rewards_farm_proxy_half() { fn test_claim_rewards_farm_proxy_twice() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -140,7 +145,8 @@ fn test_claim_rewards_farm_proxy_twice() { fn test_unstake_through_proxy_no_claim() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -170,7 +176,8 @@ fn test_unstake_through_proxy_no_claim() { fn unstake_through_proxy_after_claim() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -210,7 +217,8 @@ fn unstake_partial_position_test() { DebugApi::dummy(); let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -252,7 +260,7 @@ fn unstake_partial_position_test() { let lp_farm_rewards = results.lp_farm_rewards; assert_eq!( lp_farm_rewards.token_identifier, - managed_token_id!(RIDE_TOKEN_ID) + managed_token_id!(LOCKED_TOKEN_ID) ); assert_eq!(lp_farm_rewards.amount, 99_999 / 2); @@ -299,7 +307,7 @@ fn unstake_partial_position_test() { let lp_farm_rewards = results.lp_farm_rewards; assert_eq!( lp_farm_rewards.token_identifier, - managed_token_id!(RIDE_TOKEN_ID) + managed_token_id!(LOCKED_TOKEN_ID) ); assert_eq!(lp_farm_rewards.amount, 99_999 / 2); @@ -325,7 +333,8 @@ fn unstake_partial_position_test() { fn unbond_test() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -369,7 +378,8 @@ fn unbond_test() { fn farm_staking_compound_rewards_and_unstake_test() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -399,7 +409,8 @@ fn farm_staking_compound_rewards_and_unstake_test() { fn test_stake_farm_through_proxy_with_merging() { let mut setup = FarmStakingSetup::new( pair::contract_obj, - farm::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, farm_staking::contract_obj, farm_staking_proxy::contract_obj, ); @@ -469,3 +480,6 @@ fn test_stake_farm_through_proxy_with_merging() { ) }); } + +#[test] +fn test_farm_stake_proxy_merging_boosted_rewards() {} diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs index 4381f061b..95c734663 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_external_contracts/mod.rs @@ -1,12 +1,18 @@ #![allow(deprecated)] +use energy_factory::token_whitelist::TokenWhitelistModule; +use energy_factory::SimpleLockEnergy; +use energy_query::EnergyQueryModule; +use locking_module::lock_with_energy_module::LockWithEnergyModule; use multiversx_sc::codec::multi_types::{MultiValue3, OptionalValue}; use multiversx_sc::storage::mappers::StorageTokenWrapper; use multiversx_sc::types::{Address, EsdtLocalRole, ManagedAddress, MultiValueEncoded}; +use multiversx_sc_modules::pause::PauseModule; use multiversx_sc_scenario::whitebox_legacy::TxTokenTransfer; use multiversx_sc_scenario::{ managed_address, managed_biguint, managed_token_id, rust_biguint, whitebox_legacy::*, DebugApi, }; +use simple_lock::locked_token::LockedTokenModule; use farm::exit_penalty::ExitPenaltyModule; use pair::config as pair_config; @@ -16,10 +22,10 @@ use pair_config::ConfigModule as _; use pausable::{PausableModule, State}; use ::config as farm_config; -use farm::*; use farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule; use farm_config::ConfigModule as _; use farm_token::FarmTokenModule; +use farm_with_locked_rewards::*; use crate::constants::*; @@ -241,12 +247,13 @@ fn remove_liquidity( pub fn setup_lp_farm( owner_addr: &Address, user_addr: &Address, + energy_factory_address: &Address, b_mock: &mut BlockchainStateWrapper, farm_builder: FarmObjBuilder, user_farm_in_amount: u64, -) -> ContractObjWrapper, FarmObjBuilder> +) -> ContractObjWrapper, FarmObjBuilder> where - FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj, + FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, { let rust_zero = rust_biguint!(0u64); let farm_wrapper = @@ -256,7 +263,7 @@ where b_mock .execute_tx(owner_addr, &farm_wrapper, &rust_zero, |sc| { - let reward_token_id = managed_token_id!(RIDE_TOKEN_ID); + let reward_token_id = managed_token_id!(MEX_TOKEN_ID); let farming_token_id = managed_token_id!(LP_TOKEN_ID); let division_safety_constant = managed_biguint!(DIVISION_SAFETY_CONSTANT); let pair_address = managed_address!(&Address::zero()); @@ -282,6 +289,11 @@ where .set(&managed_biguint!(LP_FARM_PER_BLOCK_REWARD_AMOUNT)); sc.last_reward_block_nonce() .set(BLOCK_NONCE_AFTER_PAIR_SETUP); + sc.lock_epochs().set(LOCK_OPTIONS[2]); + sc.locking_sc_address() + .set(managed_address!(energy_factory_address)); + sc.energy_factory_address() + .set(managed_address!(energy_factory_address)); }) .assert_ok(); @@ -327,14 +339,80 @@ where farm_wrapper } +pub fn setup_energy_factory( + owner_addr: &Address, + b_mock: &mut BlockchainStateWrapper, + energy_factory_builder: EnergyFactoryObjBuilder, +) -> ContractObjWrapper, EnergyFactoryObjBuilder> +where + EnergyFactoryObjBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj, +{ + let rust_zero = rust_biguint!(0u64); + let energy_factory_wrapper = b_mock.create_sc_account( + &rust_zero, + Some(owner_addr), + energy_factory_builder, + "energy factory", + ); + + b_mock + .execute_tx(owner_addr, &energy_factory_wrapper, &rust_zero, |sc| { + let mut lock_options = MultiValueEncoded::new(); + for (option, penalty) in LOCK_OPTIONS.iter().zip(PENALTY_PERCENTAGES.iter()) { + lock_options.push((*option, *penalty).into()); + } + + sc.init( + managed_token_id!(LOCKED_TOKEN_ID), + managed_token_id!(LEGACY_LOCKED_TOKEN_ID), + managed_address!(energy_factory_wrapper.address_ref()), + 0, + lock_options, + ); + + sc.base_asset_token_id() + .set(managed_token_id!(MEX_TOKEN_ID)); + sc.locked_token() + .set_token_id(managed_token_id!(LOCKED_TOKEN_ID)); + sc.set_paused(false); + }) + .assert_ok(); + + b_mock.set_esdt_local_roles( + energy_factory_wrapper.address_ref(), + MEX_TOKEN_ID, + &[EsdtLocalRole::Mint, EsdtLocalRole::Burn], + ); + b_mock.set_esdt_local_roles( + energy_factory_wrapper.address_ref(), + LOCKED_TOKEN_ID, + &[ + EsdtLocalRole::NftCreate, + EsdtLocalRole::NftAddQuantity, + EsdtLocalRole::NftBurn, + EsdtLocalRole::Transfer, + ], + ); + b_mock.set_esdt_local_roles( + energy_factory_wrapper.address_ref(), + LEGACY_LOCKED_TOKEN_ID, + &[EsdtLocalRole::NftBurn], + ); + + energy_factory_wrapper +} + fn enter_farm( user_address: &Address, b_mock: &mut BlockchainStateWrapper, - farm_wrapper: &ContractObjWrapper, FarmObjBuilder>, + farm_wrapper: &ContractObjWrapper< + farm_with_locked_rewards::ContractObj, + FarmObjBuilder, + >, farm_in_amount: u64, additional_farm_tokens: &[TxTokenTransfer], ) where - FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj, + FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, { let mut payments = Vec::with_capacity(1 + additional_farm_tokens.len()); payments.push(TxTokenTransfer { diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs index 00b2e2e0f..6a272f207 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs @@ -23,7 +23,7 @@ use sc_whitelist_module::SCWhitelistModule; use crate::{ constants::*, - staking_farm_with_lp_external_contracts::{setup_lp_farm, setup_pair}, + staking_farm_with_lp_external_contracts::{setup_energy_factory, setup_lp_farm, setup_pair}, staking_farm_with_lp_staking_contract_setup::{ add_proxy_to_whitelist, setup_proxy, setup_staking_farm, }, @@ -37,11 +37,13 @@ pub struct NonceAmountPair { pub struct FarmStakingSetup< PairObjBuilder, FarmObjBuilder, + EnergyFactoryBuilder, StakingContractObjBuilder, ProxyContractObjBuilder, > where PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj, - FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj, + FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, + EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj, StakingContractObjBuilder: 'static + Copy + Fn() -> farm_staking::ContractObj, ProxyContractObjBuilder: 'static + Copy + Fn() -> farm_staking_proxy::ContractObj, { @@ -49,29 +51,41 @@ pub struct FarmStakingSetup< pub user_addr: Address, pub b_mock: BlockchainStateWrapper, pub pair_wrapper: ContractObjWrapper, PairObjBuilder>, - pub lp_farm_wrapper: ContractObjWrapper, FarmObjBuilder>, + pub lp_farm_wrapper: + ContractObjWrapper, FarmObjBuilder>, + pub energy_factory_wrapper: + ContractObjWrapper, EnergyFactoryBuilder>, pub staking_farm_wrapper: ContractObjWrapper, StakingContractObjBuilder>, pub proxy_wrapper: ContractObjWrapper, ProxyContractObjBuilder>, } -impl +impl< + PairObjBuilder, + FarmObjBuilder, + EnergyFactoryBuilder, + StakingContractObjBuilder, + ProxyContractObjBuilder, + > FarmStakingSetup< PairObjBuilder, FarmObjBuilder, + EnergyFactoryBuilder, StakingContractObjBuilder, ProxyContractObjBuilder, > where PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj, - FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj, + FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, + EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj, StakingContractObjBuilder: 'static + Copy + Fn() -> farm_staking::ContractObj, ProxyContractObjBuilder: 'static + Copy + Fn() -> farm_staking_proxy::ContractObj, { pub fn new( pair_builder: PairObjBuilder, lp_farm_builder: FarmObjBuilder, + energy_factory_builder: EnergyFactoryBuilder, staking_farm_builder: StakingContractObjBuilder, proxy_builder: ProxyContractObjBuilder, ) -> Self { @@ -80,10 +94,13 @@ where let owner_addr = b_mock.create_user_account(&rust_zero); let user_addr = b_mock.create_user_account(&rust_biguint!(100_000_000)); + let energy_factory_wrapper = + setup_energy_factory(&owner_addr, &mut b_mock, energy_factory_builder); let pair_wrapper = setup_pair(&owner_addr, &user_addr, &mut b_mock, pair_builder); let lp_farm_wrapper = setup_lp_farm( &owner_addr, &user_addr, + energy_factory_wrapper.address_ref(), &mut b_mock, lp_farm_builder, USER_TOTAL_LP_TOKENS, @@ -111,6 +128,11 @@ where sc.add_sc_address_to_whitelist(managed_address!(proxy_wrapper.address_ref())); }) .assert_ok(); + b_mock + .execute_tx(&owner_addr, &energy_factory_wrapper, &rust_zero, |sc| { + sc.add_sc_address_to_whitelist(managed_address!(lp_farm_wrapper.address_ref())); + }) + .assert_ok(); FarmStakingSetup { owner_addr, @@ -118,6 +140,7 @@ where b_mock, pair_wrapper, lp_farm_wrapper, + energy_factory_wrapper, staking_farm_wrapper, proxy_wrapper, } diff --git a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs index 59da1640a..2ab0326d4 100644 --- a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs +++ b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs @@ -21,7 +21,9 @@ use multiversx_sc_scenario::{ }; use pair::{config::ConfigModule as OtherConfigModule, Pair}; use pausable::{PausableModule, State}; -use proxy_dex::{proxy_common::ProxyCommonModule, other_sc_whitelist::OtherScWhitelistModule, ProxyDexImpl}; +use proxy_dex::{ + other_sc_whitelist::OtherScWhitelistModule, proxy_common::ProxyCommonModule, ProxyDexImpl, +}; use sc_whitelist_module::SCWhitelistModule; use simple_lock::locked_token::{LockedTokenAttributes, LockedTokenModule}; @@ -315,6 +317,8 @@ where ); sc.set_locking_sc_address(managed_address!(simple_lock_addr)); sc.set_lock_epochs(EPOCHS_IN_YEAR); + sc.energy_factory_address() + .set(managed_address!(simple_lock_addr)); }) .assert_ok(); From 1b70768f231dc0ea77f7b2dfd78b08070d9e4df5 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Mon, 16 Oct 2023 14:12:42 +0300 Subject: [PATCH 6/6] metastaking test with lp farm token merge --- .../farm-staking-proxy/tests/constants/mod.rs | 1 + .../tests/staking_farm_with_lp.rs | 144 +++++++++++++++++- .../mod.rs | 119 ++++++++++++++- 3 files changed, 261 insertions(+), 3 deletions(-) diff --git a/farm-staking/farm-staking-proxy/tests/constants/mod.rs b/farm-staking/farm-staking-proxy/tests/constants/mod.rs index 4ccf1ba79..706bc12a2 100644 --- a/farm-staking/farm-staking-proxy/tests/constants/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/constants/mod.rs @@ -39,6 +39,7 @@ pub const USER_REWARDS_ENERGY_CONST: u64 = 3; pub const USER_REWARDS_FARM_CONST: u64 = 2; pub const MIN_ENERGY_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1; pub const MIN_FARM_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1; +pub const BOOSTED_YIELDS_PERCENTAGE: u64 = 2_500; // 25% // Proxy constants diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs index 2589e1d2b..d7ee78e8d 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs @@ -482,4 +482,146 @@ fn test_stake_farm_through_proxy_with_merging() { } #[test] -fn test_farm_stake_proxy_merging_boosted_rewards() {} +fn test_farm_stake_proxy_merging_boosted_rewards() { + let mut setup = FarmStakingSetup::new( + pair::contract_obj, + farm_with_locked_rewards::contract_obj, + energy_factory::contract_obj, + farm_staking::contract_obj, + farm_staking_proxy::contract_obj, + ); + + // Boosted rewards setup + setup.set_lp_farm_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); + let farm_amount = 50_000_000u64; + let user_address = setup.user_addr.clone(); + let temp_user = setup + .b_mock + .create_user_account(&rust_biguint!(100_000_000)); + setup.exit_lp_farm(&user_address, 1, USER_TOTAL_LP_TOKENS); + setup.b_mock.set_esdt_balance( + &setup.user_addr, + LP_TOKEN_ID, + &rust_biguint!(farm_amount * 2), + ); + setup + .b_mock + .set_esdt_balance(&temp_user, LP_TOKEN_ID, &rust_biguint!(1)); + + setup.b_mock.set_block_epoch(2); + + setup.set_user_energy(&user_address, 1_000, 2, 1); + let mut farm_token_nonce = setup.enter_lp_farm(&user_address, farm_amount); + let second_farm_token_nonce = setup.enter_lp_farm(&user_address, farm_amount); // will enter Metastaking next week + + // User claims rewards to get his energy registered + farm_token_nonce = setup.claim_lp_farm(&user_address, farm_token_nonce, farm_amount, 0); + + // User enters Metastaking + let first_dual_yield_token_nonce = + setup.stake_farm_lp_proxy(farm_token_nonce, farm_amount, 1, farm_amount); + setup.b_mock.execute_in_managed_environment(|| { + setup.b_mock.check_nft_balance( + &setup.user_addr, + DUAL_YIELD_TOKEN_ID, + first_dual_yield_token_nonce, + &rust_biguint!(farm_amount), + Some(&DualYieldTokenAttributes:: { + lp_farm_token_nonce: farm_token_nonce, + lp_farm_token_amount: managed_biguint!(farm_amount), + staking_farm_token_nonce: 1, + staking_farm_token_amount: managed_biguint!(farm_amount), + }), + ) + }); + + // advance blocks - 10 blocks - 10 * 5_000 = 50_000 total rewards + // 37_500 base farm, 12_500 boosted yields + let boosted_rewards = 12_500u64; + setup.b_mock.set_block_nonce(110); + + // random tx on end of week 1, to cummulate rewards + setup.b_mock.set_block_epoch(6); + setup.set_user_energy(&user_address, 1_000, 6, 1); + setup.set_user_energy(&temp_user, 1, 6, 1); + let temp_user_farm_token_nonce = setup.enter_lp_farm(&temp_user, 1); + setup.exit_lp_farm(&temp_user, temp_user_farm_token_nonce, 1); + + // advance 1 week + setup.b_mock.set_block_epoch(10); + setup.set_user_energy(&user_address, 1_000, 10, 1); + + // check locked tokens rewards before staking farm tokens with merge + setup.b_mock.execute_in_managed_environment(|| { + setup.b_mock.check_nft_balance::( + &user_address, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(0), + None, + ) + }); + + // user enters Metastaking with second position, which should merge with the first one + let dual_yield_token_payments = vec![NonceAmountPair { + nonce: first_dual_yield_token_nonce, + amount: farm_amount, + }]; + let new_dual_yield_token_nonce = setup.stake_farm_lp_proxy_multiple( + second_farm_token_nonce, + farm_amount, + dual_yield_token_payments, + ); + + // check user staking farm tokens + setup.b_mock.check_nft_balance::( + &setup.user_addr, + DUAL_YIELD_TOKEN_ID, + first_dual_yield_token_nonce, + &rust_biguint!(0), + None, + ); + setup.b_mock.execute_in_managed_environment(|| { + setup.b_mock.check_nft_balance( + &setup.user_addr, + DUAL_YIELD_TOKEN_ID, + new_dual_yield_token_nonce, + &rust_biguint!(farm_amount * 2), + Some(&DualYieldTokenAttributes:: { + lp_farm_token_nonce: 6, + lp_farm_token_amount: managed_biguint!(farm_amount * 2), + staking_farm_token_nonce: 2, + staking_farm_token_amount: managed_biguint!(farm_amount * 2), + }), + ) + }); + + // check farm staking SC tokens + setup.b_mock.check_esdt_balance( + setup.staking_farm_wrapper.address_ref(), + RIDE_TOKEN_ID, + &rust_biguint!(1_000_000_000_000), + ); + + // check proxy SC tokens + setup.b_mock.execute_in_managed_environment(|| { + setup.b_mock.check_nft_balance::( + setup.proxy_wrapper.address_ref(), + LP_FARM_TOKEN_ID, + 6, // farm token nonce after merge + &rust_biguint!(farm_amount * 2), + None, //current attributes + ) + }); + + // check boosted rewards + setup.b_mock.execute_in_managed_environment(|| { + setup.b_mock.check_nft_balance::( + &user_address, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(boosted_rewards), + None, + ) + }); +} diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs index 6a272f207..8860a604e 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_interactions/mod.rs @@ -1,8 +1,15 @@ #![allow(deprecated)] -use multiversx_sc::{codec::multi_types::OptionalValue, types::Address}; +use energy_factory::energy::EnergyModule; +use energy_query::Energy; +use farm_boosted_yields::FarmBoostedYieldsModule; +use farm_with_locked_rewards::Farm; +use multiversx_sc::{ + codec::multi_types::OptionalValue, + types::{Address, BigInt}, +}; use multiversx_sc_scenario::{ - managed_address, managed_biguint, rust_biguint, + managed_address, managed_biguint, managed_token_id, rust_biguint, whitebox_legacy::TxTokenTransfer, whitebox_legacy::{BlockchainStateWrapper, ContractObjWrapper}, DebugApi, @@ -442,4 +449,112 @@ where unbond_token_nonce } + + pub fn enter_lp_farm(&mut self, user: &Address, farm_token_amount: u64) -> u64 { + let mut farm_token_nonce = 0; + self.b_mock + .execute_esdt_transfer( + user, + &self.lp_farm_wrapper, + LP_TOKEN_ID, + 0, + &rust_biguint!(farm_token_amount), + |sc| { + let (new_farm_token, _boosted_rewards_payment) = + sc.enter_farm_endpoint(OptionalValue::None).into_tuple(); + assert_eq!( + new_farm_token.token_identifier, + managed_token_id!(LP_FARM_TOKEN_ID) + ); + assert_eq!(new_farm_token.amount, farm_token_amount); + farm_token_nonce = new_farm_token.token_nonce; + }, + ) + .assert_ok(); + + farm_token_nonce + } + + pub fn exit_lp_farm(&mut self, user: &Address, farm_token_nonce: u64, farm_token_amount: u64) { + self.b_mock + .execute_esdt_transfer( + user, + &self.lp_farm_wrapper, + LP_FARM_TOKEN_ID, + farm_token_nonce, + &rust_biguint!(farm_token_amount), + |sc| { + let (_lp_tokens, _boosted_rewards_payment) = + sc.exit_farm_endpoint(OptionalValue::None).into_tuple(); + }, + ) + .assert_ok(); + } + + pub fn claim_lp_farm( + &mut self, + user: &Address, + farm_token_nonce: u64, + farm_token_amount: u64, + expected_lp_farm_rewards: u64, + ) -> u64 { + let mut new_farm_token_nonce = 0; + self.b_mock + .execute_esdt_transfer( + user, + &self.lp_farm_wrapper, + LP_FARM_TOKEN_ID, + farm_token_nonce, + &rust_biguint!(farm_token_amount), + |sc| { + let (output_farm_token, boosted_rewards_payment) = + sc.claim_rewards_endpoint(OptionalValue::None).into_tuple(); + assert_eq!(output_farm_token.amount, farm_token_amount); + assert_eq!(boosted_rewards_payment.amount, expected_lp_farm_rewards); + new_farm_token_nonce = output_farm_token.token_nonce; + }, + ) + .assert_ok(); + + new_farm_token_nonce + } + + pub fn set_user_energy( + &mut self, + user: &Address, + energy: u64, + last_update_epoch: u64, + locked_tokens: u64, + ) { + self.b_mock + .execute_tx( + &self.owner_addr, + &self.energy_factory_wrapper, + &rust_biguint!(0), + |sc| { + sc.user_energy(&managed_address!(user)).set(&Energy::new( + BigInt::from(managed_biguint!(energy)), + last_update_epoch, + managed_biguint!(locked_tokens), + )); + }, + ) + .assert_ok(); + } + + pub fn set_lp_farm_boosted_yields_rewards_percentage( + &mut self, + boosted_yields_rewards_percentage: u64, + ) { + self.b_mock + .execute_tx( + &self.owner_addr, + &self.lp_farm_wrapper, + &rust_biguint!(0), + |sc| { + sc.set_boosted_yields_rewards_percentage(boosted_yields_rewards_percentage); + }, + ) + .assert_ok(); + } }