Skip to content

Commit

Permalink
farm staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 1, 2024
1 parent b7ef395 commit 899188a
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 48 deletions.
203 changes: 177 additions & 26 deletions farm-staking/farm-staking/tests/farm_staking_energy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ use multiversx_sc_scenario::{

#[test]
fn farm_staking_with_energy_setup_test() {
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand All @@ -28,8 +31,11 @@ fn farm_staking_with_energy_setup_test() {
#[test]
fn farm_staking_boosted_rewards_no_energy_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -74,8 +80,11 @@ fn farm_staking_boosted_rewards_no_energy_test() {
#[test]
fn farm_staking_other_user_enter_negative_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();
let rand_user = fs_setup.b_mock.create_user_account(&rust_biguint!(0));
Expand Down Expand Up @@ -120,8 +129,11 @@ fn farm_staking_other_user_enter_negative_test() {
#[test]
fn farm_staking_boosted_rewards_with_energy_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();
let user_address2 = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -345,8 +357,11 @@ fn farm_staking_boosted_rewards_with_energy_test() {
#[test]
fn farm_staking_partial_position_handling_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -507,8 +522,11 @@ fn farm_staking_partial_position_handling_test() {
#[test]
fn farm_staking_claim_boosted_rewards_for_user_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -621,8 +639,11 @@ fn farm_staking_claim_boosted_rewards_for_user_test() {
#[test]
fn farm_staking_full_position_boosted_rewards_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user_address = fs_setup.user_address.clone();

Expand Down Expand Up @@ -736,8 +757,11 @@ fn farm_staking_full_position_boosted_rewards_test() {
#[test]
fn position_owner_change_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let first_user = fs_setup.user_address.clone();
let second_user = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -1028,8 +1052,11 @@ fn position_owner_change_test() {
#[test]
fn farm_staking_farm_position_migration_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let user = fs_setup.user_address.clone();

Expand Down Expand Up @@ -1122,8 +1149,11 @@ fn farm_staking_farm_position_migration_test() {
#[test]
fn boosted_rewards_config_change_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

let first_user = fs_setup.user_address.clone();
let second_user = fs_setup.user_address2.clone();
Expand Down Expand Up @@ -1432,8 +1462,11 @@ fn boosted_rewards_config_change_test() {
#[test]
fn claim_only_boosted_rewards_per_week_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down Expand Up @@ -1527,8 +1560,11 @@ fn claim_only_boosted_rewards_per_week_test() {
#[test]
fn claim_rewards_per_week_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down Expand Up @@ -1621,8 +1657,11 @@ fn claim_rewards_per_week_test() {
#[test]
fn claim_boosted_rewards_with_zero_position_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);
let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
Expand Down Expand Up @@ -1717,3 +1756,115 @@ fn claim_boosted_rewards_with_zero_position_test() {
Some(&expected_attributes),
);
}

#[test]
fn test_multiple_positions_on_behalf() {
DebugApi::dummy();

let mut fs_setup = FarmStakingSetup::new(
farm_staking::contract_obj,
energy_factory::contract_obj,
permissions_hub::contract_obj,
);

fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
fs_setup.set_boosted_yields_factors();
let mut block_nonce = 0u64;
fs_setup.b_mock.set_block_nonce(block_nonce);

// new external user
let external_user = fs_setup.b_mock.create_user_account(&rust_biguint!(0));
fs_setup.set_user_energy(&external_user, 1_000, 1, 1);

// authorized address
let farm_token_amount = 100_000_000;
let authorized_address = fs_setup.user_address.clone();
fs_setup.b_mock.set_esdt_balance(
&authorized_address,
FARMING_TOKEN_ID,
&rust_biguint!(farm_token_amount * 2),
);

fs_setup.whitelist_address_on_behalf(&external_user, &authorized_address);

fs_setup.check_farm_token_supply(0);
fs_setup.stake_farm_on_behalf(&authorized_address, &external_user, farm_token_amount, 0, 0);
fs_setup.check_farm_token_supply(farm_token_amount);

let block_nonce_diff = 10u64;
block_nonce += block_nonce_diff;
fs_setup.b_mock.set_block_nonce(block_nonce);

let base_rewards = 30u64;
let boosted_rewards = 10u64;
let total_rewards = base_rewards + boosted_rewards;

// Only base rewards are given
fs_setup
.b_mock
.check_esdt_balance(&external_user, REWARD_TOKEN_ID, &rust_biguint!(0));
fs_setup.claim_rewards_on_behalf(&authorized_address, 1, farm_token_amount);
fs_setup.b_mock.check_esdt_balance(
&external_user,
REWARD_TOKEN_ID,
&rust_biguint!(base_rewards),
);

// random tx on end of week 1, to cummulate rewards
fs_setup.b_mock.set_block_epoch(6);
let temp_user = fs_setup.b_mock.create_user_account(&rust_biguint!(0));
fs_setup.b_mock.set_esdt_balance(
&temp_user,
FARMING_TOKEN_ID,
&rust_biguint!(USER_TOTAL_RIDE_TOKENS),
);
fs_setup.set_user_energy(&external_user, 1_000, 6, 1);
fs_setup.set_user_energy(&temp_user, 1, 6, 1);
fs_setup.stake_farm(&temp_user, 10, &[], 3, 300_000u64, 0);
fs_setup.unstake_farm_no_checks(&temp_user, 10, 3);

// advance 1 week
block_nonce += block_nonce_diff;
fs_setup.b_mock.set_block_nonce(block_nonce);
fs_setup.b_mock.set_block_epoch(10);
fs_setup.set_user_energy(&external_user, 1_000, 10, 1);

// enter farm again for the same user (with additional payment)
fs_setup.check_farm_token_supply(farm_token_amount);
fs_setup.stake_farm_on_behalf(
&authorized_address,
&external_user,
farm_token_amount,
2, // nonce 2 as the user already claimed with this position
farm_token_amount,
);
fs_setup.check_farm_token_supply(farm_token_amount * 2);
fs_setup.b_mock.check_esdt_balance(
&external_user,
REWARD_TOKEN_ID,
&rust_biguint!(base_rewards + boosted_rewards),
);

fs_setup.claim_rewards_on_behalf(&authorized_address, 5, farm_token_amount * 2);
fs_setup.check_farm_token_supply(farm_token_amount * 2);
fs_setup.b_mock.check_esdt_balance(
&external_user,
REWARD_TOKEN_ID,
&rust_biguint!(total_rewards + base_rewards),
);

let farm_token_attributes: StakingFarmTokenAttributes<DebugApi> = StakingFarmTokenAttributes {
reward_per_share: managed_biguint!(600_000u64),
compounded_reward: managed_biguint!(0),
current_farm_amount: managed_biguint!(farm_token_amount * 2),
original_owner: managed_address!(&external_user),
};

fs_setup.b_mock.check_nft_balance(
&authorized_address,
FARM_TOKEN_ID,
6,
&rust_biguint!(farm_token_amount * 2),
Some(&farm_token_attributes),
);
}
Loading

0 comments on commit 899188a

Please sign in to comment.