Skip to content

Commit

Permalink
user total farm position split
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Jul 11, 2024
1 parent c061a85 commit 04285fa
Show file tree
Hide file tree
Showing 21 changed files with 159 additions and 207 deletions.
61 changes: 8 additions & 53 deletions common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,12 @@ use pausable::State;
pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10;
pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1;

#[derive(
ManagedVecItem,
TopEncode,
TopDecode,
NestedEncode,
NestedDecode,
TypeAbi,
Clone,
PartialEq,
Debug,
)]
pub struct UserTotalFarmPosition<M: ManagedTypeApi> {
pub total_farm_position: BigUint<M>,
pub allow_external_claim_boosted_rewards: bool,
}

impl<M: ManagedTypeApi> Default for UserTotalFarmPosition<M> {
fn default() -> Self {
Self {
total_farm_position: BigUint::zero(),
allow_external_claim_boosted_rewards: false,
}
}
}

#[multiversx_sc::module]
pub trait ConfigModule: pausable::PausableModule + permissions_module::PermissionsModule {
#[endpoint(allowExternalClaimBoostedRewards)]
fn allow_external_claim_boosted_rewards(&self, allow_external_claim: bool) {
#[endpoint(setAllowExternalClaimBoostedRewards)]
fn set_allow_external_claim(&self, allow_external_claim: bool) {
let caller = self.blockchain().get_caller();
let mut user_total_farm_position = self.get_user_total_farm_position(&caller);
user_total_farm_position.allow_external_claim_boosted_rewards = allow_external_claim;
self.user_total_farm_position(&caller)
.set(user_total_farm_position);
}

#[view(getAllowExternalClaimRewards)]
fn get_allow_external_claim_rewards(&self, user: ManagedAddress) -> bool {
let user_total_farm_position = self.get_user_total_farm_position(&user);
user_total_farm_position.allow_external_claim_boosted_rewards
self.allow_external_claim(&caller).set(allow_external_claim);
}

#[inline]
Expand All @@ -57,18 +23,6 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
state == State::Active
}

fn get_user_total_farm_position(
&self,
user: &ManagedAddress,
) -> UserTotalFarmPosition<Self::Api> {
let user_total_farm_position_mapper = self.user_total_farm_position(user);
if user_total_farm_position_mapper.is_empty() {
UserTotalFarmPosition::default()
} else {
user_total_farm_position_mapper.get()
}
}

fn is_old_farm_position(&self, token_nonce: Nonce) -> bool {
let farm_position_migration_nonce = self.farm_position_migration_nonce().get();
token_nonce > 0 && token_nonce < farm_position_migration_nonce
Expand Down Expand Up @@ -121,10 +75,11 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio

#[view(getUserTotalFarmPosition)]
#[storage_mapper("userTotalFarmPosition")]
fn user_total_farm_position(
&self,
user: &ManagedAddress,
) -> SingleValueMapper<UserTotalFarmPosition<Self::Api>>;
fn user_total_farm_position(&self, user: &ManagedAddress) -> SingleValueMapper<BigUint>;

#[view(getAllowExternalClaim)]
#[storage_mapper("allowExternalClaim")]
fn allow_external_claim(&self, user: &ManagedAddress) -> SingleValueMapper<bool>;

#[view(getFarmPositionMigrationNonce)]
#[storage_mapper("farm_position_migration_nonce")]
Expand Down
22 changes: 11 additions & 11 deletions common/modules/farm/farm_base_impl/src/base_traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,8 @@ pub trait FarmContract {
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>,
increase_farm_position_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>,
) {
let mut user_total_farm_position = sc.get_user_total_farm_position(user);
user_total_farm_position.total_farm_position += increase_farm_position_amount;
sc.user_total_farm_position(user)
.set(user_total_farm_position);
.update(|total_farm_position| *total_farm_position += increase_farm_position_amount);
}

fn decrease_user_farm_position(
Expand All @@ -248,14 +246,16 @@ pub trait FarmContract {
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

sc.user_total_farm_position(&token_attributes.original_owner)
.update(|user_total_farm_position| {
if user_total_farm_position.total_farm_position > farm_position.amount {
user_total_farm_position.total_farm_position -= &farm_position.amount;
} else {
user_total_farm_position.total_farm_position = BigUint::zero();
}
});
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();
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ pub trait Farm:
OptionalValue::Some(user) => user,
OptionalValue::None => &caller,
};
let user_total_farm_position = self.get_user_total_farm_position(user);
if user != &caller {
require!(
user_total_farm_position.allow_external_claim_boosted_rewards,
self.allow_external_claim(user).get(),
"Cannot claim rewards for this address"
);
}
Expand Down
4 changes: 2 additions & 2 deletions dex/farm-with-locked-rewards/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ multiversx_sc_wasm_adapter::endpoints! {
calculateRewardsForGivenPosition => calculate_rewards_for_given_position
getRewardPerShare => reward_per_share
getRewardReserve => reward_reserve
allowExternalClaimBoostedRewards => allow_external_claim_boosted_rewards
getAllowExternalClaimRewards => get_allow_external_claim_rewards
setAllowExternalClaimBoostedRewards => set_allow_external_claim
getFarmingTokenId => farming_token_id
getRewardTokenId => reward_token_id
getPerBlockRewardAmount => per_block_reward_amount
getLastRewardBlockNonce => last_reward_block_nonce
getDivisionSafetyConstant => division_safety_constant
getUserTotalFarmPosition => user_total_farm_position
getAllowExternalClaim => allow_external_claim
getFarmPositionMigrationNonce => farm_position_migration_nonce
setLockingScAddress => set_locking_sc_address
setLockEpochs => set_lock_epochs
Expand Down
23 changes: 13 additions & 10 deletions dex/farm/src/base_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ pub trait BaseFunctionsModule:
}

if migrated_amount > 0 {
let mut user_total_farm_position = self.get_user_total_farm_position(caller);
user_total_farm_position.total_farm_position += &migrated_amount;
self.user_total_farm_position(caller)
.set(user_total_farm_position);
.update(|total_farm_position| *total_farm_position += &migrated_amount);
}

migrated_amount
Expand All @@ -231,10 +229,16 @@ pub trait BaseFunctionsModule:
if migrated_amount == BigUint::zero() {
return;
}
self.user_total_farm_position(caller)
.update(|user_total_farm_position| {
user_total_farm_position.total_farm_position -= migrated_amount;
});

let user_total_farm_position_mapper = self.user_total_farm_position(caller);
let mut user_total_farm_position = user_total_farm_position_mapper.get();

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

fn end_produce_rewards<FC: FarmContract<FarmSc = Self>>(&self) {
Expand Down Expand Up @@ -281,10 +285,9 @@ where
sc: &<Self as FarmContract>::FarmSc,
caller: &ManagedAddress<<<Self as FarmContract>::FarmSc as ContractBase>::Api>,
) -> BigUint<<<Self as FarmContract>::FarmSc as ContractBase>::Api> {
let user_total_farm_position = sc.get_user_total_farm_position(caller);
let user_farm_position = user_total_farm_position.total_farm_position;
let user_total_farm_position = sc.user_total_farm_position(caller).get();

sc.claim_boosted_yields_rewards(caller, user_farm_position)
sc.claim_boosted_yields_rewards(caller, user_total_farm_position)
}
}

Expand Down
3 changes: 1 addition & 2 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ pub trait Farm:
OptionalValue::Some(user) => user,
OptionalValue::None => &caller,
};
let user_total_farm_position = self.get_user_total_farm_position(user);
if user != &caller {
require!(
user_total_farm_position.allow_external_claim_boosted_rewards,
self.allow_external_claim(&user).get(),

Check warning on line 226 in dex/farm/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/src/lib.rs#L226

warning: this expression creates a reference which is immediately dereferenced by the compiler --> dex/farm/src/lib.rs:226:43 | 226 | self.allow_external_claim(&user).get(), | ^^^^^ help: change this to: `user` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
dex/farm/src/lib.rs:226:43:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> dex/farm/src/lib.rs:226:43
    |
226 |                 self.allow_external_claim(&user).get(),
    |                                           ^^^^^ help: change this to: `user`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

Check warning on line 226 in dex/farm/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/src/lib.rs#L226

warning: this expression creates a reference which is immediately dereferenced by the compiler --> dex/farm/src/lib.rs:226:43 | 226 | self.allow_external_claim(&user).get(), | ^^^^^ help: change this to: `user` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
dex/farm/src/lib.rs:226:43:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> dex/farm/src/lib.rs:226:43
    |
226 |                 self.allow_external_claim(&user).get(),
    |                                           ^^^^^ help: change this to: `user`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__
"Cannot claim rewards for this address"
);
}
Expand Down
18 changes: 5 additions & 13 deletions dex/farm/tests/farm_setup/multi_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(deprecated)]

use common_structs::FarmTokenAttributes;
use config::{ConfigModule, UserTotalFarmPosition};
use config::ConfigModule;
use multiversx_sc::codec::multi_types::OptionalValue;
use multiversx_sc::{
storage::mappers::StorageTokenWrapper,
Expand Down Expand Up @@ -610,12 +610,8 @@ where
pub fn allow_external_claim_rewards(&mut self, user: &Address, allow_external_claim: bool) {
self.b_mock
.execute_tx(user, &self.farm_wrapper, &rust_biguint!(0), |sc| {
sc.user_total_farm_position(&managed_address!(user)).update(
|user_total_farm_position| {
user_total_farm_position.allow_external_claim_boosted_rewards =
allow_external_claim;
},
);
sc.allow_external_claim(&managed_address!(user))
.set(allow_external_claim);
})
.assert_ok();
}
Expand Down Expand Up @@ -711,12 +707,8 @@ where
pub fn set_user_total_farm_position(&mut self, user_addr: &Address, new_farm_position: u64) {
self.b_mock
.execute_tx(&self.owner, &self.farm_wrapper, &rust_biguint!(0), |sc| {
let user_farm_position = UserTotalFarmPosition {
total_farm_position: managed_biguint!(new_farm_position),
..Default::default()
};
sc.user_total_farm_position(&managed_address!(user_addr))
.set(user_farm_position);
.set(managed_biguint!(new_farm_position));
})
.assert_ok();
}
Expand All @@ -729,7 +721,7 @@ where
if expected_amount > 0 && !user_total_farm_position_mapper.is_empty() {
assert_eq!(
managed_biguint!(expected_amount),
user_total_farm_position_mapper.get().total_farm_position
user_total_farm_position_mapper.get()
);
}
})
Expand Down
4 changes: 2 additions & 2 deletions dex/farm/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ multiversx_sc_wasm_adapter::endpoints! {
calculateRewardsForGivenPosition => calculate_rewards_for_given_position
getRewardPerShare => reward_per_share
getRewardReserve => reward_reserve
allowExternalClaimBoostedRewards => allow_external_claim_boosted_rewards
getAllowExternalClaimRewards => get_allow_external_claim_rewards
setAllowExternalClaimBoostedRewards => set_allow_external_claim
getFarmingTokenId => farming_token_id
getRewardTokenId => reward_token_id
getPerBlockRewardAmount => per_block_reward_amount
getLastRewardBlockNonce => last_reward_block_nonce
getDivisionSafetyConstant => division_safety_constant
getUserTotalFarmPosition => user_total_farm_position
getAllowExternalClaim => allow_external_claim
getFarmPositionMigrationNonce => farm_position_migration_nonce
registerFarmToken => register_farm_token
getFarmTokenId => farm_token
Expand Down
4 changes: 2 additions & 2 deletions energy-integration/farm-boosted-yields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ pub trait FarmBoostedYieldsModule:

fn clear_user_energy_if_needed(&self, original_caller: &ManagedAddress) {
let opt_config = self.try_get_boosted_yields_config();
let user_total_farm_position = self.get_user_total_farm_position(original_caller);
let user_total_farm_position = self.user_total_farm_position(original_caller).get();
if let Some(config) = opt_config {
let boosted_yields_factors = config.get_latest_factors();
self.clear_user_energy(
original_caller,
&user_total_farm_position.total_farm_position,
&user_total_farm_position,
&boosted_yields_factors.min_farm_amount,
);
}
Expand Down
4 changes: 2 additions & 2 deletions farm-staking/farm-staking-proxy/tests/staking_farm_with_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ fn claim_for_others_positive_test() {
&setup.lp_farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.allow_external_claim_boosted_rewards(true);
sc.set_allow_external_claim(true);
},
)
.assert_ok();
Expand All @@ -785,7 +785,7 @@ fn claim_for_others_positive_test() {
&setup.staking_farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.allow_external_claim_boosted_rewards(true);
sc.set_allow_external_claim(true);
},
)
.assert_ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(deprecated)]

use config::{ConfigModule, UserTotalFarmPosition};
use config::ConfigModule;
use energy_factory::energy::EnergyModule;
use energy_query::Energy;
use farm_with_locked_rewards::Farm;
Expand Down Expand Up @@ -679,12 +679,8 @@ where
&self.staking_farm_wrapper,
&rust_biguint!(0),
|sc| {
let user_farm_position = UserTotalFarmPosition {
total_farm_position: managed_biguint!(new_farm_position),
..Default::default()
};
sc.user_total_farm_position(&managed_address!(user_addr))
.set(user_farm_position);
.set(managed_biguint!(new_farm_position));
},
)
.assert_ok();
Expand All @@ -697,12 +693,8 @@ where
&self.lp_farm_wrapper,
&rust_biguint!(0),
|sc| {
let user_farm_position = UserTotalFarmPosition {
total_farm_position: managed_biguint!(new_farm_position),
..Default::default()
};
sc.user_total_farm_position(&managed_address!(user_addr))
.set(user_farm_position);
.set(managed_biguint!(new_farm_position));
},
)
.assert_ok();
Expand All @@ -720,7 +712,7 @@ where
if expected_amount > 0 && !user_total_farm_position_mapper.is_empty() {
assert_eq!(
managed_biguint!(expected_amount),
user_total_farm_position_mapper.get().total_farm_position
user_total_farm_position_mapper.get()
);
}
})
Expand All @@ -735,7 +727,7 @@ where
if expected_amount > 0 && !user_total_farm_position_mapper.is_empty() {
assert_eq!(
managed_biguint!(expected_amount),
user_total_farm_position_mapper.get().total_farm_position
user_total_farm_position_mapper.get()
);
}
})
Expand Down
Loading

0 comments on commit 04285fa

Please sign in to comment.