Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

farm position functionality tests #776

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,110 @@ fn farm_with_boosted_yields_no_proxy_test() {
None,
);
}

#[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,
);

farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
farm_setup.set_boosted_yields_factors();
farm_setup.b_mock.set_block_epoch(2);

let temp_user = farm_setup.third_user.clone();

// first user enter farm
let farm_in_amount = 50_000_000;
let first_user = farm_setup.first_user.clone();
farm_setup.set_user_energy(&first_user, 1_000, 2, 1);
farm_setup.enter_farm(&first_user, farm_in_amount);
farm_setup.enter_farm(&first_user, farm_in_amount);

farm_setup.b_mock.check_nft_balance(
&first_user,
FARM_TOKEN_ID,
1,
&rust_biguint!(farm_in_amount),
Some(&FarmTokenAttributes::<DebugApi> {
reward_per_share: managed_biguint!(0),
compounded_reward: managed_biguint!(0),
entering_epoch: 2,
current_farm_amount: managed_biguint!(farm_in_amount),
original_owner: managed_address!(&first_user),
}),
);

farm_setup.b_mock.check_nft_balance(
&first_user,
FARM_TOKEN_ID,
2,
&rust_biguint!(farm_in_amount),
Some(&FarmTokenAttributes::<DebugApi> {
reward_per_share: managed_biguint!(0),
compounded_reward: managed_biguint!(0),
entering_epoch: 2,
current_farm_amount: managed_biguint!(farm_in_amount),
original_owner: managed_address!(&first_user),
}),
);

// users claim rewards to get their energy registered
let _ = farm_setup.claim_rewards(&first_user, 1, farm_in_amount);

// advance blocks - 10 blocks - 10 * 1_000 = 10_000 total rewards
// 7_500 base farm, 2_500 boosted yields
farm_setup.b_mock.set_block_nonce(10);

// random tx on end of week 1, to cummulate rewards
farm_setup.b_mock.set_block_epoch(6);
farm_setup.set_user_energy(&first_user, 1_000, 6, 1);
farm_setup.set_user_energy(&temp_user, 1, 6, 1);
farm_setup.enter_farm(&temp_user, 1);
farm_setup.exit_farm(&temp_user, 4, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
farm_setup.set_user_energy(&first_user, 1_000, 10, 1);

let total_farm_tokens = farm_in_amount * 2;

// first user claim with half total position
let first_base_farm_amt = farm_in_amount * 7_500 / total_farm_tokens;

// Boosted yields rewards formula
// total_boosted_rewards * (energy_const * user_energy / total_energy + farm_const * user_farm / total_farm) / (energy_const + farm_const)
// (total_boosted_rewards * energy_const * user_energy / total_energy + total_boosted_rewards * farm_const * user_farm / total_farm) / (energy_const + farm_const)
// (2_500 * 3 * 1_000 / 1_000 + 2_500 * 2 * 100_000_000 / 100_000_000) / (3 + 2)
// (7_500 + 2_500) / (5) = 2_500
let first_boosted_amt = 2_500; // 1000 energy & 100_000_000 farm tokens
let first_total_rewards = first_base_farm_amt + first_boosted_amt;

let first_received_reward_amt = farm_setup.claim_rewards(&first_user, 3, farm_in_amount);

// Should be equal to half base generated rewards + full boosted generated rewards
assert_eq!(first_received_reward_amt, first_total_rewards);

farm_setup
.b_mock
.check_nft_balance::<FarmTokenAttributes<DebugApi>>(
&first_user,
FARM_TOKEN_ID,
5,
&rust_biguint!(farm_in_amount),
None,
);

// Check user receive locked rewards
farm_setup
.b_mock
.check_nft_balance::<LockedTokenAttributes<DebugApi>>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(first_received_reward_amt),
None,
);
}
96 changes: 0 additions & 96 deletions dex/farm/tests/farm_multi_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,102 +221,6 @@ fn farm_with_boosted_yields_test() {
);
}

#[test]
fn farm_claim_boosted_yields_for_other_user_test() {
DebugApi::dummy();
let mut farm_setup = MultiUserFarmSetup::new(
farm::contract_obj,
energy_factory_mock::contract_obj,
energy_update::contract_obj,
);

farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
farm_setup.set_boosted_yields_factors();
farm_setup.b_mock.set_block_epoch(2);

// first user enter farm
let first_farm_token_amount = 100_000_000;
let first_user = farm_setup.first_user.clone();
let third_user = farm_setup.third_user.clone();

farm_setup.set_user_energy(&first_user, 1_000, 2, 1);
farm_setup.enter_farm(&first_user, first_farm_token_amount);

// second user enter farm
let second_farm_token_amount = 50_000_000;
let second_user = farm_setup.second_user.clone();
farm_setup.set_user_energy(&second_user, 4_000, 2, 1);
farm_setup.enter_farm(&second_user, second_farm_token_amount);

// users claim rewards to get their energy registered
let _ = farm_setup.claim_rewards(&first_user, 1, first_farm_token_amount);
let _ = farm_setup.claim_rewards(&second_user, 2, second_farm_token_amount);

// advance blocks - 10 blocks - 10 * 1_000 = 10_000 total rewards
// 7_500 base farm, 2_500 boosted yields
farm_setup.b_mock.set_block_nonce(10);

// random tx on end of week 1, to cummulate rewards
farm_setup.b_mock.set_block_epoch(6);
farm_setup.set_user_energy(&first_user, 1_000, 6, 1);
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
farm_setup.set_user_energy(&first_user, 1_000, 10, 1);
farm_setup.set_user_energy(&second_user, 4_000, 10, 1);

// Second user claims for himself
let total_farm_tokens = first_farm_token_amount + second_farm_token_amount;
let second_base_farm_amt = second_farm_token_amount * 7_500 / total_farm_tokens;
let second_boosted_amt = 1533; // 4000 energy & 50_000_000 farm tokens
let second_total = second_base_farm_amt + second_boosted_amt;

let second_receveived_reward_amt =
farm_setup.claim_rewards(&second_user, 4, second_farm_token_amount);
assert_eq!(second_receveived_reward_amt, second_total);

farm_setup
.b_mock
.check_nft_balance::<FarmTokenAttributes<DebugApi>>(
&second_user,
FARM_TOKEN_ID,
6,
&rust_biguint!(second_farm_token_amount),
None,
);

farm_setup.b_mock.check_esdt_balance(
&second_user,
REWARD_TOKEN_ID,
&rust_biguint!(second_receveived_reward_amt),
);

// Boosted yields rewards formula
// total_boosted_rewards * (energy_const * user_energy / total_energy + farm_const * user_farm / total_farm) / (energy_const + farm_const)
// (total_boosted_rewards * energy_const * user_energy / total_energy + total_boosted_rewards * farm_const * user_farm / total_farm) / (energy_const + farm_const)
// (2500 * 3 * 1_000 / 5_000 + 2500 * 2 * 100_000_000 / 150_000_000) / (3 + 2)
// (1500 + 3333) / (5) = 966
let first_boosted_amt = 966; // 1000 energy & 100_000_000 farm tokens

// Second user claim boosted rewards for first user
farm_setup.allow_external_claim_rewards(&first_user);

let first_receveived_boosted_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &second_user);
assert_eq!(first_receveived_boosted_amt, first_boosted_amt);

// First user should receive the boosted rewards
farm_setup.b_mock.check_esdt_balance(
&first_user,
REWARD_TOKEN_ID,
&rust_biguint!(first_receveived_boosted_amt),
);
}

#[test]
fn farm_change_boosted_yields_factors_test() {
DebugApi::dummy();
Expand Down
60 changes: 57 additions & 3 deletions dex/farm/tests/farm_setup/multi_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(deprecated)]

use common_structs::FarmTokenAttributes;
use config::ConfigModule;
use config::{ConfigModule, UserTotalFarmPosition};
use multiversx_sc::codec::multi_types::OptionalValue;
use multiversx_sc::{
storage::mappers::StorageTokenWrapper,
Expand Down Expand Up @@ -472,6 +472,18 @@ where
result
}

pub fn claim_boosted_rewards_for_user_expect_error(
&mut self,
owner: &Address,
broker: &Address,
) {
self.b_mock
.execute_tx(broker, &self.farm_wrapper, &rust_biguint!(0), |sc| {
let _ = sc.claim_boosted_rewards(OptionalValue::Some(managed_address!(owner)));
})
.assert_error(4, "Cannot claim rewards for this address");
}

pub fn claim_rewards_known_proxy(
&mut self,
user: &Address,
Expand Down Expand Up @@ -551,12 +563,13 @@ where
.assert_ok();
}

pub fn allow_external_claim_rewards(&mut self, user: &Address) {
pub fn allow_external_claim_rewards(&mut self, user: &Address, allow_external_claim: bool) {
self.b_mock
.execute_tx(user, &self.farm_wrapper, &rust_biguint!(0), |sc| {
sc.user_total_farm_position(&managed_address!(user)).update(
|user_total_farm_position| {
user_total_farm_position.allow_external_claim_boosted_rewards = true;
user_total_farm_position.allow_external_claim_boosted_rewards =
allow_external_claim;
},
);
})
Expand Down Expand Up @@ -637,4 +650,45 @@ where
})
.assert_ok();
}

pub fn check_farm_token_supply(&mut self, expected_farm_token_supply: u64) {
let b_mock = &mut self.b_mock;
b_mock
.execute_query(&self.farm_wrapper, |sc| {
let actual_farm_supply = sc.farm_token_supply().get();
assert_eq!(
managed_biguint!(expected_farm_token_supply),
actual_farm_supply
);
})
.assert_ok();
}

pub fn set_user_total_farm_position(&mut self, user_addr: &Address, new_farm_position: u64) {
self.b_mock
.execute_tx(&self.owner, &self.farm_wrapper, &rust_biguint!(0), |sc| {
let user_farm_position = UserTotalFarmPosition {
total_farm_position: managed_biguint!(new_farm_position),
..Default::default()
};
sc.user_total_farm_position(&managed_address!(user_addr))
.set(user_farm_position);
})
.assert_ok();
}

pub fn check_user_total_farm_position(&mut self, user_addr: &Address, expected_amount: u64) {
self.b_mock
.execute_query(&self.farm_wrapper, |sc| {
let user_total_farm_position_mapper =
sc.user_total_farm_position(&managed_address!(user_addr));
if expected_amount > 0 && !user_total_farm_position_mapper.is_empty() {
assert_eq!(
managed_biguint!(expected_amount),
user_total_farm_position_mapper.get().total_farm_position
);
}
})
.assert_ok();
}
}
Loading
Loading