Skip to content
Business logic Audit finding Intermediate

DnGmxJuniorVaultManager#_totalAssets current implementation doesn't properly maximize or minimize

The _totalAssets function in DnGmxJuniorVaultManager incorrectly handles the maximize parameter, sometimes returning a lower value when maximize is true than when it is false. This could lead to inaccurate net asset value (NAV) calculations, affecting critical parameters.

0x52 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-rage-trade-judging/issues/36

Found by

0x52

Summary

The maximize input to DnGmxJuniorVaultManager#_totalAssets indicates whether to either maximize or minimize the NAV. Internal logic of the function doesn't accurately reflect that because under some circumstances, maximize = true actually returns a lower value than maximize = false.

Vulnerability Detail

    uint256 unhedgedGlp = (state.unhedgedGlpInUsdc + dnUsdcDepositedPos).mulDivDown(
        PRICE_PRECISION,
        _getGlpPrice(state, !maximize)
    );

    // calculate current borrow amounts
    (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);
    uint256 totalCurrentBorrowValue = _getBorrowValue(state, currentBtc, currentEth);

    // add negative part to current borrow value which will be subtracted at the end
    // convert usdc amount into glp amount
    uint256 borrowValueGlp = (totalCurrentBorrowValue + dnUsdcDepositedNeg).mulDivDown(
        PRICE_PRECISION,
        _getGlpPrice(state, !maximize)
    );

    // if we need to minimize then add additional slippage
    if (!maximize) unhedgedGlp = unhedgedGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
    if (!maximize) borrowValueGlp = borrowValueGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);

To maximize the estimate for the NAV of the vault underlying debt should minimized and value of held assets should be maximized. Under the current settings there is a mix of both of those and the function doesn't consistently minimize or maximize. Consider when NAV is "maxmized". Under this scenario the value of when estimated the GlpPrice is minimized. This minimizes the value of both the borrowedGlp (debt) and of the unhedgedGlp (assets). The result is that the NAV is not maximized because the value of the assets are also minimized. In this scenario the GlpPrice should be maximized when calculating the assets and minimized when calculating the debt. The reverse should be true when minimizing the NAV. Slippage requirements are also applied incorrectly when adjusting borrowValueGlp. The current implementation implies that if the debt were to be paid back that the vault would repay their debt for less than expected. When paying back debt the slippage should imply paying more than expected rather than less, therefore the slippage should be added rather than subtracted.

Impact

DnGmxJuniorVaultManager#_totalAssets doesn't accurately reflect NAV. Since this is used when determining critical parameters it may lead to inaccuracies.

Code Snippet

https://github.com/sherlock-audit/2022-10-rage-trade/blob/main/dn-gmx-vaults/contracts/libraries/DnGmxJuniorVaultManager.sol#L1013-L1052

Tool used

Manual Review

Recommendation

To properly maximize the it should assume the best possible rate for exchanging it's assets. Likewise to minimize it should assume it's debt is a large as possible and this it encounters maximum possible slippage when repaying it's debt. I recommend the following changes:

    uint256 unhedgedGlp = (state.unhedgedGlpInUsdc + dnUsdcDepositedPos).mulDivDown(
        PRICE_PRECISION,
-       _getGlpPrice(state, !maximize)
+       _getGlpPrice(state, maximize)
    );

    // calculate current borrow amounts
    (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);
    uint256 totalCurrentBorrowValue = _getBorrowValue(state, currentBtc, currentEth);

    // add negative part to current borrow value which will be subtracted at the end
    // convert usdc amount into glp amount
    uint256 borrowValueGlp = (totalCurrentBorrowValue + dnUsdcDepositedNeg).mulDivDown(
        PRICE_PRECISION,
        _getGlpPrice(state, !maximize)
    );

    // if we need to minimize then add additional slippage
    if (!maximize) unhedgedGlp = unhedgedGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
-   if (!maximize) borrowValueGlp = borrowValueGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
+   if (!maximize) borrowValueGlp = borrowValueGlp.mulDivDown(MAX_BPS + state.slippageThresholdGmxBps, MAX_BPS);

Discussion

0xDosa

Dividing with minimum price would maximize the asset/borrow amount and vice versa. So the correct fix should be this. @0x00052 could you confirm?

     uint256 unhedgedGlp = (state.unhedgedGlpInUsdc + dnUsdcDepositedPos).mulDivDown(
        PRICE_PRECISION,
       _getGlpPrice(state, !maximize)
    );

    // calculate current borrow amounts
    (uint256 currentBtc, uint256 currentEth) = _getCurrentBorrows(state);
    uint256 totalCurrentBorrowValue = _getBorrowValue(state, currentBtc, currentEth);

    // add negative part to current borrow value which will be subtracted at the end
    // convert usdc amount into glp amount
    uint256 borrowValueGlp = (totalCurrentBorrowValue + dnUsdcDepositedNeg).mulDivDown(
        PRICE_PRECISION,
-       _getGlpPrice(state, !maximize)
+       _getGlpPrice(state, maximize)

    );

    // if we need to minimize then add additional slippage
    if (!maximize) unhedgedGlp = unhedgedGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
-   if (!maximize) borrowValueGlp = borrowValueGlp.mulDivDown(MAX_BPS - state.slippageThresholdGmxBps, MAX_BPS);
+   if (!maximize) borrowValueGlp = borrowValueGlp.mulDivDown(MAX_BPS + state.slippageThresholdGmxBps, MAX_BPS);

0x00052

Good catch! You're right, I got that backwards.

0xDosa

Fix PR: https://github.com/RageTrade/delta-neutral-gmx-vaults/pull/35

0x00052

Fix looks good. Debt/asset value are now properly minimized and slippage is applied in the proper direction for debt

The original report as published on Sherlock, by 0x52. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the function uses the same price calculation for both assets and debt when maximizing or minimizing, and applies slippage in the wrong direction for debt, so it does not consistently maximize or minimize the net asset value.

Impact

The function does not accurately reflect the net asset value, which may lead to inaccuracies in critical parameters.

What the attacker needed

None

The fix

The fix was to use the maximize parameter correctly for the asset price and to add slippage to the debt value when minimizing, as confirmed in the discussion.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This report is about a smart contract function that is supposed to calculate the total value of a vault's assets, either giving the highest possible value (maximize) or the lowest (minimize). The function made a mistake: when asked for the highest value, it sometimes gave a lower number than when asked for the lowest. This could cause the vault to misjudge its value, which might affect decisions like how much to borrow or lend.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0x52

View all

211 more disclosed reports, best first.

More from Rage Trade

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.