Skip to content

Commit

Permalink
Merge pull request #888 from multiversx/farm-staking-tests-with-merge…
Browse files Browse the repository at this point in the history
…-fix
  • Loading branch information
psorinionut authored Apr 17, 2024
2 parents a97b9c3 + 4544a92 commit 27085fb
Show file tree
Hide file tree
Showing 7 changed files with 903 additions and 79 deletions.
17 changes: 10 additions & 7 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,7 @@ pub trait Farm:
self.migrate_old_farm_positions(&orig_caller);
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);

let mut output_attributes = self.merge_and_return_attributes::<NoMintWrapper<Self>>();
output_attributes.original_owner = orig_caller.clone();

let new_token_amount = output_attributes.get_total_supply();
let merged_farm_token = self
.farm_token()
.nft_create(new_token_amount, &output_attributes);
let merged_farm_token = self.merge_and_update_farm_tokens(orig_caller.clone());

self.send_payment_non_zero(&caller, &merged_farm_token);
let locked_rewards_payment = self.send_to_lock_contract_non_zero(
Expand All @@ -218,6 +212,15 @@ pub trait Farm:
(merged_farm_token, locked_rewards_payment).into()
}

fn merge_and_update_farm_tokens(&self, orig_caller: ManagedAddress) -> EsdtTokenPayment {
let mut output_attributes = self.merge_and_return_attributes::<NoMintWrapper<Self>>();
output_attributes.original_owner = orig_caller;

let new_token_amount = output_attributes.get_total_supply();
self.farm_token()
.nft_create(new_token_amount, &output_attributes)
}

#[endpoint(claimBoostedRewards)]
fn claim_boosted_rewards(
&self,
Expand Down
17 changes: 10 additions & 7 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,23 @@ pub trait Farm:
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

let mut output_attributes = self.merge_and_return_attributes::<Wrapper<Self>>();
output_attributes.original_owner = orig_caller;

let new_token_amount = output_attributes.get_total_supply();
let merged_farm_token = self
.farm_token()
.nft_create(new_token_amount, &output_attributes);
let merged_farm_token = self.merge_and_update_farm_tokens(orig_caller);

self.send_payment_non_zero(&caller, &merged_farm_token);
self.send_payment_non_zero(&caller, &boosted_rewards_payment);

(merged_farm_token, boosted_rewards_payment).into()
}

fn merge_and_update_farm_tokens(&self, orig_caller: ManagedAddress) -> EsdtTokenPayment {
let mut output_attributes = self.merge_and_return_attributes::<Wrapper<Self>>();
output_attributes.original_owner = orig_caller;

let new_token_amount = output_attributes.get_total_supply();
self.farm_token()
.nft_create(new_token_amount, &output_attributes)
}

#[endpoint(claimBoostedRewards)]
fn claim_boosted_rewards(
&self,
Expand Down
27 changes: 21 additions & 6 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,34 @@ pub trait FarmStaking:
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

let payments = self.get_non_empty_payments();
let token_mapper = self.farm_token();
let output_attributes: StakingFarmTokenAttributes<Self::Api> =
self.merge_from_payments_and_burn(payments, &token_mapper);
let new_token_amount = output_attributes.get_total_supply();
let merged_farm_token = self.merge_and_update_farm_tokens(caller.clone());

let merged_farm_token = token_mapper.nft_create(new_token_amount, &output_attributes);
self.send_payment_non_zero(&caller, &merged_farm_token);
self.send_payment_non_zero(&caller, &boosted_rewards_payment);

(merged_farm_token, boosted_rewards_payment).into()
}

fn merge_and_update_farm_tokens(&self, orig_caller: ManagedAddress) -> EsdtTokenPayment {
let mut output_attributes = self.merge_farm_tokens::<FarmStakingWrapper<Self>>();
output_attributes.original_owner = orig_caller;

let new_token_amount = output_attributes.get_total_supply();
self.farm_token()
.nft_create(new_token_amount, &output_attributes)
}

fn merge_farm_tokens<FC: FarmContract<FarmSc = Self>>(&self) -> FC::AttributesType {
let payments = self.get_non_empty_payments();
let token_mapper = self.farm_token();
token_mapper.require_all_same_token(&payments);

let caller = self.blockchain().get_caller();
FC::check_and_update_user_farm_position(self, &caller, &payments);

self.merge_from_payments_and_burn(payments, &token_mapper)
}

#[endpoint(setBoostedYieldsRewardsPercentage)]
fn set_boosted_yields_rewards_percentage(&self, percentage: u64) {
self.require_caller_has_admin_permissions();
Expand Down
4 changes: 2 additions & 2 deletions farm-staking/farm-staking/src/token_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ impl<M: ManagedTypeApi> FixedSupplyToken<M> for StakingFarmTokenAttributes<M> {

impl<M: ManagedTypeApi> Mergeable<M> for StakingFarmTokenAttributes<M> {
#[inline]
fn can_merge_with(&self, other: &Self) -> bool {
self.original_owner == other.original_owner
fn can_merge_with(&self, _other: &Self) -> bool {
true
}

fn merge_with(&mut self, other: Self) {
Expand Down
Loading

0 comments on commit 27085fb

Please sign in to comment.