-
Notifications
You must be signed in to change notification settings - Fork 4
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
fix(StakeManager): use mul by PRECISION and div back later to avoid precision loss in int divisions #67
Conversation
af5ca34
to
547b910
Compare
547b910
to
81a28da
Compare
…recision loss in int divisions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great work @3esmit !
I've left some comments inline. Generally the changes look good to me
@@ -324,8 +326,8 @@ contract StakeManager is Ownable { | |||
//mint multiplier points to that epoch | |||
_mintMP(account, iEpoch.startTime + EPOCH_SIZE, iEpoch); | |||
uint256 userSupply = account.balance + account.currentMP; | |||
uint256 userShare = userSupply / iEpoch.totalSupply; //TODO: fix precision loss; | |||
uint256 userEpochReward = userShare * iEpoch.epochReward; | |||
uint256 userShare = (userSupply * PRECISION) / iEpoch.totalSupply; //TODO: fix precision loss; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can now remove the TODO:
comments no?
userVault.stake(stakeAmount, 0); | ||
|
||
assertEq(stakeManager.totalSupplyMP(), stakeAmount); | ||
for (uint256 i = 0; i < 53; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just cosmetics, but 53
here is somewhat of an arbitrary number. Would be better to give some semantic meaning using a variable or constant or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
53 is to complete a year. 53 weeks gives roughly 1 year.
console.log("# START EPOCH", stakeManager.currentEpoch()); | ||
console.log("# PND_REWARDS", stakeManager.pendingReward()); | ||
|
||
for (uint256 i = 0; i < 3; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (uint256 i = 0; i < 3; i++) { | |
for (uint256 i = 0; i < userVaults.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized i
isn't used anywhere, so I'm not sure this actually implied userVaults.length
, since you're also using userVaults.length
in the inner loop.
Is there any particular reason this is supposed to loop 3 times? If yes, can we add this via some constant or variable, so this isn't getting as confusing in the future?
assertEq(lastMint, lastMintBefore + stakeManager.EPOCH_SIZE(), "must increaase lastMint"); | ||
assertEq(epoch, epochBefore + 1, "must increase epoch"); | ||
assertGt(currentMP, currentMPBefore, "must increase MPs"); | ||
assertGt(rewards, rewardsBefore, "must increase rewards"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are great.
I wonder if we can make these assertions stronger.
Do you think it's feasible to also assert by how much MPs and rewards increase?
Or maybe a rough ballpark, so that one can say "it must increase by at least this amount" ?
Description
Alternative to #66;
This PR fixes #64 and fixes #24, uses a more simple solution than #66.
Checklist
Ensure you completed all of the steps below before submitting your pull request:
forge snapshot
?pnpm lint
?forge test
?pnpm verify
?