From b147571ad81bca791d612fd6247af4c54edb1640 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Fri, 2 Aug 2024 18:01:55 +0300 Subject: [PATCH] farm staking unbond fix + unit test --- farm-staking/farm-staking/src/unbond_farm.rs | 2 + .../tests/farm_staking_energy_test.rs | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/farm-staking/farm-staking/src/unbond_farm.rs b/farm-staking/farm-staking/src/unbond_farm.rs index ec2044897..de296e4c3 100644 --- a/farm-staking/farm-staking/src/unbond_farm.rs +++ b/farm-staking/farm-staking/src/unbond_farm.rs @@ -49,6 +49,8 @@ pub trait UnbondFarmModule: "Unbond period not over" ); + let current_week = self.get_current_week(); + self.perform_weekly_update(current_week); FarmStakingWrapper::::generate_aggregated_rewards(self, &mut storage_cache); self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply); diff --git a/farm-staking/farm-staking/tests/farm_staking_energy_test.rs b/farm-staking/farm-staking/tests/farm_staking_energy_test.rs index e71f0db34..dee471634 100644 --- a/farm-staking/farm-staking/tests/farm_staking_energy_test.rs +++ b/farm-staking/farm-staking/tests/farm_staking_energy_test.rs @@ -1730,3 +1730,95 @@ fn merge_farm_position_per_week_test() { Some(&expected_attributes), ); } + +#[test] +fn unbond_farm_position_per_week_test() { + DebugApi::dummy(); + let mut fs_setup = + FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj); + + fs_setup.set_boosted_yields_factors(); + fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE); + + let first_user = fs_setup.user_address.clone(); + let farm_in_amount = 100_000_000; + + fs_setup.set_user_energy(&first_user, 10_000, 0, 10); + fs_setup.stake_farm(&first_user, farm_in_amount, &[], 1, 0, 0); + fs_setup.check_farm_token_supply(farm_in_amount); + + fs_setup.unstake_farm_no_checks(&first_user, farm_in_amount / 2, 1); + fs_setup.check_farm_token_supply(farm_in_amount / 2); + + fs_setup.check_farm_rps(0u64); + + fs_setup.b_mock.set_block_nonce(100); + fs_setup.b_mock.set_block_epoch(6); + fs_setup.set_user_energy(&first_user, 1_000, 6, 1); + + // Reset user balance + fs_setup + .b_mock + .set_esdt_balance(&first_user, FARMING_TOKEN_ID, &rust_biguint!(0)); + + let mut current_farm_rps = 0; + fs_setup.check_farm_rps(current_farm_rps); + + // advance 1 week + fs_setup.set_user_energy(&first_user, 1_000, 13, 1); + fs_setup.b_mock.set_block_nonce(200); + fs_setup.b_mock.set_block_epoch(13); + + fs_setup.unbond_farm( + 2, + farm_in_amount / 2, + farm_in_amount / 2, + farm_in_amount / 2, + ); + + let mut user_balance = farm_in_amount / 2; + fs_setup + .b_mock + .check_esdt_balance(&first_user, REWARD_TOKEN_ID, &rust_biguint!(user_balance)); + + let farm_rps_increase = 6_000_000u64; + current_farm_rps += farm_rps_increase; + fs_setup.check_farm_rps(current_farm_rps); + + // advance 1 week + fs_setup.set_user_energy(&first_user, 1_000, 15, 1); + fs_setup.b_mock.set_block_nonce(300); + fs_setup.b_mock.set_block_epoch(15); + + let boosted_rewards_for_week = 100; + user_balance += boosted_rewards_for_week; + fs_setup.claim_boosted_rewards_for_user( + &first_user, + &first_user, + boosted_rewards_for_week, + &rust_biguint!(user_balance), + ); + + // User should have the following balance: unstake_amount + boosted_rewards_week_2 + fs_setup + .b_mock + .check_esdt_balance(&first_user, REWARD_TOKEN_ID, &rust_biguint!(user_balance)); + + current_farm_rps += farm_rps_increase / 2; //user existed with half position + fs_setup.check_farm_rps(current_farm_rps); + + let expected_attributes = StakingFarmTokenAttributes:: { + reward_per_share: managed_biguint!(0), + compounded_reward: managed_biguint!(0), + current_farm_amount: managed_biguint!(farm_in_amount), + original_owner: managed_address!(&first_user), + }; + + fs_setup.b_mock.check_nft_balance( + &first_user, + FARM_TOKEN_ID, + 1, + &rust_biguint!(farm_in_amount / 2), + Some(&expected_attributes), + ); +}