Skip to content
Business logic Audit finding Intermediate

RiskEngine::_getRequiredCollateralAtTickSinglePosition() fails to accumulate credits across multiple legs, leading to potential erroneous liquidations

The function that calculates required collateral and credits for a position overwrites the credits variable on each qualifying leg instead of accumulating it, so only the last leg's credit is counted. This undercounts a user's margin and can cause a solvent account to be wrongly liquidated.

joicygiore Follow with 0xhunter20, 0xMirce and 22 others

Originally disclosed on Code4rena

Submitted by joicygiore, also found by 0xhunter20, 0xMirce, 0xnija, agent001, Agontuk, Coachmike, Diavolo, edoscoba, eightzerofour, eternal1328, ewah, forlz, Funen, Hajime, Hakeem_is_here, happykilling, hecker_trieu_tien, Incogknito, kimnoic, lamassu, legat, NexusAudits, sahuang, and VulSight

RiskEngine.sol L1324-L1334

Finding description

The function RiskEngine::_getRequiredCollateralAtTickSinglePosition() is responsible for calculating both the required collateral (tokenRequired) and the user’s applicable credit amount (credits) for a single position at a given tick. These values are critical inputs to the protocol’s solvency and liquidation logic.

When a tokenId contains multiple legs, and more than one leg satisfies the following condition:

  • The function computes a credit amount for each qualifying leg. However, instead of accumulating these values, the implementation overwrites the credits variable on each iteration:
    function _getRequiredCollateralAtTickSinglePosition(
        TokenId tokenId,
        uint128 positionSize,
        int24 atTick,
        int16 poolUtilization,
        bool underlyingIsToken0
    ) internal view returns (uint256 tokenRequired, uint256 credits) {
        uint256 numLegs = tokenId.countLegs();

        unchecked {
            for (uint256 index = 0; index < numLegs; ++index) {
                // bypass the collateral calculation if tokenType doesn't match the requested token (underlyingIsToken0)
                if (tokenId.tokenType(index) != (underlyingIsToken0 ? 0 : 1)) continue;

@>                if (tokenId.width(index) == 0 && tokenId.isLong(index) == 1) {
                    LeftRightUnsigned amountsMoved = PanopticMath.getAmountsMoved(
                        tokenId,
                        positionSize,
                        index,
                        false
                    );
@>                    credits = tokenId.tokenType(index) == 0
                        ? amountsMoved.rightSlot()
                        : amountsMoved.leftSlot();
                }
                // Increment the tokenRequired accumulator
                tokenRequired += _getRequiredCollateralSingleLeg(
                    tokenId,
                    index,
                    positionSize,
                    atTick,
                    poolUtilization
                );
            }
        }
    }

As a result, only the credit contribution from the last matching leg is preserved, while all prior credits are silently discarded.

Impact

Because credits is undercounted, the RiskEngine underestimates the user’s effective margin when evaluating solvency.

This incorrect value propagates through the following critical call path:

isAccountSolvent()
 → _getMargin()
   → _getTotalRequiredCollateral()
     → _getRequiredCollateralAtTickSinglePosition()

Consequently, an account that is actually solvent may be incorrectly classified as insolvent, leading to unexpected and unjustified liquidations.

Recommended mitigation steps

  1. Treat credits as an accumulator and sum the credit contributions from all qualifying legs, rather than overwriting the value.

  2. Clearly distinguish between:

    • Per-leg credit contributions
    • The total credit amount for a multi-leg position
  3. Add unit tests covering multi-leg scenarios, including:

    • Multiple width == 0 && isLong == true legs
    • Mixed tokenType configurations
  4. (Optional) Update inline documentation or comments to clearly define the expected semantics of credits, reducing the risk of future regressions.

Proof of Concept

Please add the following test content to a new file under test/foundry/core/ (e.g., test/foundry/core/poc_getRequiredCollateralAtTickSinglePosition.t.sol) and execute it:

View detailed Proof of Concept

Output:

Ran 2 tests for test/foundry/core/poc_getRequiredCollateralAtTickSinglePosition.t.sol:Poc
[PASS] test__getRequiredCollateralAtTickSinglePosition_one() (gas: 7827689)
[PASS] test__getRequiredCollateralAtTickSinglePosition_two() (gas: 7493324)

Panoptic mitigated:

Combine credit amounts for multileg tokenIds.

Status: Mitigation confirmed. Full details in reports from Valves, edoscoba, and Nyx.


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

The credits variable is assigned rather than accumulated inside the loop over legs.

Impact

A solvent account may be incorrectly classified as insolvent, leading to unexpected and unjustified liquidations.

What the attacker needed

A position with multiple legs; more than one leg qualifies for credit

The fix

The credits should be summed across all qualifying legs instead of overwritten; the project mitigated this by combining credit amounts for multileg tokenIds.

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.

In a smart contract, a position can be made of several parts called legs. The code is meant to add up a credit value from each leg, but it replaces the total each time instead of adding to it, so earlier credits are lost. That makes the system think the user has less collateral than they really do, which could lead to an unfair liquidation.

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 joicygiore

View all

11 more disclosed reports, best first.

More from Panoptic: Next Core

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.