sherlock-audit / 2024-05-elfi-protocol-judging

11 stars 7 forks source link

0xPwnd - Incorrect Conversion Functions Leading to Inaccurate Token and USD Calculations #290

Closed sherlock-admin3 closed 4 months ago

sherlock-admin3 commented 4 months ago

0xPwnd

High

Incorrect Conversion Functions Leading to Inaccurate Token and USD Calculations

Summary

The functions usdToToken and tokenToUsd in CalUtils.sol fail to accurately convert between USD and tokens due to incorrect handling of decimals. This inaccuracy can lead to financial discrepancies across various parts of the protocol that rely on these conversions.

Vulnerability Detail

Scenario: Converting USD to Token Context:

Scenario: Converting Token to USD Context:

Impact

-Incorrect conversion rates can cause users to receive incorrect token amounts or USD equivalents. -Multiple functions relying on these conversions may exhibit erratic behavior, potentially leading to financial losses or incorrect collateral/liquidation calculations.

Code Snippet

https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/utils/CalUtils.sol#L76-L78 https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/utils/CalUtils.sol#L92-L98

Tool used

Manual Review

Recommendation

modify the calculation formula to assure correct conversion to/from usd

creat3xai commented 4 months ago

Hello, is there a reason why this one was excluded the library is not in scope but it has a flaw in the conversion and it's used by multiple contracts in scope as per the rules this should be valid for verification and it should be in scope In case the vulnerability exists in a library and an in-scope contract uses it and is affected by this bug this is a valid issue. please someone check this and thank you.

0xELFi commented 4 months ago

In our system, we set the precision of the USD value to 18 decimal places, and the precision of token denominations does not support exceeding 18 decimal places. uint256 public constant USD_PRECISION = 10 ** 18;