Skip to content
Business logic Audit finding Advanced

Pool._amountToBin() returns a wrong value when protocolFeeRatio = 100% .

The internal function that converts an amount into a bin balance adds one extra unit to the protocol fee, so when the protocol fee ratio is set to 100% the bin balance is updated with a value larger than it should be. This could cause bin balances to be calculated incorrectly.

hansfriese Follow

Originally disclosed on Code4rena

Submitted by hansfriese

Pool._amountToBin() returns a larger value than it should when protocolFeeRatio = 100%.

As a result, bin balances might be calculated incorrectly.

Proof of Concept

delta.deltaInBinInternal is used to update the bin balances like this (Pool.sol#L287-L293).

    if (tokenAIn) {
        binBalanceA += delta.deltaInBinInternal.toUint128();
        binBalanceB = Math.clip128(binBalanceB, delta.deltaOutErc.toUint128());
    } else {
        binBalanceB += delta.deltaInBinInternal.toUint128();
        binBalanceA = Math.clip128(binBalanceA, delta.deltaOutErc.toUint128());
    }

As we can see here (Pool.sol#L608-L611), _amountToBin() is used to calculatedelta.deltaInBinInternal from deltaInErc and feeBasis.

    uint256 feeBasis = Math.mulDiv(binAmountIn, fee, PRBMathUD60x18.SCALE - fee, true);
    delta.deltaInErc = binAmountIn + feeBasis;
    delta.deltaInBinInternal = _amountToBin(delta.deltaInErc, feeBasis);
    delta.excess = swapped ? Math.clip(amountOut, delta.deltaOutErc) : 0;

With the above code, the protocol fee should be a portion of feeBasis and it is the same as feeBasis when state.protocolFeeRatio = ONE_3_DECIMAL_SCALE.

But when we check _amountToBin(), the actual protocol fee will be feeBasis + 1 and delta.deltaInBinInternal will be less than its possible smallest value(= binAmountIn).

    function _amountToBin(uint256 deltaInErc, uint256 feeBasis) internal view returns (uint256 amount) {
        amount = state.protocolFeeRatio != 0 ? Math.clip(deltaInErc, feeBasis.mul(uint256(state.protocolFeeRatio) * PROTOCOL_FEE_SCALE) + 1) : deltaInErc;
    }

Recommended Mitigation Steps

We should modify _amountToBin() like below.

    function _amountToBin(uint256 deltaInErc, uint256 feeBasis) internal view returns (uint256 amount) {
        if (state.protocolFeeRatio == ONE_3_DECIMAL_SCALE)
            return deltaInErc - feeBasis;

        amount = state.protocolFeeRatio != 0 ? Math.clip(deltaInErc, feeBasis.mul(uint256(state.protocolFeeRatio) * PROTOCOL_FEE_SCALE) + 1) : deltaInErc;
    }

gte620v (Maverick) acknowledged


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

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 an off-by-one in the fee calculation: the code adds 1 to the computed protocol fee and clips the result, so at a 100% fee ratio the bin amount ends up one unit above the intended value.

Impact

Bin balances could be calculated incorrectly, which may leave the pool's internal accounting inconsistent with the real token amounts.

What the attacker needed

ProtocolFeeRatio set to 100%

The fix

The report recommends special-casing a 100% protocol fee ratio so the function returns the deposit minus the fee basis instead of applying the clipped calculation.

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 pool tracks how much of each token sits in each 'bin' (a small bucket of funds). A helper function works out how much of a deposit belongs in the bin after the protocol's cut is taken. Because of an off-by-one in that helper, the bin is credited with slightly too much when the protocol takes the whole fee, so the recorded balances drift from reality.

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 hansfriese

View all

65 more disclosed reports, best first.

More from Maverick contest

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.