From a9d7a4adbfc503d81ad0353f923fd9cc60226ee1 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 29 Oct 2024 13:06:06 +0200 Subject: [PATCH] fix other 2 tests --- dex/farm/tests/total_farm_position_test.rs | 670 ++++++++++----------- 1 file changed, 331 insertions(+), 339 deletions(-) diff --git a/dex/farm/tests/total_farm_position_test.rs b/dex/farm/tests/total_farm_position_test.rs index 1b5c48a0e..3887e9d54 100644 --- a/dex/farm/tests/total_farm_position_test.rs +++ b/dex/farm/tests/total_farm_position_test.rs @@ -4,7 +4,7 @@ use common_structs::FarmTokenAttributes; use config::ConfigModule; use farm_setup::multi_user_farm_setup::{ MultiUserFarmSetup, NonceAmountPair, BOOSTED_YIELDS_PERCENTAGE, MAX_PERCENTAGE, - PER_BLOCK_REWARD_AMOUNT, + PER_BLOCK_REWARD_AMOUNT, TIMESTAMP_PER_EPOCH, }; use multiversx_sc::types::EsdtLocalRole; use multiversx_sc_scenario::{ @@ -682,344 +682,336 @@ fn farm_total_position_on_merge_migration_test() { .check_esdt_balance(&first_user, REWARD_TOKEN_ID, &rust_biguint!(0)); } -// #[test] -// fn no_boosted_rewards_penalty_for_no_energy_test() { -// DebugApi::dummy(); - -// let mut farm_setup = MultiUserFarmSetup::new( -// farm::contract_obj, -// energy_factory_mock::contract_obj, -// energy_update::contract_obj, -// timestamp_oracle::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(5); -// farm_setup -// .b_mock -// .set_block_timestamp(5 * TIMESTAMP_PER_EPOCH); - -// 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, 5, 1); -// farm_setup.enter_farm(&first_user, farm_in_amount); -// farm_setup.enter_farm(&first_user, farm_in_amount); - -// // users claim rewards to get their energy registered -// let _ = farm_setup.claim_rewards(&first_user, 2, farm_in_amount); - -// // advance to week 1 - -// // 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 the week, to cummulate rewards -// farm_setup.b_mock.set_block_epoch(6); -// farm_setup -// .b_mock -// .set_block_timestamp(6 * TIMESTAMP_PER_EPOCH); - -// 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 to week 2 -// farm_setup.b_mock.set_block_nonce(20); -// farm_setup.b_mock.set_block_epoch(13); -// farm_setup -// .b_mock -// .set_block_timestamp(14 * TIMESTAMP_PER_EPOCH - 1); - -// // User unlocks XMEX and has no energy -// farm_setup.set_user_energy(&first_user, 0, 13, 1); - -// // random tx on end of the week, to cummulate rewards -// farm_setup.set_user_energy(&temp_user, 1, 13, 1); -// farm_setup.enter_farm(&temp_user, 1); -// farm_setup.exit_farm(&temp_user, 5, 1); - -// // advance to week 3 -// farm_setup.b_mock.set_block_nonce(30); - -// // random tx on end of the week, to cummulate rewards -// farm_setup.b_mock.set_block_epoch(20); -// farm_setup -// .b_mock -// .set_block_timestamp(21 * TIMESTAMP_PER_EPOCH - 1); - -// farm_setup.set_user_energy(&temp_user, 1, 20, 1); -// farm_setup.enter_farm(&temp_user, 1); -// farm_setup.exit_farm(&temp_user, 6, 1); - -// // advance to week 4 -// farm_setup.b_mock.set_block_epoch(25); -// farm_setup -// .b_mock -// .set_block_timestamp(26 * TIMESTAMP_PER_EPOCH - 1); - -// // first user claims 3 weeks worth of rewards (2-4) -// let total_farm_tokens = farm_in_amount * 2; -// let first_base_farm_amt = (farm_in_amount * 7_500 / total_farm_tokens) * 3; -// let first_boosted_amt = 2_500 * 3; -// let first_total_rewards = first_base_farm_amt + first_boosted_amt; - -// let first_receveived_reward_amt = farm_setup.claim_rewards(&first_user, 1, farm_in_amount); - -// // Should be equal to half base generated rewards + full boosted generated rewards -// assert_eq!(first_receveived_reward_amt, first_total_rewards); - -// farm_setup.b_mock.check_esdt_balance( -// &first_user, -// REWARD_TOKEN_ID, -// &rust_biguint!(first_receveived_reward_amt), -// ); -// } - -// #[test] -// fn total_farm_position_owner_change_test() { -// DebugApi::dummy(); - -// let mut farm_setup = MultiUserFarmSetup::new( -// farm::contract_obj, -// energy_factory_mock::contract_obj, -// energy_update::contract_obj, -// timestamp_oracle::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); -// farm_setup -// .b_mock -// .set_block_timestamp(2 * TIMESTAMP_PER_EPOCH); - -// // first user enters farm 6 times -// let farm_token_amount = 10_000_000; -// let half_token_amount = farm_token_amount / 2; -// let first_user = farm_setup.first_user.clone(); -// let second_user = farm_setup.second_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, farm_token_amount); -// farm_setup.enter_farm(&first_user, farm_token_amount); -// farm_setup.enter_farm(&first_user, farm_token_amount); -// farm_setup.enter_farm(&first_user, farm_token_amount); -// farm_setup.enter_farm(&first_user, farm_token_amount); -// farm_setup.enter_farm(&first_user, farm_token_amount); - -// let mut first_user_total_position = farm_token_amount * 6; -// let mut second_user_total_position = 0; -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// assert_eq!(farm_setup.last_farm_token_nonce, 6); - -// // First user transfers 5 position to second user -// farm_setup.send_farm_position(&first_user, &second_user, 1, farm_token_amount, 0, 2); -// farm_setup.send_farm_position(&first_user, &second_user, 2, farm_token_amount, 0, 2); -// farm_setup.send_farm_position(&first_user, &second_user, 3, farm_token_amount, 0, 2); -// farm_setup.send_farm_position(&first_user, &second_user, 4, farm_token_amount, 0, 2); -// farm_setup.send_farm_position(&first_user, &second_user, 5, farm_token_amount, 0, 2); - -// // Total farm position unchanged as users only transfered the farm positions -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// // second user enter farm with LP token + 50% the position from another user -// farm_setup.set_user_energy(&second_user, 4_000, 2, 1); -// farm_setup.enter_farm_with_additional_payment( -// &second_user, -// farm_token_amount, -// 1, -// half_token_amount, -// ); - -// // 1 half farm position was removed from first user and added to the second user (who entered the farm with a position of his own) -// first_user_total_position -= half_token_amount; -// second_user_total_position += farm_token_amount + half_token_amount; -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// // users claim rewards to get their energy registered -// let _ = farm_setup.claim_rewards(&first_user, 6, farm_token_amount); -// let _ = farm_setup.claim_rewards(&second_user, 7, farm_token_amount); - -// // random tx on end of week 1, to cummulate rewards -// farm_setup.b_mock.set_block_nonce(10); -// farm_setup.b_mock.set_block_epoch(6); -// farm_setup -// .b_mock -// .set_block_timestamp(7 * TIMESTAMP_PER_EPOCH - 1); - -// 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, 10, 1); - -// // advance 1 week -// farm_setup.b_mock.set_block_epoch(10); -// farm_setup -// .b_mock -// .set_block_timestamp(11 * TIMESTAMP_PER_EPOCH - 1); - -// 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 with half a position from the first user -// let base_rewards_amount = 535; -// let boosted_rewards_amount = 1414; -// let mut second_user_reward_balance = base_rewards_amount + boosted_rewards_amount; - -// let second_received_reward_amt = farm_setup.claim_rewards(&second_user, 2, half_token_amount); -// assert_eq!(second_received_reward_amt, second_user_reward_balance); - -// farm_setup.b_mock.check_esdt_balance( -// &second_user, -// REWARD_TOKEN_ID, -// &rust_biguint!(second_user_reward_balance), -// ); -// farm_setup.b_mock.check_nft_balance( -// &second_user, -// FARM_TOKEN_ID, -// 11, -// &rust_biguint!(half_token_amount), -// Some(&FarmTokenAttributes:: { -// reward_per_share: managed_biguint!(107142857), -// entering_epoch: 2, -// compounded_reward: managed_biguint!(0), -// current_farm_amount: managed_biguint!(half_token_amount), -// original_owner: managed_address!(&second_user), -// }), -// ); - -// // Check users positions after claim -// first_user_total_position -= half_token_amount; -// second_user_total_position += half_token_amount; -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// // random tx on end of week 2, to cummulate rewards -// farm_setup.b_mock.set_block_nonce(20); -// farm_setup.b_mock.set_block_epoch(13); -// farm_setup -// .b_mock -// .set_block_timestamp(14 * TIMESTAMP_PER_EPOCH - 1); - -// farm_setup.set_user_energy(&first_user, 1_000, 13, 1); -// farm_setup.set_user_energy(&second_user, 4_000, 13, 1); -// farm_setup.set_user_energy(&third_user, 1, 13, 1); -// farm_setup.enter_farm(&third_user, 1); -// farm_setup.exit_farm(&third_user, 12, 1); - -// // advance 1 week -// farm_setup.b_mock.set_block_epoch(15); -// farm_setup -// .b_mock -// .set_block_timestamp(15 * TIMESTAMP_PER_EPOCH); - -// farm_setup.set_user_energy(&first_user, 1_000, 15, 1); -// farm_setup.set_user_energy(&second_user, 4_000, 15, 1); - -// // Second user exits farm with half of a position previously owned by user 1 -// second_user_reward_balance += 1071; // base rewards -// second_user_reward_balance += 1487; // boosted rewards -// farm_setup.exit_farm(&second_user, 3, half_token_amount); -// farm_setup -// .b_mock -// .check_esdt_balance(&second_user, REWARD_TOKEN_ID, &rust_biguint!(4507)); - -// // Check users positions after exit -// first_user_total_position -= half_token_amount; -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// // random tx on end of week 3, to cummulate rewards -// farm_setup.b_mock.set_block_nonce(30); -// farm_setup.b_mock.set_block_epoch(20); -// farm_setup -// .b_mock -// .set_block_timestamp(21 * TIMESTAMP_PER_EPOCH - 1); - -// farm_setup.set_user_energy(&first_user, 1_000, 20, 1); -// farm_setup.set_user_energy(&second_user, 4_000, 20, 1); -// farm_setup.set_user_energy(&third_user, 1, 20, 1); -// farm_setup.enter_farm(&third_user, 1); -// farm_setup.exit_farm(&third_user, 13, 1); - -// // advance 1 week -// farm_setup.b_mock.set_block_epoch(25); -// farm_setup -// .b_mock -// .set_block_timestamp(26 * TIMESTAMP_PER_EPOCH - 1); - -// farm_setup.set_user_energy(&first_user, 1_000, 25, 1); -// farm_setup.set_user_energy(&second_user, 4_000, 25, 1); - -// // First user claims rewards -// let first_user_received_reward_amt = -// farm_setup.claim_rewards(&first_user, 8, farm_token_amount); -// assert_eq!(first_user_received_reward_amt, 6167); - -// // Check users positions after first user claim -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); - -// // Second user merges half from one of his original position with 2 position halves from the first user -// let farm_tokens = vec![ -// NonceAmountPair { -// nonce: 4, -// amount: half_token_amount, -// }, -// NonceAmountPair { -// nonce: 5, -// amount: half_token_amount, -// }, -// NonceAmountPair { -// nonce: 11, -// amount: half_token_amount, -// }, -// ]; - -// farm_setup.b_mock.check_esdt_balance( -// &second_user, -// REWARD_TOKEN_ID, -// &rust_biguint!(second_user_reward_balance), -// ); -// farm_setup.merge_farm_tokens(&second_user, farm_tokens); -// second_user_reward_balance += 1510; // boosted rewards -// farm_setup.b_mock.check_esdt_balance( -// &second_user, -// REWARD_TOKEN_ID, -// &rust_biguint!(second_user_reward_balance), -// ); -// farm_setup.b_mock.check_nft_balance( -// &second_user, -// FARM_TOKEN_ID, -// 15, -// &rust_biguint!(half_token_amount * 3), -// Some(&FarmTokenAttributes:: { -// reward_per_share: managed_biguint!(35714286), -// entering_epoch: 2, -// compounded_reward: managed_biguint!(0), -// current_farm_amount: managed_biguint!(half_token_amount * 3), -// original_owner: managed_address!(&second_user), -// }), -// ); - -// // Check users positions after merge -// first_user_total_position -= 2 * half_token_amount; -// second_user_total_position += 2 * half_token_amount; -// farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); -// farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); -// } +#[test] +fn no_boosted_rewards_penalty_for_no_energy_test() { + DebugApi::dummy(); + + let mut farm_setup = MultiUserFarmSetup::new( + farm::contract_obj, + energy_factory_mock::contract_obj, + energy_update::contract_obj, + timestamp_oracle::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(5); + farm_setup + .b_mock + .set_block_timestamp(6 * TIMESTAMP_PER_EPOCH - 1); + + 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, 5, 1); + farm_setup.enter_farm(&first_user, farm_in_amount); + farm_setup.enter_farm(&first_user, farm_in_amount); + + // users claim rewards to get their energy registered + let _ = farm_setup.claim_rewards(&first_user, 2, farm_in_amount); + + // advance to week 1 + + // 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 the week, to cummulate rewards + farm_setup.b_mock.set_block_epoch(6); + farm_setup + .b_mock + .set_block_timestamp(7 * TIMESTAMP_PER_EPOCH - 1); + + 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 to week 2 + farm_setup.b_mock.set_block_nonce(20); + farm_setup.b_mock.set_block_epoch(13); + farm_setup + .b_mock + .set_block_timestamp(14 * TIMESTAMP_PER_EPOCH - 1); + + // User unlocks XMEX and has no energy + farm_setup.set_user_energy(&first_user, 0, 13, 1); + + // random tx on end of the week, to cummulate rewards + farm_setup.set_user_energy(&temp_user, 1, 13, 1); + farm_setup.enter_farm(&temp_user, 1); + farm_setup.exit_farm(&temp_user, 5, 1); + + // advance to week 3 + farm_setup.b_mock.set_block_nonce(30); + + // random tx on end of the week, to cummulate rewards + farm_setup.b_mock.set_block_epoch(20); + farm_setup + .b_mock + .set_block_timestamp(21 * TIMESTAMP_PER_EPOCH - 1); + + farm_setup.set_user_energy(&temp_user, 1, 20, 1); + farm_setup.enter_farm(&temp_user, 1); + farm_setup.exit_farm(&temp_user, 6, 1); + + // advance to week 4 + farm_setup.b_mock.set_block_epoch(27); + farm_setup + .b_mock + .set_block_timestamp(28 * TIMESTAMP_PER_EPOCH - 1); + + // first user claims 3 weeks worth of rewards (2-4) + let total_farm_tokens = farm_in_amount * 2; + let first_base_farm_amt = (farm_in_amount * 7_500 / total_farm_tokens) * 3; + let first_boosted_amt = 2_500 * 3; + let first_total_rewards = first_base_farm_amt + first_boosted_amt; + + let first_receveived_reward_amt = farm_setup.claim_rewards(&first_user, 1, farm_in_amount); + + // Should be equal to half base generated rewards + full boosted generated rewards + assert_eq!(first_receveived_reward_amt, first_total_rewards); + + farm_setup.b_mock.check_esdt_balance( + &first_user, + REWARD_TOKEN_ID, + &rust_biguint!(first_receveived_reward_amt), + ); +} + +#[test] +fn total_farm_position_owner_change_test() { + DebugApi::dummy(); + + let mut farm_setup = MultiUserFarmSetup::new( + farm::contract_obj, + energy_factory_mock::contract_obj, + energy_update::contract_obj, + timestamp_oracle::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); + farm_setup + .b_mock + .set_block_timestamp(3 * TIMESTAMP_PER_EPOCH - 1); + + // first user enters farm 6 times + let farm_token_amount = 10_000_000; + let half_token_amount = farm_token_amount / 2; + let first_user = farm_setup.first_user.clone(); + let second_user = farm_setup.second_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, farm_token_amount); + farm_setup.enter_farm(&first_user, farm_token_amount); + farm_setup.enter_farm(&first_user, farm_token_amount); + farm_setup.enter_farm(&first_user, farm_token_amount); + farm_setup.enter_farm(&first_user, farm_token_amount); + farm_setup.enter_farm(&first_user, farm_token_amount); + + let mut first_user_total_position = farm_token_amount * 6; + let mut second_user_total_position = 0; + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + assert_eq!(farm_setup.last_farm_token_nonce, 6); + + // First user transfers 5 position to second user + farm_setup.send_farm_position(&first_user, &second_user, 1, farm_token_amount, 0, 2); + farm_setup.send_farm_position(&first_user, &second_user, 2, farm_token_amount, 0, 2); + farm_setup.send_farm_position(&first_user, &second_user, 3, farm_token_amount, 0, 2); + farm_setup.send_farm_position(&first_user, &second_user, 4, farm_token_amount, 0, 2); + farm_setup.send_farm_position(&first_user, &second_user, 5, farm_token_amount, 0, 2); + + // Total farm position unchanged as users only transfered the farm positions + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + // second user enter farm with LP token + 50% the position from another user + farm_setup.set_user_energy(&second_user, 4_000, 2, 1); + farm_setup.enter_farm_with_additional_payment( + &second_user, + farm_token_amount, + 1, + half_token_amount, + ); + + // 1 half farm position was removed from first user and added to the second user (who entered the farm with a position of his own) + first_user_total_position -= half_token_amount; + second_user_total_position += farm_token_amount + half_token_amount; + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + // users claim rewards to get their energy registered + let _ = farm_setup.claim_rewards(&first_user, 6, farm_token_amount); + let _ = farm_setup.claim_rewards(&second_user, 7, farm_token_amount); + + // random tx on end of week 1, to cummulate rewards + farm_setup.b_mock.set_block_nonce(10); + farm_setup.b_mock.set_block_epoch(6); + farm_setup + .b_mock + .set_block_timestamp(7 * TIMESTAMP_PER_EPOCH - 1); + + 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, 10, 1); + + // advance 1 week + farm_setup.b_mock.set_block_epoch(13); + farm_setup + .b_mock + .set_block_timestamp(14 * TIMESTAMP_PER_EPOCH - 1); + + farm_setup.set_user_energy(&first_user, 1_000, 13, 1); + farm_setup.set_user_energy(&second_user, 4_000, 13, 1); + + // Second user claims with half a position from the first user + let base_rewards_amount = 535; + let boosted_rewards_amount = 1414; + let mut second_user_reward_balance = base_rewards_amount + boosted_rewards_amount; + + let second_received_reward_amt = farm_setup.claim_rewards(&second_user, 2, half_token_amount); + assert_eq!(second_received_reward_amt, second_user_reward_balance); + + farm_setup.b_mock.check_esdt_balance( + &second_user, + REWARD_TOKEN_ID, + &rust_biguint!(second_user_reward_balance), + ); + farm_setup.b_mock.check_nft_balance( + &second_user, + FARM_TOKEN_ID, + 11, + &rust_biguint!(half_token_amount), + Some(&FarmTokenAttributes:: { + reward_per_share: managed_biguint!(107142857), + entering_epoch: 2, + compounded_reward: managed_biguint!(0), + current_farm_amount: managed_biguint!(half_token_amount), + original_owner: managed_address!(&second_user), + }), + ); + + // Check users positions after claim + first_user_total_position -= half_token_amount; + second_user_total_position += half_token_amount; + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + // random tx on end of week 2, to cummulate rewards + farm_setup.b_mock.set_block_nonce(20); + + farm_setup.set_user_energy(&first_user, 1_000, 13, 1); + farm_setup.set_user_energy(&second_user, 4_000, 13, 1); + farm_setup.set_user_energy(&third_user, 1, 13, 1); + farm_setup.enter_farm(&third_user, 1); + farm_setup.exit_farm(&third_user, 12, 1); + + // advance 1 week + farm_setup.b_mock.set_block_epoch(20); + farm_setup + .b_mock + .set_block_timestamp(21 * TIMESTAMP_PER_EPOCH - 1); + + farm_setup.set_user_energy(&first_user, 1_000, 20, 1); + farm_setup.set_user_energy(&second_user, 4_000, 20, 1); + + // Second user exits farm with half of a position previously owned by user 1 + second_user_reward_balance += 1071; // base rewards + second_user_reward_balance += 1487; // boosted rewards + farm_setup.exit_farm(&second_user, 3, half_token_amount); + farm_setup + .b_mock + .check_esdt_balance(&second_user, REWARD_TOKEN_ID, &rust_biguint!(4507)); + + // Check users positions after exit + first_user_total_position -= half_token_amount; + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + // random tx on end of week 3, to cummulate rewards + farm_setup.b_mock.set_block_nonce(30); + + farm_setup.set_user_energy(&first_user, 1_000, 20, 1); + farm_setup.set_user_energy(&second_user, 4_000, 20, 1); + farm_setup.set_user_energy(&third_user, 1, 20, 1); + farm_setup.enter_farm(&third_user, 1); + farm_setup.exit_farm(&third_user, 13, 1); + + // advance 1 week + farm_setup.b_mock.set_block_epoch(27); + farm_setup + .b_mock + .set_block_timestamp(28 * TIMESTAMP_PER_EPOCH - 1); + + farm_setup.set_user_energy(&first_user, 1_000, 27, 1); + farm_setup.set_user_energy(&second_user, 4_000, 27, 1); + + // First user claims rewards + let first_user_received_reward_amt = + farm_setup.claim_rewards(&first_user, 8, farm_token_amount); + assert_eq!(first_user_received_reward_amt, 6167); + + // Check users positions after first user claim + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); + + // Second user merges half from one of his original position with 2 position halves from the first user + let farm_tokens = vec![ + NonceAmountPair { + nonce: 4, + amount: half_token_amount, + }, + NonceAmountPair { + nonce: 5, + amount: half_token_amount, + }, + NonceAmountPair { + nonce: 11, + amount: half_token_amount, + }, + ]; + + farm_setup.b_mock.check_esdt_balance( + &second_user, + REWARD_TOKEN_ID, + &rust_biguint!(second_user_reward_balance), + ); + farm_setup.merge_farm_tokens(&second_user, farm_tokens); + second_user_reward_balance += 1510; // boosted rewards + farm_setup.b_mock.check_esdt_balance( + &second_user, + REWARD_TOKEN_ID, + &rust_biguint!(second_user_reward_balance), + ); + farm_setup.b_mock.check_nft_balance( + &second_user, + FARM_TOKEN_ID, + 15, + &rust_biguint!(half_token_amount * 3), + Some(&FarmTokenAttributes:: { + reward_per_share: managed_biguint!(35714286), + entering_epoch: 2, + compounded_reward: managed_biguint!(0), + current_farm_amount: managed_biguint!(half_token_amount * 3), + original_owner: managed_address!(&second_user), + }), + ); + + // Check users positions after merge + first_user_total_position -= 2 * half_token_amount; + second_user_total_position += 2 * half_token_amount; + farm_setup.check_user_total_farm_position(&first_user, first_user_total_position); + farm_setup.check_user_total_farm_position(&second_user, second_user_total_position); +} #[test] fn total_farm_position_through_simple_lock_test() {