Skip to content
Business logic Audit finding Intermediate

Reinvest will return sub-optimal return if the pool is imbalanced

The reinvestReward function enforces a proportional deposit of primary and secondary tokens, which is suboptimal when a Curve pool is imbalanced. This can lead to fewer LP tokens received due to deposit penalties and missed bonuses, causing a loss of gain for vault shareholders.

xiaoming90 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-notional-judging/issues/9

Found by

xiaoming90

Summary

Reinvesting only allows proportional deposit. If the pool is imbalanced due to unexpected circumstances, performing a proportional deposit is not optimal. This result in fewer pool tokens in return due to sub-optimal trade, eventually leading to a loss of gain for the vault shareholder.

Vulnerability Detail

During reinvest rewards, the vault will ensure that the amount of primary and secondary tokens deposited is of the right proportion per the comment in Line 163 below.

https://github.com/sherlock-audit/2023-02-notional/blob/main/leveraged-vaults/contracts/vaults/curve/external/Curve2TokenConvexHelper.sol#L163

Solidity
File: Curve2TokenConvexHelper.sol
146:     function reinvestReward(
147:         Curve2TokenConvexStrategyContext calldata context,
148:         ReinvestRewardParams calldata params
149:     ) external {
..SNIP..
163:         // Make sure we are joining with the right proportion to minimize slippage
164:         poolContext._validateSpotPriceAndPairPrice({
165:             strategyContext: strategyContext,
166:             oraclePrice: poolContext.basePool._getOraclePairPrice(strategyContext),
167:             primaryAmount: primaryAmount,
168:             secondaryAmount: secondaryAmount
169:         });

The Curve2TokenConvexHelper.reinvestReward function will internally call the Curve2TokenPoolUtils._checkPrimarySecondaryRatio, which will check that the primary and secondary tokens deposited are of the right proportion.

https://github.com/sherlock-audit/2023-02-notional/blob/main/leveraged-vaults/contracts/vaults/curve/internal/pool/Curve2TokenPoolUtils.sol#L147

Solidity
File: Curve2TokenPoolUtils.sol
147:     function _checkPrimarySecondaryRatio(
148:         StrategyContext memory strategyContext,
149:         uint256 primaryAmount, 
150:         uint256 secondaryAmount, 
151:         uint256 primaryPoolBalance, 
152:         uint256 secondaryPoolBalance
153:     ) private pure {
154:         uint256 totalAmount = primaryAmount + secondaryAmount;
155:         uint256 totalPoolBalance = primaryPoolBalance + secondaryPoolBalance;
156: 
157:         uint256 primaryPercentage = primaryAmount * CurveConstants.CURVE_PRECISION / totalAmount;        
158:         uint256 expectedPrimaryPercentage = primaryPoolBalance * CurveConstants.CURVE_PRECISION / totalPoolBalance;
159: 
160:         strategyContext._checkPriceLimit(expectedPrimaryPercentage, primaryPercentage);
161: 
162:         uint256 secondaryPercentage = secondaryAmount * CurveConstants.CURVE_PRECISION / totalAmount;
163:         uint256 expectedSecondaryPercentage = secondaryPoolBalance * CurveConstants.CURVE_PRECISION / totalPoolBalance;
164: 
165:         strategyContext._checkPriceLimit(expectedSecondaryPercentage, secondaryPercentage);
166:     }

This concept of proportional join appears to be taken from the design of earlier Notional's Balancer leverage vaults. For Balancer Pools, it is recommended to join with all the pool's tokens in exact proportions to minimize the price impact of the join (Reference).

However, the concept of proportional join to minimize slippage does not always hold for Curve Pools as they operate differently.

A Curve pool is considered imbalanced when there is an imbalance between the assets within it. For instance, the Curve stETH/ETH pool is considered imbalanced if it has the following reserves:

  • ETH: 340,472.34 (31.70%)
  • stETH: 733,655.65 (68.30%)

If a Curve Pool is imbalanced, attempting to perform a proportional join will not give an optimal return (e.g. result in fewer Pool LP tokens received).

In Curve Pool, there are penalties/bonuses when depositing to a pool. The pools are always trying to balance themselves. If a deposit helps the pool to reach that desired balance, a deposit bonus will be given (receive extra tokens). On the other hand, if a deposit deviates from the pool from the desired balance, a deposit penalty will be applied (receive fewer tokens).

The following is the source code of add_liquidity function taken from https://github.com/curvefi/curve-contract/blob/master/contracts/pools/steth/StableSwapSTETH.vy. As shown below, the function attempts to calculate the difference between the ideal_balance and new_balances, and uses the difference as a factor of the fee computation, which is tied to the bonus and penalty.

Python
def add_liquidity(amounts: uint256[N_COINS], min_mint_amount: uint256) -> uint256:
..SNIP..
    if token_supply > 0:
        # Only account for fees if we are not the first to deposit
        fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
        admin_fee: uint256 = self.admin_fee
        for i in range(N_COINS):
            ideal_balance: uint256 = D1 * old_balances[i] / D0
            difference: uint256 = 0
            if ideal_balance > new_balances[i]:
                difference = ideal_balance - new_balances[i]
            else:
                difference = new_balances[i] - ideal_balance
            fees[i] = fee * difference / FEE_DENOMINATOR
            if admin_fee != 0:
                self.admin_balances[i] += fees[i] * admin_fee / FEE_DENOMINATOR
            new_balances[i] -= fees[i]
        D2 = self.get_D(new_balances, amp)
        mint_amount = token_supply * (D2 - D0) / D0
    else:
        mint_amount = D1  # Take the dust if there was any
..SNIP..

Following is the mathematical explanation of the penalties/bonuses extracted from Curve's Discord channel:

  • There is a “natural” amount of D increase that corresponds to a given total deposit amount; when the pool is perfectly balanced, this D increase is optimally achieved by a balanced deposit. Any other deposit proportions for the same total amount will give you less D.
  • However, when the pool is imbalanced, a balanced deposit is no longer optimal for the D increase.

Impact

There is no guarantee that a Curve Pool will always be balanced. Historically, there are multiple instances where the largest Curve pool (stETH/ETH) becomes imbalanced (Reference #1 and #2).

If the pool is imbalanced due to unexpected circumstances, performing a proportional deposit is not optimal, leading to the trade resulting in fewer tokens than possible due to the deposit penalty. In addition, the trade also misses out on the potential gain from the deposit bonus.

The side-effect is that reinvesting the reward tokens will result in fewer pool tokens in return due to sub-optimal trade, eventually leading to a loss of gain for the vault shareholder.

Code Snippet

https://github.com/sherlock-audit/2023-02-notional/blob/main/leveraged-vaults/contracts/vaults/curve/external/Curve2TokenConvexHelper.sol#L163

https://github.com/sherlock-audit/2023-02-notional/blob/main/leveraged-vaults/contracts/vaults/curve/internal/pool/Curve2TokenPoolUtils.sol#L147

Tool used

Manual Review

Recommendation

Consider removing the _checkPrimarySecondaryRatio function from the _validateSpotPriceAndPairPrice function to give the callers the option to deposit the reward tokens in a "non-proportional" manner if a Curve Pool becomes imbalanced so that the deposit penalty could be minimized or the deposit bonus can be exploited to increase the return.

Discussion

jeffywu

Valid, should get the optimal way to join via some off chain function for Curve pools. This is will work because we are using a permissioned reward reinvestment role.

The original report as published on Sherlock, by xiaoming90. 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

The code enforces proportional deposits via _checkPrimarySecondaryRatio, which is suboptimal for imbalanced Curve pools that reward balancing deposits.

Impact

Reinvesting rewards in an imbalanced pool results in fewer pool tokens due to deposit penalties and missed bonuses, leading to a loss of gain for vault shareholders.

What the attacker needed

Curve pool becomes imbalanced; reinvestReward is called

The fix

The _checkPrimarySecondaryRatio function should be removed from _validateSpotPriceAndPairPrice to allow non-proportional deposits when the pool is imbalanced.

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.

When you add money to a liquidity pool, the pool may give you a bonus if your deposit helps balance it, or a penalty if it makes it more unbalanced. The code always deposits in the same proportion as the pool's current balance, which is fine when the pool is balanced but not when it's imbalanced. So the vault may get fewer pool tokens than it could, meaning less profit for people who put money in the vault.

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 xiaoming90

View all

122 more disclosed reports, best first.

More from Notional Update #4

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.