sherlock-audit / 2023-06-Index-judging

6 stars 2 forks source link

0x52 - BoundedStepwiseExponentialPriceAdapter#getPrice uses incorrect order of operation when calculating priceChange #43

Closed sherlock-admin closed 1 year ago

sherlock-admin commented 1 year ago

0x52

high

BoundedStepwiseExponentialPriceAdapter#getPrice uses incorrect order of operation when calculating priceChange

Summary

BoundedStepwiseExponentialPriceAdapter#getPrice incorrectly applies the scaling factor leading to wildly incorrect pricing when scalingFactor is used.

Vulnerability Detail

BoundedStepwiseExponentialPriceAdapter.sol#L73

    uint256 priceChange = scalingFactor * expExpression - WAD;

For exponential pricing that utilizes the scaling factor, the above line is completely incorrect, since it will multiply before removing the base WAD from the expression.

Example:

Assume a scaling factor of 2. A time 0 our price should be the initial price however this isn't the case:

expWad(0) = 1e18

priceChange = 2 * 1e18 - 1e18 = 1e18

This lead to the price being wildly under the price that it should be selling the asset at a large loss.

Impact

Math error will cause assets to be sold a very low prices causing loss to the set token

Code Snippet

BoundedStepwiseExponentialPriceAdapter.sol#L28-L88

Tool used

Manual Review

Recommendation

Fix the order of operation:

-   uint256 priceChange = scalingFactor * expExpression - WAD;
+   uint256 priceChange = scalingFactor * (expExpression - WAD);

Duplicate of #39