-
Notifications
You must be signed in to change notification settings - Fork 20
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: rounding in the protocol's favor pt. 3 #489
Conversation
@0xJabberwock , can you create some tests with the cases you found, to make sure the math doesn't break for these cases again? |
e3be01c
to
0b0a513
Compare
0b0a513
to
85f9e3b
Compare
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.
It seems right, but it's getting late and I'm getting bleary eyed :) Similar text comments to those on StableMath.
@@ -182,6 +189,8 @@ library WeightedMath { | |||
|
|||
uint256 invariantRatioWithFees = 0; | |||
for (uint256 i = 0; i < balances.length; ++i) { | |||
// Round down to ultimately reduce the ratios with fees, | |||
// which will be used later upon calculating the `nonTaxableAmount` |
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.
// which will be used later upon calculating the `nonTaxableAmount` | |
// which will be used later when calculating the `nonTaxableAmount` for each token. |
Comments should always end with a period (here and elsewhere).
? bptTotalSupply.mulDown(invariantRatio - FixedPoint.ONE) | ||
: 0; | ||
return bptOut; | ||
// If the invariant didn't increase for any reason, we simply don't mint BPT |
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.
Is using multiple returns more gas efficient, or do you just think it's clearer?
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.
What is more gas efficient is not to declare the memory variable bptOut
, that is achieved by using multiple returns. Besides, it keeps consistency with the same logic found in StableMath
.
@@ -212,16 +224,21 @@ library WeightedMath { | |||
|
|||
uint256 amountInWithoutFee; | |||
{ | |||
// Round down to ultimately reduce the ratios with fees, | |||
// which will be used later upon calculating the `nonTaxableAmount` |
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.
// which will be used later upon calculating the `nonTaxableAmount` | |
// which will be used later when calculating the `nonTaxableAmount` for each token. |
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.
In this case, the nonTaxableAmount
is calculated later only for the token in.
uint256 amountOutWithFee; | ||
if (invariantRatioWithoutFees > balanceRatiosWithoutFee[i]) { | ||
uint256 nonTaxableAmount = balances[i].mulDown(invariantRatioWithoutFees.complement()); | ||
// Round accordingly to ultimately enlarge the `amountOutWithFee`, consequently reducing the | ||
// `balanceRatio`; we prioritize incrementing the `nonTaxableAmount` over the `taxableAmount`. |
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.
// `balanceRatio`; we prioritize incrementing the `nonTaxableAmount` over the `taxableAmount`. | |
// `balanceRatio`; we prioritize maximizing the `nonTaxableAmount`. |
As in StableMath, would be good to explain the reasoning here.
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.
See #487 (comment).
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.
It's probably not easy to make these tests, but I hesitate to change anything in our core math libraries without some assurance via testing that they're behaving as we expect. (In the ambiguous cases, we're betting that other "swiss cheese layers," such as adding/subtracting buffer wei to final results, will protect us when the rounding direction isn't clear.) The tests (BasePool, Weighted, and Stable) could be done in a follow-up PR if that's easier. |
Thanks for the feedback @joaobrunoah and @EndymionJkb! I do agree that properly testing the rounding direction would be beneficial for the health of the codebase. Therefore, I'll be tackling #505 in an upcoming PR. |
Sounds good. You can resolve the comment things above so it's easier to see when it's complete, but as with the others, these should be fine once updated and CI passes, pending final validation with tests, per #505. |
Description
After thoroughly reviewing the
WeightedMath
library in search of rounding inconsistencies, I've found some cases in which multiplications (mulUp
/mulDown
) or divisions (divUp
/divDown
) weren't favorable to the protocol.For reference,
BasePoolMath
has undergone such review in #468, andStableMath
in #487.Type of change
Checklist:
main
, or there's a description of how to mergeIssue Resolution
Closes #488.