Skip to content

Commit

Permalink
some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Nov 8, 2024
1 parent 1fc5047 commit cfe0a51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
20 changes: 13 additions & 7 deletions energy-integration/farm-boosted-yields/src/custom_reward_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,30 @@ pub trait CustomRewardLogicModule:
user_reward * BigUint::from(percent_leftover) / MAX_PERCENT
}

fn get_week_start_and_end_timestamp(&self, week: Week) -> WeekTimestamps {
fn get_week_start_and_end_timestamp(&self, week: Week) -> Option<WeekTimestamps> {
let week_start_epoch = self.get_start_epoch_for_week(week);
let week_end_epoch = week_start_epoch + EPOCHS_IN_WEEK;

let mut needed_epoch_timestamps = MultiValueEncoded::new();
needed_epoch_timestamps.push(week_start_epoch);
needed_epoch_timestamps.push(week_end_epoch);

let timestamps = self
let opt_timestamps = self
.get_multiple_epochs_start_timestamp(needed_epoch_timestamps)
.to_vec();
let week_start_timestamp = timestamps.get(0);
let week_end_timestamp = timestamps.get(1) - 1;
let week_start_timestamp = match opt_timestamps.get(0) {
Some(timestamp) => timestamp,
None => return None,
};
let week_end_timestamp = match opt_timestamps.get(1) {
Some(timestamp) => timestamp - 1,
None => return None,
};

WeekTimestamps {
Some(WeekTimestamps {
start: week_start_timestamp,
end: week_end_timestamp,
}
})
}

#[inline]
Expand All @@ -201,7 +207,7 @@ pub trait CustomRewardLogicModule:
fn get_multiple_epochs_start_timestamp(
&self,
epochs: MultiValueEncoded<Epoch>,
) -> MultiValueEncoded<Timestamp> {
) -> MultiValueEncoded<Option<Timestamp>> {
let timestamp_oracle_addr = self.timestamp_oracle_address().get();
self.timestamp_oracle_proxy_obj(timestamp_oracle_addr)
.get_start_timestamp_multiple_epochs(epochs)
Expand Down
23 changes: 15 additions & 8 deletions energy-integration/farm-boosted-yields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ where
return user_rewards;
}

let user_reward = sc.calculate_user_boosted_rewards(CalculateRewardsArgs {
let mut user_reward = sc.calculate_user_boosted_rewards(CalculateRewardsArgs {
factors,
weekly_reward_amount: &weekly_reward.amount,
user_farm_amount: &self.user_farm_amount,
Expand All @@ -158,20 +158,27 @@ where
return user_rewards;
}

let week_timestamps = sc.get_week_start_and_end_timestamp(claim_progress.week);
let new_user_reward =
sc.limit_boosted_rewards_by_claim_time(user_reward, &week_timestamps, claim_progress);
if new_user_reward == 0 {
return user_rewards;
if claim_progress.enter_timestamp != 0 {
let opt_week_timestamps = sc.get_week_start_and_end_timestamp(claim_progress.week);
if let Some(week_timestamps) = opt_week_timestamps {
user_reward = sc.limit_boosted_rewards_by_claim_time(
user_reward,
&week_timestamps,
claim_progress,
);
if user_reward == 0 {
return user_rewards;
}
}
}

sc.remaining_boosted_rewards_to_distribute(claim_progress.week)
.update(|amount| *amount -= &new_user_reward);
.update(|amount| *amount -= &user_reward);

user_rewards.push(EsdtTokenPayment::new(
weekly_reward.token_identifier,
0,
new_user_reward,
user_reward,
));

user_rewards
Expand Down
16 changes: 9 additions & 7 deletions energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@ pub trait EpochToTimestampModule {
}

#[view(getStartTimestampForEpoch)]
fn get_start_timestamp_for_epoch(&self, epoch: Epoch) -> Timestamp {
fn get_start_timestamp_for_epoch(&self, epoch: Epoch) -> Option<Timestamp> {
let mapper = self.timestamp_for_epoch(epoch);
require!(!mapper.is_empty(), "No timestamp available");

mapper.get()
if !mapper.is_empty() {
Some(mapper.get())
} else {
None
}
}

#[view(getStartTimestampMultipleEpochs)]
fn get_start_timestamp_multiple_epochs(
&self,
epochs: MultiValueEncoded<Epoch>,
) -> MultiValueEncoded<Timestamp> {
) -> MultiValueEncoded<Option<Timestamp>> {
let mut timestamps = MultiValueEncoded::new();
for epoch in epochs {
let timestamp = self.get_start_timestamp_for_epoch(epoch);
timestamps.push(timestamp);
let opt_timestamp = self.get_start_timestamp_for_epoch(epoch);
timestamps.push(opt_timestamp);
}

timestamps
Expand Down

0 comments on commit cfe0a51

Please sign in to comment.