Skip to content

Commit

Permalink
fix farm staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 30, 2024
1 parent 3636f8a commit a9b52d7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dex/farm-with-locked-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common_structs::FarmTokenAttributes;
use common_structs::{FarmTokenAttributes, Timestamp};
use config::ConfigModule;
use multiversx_sc::{
codec::multi_types::OptionalValue,
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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;

Expand All @@ -55,10 +60,11 @@ pub struct RawFarmTokenAttributes {
pub original_owner_bytes: [u8; 32],
}

pub struct FarmSetup<FarmObjBuilder, EnergyFactoryBuilder>
pub struct FarmSetup<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj<DebugApi>,
EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
pub b_mock: BlockchainStateWrapper,
pub owner: Address,
Expand All @@ -70,14 +76,24 @@ where
ContractObjWrapper<farm_with_locked_rewards::ContractObj<DebugApi>, FarmObjBuilder>,
pub energy_factory_wrapper:
ContractObjWrapper<energy_factory::ContractObj<DebugApi>, EnergyFactoryBuilder>,

#[allow(dead_code)]
pub timestamp_oracle_wrapper:
ContractObjWrapper<timestamp_oracle::ContractObj<DebugApi>, TimestampOracleObjBuilder>,
}

impl<FarmObjBuilder, EnergyFactoryBuilder> FarmSetup<FarmObjBuilder, EnergyFactoryBuilder>
impl<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
FarmSetup<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj<DebugApi>,
EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
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);
Expand All @@ -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, &timestamp_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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -223,6 +258,7 @@ where
last_farm_token_nonce: 0,
farm_wrapper,
energy_factory_wrapper,
timestamp_oracle_wrapper,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a9b52d7

Please sign in to comment.