From a9b52d795c5bc234c8c58f62a7b73aa40242b8a9 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Wed, 30 Oct 2024 09:40:27 +0200 Subject: [PATCH] fix farm staking tests --- Cargo.lock | 1 + dex/farm-with-locked-rewards/Cargo.toml | 3 ++ .../farm_with_locked_rewards_setup/mod.rs | 46 +++++++++++++++++-- .../tests/farm_with_locked_rewards_test.rs | 14 ++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62acaab8a..6780ce78b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -769,6 +769,7 @@ dependencies = [ "rewards", "sc_whitelist_module", "simple-lock", + "timestamp-oracle", "token_send", "utils", "week-timekeeping", diff --git a/dex/farm-with-locked-rewards/Cargo.toml b/dex/farm-with-locked-rewards/Cargo.toml index eedd45925..b7a616146 100644 --- a/dex/farm-with-locked-rewards/Cargo.toml +++ b/dex/farm-with-locked-rewards/Cargo.toml @@ -89,3 +89,6 @@ version = "=0.53.2" [dev-dependencies.simple-lock] path = "../../locked-asset/simple-lock" + +[dev-dependencies.timestamp-oracle] +path = "../../energy-integration/timestamp-oracle" 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 71b629fb1..c03c1327d 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 @@ -1,4 +1,4 @@ -use common_structs::FarmTokenAttributes; +use common_structs::{FarmTokenAttributes, Timestamp}; use config::ConfigModule; use multiversx_sc::{ codec::multi_types::OptionalValue, @@ -16,7 +16,10 @@ use fees_collector_mock::*; use energy_factory::{energy::EnergyModule, SimpleLockEnergy}; use energy_query::{Energy, EnergyQueryModule}; -use farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule; +use farm_boosted_yields::{ + boosted_yields_factors::BoostedYieldsFactorsModule, + custom_reward_logic::CustomRewardLogicModule, +}; use farm_token::FarmTokenModule; use farm_with_locked_rewards::Farm; use locking_module::lock_with_energy_module::LockWithEnergyModule; @@ -25,6 +28,7 @@ use pausable::{PausableModule, State}; use rewards::RewardsModule; use sc_whitelist_module::SCWhitelistModule; use simple_lock::locked_token::LockedTokenModule; +use timestamp_oracle::{epoch_to_timestamp::EpochToTimestampModule, TimestampOracle}; use week_timekeeping::Epoch; pub static REWARD_TOKEN_ID: &[u8] = b"MEX-123456"; @@ -41,6 +45,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 TIMESTAMP_PER_EPOCH: Timestamp = 24 * 60 * 60; pub const EPOCHS_IN_YEAR: u64 = 360; @@ -55,10 +60,11 @@ pub struct RawFarmTokenAttributes { pub original_owner_bytes: [u8; 32], } -pub struct FarmSetup +pub struct FarmSetup where FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj, + TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj, { pub b_mock: BlockchainStateWrapper, pub owner: Address, @@ -70,14 +76,24 @@ where ContractObjWrapper, FarmObjBuilder>, pub energy_factory_wrapper: ContractObjWrapper, EnergyFactoryBuilder>, + + #[allow(dead_code)] + pub timestamp_oracle_wrapper: + ContractObjWrapper, TimestampOracleObjBuilder>, } -impl FarmSetup +impl + FarmSetup where FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj, EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj, + TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj, { - pub fn new(farm_builder: FarmObjBuilder, energy_factory_builder: EnergyFactoryBuilder) -> Self { + pub fn new( + farm_builder: FarmObjBuilder, + energy_factory_builder: EnergyFactoryBuilder, + timestamp_oracle_builder: TimestampOracleObjBuilder, + ) -> Self { let rust_zero = rust_biguint!(0); let mut b_mock = BlockchainStateWrapper::new(); let owner = b_mock.create_user_account(&rust_zero); @@ -103,6 +119,22 @@ where "fees collector mock", ); + let timestamp_oracle_wrapper = b_mock.create_sc_account( + &rust_zero, + Some(&owner), + timestamp_oracle_builder, + "timestamp oracle", + ); + b_mock + .execute_tx(&owner, ×tamp_oracle_wrapper, &rust_zero, |sc| { + sc.init(0); + + for i in 0..=100 { + sc.set_start_timestamp_for_epoch(i, i * TIMESTAMP_PER_EPOCH + 1); + } + }) + .assert_ok(); + b_mock .execute_tx(&owner, &energy_factory_wrapper, &rust_zero, |sc| { let mut lock_options = MultiValueEncoded::new(); @@ -158,6 +190,9 @@ where sc.set_energy_factory_address(managed_address!( energy_factory_wrapper.address_ref() )); + sc.set_timestamp_oracle_address(managed_address!( + timestamp_oracle_wrapper.address_ref() + )); }) .assert_ok(); @@ -223,6 +258,7 @@ where last_farm_token_nonce: 0, farm_wrapper, energy_factory_wrapper, + timestamp_oracle_wrapper, } } diff --git a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs index 389688dd3..e137f1321 100644 --- a/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs +++ b/dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs @@ -13,9 +13,11 @@ mod farm_with_locked_rewards_setup; #[test] fn farm_with_no_boost_no_proxy_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); // first user enter farm @@ -113,9 +115,11 @@ fn farm_with_no_boost_no_proxy_test() { #[test] fn farm_with_boosted_yields_no_proxy_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); @@ -232,9 +236,11 @@ fn farm_with_boosted_yields_no_proxy_test() { #[test] fn total_farm_position_claim_with_locked_rewards_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); @@ -339,9 +345,11 @@ fn total_farm_position_claim_with_locked_rewards_test() { #[test] fn claim_only_boosted_rewards_per_week_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); @@ -402,9 +410,11 @@ fn claim_only_boosted_rewards_per_week_test() { #[test] fn claim_rewards_per_week_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); @@ -468,9 +478,11 @@ fn claim_rewards_per_week_test() { #[test] fn claim_boosted_rewards_with_zero_position_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); @@ -539,9 +551,11 @@ fn claim_boosted_rewards_with_zero_position_test() { #[test] fn claim_boosted_rewards_user_energy_not_registered_test() { DebugApi::dummy(); + let mut farm_setup = FarmSetup::new( farm_with_locked_rewards::contract_obj, energy_factory::contract_obj, + timestamp_oracle::contract_obj, ); farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);