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

Rc/v3.3 #980

Draft
wants to merge 33 commits into
base: farm-on-behalf-features
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
af382ad
remove penalty code
dorin-iancu Oct 22, 2024
64b63c2
timestamp sc
dorin-iancu Oct 22, 2024
9c57bbf
temp commit
dorin-iancu Oct 22, 2024
923c7a2
fix build errors
dorin-iancu Oct 24, 2024
25a650f
code updates + remove useless crate
dorin-iancu Oct 24, 2024
561268b
finish impl
dorin-iancu Oct 24, 2024
7bcbc0e
some more fixes
dorin-iancu Oct 24, 2024
98c53cf
some impl fixes
dorin-iancu Oct 25, 2024
ae1b782
remove unneeded annotation
dorin-iancu Oct 25, 2024
d2324ad
impl fixes + some test fixes
dorin-iancu Oct 28, 2024
3960df8
evil clippy
dorin-iancu Oct 28, 2024
9e13688
fix
dorin-iancu Oct 29, 2024
655ad95
fix test
dorin-iancu Oct 29, 2024
a9d7a4a
fix other 2 tests
dorin-iancu Oct 29, 2024
89abd34
fix base farm staking tests
dorin-iancu Oct 29, 2024
3636f8a
even more fixes
dorin-iancu Oct 30, 2024
a9b52d7
fix farm staking tests
dorin-iancu Oct 30, 2024
27a7b86
change impl... again
dorin-iancu Oct 31, 2024
6fdcde1
fix farm tests
dorin-iancu Oct 31, 2024
1b91fba
comment test
dorin-iancu Oct 31, 2024
16138a5
fix farm staking tests
dorin-iancu Oct 31, 2024
fc7bb6e
fix proxy dex tests
dorin-iancu Oct 31, 2024
1fc5047
test
dorin-iancu Oct 31, 2024
cfe0a51
some optimizations
dorin-iancu Nov 8, 2024
c90362f
merge with new base
dorin-iancu Nov 8, 2024
a26137a
cleanup
dorin-iancu Nov 8, 2024
76f1a4d
move test to separate file
dorin-iancu Nov 27, 2024
3951ff2
Merge pull request #966 from multiversx/remove-farm-exit-fee
dorin-iancu Dec 2, 2024
7a23523
Merge branch 'rc/v3.3' into move-farm-pos-code
dorin-iancu Dec 2, 2024
ec0d542
merge with base
dorin-iancu Dec 2, 2024
a2202a4
Merge pull request #968 from multiversx/move-farm-pos-code
dorin-iancu Dec 2, 2024
20bf814
merge again
dorin-iancu Dec 2, 2024
22eab17
Merge pull request #982 from multiversx/merge-rc-v3.3-with-farm-on-be…
dorin-iancu Dec 2, 2024
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
47 changes: 32 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ members = [
"energy-integration/fees-collector/meta",
"energy-integration/governance-v2",
"energy-integration/governance-v2/meta",
"energy-integration/timestamp-oracle",
"energy-integration/timestamp-oracle/meta",

"farm-staking/farm-staking",
"farm-staking/farm-staking/meta",
Expand Down
1 change: 1 addition & 0 deletions common/common_structs/src/alias_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type Nonce = u64;
pub type Epoch = u64;
pub type Week = usize;
pub type Percent = u64;
pub type Timestamp = u64;
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type UnlockPeriod<M> = UnlockSchedule<M>;
pub type OldLockedTokenAttributes<M> = LockedAssetTokenAttributesEx<M>;
Expand Down
57 changes: 56 additions & 1 deletion common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::Nonce;
use common_structs::{FarmToken, Nonce, PaymentsVec};
use pausable::State;

pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10;
Expand Down Expand Up @@ -44,6 +44,61 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
.set(migration_farm_token_nonce);
}

fn check_and_update_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
&self,
user: &ManagedAddress,
farm_positions: &PaymentsVec<Self::Api>,
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
) {
for farm_position in farm_positions {
farm_token_mapper.require_same_token(&farm_position.token_identifier);

if self.is_old_farm_position(farm_position.token_nonce) {
continue;
}

let token_attributes: T =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

if &token_attributes.get_original_owner() != user {
self.decrease_user_farm_position::<T>(&farm_position, farm_token_mapper);
self.increase_user_farm_position(user, &farm_position.amount);
}
}
}

#[inline]
fn increase_user_farm_position(
&self,
user: &ManagedAddress,
increase_farm_position_amount: &BigUint,
) {
self.user_total_farm_position(user)
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
}

fn decrease_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
&self,
farm_position: &EsdtTokenPayment,
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
) {
if self.is_old_farm_position(farm_position.token_nonce) {
return;
}

let token_attributes: T = farm_token_mapper.get_token_attributes(farm_position.token_nonce);
let user_total_farm_position_mapper =
self.user_total_farm_position(&token_attributes.get_original_owner());
let mut user_total_farm_position = user_total_farm_position_mapper.get();

if user_total_farm_position > farm_position.amount {
user_total_farm_position -= &farm_position.amount;
user_total_farm_position_mapper.set(user_total_farm_position);
} else {
user_total_farm_position_mapper.clear();
}
}

#[view(getFarmingTokenId)]
#[storage_mapper("farming_token_id")]
fn farming_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
Expand Down
75 changes: 0 additions & 75 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use common_structs::{FarmToken, FarmTokenAttributes, Nonce};
use config::ConfigModule;
use contexts::storage_cache::StorageCache;
use core::marker::PhantomData;
use farm_token::FarmTokenModule;
use fixed_supply_token::FixedSupplyToken;
use mergeable::Mergeable;
use multiversx_sc_modules::transfer_role_proxy::PaymentsVec;
use rewards::RewardsModule;

pub trait AllBaseFarmImplTraits:
Expand Down Expand Up @@ -184,79 +182,6 @@ pub trait FarmContract {

new_attributes.into()
}

fn get_exit_penalty(
_sc: &Self::FarmSc,
_total_exit_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
_token_attributes: &Self::AttributesType,
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
BigUint::zero()
}

fn apply_penalty(
_sc: &Self::FarmSc,
_total_exit_amount: &mut BigUint<<Self::FarmSc as ContractBase>::Api>,
_token_attributes: &Self::AttributesType,
_storage_cache: &StorageCache<Self::FarmSc>,
) {
}

fn check_and_update_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
farm_positions: &PaymentsVec<<Self::FarmSc as ContractBase>::Api>,
) {
let farm_token_mapper = sc.farm_token();
for farm_position in farm_positions {
farm_token_mapper.require_same_token(&farm_position.token_identifier);

if sc.is_old_farm_position(farm_position.token_nonce) {
continue;
}

let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

if &token_attributes.original_owner != user {
Self::decrease_user_farm_position(sc, &farm_position);
Self::increase_user_farm_position(sc, user, &farm_position.amount);
}
}
}

#[inline]
fn increase_user_farm_position(
sc: &Self::FarmSc,
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
increase_farm_position_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
) {
sc.user_total_farm_position(user)
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
}

fn decrease_user_farm_position(
sc: &Self::FarmSc,
farm_position: &EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
) {
if sc.is_old_farm_position(farm_position.token_nonce) {
return;
}

let farm_token_mapper = sc.farm_token();
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

let user_total_farm_position_mapper =
sc.user_total_farm_position(&token_attributes.original_owner);
let mut user_total_farm_position = user_total_farm_position_mapper.get();

if user_total_farm_position > farm_position.amount {
user_total_farm_position -= &farm_position.amount;
user_total_farm_position_mapper.set(user_total_farm_position);
} else {
user_total_farm_position_mapper.clear();
}
}
}

pub struct DefaultFarmWrapper<T>
Expand Down
6 changes: 5 additions & 1 deletion common/modules/farm/farm_base_impl/src/claim_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub trait BaseClaimRewardsModule:
);
storage_cache.reward_reserve -= &reward;

FC::check_and_update_user_farm_position(self, &caller, &payments);
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&payments,
&self.farm_token(),
);

let farm_token_mapper = self.farm_token();
let base_attributes = FC::create_claim_rewards_initial_attributes(
Expand Down
8 changes: 6 additions & 2 deletions common/modules/farm/farm_base_impl/src/compound_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ pub trait BaseCompoundRewardsModule:
storage_cache.reward_reserve -= &reward;
storage_cache.farm_token_supply += &reward;

FC::check_and_update_user_farm_position(self, &caller, &payments);
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&payments,
&self.farm_token(),
);

let farm_token_mapper = self.farm_token();
let base_attributes = FC::create_compound_rewards_initial_attributes(
Expand All @@ -86,7 +90,7 @@ pub trait BaseCompoundRewardsModule:
&farm_token_mapper,
);

FC::increase_user_farm_position(self, &caller, &reward);
self.increase_user_farm_position(&caller, &reward);

let first_farm_token = &compound_rewards_context.first_farm_token.payment;
farm_token_mapper.nft_burn(first_farm_token.token_nonce, &first_farm_token.amount);
Expand Down
10 changes: 3 additions & 7 deletions common/modules/farm/farm_base_impl/src/enter_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ pub trait BaseEnterFarmModule:
);

// The order is important - first check and update, then increase position
FC::check_and_update_user_farm_position(
self,
self.check_and_update_user_farm_position::<FC::AttributesType>(
&caller,
&enter_farm_context.additional_farm_tokens,
&self.farm_token(),
);
FC::increase_user_farm_position(
self,
&caller,
&enter_farm_context.farming_token_payment.amount,
);
self.increase_user_farm_position(&caller, &enter_farm_context.farming_token_payment.amount);

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

Expand Down
2 changes: 1 addition & 1 deletion common/modules/farm/farm_base_impl/src/exit_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait BaseExitFarmModule:
);
storage_cache.reward_reserve -= &reward;

FC::decrease_user_farm_position(self, &payment);
self.decrease_user_farm_position::<FC::AttributesType>(&payment, &self.farm_token());

let farming_token_amount = token_attributes.get_total_supply();
let farming_token_payment = EsdtTokenPayment::new(
Expand Down
Loading