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

Move farm position code outside trait #968

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions common/common_structs/src/farm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub trait FarmToken<M: ManagedTypeApi> {
fn get_compounded_rewards(&self) -> BigUint<M>;

fn get_initial_farming_tokens(&self) -> BigUint<M>;

fn get_original_owner(&self) -> ManagedAddress<M>;
}

impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
Expand All @@ -97,4 +99,9 @@ impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
fn get_initial_farming_tokens(&self) -> BigUint<M> {
&self.current_farm_amount - &self.compounded_reward
}

#[inline]
fn get_original_owner(&self) -> ManagedAddress<M> {
self.original_owner.clone()
}
}
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
59 changes: 0 additions & 59 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,63 +182,6 @@ pub trait FarmContract {

new_attributes.into()
}

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
12 changes: 0 additions & 12 deletions common/modules/farm/farm_token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 5 additions & 5 deletions common/modules/farm/farm_token/src/farm_token.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::{Nonce, PaymentsVec};

#[multiversx_sc::module]
pub trait FarmTokenModule:
Expand Down Expand Up @@ -31,7 +31,7 @@ pub trait FarmTokenModule:
);
}

fn burn_farm_tokens_from_payments(&self, payments: &ManagedVec<EsdtTokenPayment<Self::Api>>) {
fn burn_farm_tokens_from_payments(&self, payments: &PaymentsVec<Self::Api>) {
let mut total_amount = BigUint::zero();
for entry in payments.iter() {
total_amount += &entry.amount;
Expand All @@ -47,7 +47,7 @@ pub trait FarmTokenModule:
token_id: TokenIdentifier,
amount: BigUint,
attributes: &T,
) -> EsdtTokenPayment<Self::Api> {
) -> EsdtTokenPayment {
let new_nonce = self
.send()
.esdt_nft_create_compact(&token_id, &amount, attributes);
Expand All @@ -61,7 +61,7 @@ pub trait FarmTokenModule:
self.farm_token_supply().update(|x| *x -= amount);
}

fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment<Self::Api>) {
fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment) {
self.burn_farm_tokens(
&payment.token_identifier,
payment.token_nonce,
Expand All @@ -72,7 +72,7 @@ pub trait FarmTokenModule:
fn get_farm_token_attributes<T: TopDecode>(
&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(),
Expand Down
4 changes: 0 additions & 4 deletions dex/farm-with-locked-rewards/wasm/Cargo.lock

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

6 changes: 5 additions & 1 deletion dex/farm/src/base_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FC::AttributesType>(
orig_caller,
&payments,
&self.farm_token(),
);

self.merge_from_payments_and_burn(payments, &token_mapper)
}
Expand Down
4 changes: 0 additions & 4 deletions dex/farm/wasm/Cargo.lock

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

4 changes: 0 additions & 4 deletions dex/proxy-deployer/wasm/Cargo.lock

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

4 changes: 0 additions & 4 deletions energy-integration/energy-update/wasm/Cargo.lock

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

4 changes: 0 additions & 4 deletions farm-staking/farm-staking-proxy/wasm/Cargo.lock

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

Loading