Skip to content

Commit

Permalink
try_set_farm_position_migration_nonce: code dup
Browse files Browse the repository at this point in the history
Moved try_set_farm_position_migration_nonce to common modules to
remove code duplication.
  • Loading branch information
CostinCarabas committed Sep 21, 2023
1 parent 767f452 commit 8100d57
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 50 deletions.
34 changes: 27 additions & 7 deletions common/modules/farm/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use common_structs::Nonce;
use pausable::State;

pub const DEFAULT_NFT_DEPOSIT_MAX_LEN: usize = 10;
pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1;

#[derive(
ManagedVecItem,
Expand Down Expand Up @@ -35,6 +36,16 @@ impl<M: ManagedTypeApi> Default for UserTotalFarmPosition<M> {

#[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);
}

#[inline]
fn is_active(&self) -> bool {
let state = self.state().get();
Expand All @@ -58,15 +69,24 @@ pub trait ConfigModule: pausable::PausableModule + permissions_module::Permissio
token_nonce > 0 && token_nonce < farm_position_migration_nonce
}

#[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);
fn try_set_farm_position_migration_nonce(&self, farm_token_mapper: NonFungibleTokenMapper<Self::Api>) {
if !self.farm_position_migration_nonce().is_empty() {
return;
}

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
let token_identifier = farm_token_mapper.get_token_id_ref();
self.blockchain()
.get_current_esdt_nft_nonce(&self.blockchain().get_sc_address(), token_identifier)
} else {
DEFAULT_FARM_POSITION_MIGRATION_NONCE
};

self.farm_position_migration_nonce()
.set(migration_farm_token_nonce);
}


#[view(getFarmingTokenId)]
#[storage_mapper("farming_token_id")]
fn farming_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
Expand Down
3 changes: 2 additions & 1 deletion dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub trait Farm:
self.first_week_start_epoch().set_if_empty(current_epoch);

// Farm position migration code
self.try_set_farm_position_migration_nonce();
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);
}

#[payable("*")]
Expand Down
19 changes: 0 additions & 19 deletions dex/farm/src/base_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,6 @@ pub trait BaseFunctionsModule:
});
}

fn try_set_farm_position_migration_nonce(&self) {
if !self.farm_position_migration_nonce().is_empty() {
return;
}

let farm_token_mapper = self.farm_token();

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
let token_identifier = farm_token_mapper.get_token_id_ref();
self.blockchain()
.get_current_esdt_nft_nonce(&self.blockchain().get_sc_address(), token_identifier)
} else {
DEFAULT_FARM_POSITION_MIGRATION_NONCE
};

self.farm_position_migration_nonce()
.set(migration_farm_token_nonce);
}

fn end_produce_rewards<FC: FarmContract<FarmSc = Self>>(&self) {
let mut storage = StorageCache::new(self);
FC::generate_aggregated_rewards(self, &mut storage);
Expand Down
3 changes: 2 additions & 1 deletion dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ pub trait Farm:
self.first_week_start_epoch().set_if_empty(current_epoch);

// Farm position migration code
self.try_set_farm_position_migration_nonce();
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);
}

#[payable("*")]
Expand Down
24 changes: 2 additions & 22 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub mod token_attributes;
pub mod unbond_farm;
pub mod unstake_farm;

pub const DEFAULT_FARM_POSITION_MIGRATION_NONCE: u64 = 1;

#[multiversx_sc::contract]
pub trait FarmStaking:
custom_rewards::CustomRewardsModule
Expand Down Expand Up @@ -92,7 +90,8 @@ pub trait FarmStaking:
self.min_unbond_epochs().set_if_empty(min_unbond_epochs);

// Farm position migration code
self.try_set_farm_position_migration_nonce();
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);
}

#[payable("*")]
Expand Down Expand Up @@ -149,23 +148,4 @@ pub trait FarmStaking:
"May only call this function through VM query"
);
}

fn try_set_farm_position_migration_nonce(&self) {
if !self.farm_position_migration_nonce().is_empty() {
return;
}

let farm_token_mapper = self.farm_token();

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
let token_identifier = farm_token_mapper.get_token_id_ref();
self.blockchain()
.get_current_esdt_nft_nonce(&self.blockchain().get_sc_address(), token_identifier)
} else {
DEFAULT_FARM_POSITION_MIGRATION_NONCE
};

self.farm_position_migration_nonce()
.set(migration_farm_token_nonce);
}
}

0 comments on commit 8100d57

Please sign in to comment.