The Standard

The Standard
DeFiHardhat
20,000 USDC
View results
Submission Details
Severity: medium
Valid

Inconsistency in Price Calculation Leading to Inaccurate Collateral Swaps

Summary:

Users can create Smart Vaults to deposit collateral and borrow EURO stablecoins. They also have the option to swap collateral types, as long as the return amount from the swap keeps the position at a healthy ratio. This is enforced by the calculateMinimumAmountOut function, however there is a problem in the way it retrieves its prices which could lead inaccurate amounts.

Vulnerability Details:

The calculateMinimumAmountOut function calculates collateralValueMinusSwapValue with the following formula:

uint256 collateralValueMinusSwapValue =
euroCollateral() - calculator.tokenToEur(getToken(_inTokenSymbol), _amount);

To calculate this, the function initially assesses the total value of the user's collateral in euros. This is done using the tokenToEurAvg method from the calculator contract, which calculates an average based on the last four prices of the token:

function euroCollateral() private view returns (uint256 euros) {
ITokenManager.Token[] memory acceptedTokens = getTokenManager().getAcceptedTokens();
for (uint256 i = 0; i < acceptedTokens.length; i++) {
ITokenManager.Token memory token = acceptedTokens[i];
euros += calculator.tokenToEurAvg(token, getAssetBalance(token.symbol, token.addr));
}
}

However, when subtracting the value of the token being swapped from the total collateral, the formula employs tokenToEur, which provides the latest price of the token, rather than an average. This results in a potential mismatch in the valuation method used within the same calculation:

Scenario illustration:

  • User A swaps their entire collateral of 1 ETH for USD.

  • The latest ETH price experiences a sharp decline (e.g., 200, 500, 501, 502).

  • The average price (tokenToEurAvg) would be significantly higher than the latest price (tokenToEur at 200).

The potential inflation or deflation of the calculateMinimumAmountOut value due to inconsistent pricing methods can significantly impact the swap process. This value, used as amountOutMinimum in swaps, can lead to inaccuracies in collateral value assessments. In the worst case, it could lead to the user's position becoming undercollateralized, potentially triggering an unwarranted liquidation.

function swap(bytes32 _inToken, bytes32 _outToken, uint256 _amount) external onlyOwner {
...
uint256 minimumAmountOut = calculateMinimumAmountOut(_inToken, _outToken, _amount);
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
...
amountOutMinimum: minimumAmountOut,
...
}

Impact

This inconsistency in price calculations can result in an inaccurate evaluation of swaps, affecting the collateral ratios and potentially leading to liquidations.

Tools Used:

Manual analysis

Recommendation:

The protocol should standardize the price retrieval method in the calculateMinimumAmountOut function to ensure consistency. Either both components of the formula should use the average price or the latest price to maintain accuracy in collateral swap calculations.

Updates

Lead Judging Commences

hrishibhat Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Slippage-issue

0xCiphky Submitter
over 1 year ago
hrishibhat Lead Judge
over 1 year ago
hrishibhat Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

avg-spot-price

Support

FAQs

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