Skip to content
Audit finding Intermediate

Inconsistency in CurvePoolUtil

Two of the three preview functions in CurvePoolUtil calculate expected withdrawal amounts with plain multiplication and division instead of the mulDiv helper used by the third. This can produce less precise results and slightly larger rounding errors when computing the amounts of IBT and PT returned for burning LP tokens.

mrMorningstar Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

#44083 [SC-Insight] Inconsistency in `CurvePoolUtil`

Submitted on Apr 16th 2025 at 18:50:25 UTC by @MrMorningstar for Audit Comp | Spectra Finance

Description

Brief/Intro

As we can see below the previewRemoveLiquidity, previewRemoveLiquidityNG and previewRemoveLiquiditySNG are very similiar and they have same purpose and that is to return expected amounts of IBT and PT withdrawn from curve pool:

    /**
     * @notice Returns the IBT and PT amounts received for burning a given amount of LP tokens
     * @notice Method to be used with legacy Curve Cryptoswap pools
     * @param _curvePool The address of the curve pool
     * @param _lpTokenAmount The amount of the lp token to burn
     * @return minAmounts The expected respective amounts of IBT and PT withdrawn from the curve pool
     */
    function previewRemoveLiquidity(
        address _curvePool,
        uint256 _lpTokenAmount
    ) external view returns (uint256[2] memory minAmounts) {
        address lpToken = ICurvePool(_curvePool).token();
        uint256 totalSupply = IERC20(lpToken).totalSupply();
        (uint256 ibtBalance, uint256 ptBalance) = _getCurvePoolBalances(_curvePool);
        // decrement following what Curve is doing
        if (_lpTokenAmount > APPROXIMATION_DECREMENT && totalSupply != 0) {
            _lpTokenAmount -= APPROXIMATION_DECREMENT;
            minAmounts = [
                (ibtBalance * _lpTokenAmount) / totalSupply,
                (ptBalance * _lpTokenAmount) / totalSupply
            ];
        } else {
            minAmounts = [uint256(0), uint256(0)];
        }
    }

    /**
     * @notice Returns the IBT and PT amounts received for burning a given amount of LP tokens
     * @notice Method to be used with Curve Cryptoswap NG pools
     * @param _curvePool The address of the curve pool
     * @param _lpTokenAmount The amount of the lp token to burn
     * @return minAmounts The expected respective amounts of IBT and PT withdrawn from the curve pool
     */
    function previewRemoveLiquidityNG(
        address _curvePool,
        uint256 _lpTokenAmount
    ) external view returns (uint256[2] memory minAmounts) {
        uint256 totalSupply = ICurveNGPool(_curvePool).totalSupply();
        (uint256 ibtBalance, uint256 ptBalance) = _getCurvePoolBalances(_curvePool);
        // reproduces Curve implementation
        if (_lpTokenAmount == totalSupply) {
            minAmounts = [ibtBalance, ptBalance];
        } else if (_lpTokenAmount > APPROXIMATION_DECREMENT && totalSupply != 0) {
            _lpTokenAmount -= APPROXIMATION_DECREMENT;
            minAmounts = [
                ibtBalance.mulDiv(_lpTokenAmount, totalSupply),
                ptBalance.mulDiv(_lpTokenAmount, totalSupply)
            ];
        } else {
            minAmounts = [uint256(0), uint256(0)];
        }
    }

    /**
     * @notice Returns the IBT and PT amounts received for burning a given amount of LP tokens
     * @notice Method to be used with StableSwap NG pools
     * @param _curvePool The address of the curve pool
     * @param _lpTokenAmount The amount of the lp token to burn
     * @return minAmounts The expected respective amounts of IBT and PT withdrawn from the curve pool
     */
    function previewRemoveLiquiditySNG(
        address _curvePool,
        uint256 _lpTokenAmount
    ) external view returns (uint256[] memory) {
        uint256 totalSupply = IERC20(_curvePool).totalSupply();
        (uint256 ibtBalance, uint256 ptBalance) = _getCurvePoolBalances(_curvePool);
        // decrement following what Curve is doing
        uint256[] memory minAmounts = new uint256[](2);
        if (_lpTokenAmount > APPROXIMATION_DECREMENT && totalSupply != 0) {
            _lpTokenAmount -= APPROXIMATION_DECREMENT;
            minAmounts[0] = (ibtBalance * _lpTokenAmount) / totalSupply;
            minAmounts[1] = (ptBalance * _lpTokenAmount) / totalSupply;
        } else {
            minAmounts[0] = 0;
            minAmounts[1] = 0;
        }
        return minAmounts;
    }

However only previewRemoveLiquidityNG use mulDiv function when calculating min amounts while other two don't.

It is consider best practice to use mulDiv as it is more secure and more precise when doing calculations as it improve accuracy because it reduce rounding errors by combining the operations more precisely, especially when working with large numbers.

Recommendation

Use mulDiv when calculating minAmounts in previewRemoveLiquiditySNG and previewRemoveLiquidity

Proof of Concept

Proof of Concept

PoC not needed as this is Insight under Security best practices and Code Optimizations and Enhancements category.

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

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 two older preview functions were written before the mulDiv helper was adopted, so they still use plain multiplication and division instead of the more precise mulDiv.

Impact

The estimates returned by these view functions may be slightly less precise due to rounding, though the text does not claim any direct loss of funds.

What the attacker needed

None

The fix

The two functions should be changed to use mulDiv when calculating minAmounts, matching the NG variant.

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.

A smart contract is a program that runs on a blockchain. When someone burns pool tokens, the contract estimates how much of two other tokens they should get back. Two of these estimate functions do the maths in a simpler way than the third, which can make the numbers slightly less accurate.

More by mrMorningstar

View all

6 more disclosed reports, best first.

More from Spectra Finance

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.