sherlock-audit / 2023-06-Index-judging

6 stars 2 forks source link

Oxhunter526 - Precision Loss in `getPrice` Function #49

Closed sherlock-admin closed 1 year ago

sherlock-admin commented 1 year ago

Oxhunter526

medium

Precision Loss in getPrice Function

Summary

The getPrice function in the provided code snippet contains an order of operations issue that can lead to precision loss. The problem arises from the sequence of division and multiplication operations used in the calculation, which can result in inaccurate values due to truncation.

Vulnerability Detail

uint256 bucket = _timeElapsed / bucketSize;
uint256 priceChange = bucket * slope;

The bucket variable is calculated by dividing _timeElapsed by bucketSize, which performs an integer division operation in Solidity. If there is a remainder from the division, it is truncated, potentially leading to precision loss. Subsequently, the priceChange variable is calculated by multiplying bucket with slope. However, since bucket may have undergone truncation, the multiplication can introduce further precision loss.

Impact

Inaccurate price calculations in the getPrice function.

Code Snippet

Code Snippet

Tool used

Manual Review

Recommendation

Integrate decimal arithmetic into the codebase, the getPrice function will produce accurate results, maintaining the necessary precision for reliable price calculations.