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 supply for week fix #938

Merged
merged 15 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ pub trait Farm:
);
}

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

let boosted_rewards = self.claim_only_boosted_payment(user);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to do this every single time ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented above. Also, this is how it already works for the other endpoints (enter, exit, claim).


self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
Expand Down
5 changes: 5 additions & 0 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ pub trait Farm:
);
}

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

let boosted_rewards = self.claim_only_boosted_payment(user);
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to do this every single time ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also update all the claim endpoints to pass through an if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do this on claim, only when the storage is empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about adding it but I don't think it will bring any real improvement. An if statement would require a few extra storage reads (we would need to compute the current week), so in the end gas won't be saved, but at the same time it would add some extra logic with the if statement, which in turn would complicate the review/audit/testing parts.


self.send_payment_non_zero(user, &boosted_rewards_payment);

boosted_rewards_payment
Expand Down
58 changes: 58 additions & 0 deletions dex/farm/tests/total_farm_position_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,61 @@ fn total_farm_position_through_simple_lock_test() {
&rust_biguint!(first_received_reward_amt),
);
}

#[test]
fn claim_only_boosted_rewards_per_week_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);

let temp_user = farm_setup.third_user.clone();

// first user enter farm
let farm_in_amount = 100_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.check_farm_token_supply(farm_in_amount);

farm_setup.b_mock.set_block_nonce(10);
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, 2, 1);

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

let boosted_rewards = 2_500;
let second_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user);

assert_eq!(second_week_received_reward_amt, boosted_rewards);

// advance 1 week
farm_setup.set_user_energy(&first_user, 1_000, 15, 1);
farm_setup.b_mock.set_block_nonce(30);
farm_setup.b_mock.set_block_epoch(15);
let third_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user);

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

farm_setup.b_mock.check_esdt_balance(
&first_user,
REWARD_TOKEN_ID,
&rust_biguint!(boosted_rewards * 2),
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use contexts::storage_cache::StorageCache;
use farm_base_impl::base_traits_impl::FarmContract;

use crate::base_impl_wrapper::FarmStakingWrapper;

multiversx_sc::imports!();
Expand Down Expand Up @@ -38,10 +41,15 @@ pub trait ClaimOnlyBoostedStakingRewardsModule:
);
}

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

let boosted_rewards = self.claim_only_boosted_payment(user);
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

self.send_payment_non_zero(user, &boosted_rewards_payment);

boosted_rewards_payment
Expand Down
4 changes: 2 additions & 2 deletions farm-staking/farm-staking/src/claim_stake_farm_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ pub trait ClaimStakeFarmRewardsModule:

virtual_farm_token.payment.amount = new_amount.clone();
virtual_farm_token.attributes.current_farm_amount = new_amount;

self.set_farm_supply_for_current_week(&claim_result.storage_cache.farm_token_supply);
}

self.set_farm_supply_for_current_week(&claim_result.storage_cache.farm_token_supply);

self.update_energy_and_progress(&original_caller);

let new_farm_token_nonce = self.send().esdt_nft_create_compact(
Expand Down
Loading