From c05ec53e0037bfd59fb46be73830a6eaf8a0b1d4 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Mon, 16 Oct 2023 01:38:32 +0300 Subject: [PATCH] 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();