# Live-price margin recompute bugs

Margin stored in token units but deltas computed in USD at a different price; refunds ignoring unrealized loss

_Area: DeFi accounting and math bugs. Seen in 2 disclosed reports; latest 2026-02-05. Updated 2026-10-23._

## How it works
A position stores margin or collateral in token units at one price, but later operations (leverage change, migration, settlement) compute a delta in USD and convert it back using the *current* oracle price. If the price moved between deposit and recompute, the converted delta no longer matches the stored balance. The subtraction can exceed the stored value, flooring it to zero, or refund margin that was actually backing an unrealized loss. The broken assumption: that a USD-denominated delta and a token-denominated balance are interchangeable at any time. They are only equal at the price used when the balance was written.

## Where it shows up
- Functions that compute a margin/collateral delta in USD then convert to tokens via a live oracle [E1](https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-10.md).
- Leverage-change or settlement paths that move raw margin without reading unrealized PnL [E2](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-01.md).
- Migration/swap functions that validate solvency at a live rate while downstream claims re-read that rate later [E3](https://bugbountylibrary.com/r/cantina%3Aalchemix_may2025%3A3.2.9.md).
- Any subtraction of a freshly converted amount from a stored balance, with a floor/clamp to zero.

## How to review
Identify the price at which each stored balance was written, and the price used in every later delta. The invariant: a recompute must use the same basis (price and units) as the stored value, or explicitly reconcile the difference. Check that settlement moves margin *and* accounts for negative uPnL, so the equity basis (margin + uPnL) used in checks matches the basis used in state mutation [E2](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-01.md). For migrations, confirm the old component stays authorized for outstanding claims, or that claims are settled at the rate used to validate [E3](https://bugbountylibrary.com/r/cantina%3Aalchemix_may2025%3A3.2.9.md).

## Variants seen
- Leverage increase converts reduced margin at current price, zeroing initialMargin and escaping fees/losses [E1](https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-10.md).
- Leverage change refunds margin backing unrealized loss, shifting bad debt to insurance fund [E2](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-01.md).
- Transmuter swap validates solvency at live rate; later rate drift locks funds [E3](https://bugbountylibrary.com/r/cantina%3Aalchemix_may2025%3A3.2.9.md).

## Impact and severity
High: positions can be opened or closed with zero effective margin, fees bypassed, losses escaped, and bad debt pushed onto insurance funds or other users [E1](https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-10.md)[E2](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-01.md). Locked user funds in migration cases [E3](https://bugbountylibrary.com/r/cantina%3Aalchemix_may2025%3A3.2.9.md). Severity rises with oracle volatility and with any clamp-to-zero that silently hides the mismatch.

## Why fixes fail
Clamping the result to zero hides the mismatch instead of reconciling bases; the position still holds an economically wrong margin [E1](https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-10.md).

## Evidence
- E1: https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-10.md
- E2: https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-01.md
- E3: https://bugbountylibrary.com/r/cantina%3Aalchemix_may2025%3A3.2.9.md
