Skip to content
Business logic Audit finding Intermediate

TWAP misweights EMAs in RiskEngine, anchoring liquidation price to slow EMA and letting insolvent accounts dodge liquidation

The twapEMA function in RiskEngine destructured the return values of getEMAs in the wrong order, causing the TWAP to be weighted 60% slow EMA, 30% fast EMA, and 10% spot EMA instead of the intended 60% fast, 30% slow, and 10% eons. This misweighting anchored the TWAP to stale prices, allowing insolvent accounts to appear solvent and block liquidation or force-exercise.

mrdafidi Follow with 0xanony, Ahmedsec and 22 others

Originally disclosed on Code4rena

Submitted by mrdafidi, also found by 0xanony, Ahmedsec, Charming, Diavolo, dtang, edoscoba, ewah, Funen, fx, hecker_trieu_tien, hiia, I1iveF0rTh1Sh1t, Incogknito, KineticsOfWeb3, M_o7amed_T, Manosh19, mohamedfahmy, Neo3141, niffylord, odeili, one, qwqkol, SAGEisbuilding, and Wakei

This issue was also found with V12.

Root cause

twapEMA destructures getEMAs() as (int256 eonsEMA, int256 slowEMA, int256 fastEMA, ) even though getEMAs() returns (spotEMA, fastEMA, slowEMA, eonsEMA, medianTick) (RiskEngine.sol line 836 vs OraclePack.sol lines 209-224). The weighting int24((6 * fastEMA + 3 * slowEMA + eonsEMA) / 10) therefore becomes 60% slow, 30% fast, 10% spot and ignores the eons EMA entirely.

Impact

PanopticPool.getTWAP() uses this TWAP for liquidation/force-exercise checks (PanopticPool.sol lines 1944-1948). dispatchFrom requires insolvency at all ticks [spot, twap, latest, current] before proceeding (PanopticPool.sol lines 1368-1438). Because the TWAP is anchored to the slow EMA, rapid adverse moves leave the TWAP near stale prices, so unhealthy accounts can appear solvent at that tick and block liquidation/force-exercise (NotMarginCalled), delaying cleanup and risking larger bad debt or mispriced exercises/settlements.

Recommended mitigation steps

Destructure EMAs in the correct order and keep the intended 60/30/10 fast/slow/eons weighting.

// contracts/RiskEngine.sol
function twapEMA(OraclePack oraclePack) external pure returns (int24) {
    ( , int256 fastEMA, int256 slowEMA, int256 eonsEMA, ) = oraclePack.getEMAs();
    return int24((6 * fastEMA + 3 * slowEMA + eonsEMA) / 10);
}

Add a regression test ensuring twapEMA weights fast/slow/eons and does not depend on spot.

Proof of Concept

forge test --match-path test/foundry/core/RiskEngine/RiskEngineTwapEMAPoC.t.sol --match-test twapEMA_weights_fast_slow_eons_not_spot

View detailed Proof of Concept

Panoptic mitigated:

Use correct returned ema order in twapEMA.

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


The original report as published on Code4rena, by mrdafidi. 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 twapEMA function destructured the return values of getEMAs in the wrong order, swapping fastEMA and slowEMA and omitting eonsEMA.

Impact

Because the TWAP was anchored to the slow EMA, rapid adverse price moves left the TWAP near stale prices, so unhealthy accounts could appear solvent at that tick and block liquidation or force-exercise, delaying cleanup and risking larger bad debt or mispriced exercises/settlements.

What the attacker needed

Rapid adverse price move; account becomes insolvent

The fix

The mitigation was to use the correct returned EMA order in twapEMA, and the fix was confirmed.

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 TWAP is a time-weighted average price used to smooth out price swings. The code meant to combine three price averages (fast, slow, and eons) in a 60/30/10 ratio, but it read them in the wrong order, so the result was mostly the slow average. This made the price used for liquidation checks lag behind real prices, so unhealthy accounts could avoid being liquidated.

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 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.