Skip to content

Commit

Permalink
farm staking proxy test setup updates
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Oct 15, 2023
1 parent 69a57e3 commit c05ec53
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 25 deletions.
9 changes: 9 additions & 0 deletions farm-staking/farm-staking-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions farm-staking/farm-staking-proxy/tests/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
40 changes: 27 additions & 13 deletions farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -469,3 +480,6 @@ fn test_stake_farm_through_proxy_with_merging() {
)
});
}

#[test]
fn test_farm_stake_proxy_merging_boosted_rewards() {}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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::*;

Expand Down Expand Up @@ -241,12 +247,13 @@ fn remove_liquidity<PairObjBuilder>(
pub fn setup_lp_farm<FarmObjBuilder>(
owner_addr: &Address,
user_addr: &Address,
energy_factory_address: &Address,
b_mock: &mut BlockchainStateWrapper,
farm_builder: FarmObjBuilder,
user_farm_in_amount: u64,
) -> ContractObjWrapper<farm::ContractObj<DebugApi>, FarmObjBuilder>
) -> ContractObjWrapper<farm_with_locked_rewards::ContractObj<DebugApi>, FarmObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj<DebugApi>,
{
let rust_zero = rust_biguint!(0u64);
let farm_wrapper =
Expand All @@ -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());
Expand All @@ -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();

Expand Down Expand Up @@ -327,14 +339,80 @@ where
farm_wrapper
}

pub fn setup_energy_factory<EnergyFactoryObjBuilder>(
owner_addr: &Address,
b_mock: &mut BlockchainStateWrapper,
energy_factory_builder: EnergyFactoryObjBuilder,
) -> ContractObjWrapper<energy_factory::ContractObj<DebugApi>, EnergyFactoryObjBuilder>
where
EnergyFactoryObjBuilder: 'static + Copy + Fn() -> energy_factory::ContractObj<DebugApi>,
{
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<FarmObjBuilder>(
user_address: &Address,
b_mock: &mut BlockchainStateWrapper,
farm_wrapper: &ContractObjWrapper<farm::ContractObj<DebugApi>, FarmObjBuilder>,
farm_wrapper: &ContractObjWrapper<
farm_with_locked_rewards::ContractObj<DebugApi>,
FarmObjBuilder,
>,
farm_in_amount: u64,
additional_farm_tokens: &[TxTokenTransfer],
) where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
FarmObjBuilder: 'static + Copy + Fn() -> farm_with_locked_rewards::ContractObj<DebugApi>,
{
let mut payments = Vec::with_capacity(1 + additional_farm_tokens.len());
payments.push(TxTokenTransfer {
Expand Down
Loading

0 comments on commit c05ec53

Please sign in to comment.