diff --git a/Cargo.lock b/Cargo.lock index 2cff2edf8..4ac1016f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -816,14 +816,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/common/common_structs/src/farm_types.rs b/common/common_structs/src/farm_types.rs index d7d85e20b..676b26e36 100644 --- a/common/common_structs/src/farm_types.rs +++ b/common/common_structs/src/farm_types.rs @@ -80,6 +80,8 @@ pub trait FarmToken { fn get_compounded_rewards(&self) -> BigUint; fn get_initial_farming_tokens(&self) -> BigUint; + + fn get_original_owner(&self) -> ManagedAddress; } impl FarmToken for FarmTokenAttributes { @@ -97,4 +99,9 @@ impl FarmToken for FarmTokenAttributes { fn get_initial_farming_tokens(&self) -> BigUint { &self.current_farm_amount - &self.compounded_reward } + + #[inline] + fn get_original_owner(&self) -> ManagedAddress { + self.original_owner.clone() + } } diff --git a/common/modules/farm/config/src/config.rs b/common/modules/farm/config/src/config.rs index 88858153e..92616cbfc 100644 --- a/common/modules/farm/config/src/config.rs +++ b/common/modules/farm/config/src/config.rs @@ -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; @@ -44,6 +44,61 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio .set(migration_farm_token_nonce); } + fn check_and_update_user_farm_position + TopDecode>( + &self, + user: &ManagedAddress, + farm_positions: &PaymentsVec, + farm_token_mapper: &NonFungibleTokenMapper, + ) { + 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::(&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 + TopDecode>( + &self, + farm_position: &EsdtTokenPayment, + farm_token_mapper: &NonFungibleTokenMapper, + ) { + 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; diff --git a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs index 3804d223b..f5cc83be3 100644 --- a/common/modules/farm/farm_base_impl/src/base_traits_impl.rs +++ b/common/modules/farm/farm_base_impl/src/base_traits_impl.rs @@ -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: @@ -184,63 +182,6 @@ pub trait FarmContract { new_attributes.into() } - - fn check_and_update_user_farm_position( - sc: &Self::FarmSc, - user: &ManagedAddress<::Api>, - farm_positions: &PaymentsVec<::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<::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<::Api>, - increase_farm_position_amount: &BigUint<::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<::Api>, - ) { - if sc.is_old_farm_position(farm_position.token_nonce) { - return; - } - - let farm_token_mapper = sc.farm_token(); - let token_attributes: FarmTokenAttributes<::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 diff --git a/common/modules/farm/farm_base_impl/src/claim_rewards.rs b/common/modules/farm/farm_base_impl/src/claim_rewards.rs index d9dfe293e..b862b1355 100644 --- a/common/modules/farm/farm_base_impl/src/claim_rewards.rs +++ b/common/modules/farm/farm_base_impl/src/claim_rewards.rs @@ -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::( + &caller, + &payments, + &self.farm_token(), + ); let farm_token_mapper = self.farm_token(); let base_attributes = FC::create_claim_rewards_initial_attributes( diff --git a/common/modules/farm/farm_base_impl/src/compound_rewards.rs b/common/modules/farm/farm_base_impl/src/compound_rewards.rs index 06e52585c..cbde8f124 100644 --- a/common/modules/farm/farm_base_impl/src/compound_rewards.rs +++ b/common/modules/farm/farm_base_impl/src/compound_rewards.rs @@ -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::( + &caller, + &payments, + &self.farm_token(), + ); let farm_token_mapper = self.farm_token(); let base_attributes = FC::create_compound_rewards_initial_attributes( @@ -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); diff --git a/common/modules/farm/farm_base_impl/src/enter_farm.rs b/common/modules/farm/farm_base_impl/src/enter_farm.rs index 02e20d42a..5ef08c699 100644 --- a/common/modules/farm/farm_base_impl/src/enter_farm.rs +++ b/common/modules/farm/farm_base_impl/src/enter_farm.rs @@ -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::( &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); diff --git a/common/modules/farm/farm_base_impl/src/exit_farm.rs b/common/modules/farm/farm_base_impl/src/exit_farm.rs index 315abe421..6cb9d8f05 100644 --- a/common/modules/farm/farm_base_impl/src/exit_farm.rs +++ b/common/modules/farm/farm_base_impl/src/exit_farm.rs @@ -62,7 +62,7 @@ pub trait BaseExitFarmModule: ); storage_cache.reward_reserve -= &reward; - FC::decrease_user_farm_position(self, &payment); + self.decrease_user_farm_position::(&payment, &self.farm_token()); let farming_token_amount = token_attributes.get_total_supply(); let farming_token_payment = EsdtTokenPayment::new( diff --git a/common/modules/farm/farm_token/Cargo.toml b/common/modules/farm/farm_token/Cargo.toml index aef369e13..bff6d8b5c 100644 --- a/common/modules/farm/farm_token/Cargo.toml +++ b/common/modules/farm/farm_token/Cargo.toml @@ -10,18 +10,6 @@ path = "src/farm_token.rs" [dependencies.common_structs] path = "../../../common_structs" -[dependencies.common_errors] -path = "../../../common_errors" - -[dependencies.config] -path = "../config" - -[dependencies.token_send] -path = "../../token_send" - -[dependencies.pausable] -path = "../../pausable" - [dependencies.permissions_module] path = "../../permissions_module" diff --git a/common/modules/farm/farm_token/src/farm_token.rs b/common/modules/farm/farm_token/src/farm_token.rs index de3f32d5d..184c02c3e 100644 --- a/common/modules/farm/farm_token/src/farm_token.rs +++ b/common/modules/farm/farm_token/src/farm_token.rs @@ -3,7 +3,7 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); -use common_structs::Nonce; +use common_structs::{Nonce, PaymentsVec}; #[multiversx_sc::module] pub trait FarmTokenModule: @@ -31,7 +31,7 @@ pub trait FarmTokenModule: ); } - fn burn_farm_tokens_from_payments(&self, payments: &ManagedVec>) { + fn burn_farm_tokens_from_payments(&self, payments: &PaymentsVec) { let mut total_amount = BigUint::zero(); for entry in payments.iter() { total_amount += &entry.amount; @@ -47,7 +47,7 @@ pub trait FarmTokenModule: token_id: TokenIdentifier, amount: BigUint, attributes: &T, - ) -> EsdtTokenPayment { + ) -> EsdtTokenPayment { let new_nonce = self .send() .esdt_nft_create_compact(&token_id, &amount, attributes); @@ -61,7 +61,7 @@ pub trait FarmTokenModule: self.farm_token_supply().update(|x| *x -= amount); } - fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment) { + fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment) { self.burn_farm_tokens( &payment.token_identifier, payment.token_nonce, @@ -72,7 +72,7 @@ pub trait FarmTokenModule: fn get_farm_token_attributes( &self, token_id: &TokenIdentifier, - token_nonce: u64, + token_nonce: Nonce, ) -> T { let token_info = self.blockchain().get_esdt_token_data( &self.blockchain().get_sc_address(), diff --git a/dex/farm-with-locked-rewards/wasm/Cargo.lock b/dex/farm-with-locked-rewards/wasm/Cargo.lock index 5ca93780b..4461437a9 100644 --- a/dex/farm-with-locked-rewards/wasm/Cargo.lock +++ b/dex/farm-with-locked-rewards/wasm/Cargo.lock @@ -223,14 +223,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/dex/farm/src/base_functions.rs b/dex/farm/src/base_functions.rs index 77ca0d110..57fa89d41 100644 --- a/dex/farm/src/base_functions.rs +++ b/dex/farm/src/base_functions.rs @@ -181,7 +181,11 @@ pub trait BaseFunctionsModule: let token_mapper = self.farm_token(); token_mapper.require_all_same_token(&payments); - FC::check_and_update_user_farm_position(self, orig_caller, &payments); + self.check_and_update_user_farm_position::( + orig_caller, + &payments, + &self.farm_token(), + ); self.merge_from_payments_and_burn(payments, &token_mapper) } diff --git a/dex/farm/wasm/Cargo.lock b/dex/farm/wasm/Cargo.lock index 35713b9de..a40853d06 100644 --- a/dex/farm/wasm/Cargo.lock +++ b/dex/farm/wasm/Cargo.lock @@ -192,14 +192,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/dex/proxy-deployer/wasm/Cargo.lock b/dex/proxy-deployer/wasm/Cargo.lock index aac9fafbd..d012a5597 100644 --- a/dex/proxy-deployer/wasm/Cargo.lock +++ b/dex/proxy-deployer/wasm/Cargo.lock @@ -184,14 +184,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/energy-integration/energy-update/wasm/Cargo.lock b/energy-integration/energy-update/wasm/Cargo.lock index d23fe0b09..5879e51a0 100644 --- a/energy-integration/energy-update/wasm/Cargo.lock +++ b/energy-integration/energy-update/wasm/Cargo.lock @@ -202,14 +202,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/farm-staking/farm-staking-proxy/wasm/Cargo.lock b/farm-staking/farm-staking-proxy/wasm/Cargo.lock index 280fac82d..211fad7a4 100644 --- a/farm-staking/farm-staking-proxy/wasm/Cargo.lock +++ b/farm-staking/farm-staking-proxy/wasm/Cargo.lock @@ -280,14 +280,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/farm-staking/farm-staking/src/base_impl_wrapper.rs b/farm-staking/farm-staking/src/base_impl_wrapper.rs index 21d98f5bc..a6a6673dd 100644 --- a/farm-staking/farm-staking/src/base_impl_wrapper.rs +++ b/farm-staking/farm-staking/src/base_impl_wrapper.rs @@ -5,7 +5,6 @@ use core::marker::PhantomData; use common_structs::FarmToken; use contexts::storage_cache::StorageCache; use farm_base_impl::base_traits_impl::FarmContract; -use multiversx_sc_modules::transfer_role_proxy::PaymentsVec; use crate::token_attributes::StakingFarmTokenAttributes; @@ -192,61 +191,4 @@ where original_owner: caller, } } - - fn check_and_update_user_farm_position( - sc: &Self::FarmSc, - user: &ManagedAddress<::Api>, - farm_positions: &PaymentsVec<::Api>, - ) { - let farm_token_mapper = sc.farm_token(); - for farm_position in farm_positions { - if sc.is_old_farm_position(farm_position.token_nonce) { - continue; - } - - farm_token_mapper.require_same_token(&farm_position.token_identifier); - - let token_attributes: StakingFarmTokenAttributes<::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<::Api>, - increase_farm_position_amount: &BigUint<::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<::Api>, - ) { - if sc.is_old_farm_position(farm_position.token_nonce) { - return; - } - - let farm_token_mapper = sc.farm_token(); - let token_attributes: StakingFarmTokenAttributes<::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(); - } - } } diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index 769f2e15f..16d73d80c 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -147,7 +147,11 @@ pub trait FarmStaking: let token_mapper = self.farm_token(); token_mapper.require_all_same_token(&payments); - FC::check_and_update_user_farm_position(self, orig_caller, &payments); + self.check_and_update_user_farm_position::( + orig_caller, + &payments, + &self.farm_token(), + ); self.merge_from_payments_and_burn(payments, &token_mapper) } diff --git a/farm-staking/farm-staking/src/token_attributes.rs b/farm-staking/farm-staking/src/token_attributes.rs index 59f8c6326..9b789a288 100644 --- a/farm-staking/farm-staking/src/token_attributes.rs +++ b/farm-staking/farm-staking/src/token_attributes.rs @@ -71,17 +71,25 @@ impl Into> for StakingFarmTokenAttribu } impl FarmToken for StakingFarmTokenAttributes { + #[inline] fn get_reward_per_share(&self) -> BigUint { self.reward_per_share.clone() } + #[inline] fn get_compounded_rewards(&self) -> BigUint { self.compounded_reward.clone() } + #[inline] fn get_initial_farming_tokens(&self) -> BigUint { &self.current_farm_amount - &self.compounded_reward } + + #[inline] + fn get_original_owner(&self) -> ManagedAddress { + self.original_owner.clone() + } } impl FixedSupplyToken for StakingFarmTokenAttributes { diff --git a/farm-staking/farm-staking/wasm/Cargo.lock b/farm-staking/farm-staking/wasm/Cargo.lock index 71f9590c1..fb13bc4e4 100644 --- a/farm-staking/farm-staking/wasm/Cargo.lock +++ b/farm-staking/farm-staking/wasm/Cargo.lock @@ -224,14 +224,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock b/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock index 7ac8e030b..6c0c145ee 100644 --- a/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock +++ b/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock @@ -244,14 +244,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]] diff --git a/locked-asset/proxy_dex/wasm/Cargo.lock b/locked-asset/proxy_dex/wasm/Cargo.lock index 781bc8732..bfc773ea6 100644 --- a/locked-asset/proxy_dex/wasm/Cargo.lock +++ b/locked-asset/proxy_dex/wasm/Cargo.lock @@ -215,14 +215,10 @@ dependencies = [ name = "farm_token" version = "0.0.0" dependencies = [ - "common_errors", "common_structs", - "config", "multiversx-sc", "multiversx-sc-modules", - "pausable", "permissions_module", - "token_send", ] [[package]]