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

claimBoostedRewards extra check #951

Merged
merged 4 commits into from
Oct 2, 2024
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
5 changes: 5 additions & 0 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ pub trait Farm:
);
}

require!(
!self.user_total_farm_position(user).is_empty(),
"User total farm position is empty!"
);

let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![allow(deprecated)]

use common_structs::FarmTokenAttributes;
use multiversx_sc::codec::Empty;
use farm_with_locked_rewards::Farm;
use multiversx_sc::{codec::Empty, imports::OptionalValue};
use multiversx_sc_scenario::{managed_address, managed_biguint, rust_biguint, DebugApi};
use simple_lock::locked_token::LockedTokenAttributes;

Expand Down Expand Up @@ -503,11 +504,19 @@ fn claim_boosted_rewards_with_zero_position_test() {
farm_setup.b_mock.set_block_nonce(20);
farm_setup.b_mock.set_block_epoch(13);

let second_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&temp_user, &temp_user, 0);
farm_setup
.b_mock
.execute_tx(
&temp_user,
&farm_setup.farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.claim_boosted_rewards(OptionalValue::Some(managed_address!(&temp_user)));
},
)
.assert_error(4, "User total farm position is empty!");

assert_eq!(second_week_received_reward_amt, 0);
farm_setup.check_farm_rps(150_000_000u64);
farm_setup.check_farm_rps(75_000_000u64);

// advance 1 week
let boosted_rewards = 2_500;
Expand All @@ -517,14 +526,52 @@ fn claim_boosted_rewards_with_zero_position_test() {
let third_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user, 1);

assert_eq!(third_week_received_reward_amt, boosted_rewards * 2); // user receives rewards for weeks 1 and 2)
assert_eq!(third_week_received_reward_amt, boosted_rewards);
farm_setup.check_farm_rps(225_000_000u64);

farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(boosted_rewards * 2),
&rust_biguint!(boosted_rewards),
None,
);
}

#[test]
fn claim_boosted_rewards_user_energy_not_registered_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 first_user = farm_setup.first_user.clone();
let farm_in_amount = 100_000_000;

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

// Attempt to claim boosted rewards without entering the farm
farm_setup
.b_mock
.execute_tx(
&first_user,
&farm_setup.farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.claim_boosted_rewards(OptionalValue::Some(managed_address!(&first_user)));
},
)
.assert_error(4, "User total farm position is empty!");

// User enters the farm
farm_setup.enter_farm(&first_user, farm_in_amount);

// Now the user should be able to claim boosted rewards
// Rewards computation is out of scope
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user, 0);
}
5 changes: 5 additions & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ pub trait Farm:
);
}

require!(
!self.user_total_farm_position(user).is_empty(),
"User total farm position is empty!"
);

let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);
Wrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub trait ClaimOnlyBoostedStakingRewardsModule:
);
}

require!(
!self.user_total_farm_position(user).is_empty(),
"User total farm position is empty!"
);

let mut storage_cache = StorageCache::new(self);
FarmStakingWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

Expand Down
23 changes: 17 additions & 6 deletions farm-staking/farm-staking/tests/farm_staking_energy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod farm_staking_setup;
use config::ConfigModule;
use farm_staking::{
claim_only_boosted_staking_rewards::ClaimOnlyBoostedStakingRewardsModule,
claim_stake_farm_rewards::ClaimStakeFarmRewardsModule,
stake_farm::StakeFarmModule,
token_attributes::{StakingFarmTokenAttributes, UnbondSftAttributes},
Expand Down Expand Up @@ -1667,9 +1668,19 @@ fn claim_boosted_rewards_with_zero_position_test() {
fs_setup.b_mock.set_block_epoch(13);

let boosted_rewards_for_week = 100;
fs_setup.claim_boosted_rewards_for_user(&second_user, &second_user, 0, &rust_biguint!(0));

current_farm_rps += farm_rps_increase;
fs_setup
.b_mock
.execute_tx(
&second_user,
&fs_setup.farm_wrapper,
&rust_biguint!(0u64),
|sc| {
sc.claim_boosted_rewards(OptionalValue::Some(managed_address!(&second_user)));
},
)
.assert_error(4, "User total farm position is empty!");

fs_setup.check_farm_rps(current_farm_rps);

// advance 1 week
Expand All @@ -1679,16 +1690,16 @@ fn claim_boosted_rewards_with_zero_position_test() {
fs_setup.claim_boosted_rewards_for_user(
&first_user,
&first_user,
boosted_rewards_for_week * 2,
&rust_biguint!(boosted_rewards_for_week * 2),
boosted_rewards_for_week,
&rust_biguint!(boosted_rewards_for_week),
);

current_farm_rps += farm_rps_increase;
current_farm_rps += farm_rps_increase * 2;
fs_setup.check_farm_rps(current_farm_rps);
fs_setup.b_mock.check_esdt_balance(
&first_user,
REWARD_TOKEN_ID,
&rust_biguint!(boosted_rewards_for_week * 2),
&rust_biguint!(boosted_rewards_for_week),
);

let expected_attributes = StakingFarmTokenAttributes::<DebugApi> {
Expand Down
Loading