Open code423n4 opened 1 year ago
approxPrice isn't used to show the price + we want the updated price after the stake in the event
This is correct. I added this case in the MR for M-01, which originally talked about "division before multiplication" issues: https://github.com/code-423n4/2023-05-asymmetry-mitigation-findings/issues/42
Picodes marked the issue as primary issue
Picodes changed the severity to QA (Quality Assurance)
This previously downgraded issue has been upgraded by Picodes
Rounding loss in and with approxPrice()
https://github.com/asymmetryfinance/smart-contracts/blob/ec582149ae9733eed6b11089cd92ca72ee5425d6/contracts/SafEth/SafEth.sol#L87-L119 https://github.com/asymmetryfinance/smart-contracts/blob/ec582149ae9733eed6b11089cd92ca72ee5425d6/contracts/SafEth/SafEth.sol#L359-L373
Description
SafEth.approxPrice()
contains a rounding loss of the forma/k + b/k <= (a + b)/k
which can be refactored as follows:But even with this refactoring, in
stake()
we have the linemintedAmount = (totalStakeValueEth * 1e18) / preDepositPrice;
wherepreDepositPrice = approxPrice()
, so this suffers a rounding loss of the forma/(b/c) >= a*c/b
. We would want to refactor this line tomintedAmount = (totalStakeValueEth * 1e18 * safEthTotalSupply) / underlyingValue;
.We have another case of
a/k + b/k <= (a + b)/k
instake()
:So we can do the same here and defer the division by
1e18
to after the summation, which gives usRecommendation
Do the above refactoring in
approxPrice()
. This function is still needed to estimate_minOut
. Note thatapproxPrice()
is calculated anew in the emitted event at the end ofstake()
. This means that this was not the price paid for the stake just made, but the price to pay for the next stake. It seems more appropriate to emit the price just paid, and this would also save gas by just reusing the already calculated price.As for
stake()
, the rounding loss would have to be eliminated by inlining the calculation. Note that the two for-loops may also be combined. Here is a complete refactoring:where, following the discussion above, the last part may be replaced with