Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: medium
Invalid

Unnecessary Estimation of USDC Needed when vault is in credit

Summary

In the function CreditDelegationBranch::settleVaultsDebt, when the vault is in credit, the protocol only needs to swap the excess USDC to asset. However, the current approach involves estimating the amount of USDC needed to obtain the required amount of the vault's asset, which leads to unnecessary swaps and potential deficits in the vault's USDC balance.

Root Cause

The problematic code snippet is as follows:

https://github.com/Cyfrin/2025-01-zaros-part-2/blob/35deb3e92b2a32cd304bf61d27e6071ef36e446d/src/market-making/branches/CreditDelegationBranch.sol#L483

// Get swap amount; both input and output in native precision
ctx.usdcIn = calculateSwapAmount(
dexSwapStrategy.dexAdapter,
ctx.vaultAsset,
ctx.usdc,
usdcCollateralConfig.convertSd59x18ToTokenAmount(ctx.vaultUnsettledRealizedDebtUsdX18.abs())
);
ctx.usdcIn = (ctx.usdcIn <= ctx.vaultUsdcBalance) ? ctx.usdcIn : ctx.vaultUsdcBalance;

Example Scenario:

  • Debt: 100.000 USDC

  • USDC Balance: 120.000 USDC

  • Excess USDC (in credit): 120.000 - 100.000 = 20.000 USDC

In this case, the protocol only needs to swap the excess 20.000 USDC into the vault asset. However, the protocol is currently performing the following:

  1. Convert 20.000 USDC to vault asset (x).

  2. Estimate the amount of USDC (y) required by the DEX to get x vault asset.

  3. Swap y USDC to get the vault asset.

The Problem:

The amount of USDC needed (y) can be more than the 20.000 USDC originally available. For example, due to the DEX’s behavior, the calculated amount y might be 20.020 USDC, which is more than the 20.000 USDC the protocol originally wanted to swap. As a result, the protocol will end up with a deficit of 20 USDC.

Impact

The current approach of estimating the amount of USDC needed for swaps in the settleVaultsDebt function introduces several issues, including potential deficits in the vault's USDC balance, inefficient use of resources from unnecessary swaps. Additionally, the added complexity of estimation increases the risk of errors and unpredictability in the debt settlement process, making the protocol harder to maintain and less reliable.

Proposed Solution

Instead of estimating the USDC required and potentially swapping more than the excess 20.000 USDC, the protocol should directly swap the exact 20.000 USDC to obtain the necessary vault asset. This approach eliminates any risk of excess swap amounts.

The Correct Process:

  1. Directly swap 20.000 USDC to the vault asset (which is exactly what is needed to cover the debt).

  2. Avoid unnecessary estimation or excess swaps.

By directly swapping the exact amount of excess USDC to the vault asset, the protocol ensures it doesn't end up in a deficit.

Updates

Lead Judging Commences

inallhonesty Lead Judge 4 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Appeal created

oxelmiguel Submitter
4 months ago
inallhonesty Lead Judge
4 months ago
inallhonesty Lead Judge 4 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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