DeFiHardhat
35,000 USDC
View results
Submission Details
Severity: low
Invalid

Precision loss in `remainingRecapitalization()`

Summary

The potential precision loss in the provided remainingRecapitalization() function occurs due to the intermediate division operation.

Vulnerability Details

function remainingRecapitalization()
internal
view
returns (uint256 remaining)
{
AppStorage storage s = LibAppStorage.diamondStorage();
uint256 totalDollars = C
.dollarPerUnripeLP()
.mul(C.unripeLP().totalSupply())
.div(DECIMALS);
totalDollars = totalDollars / 1e6 * 1e6; // round down to nearest USDC
if (s.recapitalized >= totalDollars) return 0;
return totalDollars.sub(s.recapitalized);
}

Here:

totalDollars = totalDollars / 1e6 * 1e6;

This division and multiplication by 1e6 aims to round down totalDollars to the nearest USDC. However, this operation could result in precision loss due to truncation during the division process.

Impact

This imprecision could result in wrong calculations such as calculation of how many new Deposited Beans will be minted in addUnderlying() where remainingRecapitalization() is used i.e:

// Calculate how many new Deposited Beans will be minted
uint256 percentToFill = usdAmount.mul(C.precision()).div(
remainingRecapitalization() // @audit Above imprecision introduced here
);

Tools Used

Manual Review

Recommendations

function remainingRecapitalization() internal view returns (uint256 remaining) {
AppStorage storage s = LibAppStorage.diamondStorage();
uint256 totalDollars = C.dollarPerUnripeLP().mul(C.unripeLP().totalSupply()).div(DECIMALS);
// Round down totalDollars to the nearest USDC precision
totalDollars = (totalDollars * 1e6) / 1e6; // @audit-info Correct operation
// Check if recapitalized amount exceeds or equals total dollars, if yes, remaining is 0
if (s.recapitalized >= totalDollars)
return 0;
else
return totalDollars - s.recapitalized;
}

This way, the multiplication operation is carried out first, followed by the division, maintaining precision.

Updates

Lead Judging Commences

giovannidisiena Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Design choice
Assigned finding tags:

Precision loss

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.