Killua
← All parts Bug Hunting Field Guide · Part 1 of 8

Spot-price oracles: borrowing against a lie

Killua · 8 min read

A DEX spot price is just a pool’s current reserve ratio, and anyone can move it inside one transaction. If a protocol turns that number into borrowing power with no second opinion, the attacker sets their own credit limit.

This is one of the oldest bugs in DeFi, and it still shows up every month. This guide covers how it works, a real incident, how to spot it in a codebase, and the lookalikes that are actually safe.

The bug in one sentence

A lending or margin protocol values collateral from a live AMM quote, so an attacker with a flash loan can push the price up, deposit, borrow against the inflated value, unwind the price, and keep the difference.

The key distinction is between an oracle and an execution venue:

When a protocol uses the second answer as if it were the first, it has turned a pool anyone can trade against into an authority.

A real case: Sharwa Margin Trading (Arbitrum, May 2026, about $32.85K)

Sharwa was a margin trading and lending platform on Arbitrum. Users could deposit collateral and borrow USDC, WETH and WBTC. Unusually, it also accepted Hegic options NFTs as collateral.

To value an options NFT, Sharwa called a pluggable pricing module. The team had written two versions:

In production, the options collateral was wired to the unguarded module. Its whole valuation logic was this:

// One Uniswap V3 pool hop, fixed at construction: USDC.e --(0.05%)--> USDC.
path = abi.encodePacked(tokenInContract, poolFee, tokenOutContract);

function getInputPositionValue(uint256 amountIn) external returns (uint amountOut) {
    amountOut = quoter.quoteExactInput(path, amountIn); // spot only, no clamp
}

quoteExactInput simulates a swap against the pool’s current liquidity and returns the output. It reflects the instantaneous price, and Uniswap’s docs say the Quoter isn’t meant to be called on-chain. Because the path ran through a single, shallow USDC/USDC.e pool, whoever traded in that pool right before depositing controlled the collateral’s value.

Why a small trade moved the price so much

In a constant-product pool with reserves x and y:

invariant      : x · y = k
spot price     : P = y / x
price impact   : ΔP / P ≈ Δx / x

The price shift scales with the trade size relative to the pool’s depth. Concentrated liquidity in V3 makes this worse when the liquidity around the current price is thin.

The attacker flash-borrowed 22,000 USDC and swapped it into the pool for USDC.e. USDC.e became scarce in that pool and USDC became cheap. When Sharwa then asked what USDC.e was worth in USDC, the Quoter reported a hugely inflated number.

In a pool with $50M of liquidity, a $22K trade barely moves the price. In a pool with tens of thousands of dollars, it breaks the peg. Depth decides how cheap the lie is.

The attack, step by step (one transaction)

  1. Stage. The attacker sends the options NFT to their attack contract. The NFT receive callback kicks off the rest.
  2. Borrow. Flash-borrow 22,000 USDC from Balancer at zero fee.
  3. Pump. Swap it through the thin pool, pushing USDC.e far above its 1:1 peg.
  4. Deposit. Open a margin account and deposit the NFT. Sharwa prices it through the unguarded module and records an inflated credit limit.
  5. Borrow out. Borrow the maximum USDC, WETH and WBTC. The attacker routes some borrowed USDC back into the pool to keep the price up while borrowing the rest. About $54.85K leaves the protocol.
  6. Unwind and repay. Sell everything back to USDC through deep pools, repay the 22,000 USDC, and keep about $32.85K. Sharwa is left holding a nearly worthless NFT and bad debt.

Why it worked: three facts

  1. The spot price is controllable. A reserve ratio has no memory and no idea of fair value. Flash loans let anyone rent enough capital to move a shallow pool for one transaction.
  2. The protocol trusts the quote blindly. There’s no TWAP, no Chainlink cross-check, no deviation bound and no borrow cap.
  3. Settlement is atomic. The lie is created, recorded, spent and unwound in one transaction, so no arbitrageur gets a chance to fix the price in between.

Every working defense breaks at least one of these.

The five ingredients (all must hold)

Before you report this bug, check that all five are present:

  1. Spot quote valuation. Collateral or borrow capacity comes from an instantaneous AMM read, such as quoteExactInput, quoteExactOutput, slot0, getAmountsOut or getReserves.
  2. Manipulable path. The pool is shallow compared with the capital available through flash loans.
  3. Consumed in the attacker’s transaction. The inflated value sizes a borrow, mint or payout before the price can recover.
  4. Accessible collateral. The attacker can get and deposit the collateral with no whitelist in the way.
  5. No guardrail. There’s no cross-check against an independent feed, no deviation clamp and no absolute cap.

How to hunt for it

  1. Trace every pricing call that isn’t just for display. bash grep -rnE "quoteExactInput|quoteExactOutput|getAmountsOut|slot0|getReserves" src/ For each hit, follow the return value. Does it only feed a UI preview, or does it reach borrow power, collateral value, liquidation thresholds or minting?
  2. Check what is actually wired up. A safe oracle module in the repo means nothing if deployment wires the unsafe one. Read the deployment scripts and the on-chain storage, not just the source. This is exactly how Sharwa was lost.
  3. Compare pool depth with flash-loan capital. Work out how much capital it takes to move the quote by 20%, 50% or 200%, and compare that with what Aave, Balancer and Uniswap can lend. If flash liquidity is far larger, treat the pool as trivially manipulable.
  4. Check the timing. Can deposit() and borrow() happen in the same transaction? Is collateral re-priced at borrow time, or is a deposit-time snapshot reused?
  5. Look for circuit breakers and caps. bash grep -rnE "Chainlink|Aggregator|observe|TWAP|deviation|cap|bound" src/ Is there a deviation clamp against an independent feed? Are there per-account and per-deposit caps?

Lookalikes that are safe, and why

Fixes that work

The same bug in other clothes

The pattern isn’t limited to lending. It appears wherever a live price sizes an entitlement:

Test yourself

  1. A protocol prices collateral with a 30-minute TWAP. Vulnerable to a flash-loan oracle attack? No, ingredient 1 fails. But confirm it really calls observe() over that window and isn’t reading slot0 under a TWAP name.
  2. Spot quote, but a mandatory 24-hour wait between deposit and borrow. Vulnerable? Not to flash loans, as long as collateral is re-priced at borrow time. If the deposit-time value is stored and reused, it’s still exploitable, as ElevateFi showed.
  3. Spot quote with a 2% Chainlink deviation clamp. Valid finding? No, the clamp stops it. Still confirm the clamp is wired for every collateral type and the feed checks staleness.
  4. Your fork test works mechanically, but net profit is negative after fees. High severity? No. A working mechanism with negative economics is a near miss. Try other parameters first. If it stays unprofitable, report it as Low or Informational.

Recap