sherlock-audit / 2023-11-covalent-judging

3 stars 2 forks source link

cheatcode - Accumulated Discrepancies from Repeated Rounding in Token Conversion #114

Closed sherlock-admin closed 7 months ago

sherlock-admin commented 7 months ago

cheatcode

medium

Accumulated Discrepancies from Repeated Rounding in Token Conversion

Summary

The OperationalStaking contract contains conversion functions like _sharesToTokens and _tokensToShares that are vulnerable to rounding errors leading to balance discrepancies over time.

Vulnerability Detail

The logic uses fixed-point arithmetic to convert between shares and tokens. However, the use of the DIVIDER constant and rounding down can introduce minor discrepancies that accumulate through repeated transactions.

Impact

Users may notice inconsistent staked balances and rewards, potentially losing trust. Large unaccounted rounding errors may also represent a monetary loss for users.

Code Snippet

https://github.com/sherlock-audit/2023-11-covalent/blob/main/cqt-staking/contracts/OperationalStaking.sol#L386C5-L396C1

function _sharesToTokens(uint128 sharesN, uint128 rate) internal pure returns (uint128) {

  return uint128((uint256(sharesN) * uint256(rate)) / DIVIDER); 

}

function _tokensToShares(uint128 amount, uint128 rate) internal pure returns (uint128) {

  return uint128((uint256(amount) * DIVIDER) / uint256(rate));

}

Tool Used

Manual review

Recommendation

Higher precision constants

Duplicate of #39