Skip to content

Commit

Permalink
fix base farm staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 29, 2024
1 parent a9d7a4a commit 89abd34
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 43 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.

4 changes: 0 additions & 4 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ pub trait Farm:

let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set(current_epoch);

// Farm position migration code
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);
}

#[upgrade]
Expand Down
3 changes: 3 additions & 0 deletions farm-staking/farm-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ num-bigint = "0.4.2"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.53.2"

[dev-dependencies.timestamp-oracle]
path = "../../energy-integration/timestamp-oracle"
16 changes: 7 additions & 9 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,24 @@ 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);
self.min_unbond_epochs().set(min_unbond_epochs);

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);
}

#[upgrade]
fn upgrade(&self, timestamp_oracle_address: ManagedAddress) {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set_if_empty(current_epoch);
if self.first_week_start_epoch().is_empty() {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set(current_epoch);
}

// Farm position migration code
let farm_token_mapper = self.farm_token();
Expand Down
57 changes: 46 additions & 11 deletions farm-staking/farm-staking/tests/farm_staking_setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use common_structs::Timestamp;
use farm_boosted_yields::custom_reward_logic::CustomRewardLogicModule;
use farm_staking::claim_only_boosted_staking_rewards::ClaimOnlyBoostedStakingRewardsModule;
use farm_staking::compound_stake_farm_rewards::CompoundStakeFarmRewardsModule;
use multiversx_sc::codec::multi_types::OptionalValue;
Expand All @@ -24,6 +26,8 @@ use farm_staking::*;
use farm_token::FarmTokenModule;
use pausable::{PausableModule, State};
use rewards::RewardsModule;
use timestamp_oracle::epoch_to_timestamp::EpochToTimestampModule;
use timestamp_oracle::TimestampOracle;

pub static REWARD_TOKEN_ID: &[u8] = b"RIDE-abcdef"; // reward token ID
pub static FARMING_TOKEN_ID: &[u8] = b"RIDE-abcdef"; // farming token ID
Expand All @@ -42,18 +46,21 @@ 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 WITHDRAW_AMOUNT_TOO_HIGH: &str =
pub static WITHDRAW_AMOUNT_TOO_HIGH: &str =
"Withdraw amount is higher than the remaining uncollected rewards!";

pub const TIMESTAMP_PER_EPOCH: Timestamp = 24 * 60 * 60;

pub struct NonceAmountPair {
pub nonce: u64,
pub amount: u64,
}

pub struct FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder>
pub struct FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm_staking::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: Address,
Expand All @@ -62,31 +69,55 @@ where
pub farm_wrapper: ContractObjWrapper<farm_staking::ContractObj<DebugApi>, FarmObjBuilder>,
pub energy_factory_wrapper:
ContractObjWrapper<energy_factory::ContractObj<DebugApi>, EnergyFactoryBuilder>,
pub timestamp_oracle_wrapper:
ContractObjWrapper<timestamp_oracle::ContractObj<DebugApi>, TimestampOracleObjBuilder>,
}

impl<FarmObjBuilder, EnergyFactoryBuilder> FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder>
impl<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm_staking::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!(0u64);
let mut b_mock = BlockchainStateWrapper::new();
let owner_addr = b_mock.create_user_account(&rust_zero);
let owner = b_mock.create_user_account(&rust_zero);
let farm_wrapper =
b_mock.create_sc_account(&rust_zero, Some(&owner_addr), farm_builder, "farm-staking");
b_mock.create_sc_account(&rust_zero, Some(&owner), farm_builder, "farm-staking");

let energy_factory_wrapper = b_mock.create_sc_account(
&rust_zero,
Some(&owner_addr),
Some(&owner),
energy_factory_builder,
"energy_factory.wasm",
);

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();

// init farm contract

b_mock
.execute_tx(&owner_addr, &farm_wrapper, &rust_zero, |sc| {
.execute_tx(&owner, &farm_wrapper, &rust_zero, |sc| {
let farming_token_id = managed_token_id!(FARMING_TOKEN_ID);
let division_safety_constant = managed_biguint!(DIVISION_SAFETY_CONSTANT);

Expand All @@ -110,13 +141,16 @@ where

sc.energy_factory_address()
.set(managed_address!(energy_factory_wrapper.address_ref()));
sc.set_timestamp_oracle_address(managed_address!(
timestamp_oracle_wrapper.address_ref()
));
})
.assert_ok();

b_mock.set_esdt_balance(&owner_addr, REWARD_TOKEN_ID, &TOTAL_REWARDS_AMOUNT.into());
b_mock.set_esdt_balance(&owner, REWARD_TOKEN_ID, &TOTAL_REWARDS_AMOUNT.into());
b_mock
.execute_esdt_transfer(
&owner_addr,
&owner,
&farm_wrapper,
REWARD_TOKEN_ID,
0,
Expand Down Expand Up @@ -160,11 +194,12 @@ where

FarmStakingSetup {
b_mock,
owner_address: owner_addr,
owner_address: owner,
user_address: user_addr,
user_address2: user_addr2,
farm_wrapper,
energy_factory_wrapper,
timestamp_oracle_wrapper,
}
}

Expand Down
81 changes: 62 additions & 19 deletions farm-staking/farm-staking/tests/farm_staking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ use farm_staking_setup::*;

#[test]
fn test_farm_setup() {
let _ = FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let _ = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);
}

#[test]
fn test_enter_farm() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand All @@ -36,8 +44,12 @@ fn test_enter_farm() {
#[test]
fn test_unstake_farm() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand Down Expand Up @@ -88,8 +100,12 @@ fn test_unstake_farm() {
#[test]
fn test_claim_rewards() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand Down Expand Up @@ -126,15 +142,21 @@ fn test_claim_rewards() {
farm_setup.check_farm_token_supply(farm_in_amount);
}

fn steps_enter_farm_twice<FarmObjBuilder, EnergyFactoryBuilder>(
fn steps_enter_farm_twice<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>(
farm_builder: FarmObjBuilder,
energy_factory_builder: EnergyFactoryBuilder,
) -> FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder>
timestamp_oracle_builder: TimestampOracleObjBuilder,
) -> FarmStakingSetup<FarmObjBuilder, EnergyFactoryBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm_staking::ContractObj<DebugApi>,
EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
let mut farm_setup = FarmStakingSetup::new(farm_builder, energy_factory_builder);
let mut farm_setup = FarmStakingSetup::new(
farm_builder,
energy_factory_builder,
timestamp_oracle_builder,
);

let user_address = farm_setup.user_address.clone();

Expand Down Expand Up @@ -185,14 +207,23 @@ where
#[test]
fn test_enter_farm_twice() {
DebugApi::dummy();
let _ = steps_enter_farm_twice(farm_staking::contract_obj, energy_factory::contract_obj);

let _ = steps_enter_farm_twice(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);
}

#[test]
fn test_exit_farm_after_enter_twice() {
DebugApi::dummy();
let mut farm_setup =
steps_enter_farm_twice(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = steps_enter_farm_twice(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand Down Expand Up @@ -225,8 +256,12 @@ fn test_exit_farm_after_enter_twice() {
#[test]
fn test_unbond() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand Down Expand Up @@ -286,8 +321,12 @@ fn test_unbond() {
#[test]
fn test_withdraw_rewards() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let initial_rewards_capacity = 1_000_000_000_000u64;
farm_setup.check_rewards_capacity(initial_rewards_capacity);
Expand All @@ -302,8 +341,12 @@ fn test_withdraw_rewards() {
#[test]
fn test_withdraw_after_produced_rewards() {
DebugApi::dummy();
let mut farm_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

let mut farm_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
timestamp_oracle::contract_obj,
);

let user_address = farm_setup.user_address.clone();

Expand Down

0 comments on commit 89abd34

Please sign in to comment.