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 4 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
31 changes: 4 additions & 27 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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},
Expand Down Expand Up @@ -150,36 +149,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 payment = self.call_value().single_esdt();

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 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,12 +172,7 @@ 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("*")]
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
36 changes: 5 additions & 31 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,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 Down Expand Up @@ -156,47 +154,23 @@ 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 payment = self.call_value().single_esdt();

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("*")]
Expand Down
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
5 changes: 2 additions & 3 deletions dex/farm/tests/farm_setup/farm_rewards_distr_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ where
farm_token_nonce,
&farm_out_amount.clone(),
|sc| {
let exit_amount = to_managed_biguint(farm_out_amount);
let multi_result = sc.exit_farm_endpoint(exit_amount, OptionalValue::None);
let multi_result = sc.exit_farm_endpoint(OptionalValue::None);

let (first_result, second_result, _third_result) = multi_result.into_tuple();
let (first_result, second_result) = multi_result.into_tuple();

assert_eq!(
first_result.token_identifier,
Expand Down
23 changes: 5 additions & 18 deletions dex/farm/tests/farm_setup/multi_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,25 +513,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::None,
);
let _ = sc.exit_farm_endpoint(OptionalValue::None);
},
)
.assert_ok();
Expand All @@ -541,7 +532,6 @@ where
&mut self,
user: &Address,
farm_token_nonce: u64,
farm_token_amount: u64,
exit_farm_amount: u64,
known_proxy: &Address,
) {
Expand All @@ -551,12 +541,9 @@ where
&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
9 changes: 2 additions & 7 deletions dex/farm/tests/farm_setup/single_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,9 @@ where
farm_token_nonce,
&rust_biguint!(farm_token_amount),
|sc| {
let multi_result = sc.exit_farm_endpoint(
managed_biguint!(farm_token_amount),
OptionalValue::None,
);
let multi_result = sc.exit_farm_endpoint(OptionalValue::None);

let (first_result, second_result, remaining_farm_amount) =
multi_result.into_tuple();
let (first_result, second_result) = multi_result.into_tuple();

assert_eq!(
first_result.token_identifier,
Expand All @@ -246,7 +242,6 @@ where
);
assert_eq!(second_result.token_nonce, 0);
assert_eq!(second_result.amount, managed_biguint!(expected_mex_out));
assert_eq!(remaining_farm_amount.amount, managed_biguint!(0));
},
)
.assert_ok();
Expand Down
4 changes: 2 additions & 2 deletions dex/farm/tests/farm_single_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ fn test_farm_through_simple_lock() {
2,
&rust_biguint!(1_000_000_000),
|sc| {
let exit_farm_result = sc.exit_farm_locked_token(managed_biguint!(1_000_000_000));
let exit_farm_result = sc.exit_farm_locked_token();
let (locked_tokens, reward_tokens) = exit_farm_result.into_tuple();

assert_eq!(
Expand Down Expand Up @@ -594,7 +594,7 @@ fn test_farm_through_simple_lock() {
7,
&rust_biguint!(1_000_000_000),
|sc| {
let exit_farm_result = sc.exit_farm_locked_token(managed_biguint!(1_000_000_000));
let exit_farm_result = sc.exit_farm_locked_token();
let (locked_tokens, _reward_tokens) = exit_farm_result.into_tuple();

assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions dex/fuzz/src/fuzz_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod fuzz_farm_test {
use std::cmp::Ordering;

use multiversx_sc_scenario::whitebox_legacy::TxTokenTransfer;
use multiversx_sc_scenario::{managed_biguint, rust_biguint, DebugApi};
use multiversx_sc_scenario::{rust_biguint, DebugApi};

use crate::fuzz_data::fuzz_data_tests::*;
use farm::*;
Expand Down Expand Up @@ -179,7 +179,7 @@ pub mod fuzz_farm_test {
farm_token_nonce,
&farm_out_amount,
|sc| {
sc.exit_farm_endpoint(managed_biguint!(seed), OptionalValue::None);
sc.exit_farm_endpoint(OptionalValue::None);
},
);

Expand Down
Loading