Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stake: Try to optimize the withdraw rewards function #395

Merged
merged 9 commits into from
Nov 22, 2024

Conversation

ueco-jb
Copy link
Member

@ueco-jb ueco-jb commented Nov 22, 2024

No description provided.

@ueco-jb ueco-jb requested a review from gangov November 22, 2024 11:57
@ueco-jb ueco-jb self-assigned this Nov 22, 2024
@totallynotjakub totallynotjakub force-pushed the jakub/stake-rewards-optimization branch from cb1c3af to 7b016d5 Compare November 22, 2024 12:21
@totallynotjakub totallynotjakub force-pushed the jakub/stake-rewards-optimization branch from 4a625b4 to 1b62564 Compare November 22, 2024 12:59
@ueco-jb ueco-jb marked this pull request as ready for review November 22, 2024 14:07
@@ -70,6 +72,13 @@ pub trait StakingTrait {

fn query_withdrawable_rewards(env: Env, address: Address) -> WithdrawableRewardsResponse;

fn query_withdrawable_rewards_ch(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you using shortened query_withdrawable_rewards_ch instead of query_withdrawable_rewards_chunks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Character limit per function name.

Comment on lines 310 to 313
token_contract::Client::new(&env, &reward_token).transfer(
&env.current_contract_address(),
&sender,
&pending_rewards,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it better if we use env.contract_invoke - should be better on the storage and (I'm not 100%) but it will avoid migration issues when we upgrade to next soroban-sdk

Comment on lines +413 to +414
let (pending_reward, _) =
calculate_pending_rewards_chunked(&env, &asset, &stakes, chunk_size, start_day);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we return just pending_reward since we're dropping the 2nd part of the tuple with _ from memory

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in another call of that function.

Comment on lines +159 to +160
let stake_age_days =
((reward_day - stake.stake_timestamp) / SECONDS_PER_DAY).min(60);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would happen if the user stakes their tokens after the reward_day in question. Their stake.stake_timestamp is set to the time they staked, which is more recent than reward_day.

I'm not 100% sure that .skip_while(|&day| day < start_day.unwrap_or(last_claim_time)) would prevent us from getting to thath state, but last_claim_time may still be before stake.stake_timestamp. what if the user hasn't claimed rewards for a long time, last_claim_time could be an old timestamp

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would happen if the user stakes their tokens after the reward_day in question

Then it's getting filtered through skip_while(|&day: u64| day < last_claim_time))

what if the user hasn't claimed rewards for a long time, last_claim_time could be an old timestamp

last_claim_time can be even 0 if user never claimed his rewards; That means he needs to start from the beginning of history of the rewards.

Copy link
Collaborator

@gangov gangov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

stakes.last_reward_time = last_reward_day;
save_stakes(&env, &sender, &stakes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I know logs are useless, but we should have some

@ueco-jb ueco-jb merged commit 557b6cb into main Nov 22, 2024
3 checks passed
@ueco-jb ueco-jb deleted the jakub/stake-rewards-optimization branch November 22, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants