-
Notifications
You must be signed in to change notification settings - Fork 46
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 first audit fixes #772
Merged
psorinionut
merged 28 commits into
farm-position-functionality
from
farm-position-fixes
Sep 29, 2023
Merged
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4b4e0b7
farm position first audit fixes
psorinionut c044821
metastaking refactor
psorinionut 16576f1
readded claim progress check
psorinionut 3fc935a
clippy fixes
psorinionut be142a3
dedicated migration function for old positions
psorinionut a77b760
remove caller check in migrate old position func
psorinionut 3fe300e
remove exit amount parameter from farms
psorinionut 37c9ebc
clippy fix
psorinionut 9859e47
tests fixes
psorinionut d9fd841
remove metastaking ProxyMergePosModule
psorinionut 65930ce
send rewards to user in claim_boosted_rewards
psorinionut ee6644c
claim boosted in merge endpoints
psorinionut 1e9ec92
clippy and test fix
psorinionut bbff637
audit fixes (2)
psorinionut c609e42
Merge pull request #774 from multiversx/claim-boosted-in-merge-endpoints
psorinionut 8ac9c19
Merge pull request #773 from multiversx/remove-exit-amount-parameter
psorinionut 4303c0c
default migration nonce fix
psorinionut 767f452
use GetCurrentESDTNFTNonce VM endpoint
psorinionut 8100d57
try_set_farm_position_migration_nonce: code dup
CostinCarabas aba2e4e
fix farm_position_migration_nonce set function
psorinionut 1d92432
Merge pull request #775 from multiversx/farm-position-audit-fixes-2
psorinionut 494b301
farm position functionality tests
psorinionut 772779a
allow_external_claim_rewards_setting extra check
psorinionut d790945
total_farm_position_claim_for_other extra check
psorinionut 0ee2b42
farm position tests updates
psorinionut 4262fa1
farm staking full position claim test
psorinionut a6e3e00
Merge pull request #776 from multiversx/farm-position-tests
sasurobert 36be963
clippy fix
psorinionut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ pub trait Farm: | |
+ farm::base_functions::BaseFunctionsModule | ||
+ farm::exit_penalty::ExitPenaltyModule | ||
+ farm::progress_update::ProgressUpdateModule | ||
+ farm::claim_boost_only::ClaimBoostOnlyModule | ||
+ farm_base_impl::base_farm_init::BaseFarmInitModule | ||
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule | ||
+ farm_base_impl::enter_farm::BaseEnterFarmModule | ||
|
@@ -89,27 +88,25 @@ pub trait Farm: | |
let caller = self.blockchain().get_caller(); | ||
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); | ||
|
||
let payments = self.get_non_empty_payments(); | ||
let first_additional_payment_index = 1; | ||
let boosted_rewards = match payments.try_get(first_additional_payment_index) { | ||
Some(p) => { | ||
let unlocked_rewards = self.claim_only_boosted_payment(&orig_caller, &p); | ||
self.send_to_lock_contract_non_zero( | ||
unlocked_rewards.token_identifier, | ||
unlocked_rewards.amount, | ||
caller.clone(), | ||
orig_caller.clone(), | ||
) | ||
} | ||
None => EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()), | ||
self.migrate_old_farm_positions(&orig_caller); | ||
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); | ||
let boosted_rewards_payment = if boosted_rewards > 0 { | ||
self.send_to_lock_contract_non_zero( | ||
self.reward_token_id().get(), | ||
boosted_rewards, | ||
caller.clone(), | ||
orig_caller.clone(), | ||
) | ||
} else { | ||
EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero()) | ||
}; | ||
|
||
let new_farm_token = self.enter_farm::<NoMintWrapper<Self>>(orig_caller.clone()); | ||
self.send_payment_non_zero(&caller, &new_farm_token); | ||
|
||
self.update_energy_and_progress(&orig_caller); | ||
|
||
(new_farm_token, boosted_rewards).into() | ||
(new_farm_token, boosted_rewards_payment).into() | ||
} | ||
|
||
#[payable("*")] | ||
|
@@ -121,6 +118,8 @@ pub trait Farm: | |
let caller = self.blockchain().get_caller(); | ||
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); | ||
|
||
self.migrate_old_farm_positions(&orig_caller); | ||
|
||
let payments = self.call_value().all_esdt_transfers().clone_value(); | ||
let base_claim_rewards_result = | ||
self.claim_rewards_base::<NoMintWrapper<Self>>(orig_caller.clone(), payments); | ||
|
@@ -163,7 +162,10 @@ pub trait Farm: | |
"Exit amount is bigger than the payment amount" | ||
); | ||
|
||
let boosted_rewards_full_position = self.claim_only_boosted_payment(&orig_caller, &payment); | ||
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call migrate_old_farm_position here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. claim rewards and make a new position for the whole payment. Do the same on farm-staking. |
||
let boosted_rewards_full_position = | ||
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards); | ||
|
||
let remaining_farm_payment = EsdtTokenPayment::new( | ||
payment.token_identifier.clone(), | ||
payment.token_nonce, | ||
|
@@ -186,7 +188,7 @@ pub trait Farm: | |
orig_caller.clone(), | ||
); | ||
|
||
self.clear_user_energy_if_needed(&orig_caller, &remaining_farm_payment.amount); | ||
self.clear_user_energy_if_needed(&orig_caller); | ||
|
||
( | ||
exit_farm_result.farming_tokens, | ||
|
@@ -196,27 +198,6 @@ pub trait Farm: | |
.into() | ||
} | ||
|
||
#[view(calculateRewardsForGivenPosition)] | ||
fn calculate_rewards_for_given_position( | ||
&self, | ||
user: ManagedAddress, | ||
farm_token_amount: BigUint, | ||
attributes: FarmTokenAttributes<Self::Api>, | ||
) -> BigUint { | ||
self.require_queried(); | ||
|
||
let mut storage_cache = StorageCache::new(self); | ||
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache); | ||
|
||
NoMintWrapper::<Self>::calculate_rewards( | ||
self, | ||
&user, | ||
&farm_token_amount, | ||
&attributes, | ||
&storage_cache, | ||
) | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(mergeFarmTokens)] | ||
fn merge_farm_tokens_endpoint( | ||
|
@@ -226,13 +207,41 @@ pub trait Farm: | |
let caller = self.blockchain().get_caller(); | ||
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); | ||
self.check_claim_progress_for_merge(&orig_caller); | ||
self.migrate_old_farm_positions(&orig_caller); | ||
|
||
let merged_farm_token = self.merge_farm_tokens::<NoMintWrapper<Self>>(); | ||
self.send_payment_non_zero(&caller, &merged_farm_token); | ||
|
||
merged_farm_token | ||
} | ||
|
||
#[endpoint(claimBoostedRewards)] | ||
fn claim_boosted_rewards( | ||
&self, | ||
opt_user: OptionalValue<ManagedAddress>, | ||
) -> EsdtTokenPayment<Self::Api> { | ||
let caller = self.blockchain().get_caller(); | ||
let user = match &opt_user { | ||
OptionalValue::Some(user) => user, | ||
OptionalValue::None => &caller, | ||
}; | ||
let user_total_farm_position = self.get_user_total_farm_position(user); | ||
if user != &caller { | ||
require!( | ||
user_total_farm_position.allow_external_claim_boosted_rewards, | ||
"Cannot claim rewards for this address" | ||
); | ||
} | ||
|
||
let boosted_rewards = self.claim_only_boosted_payment(user); | ||
self.send_to_lock_contract_non_zero( | ||
self.reward_token_id().get(), | ||
boosted_rewards, | ||
caller.clone(), | ||
user.clone(), | ||
) | ||
} | ||
|
||
#[endpoint(startProduceRewards)] | ||
fn start_produce_rewards_endpoint(&self) { | ||
self.require_caller_has_admin_permissions(); | ||
|
@@ -251,6 +260,27 @@ pub trait Farm: | |
self.set_per_block_rewards::<NoMintWrapper<Self>>(per_block_amount); | ||
} | ||
|
||
#[view(calculateRewardsForGivenPosition)] | ||
fn calculate_rewards_for_given_position( | ||
&self, | ||
user: ManagedAddress, | ||
farm_token_amount: BigUint, | ||
attributes: FarmTokenAttributes<Self::Api>, | ||
) -> BigUint { | ||
self.require_queried(); | ||
|
||
let mut storage_cache = StorageCache::new(self); | ||
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache); | ||
|
||
NoMintWrapper::<Self>::calculate_rewards( | ||
self, | ||
&user, | ||
&farm_token_amount, | ||
&attributes, | ||
&storage_cache, | ||
) | ||
} | ||
|
||
fn send_to_lock_contract_non_zero( | ||
&self, | ||
token_id: TokenIdentifier, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need a new bool here: migrated. And to keep that even if total_farm_position gets to 0 again. Or to not delete claimProgressWeek from user.