Skip to content

Commit a26137a

Browse files
committed
cleanup
1 parent c90362f commit a26137a

File tree

22 files changed

+101
-184
lines changed

22 files changed

+101
-184
lines changed

Cargo.lock

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

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 & 59 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,63 +182,6 @@ pub trait FarmContract {
184182

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

246187
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub trait BaseExitFarmModule:
6262
);
6363
storage_cache.reward_reserve -= &reward;
6464

65-
FC::decrease_user_farm_position(self, &payment);
65+
self.decrease_user_farm_position::<FC::AttributesType>(&payment, &self.farm_token());
6666

6767
let farming_token_amount = token_attributes.get_total_supply();
6868
let farming_token_payment = EsdtTokenPayment::new(

common/modules/farm/farm_token/Cargo.toml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ path = "src/farm_token.rs"
1010
[dependencies.common_structs]
1111
path = "../../../common_structs"
1212

13-
[dependencies.common_errors]
14-
path = "../../../common_errors"
15-
16-
[dependencies.config]
17-
path = "../config"
18-
19-
[dependencies.token_send]
20-
path = "../../token_send"
21-
22-
[dependencies.pausable]
23-
path = "../../pausable"
24-
2513
[dependencies.permissions_module]
2614
path = "../../permissions_module"
2715

common/modules/farm/farm_token/src/farm_token.rs

Lines changed: 5 additions & 5 deletions
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::{Nonce, PaymentsVec};
77

88
#[multiversx_sc::module]
99
pub trait FarmTokenModule:
@@ -31,7 +31,7 @@ pub trait FarmTokenModule:
3131
);
3232
}
3333

34-
fn burn_farm_tokens_from_payments(&self, payments: &ManagedVec<EsdtTokenPayment<Self::Api>>) {
34+
fn burn_farm_tokens_from_payments(&self, payments: &PaymentsVec<Self::Api>) {
3535
let mut total_amount = BigUint::zero();
3636
for entry in payments.iter() {
3737
total_amount += &entry.amount;
@@ -47,7 +47,7 @@ pub trait FarmTokenModule:
4747
token_id: TokenIdentifier,
4848
amount: BigUint,
4949
attributes: &T,
50-
) -> EsdtTokenPayment<Self::Api> {
50+
) -> EsdtTokenPayment {
5151
let new_nonce = self
5252
.send()
5353
.esdt_nft_create_compact(&token_id, &amount, attributes);
@@ -61,7 +61,7 @@ pub trait FarmTokenModule:
6161
self.farm_token_supply().update(|x| *x -= amount);
6262
}
6363

64-
fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment<Self::Api>) {
64+
fn burn_farm_token_payment(&self, payment: &EsdtTokenPayment) {
6565
self.burn_farm_tokens(
6666
&payment.token_identifier,
6767
payment.token_nonce,
@@ -72,7 +72,7 @@ pub trait FarmTokenModule:
7272
fn get_farm_token_attributes<T: TopDecode>(
7373
&self,
7474
token_id: &TokenIdentifier,
75-
token_nonce: u64,
75+
token_nonce: Nonce,
7676
) -> T {
7777
let token_info = self.blockchain().get_esdt_token_data(
7878
&self.blockchain().get_sc_address(),

0 commit comments

Comments
 (0)