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

remove exit amount parameter from farms #773

Merged
merged 8 commits into from
Sep 20, 2023
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
1 change: 0 additions & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ main, feat/* ]
pull_request:
branches: [ main, feat/* ]
workflow_dispatch:

permissions:
Expand Down
2 changes: 1 addition & 1 deletion common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio

fn is_old_farm_position(&self, token_nonce: Nonce) -> bool {
let farm_position_migration_block_nonce = self.farm_position_migration_block_nonce().get();
token_nonce < farm_position_migration_block_nonce
token_nonce > 0 && token_nonce < farm_position_migration_block_nonce
}

#[endpoint(allowExternalClaimBoostedRewards)]
Expand Down
4 changes: 2 additions & 2 deletions common/modules/farm/farm_base_impl/src/exit_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ pub trait BaseExitFarmModule:
self.blockchain(),
);

FC::decrease_user_farm_position(self, &payment);

FC::generate_aggregated_rewards(self, &mut storage_cache);

let farm_token_amount = &exit_farm_context.farm_token.payment.amount;
Expand All @@ -64,6 +62,8 @@ pub trait BaseExitFarmModule:
);
storage_cache.reward_reserve -= &reward;

FC::decrease_user_farm_position(self, &payment);

let farming_token_amount = token_attributes.get_total_supply();
let farming_token_payment = EsdtTokenPayment::new(
storage_cache.farming_token_id.clone(),
Expand Down
50 changes: 17 additions & 33 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ multiversx_sc::derive_imports!();
use common_structs::FarmTokenAttributes;
use contexts::storage_cache::StorageCache;
use core::marker::PhantomData;
use mergeable::Mergeable;

use farm::{
base_functions::{BaseFunctionsModule, ClaimRewardsResultType, Wrapper},
base_functions::{BaseFunctionsModule, ClaimRewardsResultType, DoubleMultiPayment, Wrapper},
exit_penalty::{
DEFAULT_BURN_GAS_LIMIT, DEFAULT_MINUMUM_FARMING_EPOCHS, DEFAULT_PENALTY_PERCENT,
},
Expand All @@ -34,7 +33,6 @@ pub trait Farm:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ farm::base_functions::BaseFunctionsModule
+ farm::exit_penalty::ExitPenaltyModule
+ farm::progress_update::ProgressUpdateModule
+ farm_base_impl::base_farm_init::BaseFarmInitModule
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule
+ farm_base_impl::enter_farm::BaseEnterFarmModule
Expand Down Expand Up @@ -150,36 +148,19 @@ pub trait Farm:
#[endpoint(exitFarm)]
fn exit_farm_endpoint(
&self,
exit_amount: BigUint,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> ExitFarmWithPartialPosResultType<Self::Api> {
let caller = self.blockchain().get_caller();
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

let mut payment = self.call_value().single_esdt();
require!(
payment.amount >= exit_amount,
"Exit amount is bigger than the payment amount"
);

let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
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,
&payment.amount - &exit_amount,
);
let payment = self.call_value().single_esdt();

payment.amount = exit_amount;
self.migrate_old_farm_positions(&orig_caller);

let exit_farm_result = self.exit_farm::<NoMintWrapper<Self>>(orig_caller.clone(), payment);
let mut rewards = exit_farm_result.rewards;
rewards.merge_with(boosted_rewards_full_position);
let rewards = exit_farm_result.rewards;

self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens);
self.send_payment_non_zero(&caller, &remaining_farm_payment);

let locked_rewards_payment = self.send_to_lock_contract_non_zero(
rewards.token_identifier.clone(),
Expand All @@ -190,29 +171,32 @@ pub trait Farm:

self.clear_user_energy_if_needed(&orig_caller);

(
exit_farm_result.farming_tokens,
locked_rewards_payment,
remaining_farm_payment,
)
.into()
(exit_farm_result.farming_tokens, locked_rewards_payment).into()
}

#[payable("*")]
#[endpoint(mergeFarmTokens)]
fn merge_farm_tokens_endpoint(
&self,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> EsdtTokenPayment<Self::Api> {
) -> DoubleMultiPayment<Self::Api> {
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 boosted_rewards = self.claim_only_boosted_payment(&orig_caller);

let merged_farm_token = self.merge_farm_tokens::<NoMintWrapper<Self>>();

self.send_payment_non_zero(&caller, &merged_farm_token);
let locked_rewards_payment = self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
caller,
orig_caller,
);

merged_farm_token
(merged_farm_token, locked_rewards_payment).into()
}

#[endpoint(claimBoostedRewards)]
Expand All @@ -237,7 +221,7 @@ pub trait Farm:
self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
caller.clone(),
user.clone(),
user.clone(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,16 @@ where
result
}

pub fn exit_farm(
&mut self,
user: &Address,
farm_token_nonce: u64,
farm_token_amount: u64,
exit_farm_amount: u64,
) {
pub fn exit_farm(&mut self, user: &Address, farm_token_nonce: u64, exit_farm_amount: u64) {
self.b_mock
.execute_esdt_transfer(
user,
&self.farm_wrapper,
FARM_TOKEN_ID,
farm_token_nonce,
&rust_biguint!(farm_token_amount),
&rust_biguint!(exit_farm_amount),
|sc| {
let _ = sc.exit_farm_endpoint(
managed_biguint!(exit_farm_amount),
OptionalValue::Some(managed_address!(user)),
);
let _ = sc.exit_farm_endpoint(OptionalValue::Some(managed_address!(user)));
},
)
.assert_ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn farm_with_boosted_yields_no_proxy_test() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down
51 changes: 15 additions & 36 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ multiversx_sc::derive_imports!();

pub mod base_functions;
pub mod exit_penalty;
pub mod progress_update;

use base_functions::{ClaimRewardsResultType, DoubleMultiPayment, Wrapper};
use common_structs::FarmTokenAttributes;
Expand All @@ -17,11 +16,9 @@ use exit_penalty::{
DEFAULT_BURN_GAS_LIMIT, DEFAULT_MINUMUM_FARMING_EPOCHS, DEFAULT_PENALTY_PERCENT,
};
use farm_base_impl::base_traits_impl::FarmContract;
use mergeable::Mergeable;

pub type EnterFarmResultType<M> = DoubleMultiPayment<M>;
pub type ExitFarmWithPartialPosResultType<M> =
MultiValue3<EsdtTokenPayment<M>, EsdtTokenPayment<M>, EsdtTokenPayment<M>>;
pub type ExitFarmWithPartialPosResultType<M> = DoubleMultiPayment<M>;

#[multiversx_sc::contract]
pub trait Farm:
Expand All @@ -36,7 +33,6 @@ pub trait Farm:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ base_functions::BaseFunctionsModule
+ exit_penalty::ExitPenaltyModule
+ progress_update::ProgressUpdateModule
+ farm_base_impl::base_farm_init::BaseFarmInitModule
+ farm_base_impl::base_farm_validation::BaseFarmValidationModule
+ farm_base_impl::enter_farm::BaseEnterFarmModule
Expand Down Expand Up @@ -156,64 +152,47 @@ pub trait Farm:
#[endpoint(exitFarm)]
fn exit_farm_endpoint(
&self,
exit_amount: BigUint,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> ExitFarmWithPartialPosResultType<Self::Api> {
let caller = self.blockchain().get_caller();
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

let mut payment = self.call_value().single_esdt();
require!(
payment.amount >= exit_amount,
"Exit amount is bigger than the payment amount"
);
let payment = self.call_value().single_esdt();

let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
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,
&payment.amount - &exit_amount,
);

payment.amount = exit_amount;
self.migrate_old_farm_positions(&orig_caller);

let mut exit_farm_result = self.exit_farm::<Wrapper<Self>>(orig_caller.clone(), payment);
exit_farm_result
.rewards
.merge_with(boosted_rewards_full_position);
let exit_farm_result = self.exit_farm::<Wrapper<Self>>(orig_caller.clone(), payment);

self.send_payment_non_zero(&caller, &exit_farm_result.farming_tokens);
self.send_payment_non_zero(&caller, &exit_farm_result.rewards);
self.send_payment_non_zero(&caller, &remaining_farm_payment);

self.clear_user_energy_if_needed(&orig_caller);

(
exit_farm_result.farming_tokens,
exit_farm_result.rewards,
remaining_farm_payment,
)
.into()
(exit_farm_result.farming_tokens, exit_farm_result.rewards).into()
}

#[payable("*")]
#[endpoint(mergeFarmTokens)]
fn merge_farm_tokens_endpoint(
&self,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> EsdtTokenPayment<Self::Api> {
) -> DoubleMultiPayment<Self::Api> {
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 boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
let boosted_rewards_payment = if boosted_rewards > 0 {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards)
} else {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero())
};

let merged_farm_token = self.merge_farm_tokens::<Wrapper<Self>>();
self.send_payment_non_zero(&caller, &merged_farm_token);
self.send_payment_non_zero(&caller, &boosted_rewards_payment);

merged_farm_token
(merged_farm_token, boosted_rewards_payment).into()
}

#[endpoint(claimBoostedRewards)]
Expand Down
27 changes: 0 additions & 27 deletions dex/farm/src/progress_update.rs

This file was deleted.

16 changes: 8 additions & 8 deletions dex/farm/tests/farm_multi_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn farm_with_boosted_yields_test() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down Expand Up @@ -262,7 +262,7 @@ fn farm_claim_boosted_yields_for_other_user_test() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down Expand Up @@ -434,7 +434,7 @@ fn farm_boosted_yields_claim_with_different_user_pos_test() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down Expand Up @@ -625,7 +625,7 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down Expand Up @@ -706,7 +706,7 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() {
farm_setup.set_user_energy(&second_user, 4_000, 13, 1);
farm_setup.set_user_energy(&third_user, 1, 13, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 8, 1, 1);
farm_setup.exit_farm(&third_user, 8, 1);

// advance blocks - 10 blocks - 10 * 1_000 = 10_000 total rewards
// 7_500 base farm, 2_500 boosted yields
Expand All @@ -718,7 +718,7 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() {
farm_setup.set_user_energy(&second_user, 4_000, 20, 1);
farm_setup.set_user_energy(&third_user, 1, 20, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 9, 1, 1);
farm_setup.exit_farm(&third_user, 9, 1);

// advance week
farm_setup.b_mock.set_block_epoch(22);
Expand Down Expand Up @@ -875,7 +875,7 @@ fn farm_enter_with_multiple_farm_token() {
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down Expand Up @@ -990,7 +990,7 @@ fn farm_claim_with_minimum_tokens() {
farm_setup.set_user_energy(&second_user, 90_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
farm_setup.enter_farm(&third_user, 1);
farm_setup.exit_farm(&third_user, 5, 1, 1);
farm_setup.exit_farm(&third_user, 5, 1);

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
Expand Down
Loading
Loading