Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: low
Valid

`fulfillSwap` and `initiateSwap` should execute recalculateVaultsCreditCapacity

Summary

The fulfillSwap function is designed to convert a specified USD amount into a collateral asset. However, the underlying logic in getAmountOfAssetOut, which calculates the discount for the swap, relies on potentially stale data. This is because the recalculateVaultsCreditCapacity function is not invoked before the calculation, leading to incorrect results.

Vulnerability Details

The vulnerability stems from the following code:

UD60x18 vaultAssetsUsdX18 = ud60x18(IERC4626(vault.indexToken).totalAssets()).mul(indexPriceX18); // audit bug
if (vaultAssetsUsdX18.isZero()) revert Errors.InsufficientVaultBalance(vaultId, 0, 0);
// We use the vault's net sum of all debt types coming from its connected markets to determine the swap rate
SD59x18 vaultDebtUsdX18 = vault.getTotalDebt();
// Calculate the premium or discount that may be applied to the vault asset's index price
// Note: If no premium or discount needs to be applied, the premiumDiscountFactorX18 will be 1e18 (UD60x18 one value)
UD60x18 premiumDiscountFactorX18 =
UsdTokenSwapConfig.load().getPremiumDiscountFactor(vaultAssetsUsdX18, vaultDebtUsdX18);
// Get amounts out, taking into consideration the CL price and the premium/discount
amountOutX18 = usdAmountInX18.div(indexPriceX18).mul(premiumDiscountFactorX18);

The issue lies in the fact that getPremiumDiscountFactor relies on vaultAssetsUsdX18 and vaultDebtUsdX18 to compute the premium or discount. However, these values may be stale if recalculateVaultsCreditCapacity is not called beforehand. This can lead to incorrect calculations of the premiumDiscountFactorX18, as the vault's debt and asset values may not reflect the current state.

Impact

The failure to invoke recalculateVaultsCreditCapacity before computing the premiumDiscountFactorX18 can result in incorrect premium or discount values. This is particularly problematic in scenarios where the vault has excessive debt or credit. If the vault's debt is understated due to stale data, a lower premium factor may be applied, leading to an unfavorable swap rate. This could harm the vault by allowing more assets to be swapped than intended, potentially resulting in financial losses.

Tools Used

Manual code review.

Recommendations

To address this issue, refactor the code to ensure that recalculateVaultsCreditCapacity is called before calculating the premiumDiscountFactorX18. Here’s an example of how the code can be updated:

ctx.vaultId = request.vaultId;
Vault.Data storage vault = Vault.loadLive(ctx.vaultId);
+ recalculateVaultsCreditCapacity(vaultId);

Same fix should be added to the initiateSwap, so the minAmount check to be applied correctly

Updates

Lead Judging Commences

inallhonesty Lead Judge 3 months ago
Submission Judgement Published
Validated
Assigned finding tags:

initiateSwap should call `recalculateVaultsCreditCapacity`

fulfillSwap should call recalculateVaultsCreditCapacity

Support

FAQs

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