Skip to content

Commit

Permalink
Merge pull request #932 from multiversx/user-total-farm-position-split
Browse files Browse the repository at this point in the history
user total farm position split
  • Loading branch information
psorinionut authored Jul 31, 2024
2 parents c061a85 + d2ae880 commit 6cd702c
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 226 deletions.
62 changes: 6 additions & 56 deletions common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,14 @@ 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) {
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
}

#[inline]
fn is_active(&self) -> bool {
let state = self.state().get();
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 +69,12 @@ 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>;

// Update for this storage disabled for this version of the exchange
#[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
7 changes: 3 additions & 4 deletions dex/farm-with-locked-rewards/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 67
// Endpoints: 66
// Async Callback: 1
// Total number of exported functions: 70
// Total number of exported functions: 69

#![no_std]

Expand All @@ -32,14 +32,13 @@ 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
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(),
"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
7 changes: 3 additions & 4 deletions dex/farm/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 64
// Endpoints: 63
// Async Callback: 1
// Total number of exported functions: 67
// Total number of exported functions: 66

#![no_std]

Expand All @@ -33,14 +33,13 @@ 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
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
8 changes: 1 addition & 7 deletions energy-integration/fees-collector/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ pub trait ConfigModule {
self.all_tokens().set(&all_tokens_vec);
}

#[endpoint(setAllowExternalClaimRewards)]
fn set_allow_external_claim_rewards(&self, allow_external_claim_rewards: bool) {
let caller = self.blockchain().get_caller();
self.allow_external_claim_rewards(&caller)
.set(allow_external_claim_rewards);
}

#[view(getLockedTokenId)]
#[storage_mapper("lockedTokenId")]
fn locked_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
Expand All @@ -86,6 +79,7 @@ pub trait ConfigModule {
#[storage_mapper("allTokens")]
fn all_tokens(&self) -> SingleValueMapper<ManagedVec<TokenIdentifier>>;

// Update for this storage disabled for this version of the exchange
#[view(getAllowExternalClaimRewards)]
#[storage_mapper("allowExternalClaimRewards")]
fn allow_external_claim_rewards(&self, user: &ManagedAddress) -> SingleValueMapper<bool>;
Expand Down
5 changes: 2 additions & 3 deletions energy-integration/fees-collector/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 38
// Endpoints: 37
// Async Callback (empty): 1
// Total number of exported functions: 41
// Total number of exported functions: 40

#![no_std]

Expand All @@ -26,7 +26,6 @@ multiversx_sc_wasm_adapter::endpoints! {
removeKnownContracts => remove_known_contracts
addKnownTokens => add_known_tokens
removeKnownTokens => remove_known_tokens
setAllowExternalClaimRewards => set_allow_external_claim_rewards
getLockedTokenId => locked_token_id
getAllTokens => get_all_tokens
getAllKnownContracts => known_contracts
Expand Down
6 changes: 4 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,8 @@ fn claim_for_others_positive_test() {
&setup.lp_farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.allow_external_claim_boosted_rewards(true);
sc.allow_external_claim(&managed_address!(&user_address))
.set(true);
},
)
.assert_ok();
Expand All @@ -785,7 +786,8 @@ fn claim_for_others_positive_test() {
&setup.staking_farm_wrapper,
&rust_biguint!(0),
|sc| {
sc.allow_external_claim_boosted_rewards(true);
sc.allow_external_claim(&managed_address!(&user_address))
.set(true);
},
)
.assert_ok();
Expand Down
Loading

0 comments on commit 6cd702c

Please sign in to comment.