Skip to content

Commit 95c3f31

Browse files
authored
Merge pull request #979 from multiversx/merge-with-farm-remove-penalty
Merge with farm remove penalty
2 parents 17b9601 + a26137a commit 95c3f31

File tree

144 files changed

+2906
-2080
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+2906
-2080
lines changed

Cargo.lock

Lines changed: 32 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ members = [
3131
"energy-integration/fees-collector/meta",
3232
"energy-integration/governance-v2",
3333
"energy-integration/governance-v2/meta",
34+
"energy-integration/timestamp-oracle",
35+
"energy-integration/timestamp-oracle/meta",
3436

3537
"farm-staking/farm-staking",
3638
"farm-staking/farm-staking/meta",

common/common_structs/src/alias_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub type Nonce = u64;
66
pub type Epoch = u64;
77
pub type Week = usize;
88
pub type Percent = u64;
9+
pub type Timestamp = u64;
910
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
1011
pub type UnlockPeriod<M> = UnlockSchedule<M>;
1112
pub type OldLockedTokenAttributes<M> = LockedAssetTokenAttributesEx<M>;

common/common_structs/src/farm_types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ pub trait FarmToken<M: ManagedTypeApi> {
8080
fn get_compounded_rewards(&self) -> BigUint<M>;
8181

8282
fn get_initial_farming_tokens(&self) -> BigUint<M>;
83+
84+
fn get_original_owner(&self) -> ManagedAddress<M>;
8385
}
8486

8587
impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
@@ -97,4 +99,9 @@ impl<M: ManagedTypeApi> FarmToken<M> for FarmTokenAttributes<M> {
9799
fn get_initial_farming_tokens(&self) -> BigUint<M> {
98100
&self.current_farm_amount - &self.compounded_reward
99101
}
102+
103+
#[inline]
104+
fn get_original_owner(&self) -> ManagedAddress<M> {
105+
self.original_owner.clone()
106+
}
100107
}

common/modules/farm/config/src/config.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
multiversx_sc::imports!();
44
multiversx_sc::derive_imports!();
55

6-
use common_structs::Nonce;
6+
use common_structs::{FarmToken, Nonce, PaymentsVec};
77
use pausable::State;
88

99
pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10;
@@ -44,6 +44,61 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
4444
.set(migration_farm_token_nonce);
4545
}
4646

47+
fn check_and_update_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
48+
&self,
49+
user: &ManagedAddress,
50+
farm_positions: &PaymentsVec<Self::Api>,
51+
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
52+
) {
53+
for farm_position in farm_positions {
54+
farm_token_mapper.require_same_token(&farm_position.token_identifier);
55+
56+
if self.is_old_farm_position(farm_position.token_nonce) {
57+
continue;
58+
}
59+
60+
let token_attributes: T =
61+
farm_token_mapper.get_token_attributes(farm_position.token_nonce);
62+
63+
if &token_attributes.get_original_owner() != user {
64+
self.decrease_user_farm_position::<T>(&farm_position, farm_token_mapper);
65+
self.increase_user_farm_position(user, &farm_position.amount);
66+
}
67+
}
68+
}
69+
70+
#[inline]
71+
fn increase_user_farm_position(
72+
&self,
73+
user: &ManagedAddress,
74+
increase_farm_position_amount: &BigUint,
75+
) {
76+
self.user_total_farm_position(user)
77+
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
78+
}
79+
80+
fn decrease_user_farm_position<T: FarmToken<Self::Api> + TopDecode>(
81+
&self,
82+
farm_position: &EsdtTokenPayment,
83+
farm_token_mapper: &NonFungibleTokenMapper<Self::Api>,
84+
) {
85+
if self.is_old_farm_position(farm_position.token_nonce) {
86+
return;
87+
}
88+
89+
let token_attributes: T = farm_token_mapper.get_token_attributes(farm_position.token_nonce);
90+
let user_total_farm_position_mapper =
91+
self.user_total_farm_position(&token_attributes.get_original_owner());
92+
let mut user_total_farm_position = user_total_farm_position_mapper.get();
93+
94+
if user_total_farm_position > farm_position.amount {
95+
user_total_farm_position -= &farm_position.amount;
96+
user_total_farm_position_mapper.set(user_total_farm_position);
97+
} else {
98+
user_total_farm_position_mapper.clear();
99+
}
100+
}
101+
47102
#[view(getFarmingTokenId)]
48103
#[storage_mapper("farming_token_id")]
49104
fn farming_token_id(&self) -> SingleValueMapper<TokenIdentifier>;

common/modules/farm/farm_base_impl/src/base_traits_impl.rs

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use common_structs::{FarmToken, FarmTokenAttributes, Nonce};
44
use config::ConfigModule;
55
use contexts::storage_cache::StorageCache;
66
use core::marker::PhantomData;
7-
use farm_token::FarmTokenModule;
87
use fixed_supply_token::FixedSupplyToken;
98
use mergeable::Mergeable;
10-
use multiversx_sc_modules::transfer_role_proxy::PaymentsVec;
119
use rewards::RewardsModule;
1210

1311
pub trait AllBaseFarmImplTraits:
@@ -184,79 +182,6 @@ pub trait FarmContract {
184182

185183
new_attributes.into()
186184
}
187-
188-
fn get_exit_penalty(
189-
_sc: &Self::FarmSc,
190-
_total_exit_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
191-
_token_attributes: &Self::AttributesType,
192-
) -> BigUint<<Self::FarmSc as ContractBase>::Api> {
193-
BigUint::zero()
194-
}
195-
196-
fn apply_penalty(
197-
_sc: &Self::FarmSc,
198-
_total_exit_amount: &mut BigUint<<Self::FarmSc as ContractBase>::Api>,
199-
_token_attributes: &Self::AttributesType,
200-
_storage_cache: &StorageCache<Self::FarmSc>,
201-
) {
202-
}
203-
204-
fn check_and_update_user_farm_position(
205-
sc: &Self::FarmSc,
206-
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
207-
farm_positions: &PaymentsVec<<Self::FarmSc as ContractBase>::Api>,
208-
) {
209-
let farm_token_mapper = sc.farm_token();
210-
for farm_position in farm_positions {
211-
farm_token_mapper.require_same_token(&farm_position.token_identifier);
212-
213-
if sc.is_old_farm_position(farm_position.token_nonce) {
214-
continue;
215-
}
216-
217-
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
218-
farm_token_mapper.get_token_attributes(farm_position.token_nonce);
219-
220-
if &token_attributes.original_owner != user {
221-
Self::decrease_user_farm_position(sc, &farm_position);
222-
Self::increase_user_farm_position(sc, user, &farm_position.amount);
223-
}
224-
}
225-
}
226-
227-
#[inline]
228-
fn increase_user_farm_position(
229-
sc: &Self::FarmSc,
230-
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
231-
increase_farm_position_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
232-
) {
233-
sc.user_total_farm_position(user)
234-
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
235-
}
236-
237-
fn decrease_user_farm_position(
238-
sc: &Self::FarmSc,
239-
farm_position: &EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>,
240-
) {
241-
if sc.is_old_farm_position(farm_position.token_nonce) {
242-
return;
243-
}
244-
245-
let farm_token_mapper = sc.farm_token();
246-
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
247-
farm_token_mapper.get_token_attributes(farm_position.token_nonce);
248-
249-
let user_total_farm_position_mapper =
250-
sc.user_total_farm_position(&token_attributes.original_owner);
251-
let mut user_total_farm_position = user_total_farm_position_mapper.get();
252-
253-
if user_total_farm_position > farm_position.amount {
254-
user_total_farm_position -= &farm_position.amount;
255-
user_total_farm_position_mapper.set(user_total_farm_position);
256-
} else {
257-
user_total_farm_position_mapper.clear();
258-
}
259-
}
260185
}
261186

262187
pub struct DefaultFarmWrapper<T>

common/modules/farm/farm_base_impl/src/claim_rewards.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ pub trait BaseClaimRewardsModule:
8181
);
8282
storage_cache.reward_reserve -= &reward;
8383

84-
FC::check_and_update_user_farm_position(self, &caller, &payments);
84+
self.check_and_update_user_farm_position::<FC::AttributesType>(
85+
&caller,
86+
&payments,
87+
&self.farm_token(),
88+
);
8589

8690
let farm_token_mapper = self.farm_token();
8791
let base_attributes = FC::create_claim_rewards_initial_attributes(

common/modules/farm/farm_base_impl/src/compound_rewards.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ pub trait BaseCompoundRewardsModule:
7070
storage_cache.reward_reserve -= &reward;
7171
storage_cache.farm_token_supply += &reward;
7272

73-
FC::check_and_update_user_farm_position(self, &caller, &payments);
73+
self.check_and_update_user_farm_position::<FC::AttributesType>(
74+
&caller,
75+
&payments,
76+
&self.farm_token(),
77+
);
7478

7579
let farm_token_mapper = self.farm_token();
7680
let base_attributes = FC::create_compound_rewards_initial_attributes(
@@ -86,7 +90,7 @@ pub trait BaseCompoundRewardsModule:
8690
&farm_token_mapper,
8791
);
8892

89-
FC::increase_user_farm_position(self, &caller, &reward);
93+
self.increase_user_farm_position(&caller, &reward);
9094

9195
let first_farm_token = &compound_rewards_context.first_farm_token.payment;
9296
farm_token_mapper.nft_burn(first_farm_token.token_nonce, &first_farm_token.amount);

common/modules/farm/farm_base_impl/src/enter_farm.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@ pub trait BaseEnterFarmModule:
4545
);
4646

4747
// The order is important - first check and update, then increase position
48-
FC::check_and_update_user_farm_position(
49-
self,
48+
self.check_and_update_user_farm_position::<FC::AttributesType>(
5049
&caller,
5150
&enter_farm_context.additional_farm_tokens,
51+
&self.farm_token(),
5252
);
53-
FC::increase_user_farm_position(
54-
self,
55-
&caller,
56-
&enter_farm_context.farming_token_payment.amount,
57-
);
53+
self.increase_user_farm_position(&caller, &enter_farm_context.farming_token_payment.amount);
5854

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

0 commit comments

Comments
 (0)