Skip to content

feat(contracts): add TWAP deviation guard for settlement price validation (#1762) - #1919

Open
frammawiliansyah wants to merge 1 commit into
Arena1X:mainfrom
frammawiliansyah:feat/twap-settlement-guard-1762
Open

frammawiliansyah wants to merge 1 commit into
Arena1X:mainfrom
frammawiliansyah:feat/twap-settlement-guard-1762

Conversation

@frammawiliansyah

Copy link
Copy Markdown

Description

Resolves #1762.

Adds a TWAP deviation guard for open-market settlement to prevent last-block price manipulation attacks via large spot swaps.

Key Changes

  • Settlement Helper (contracts/open-market/src/liquidity.rs & lib.rs):
    • Implemented validate_settlement_price(env, market_id, outcome, window):
      • Calculates the TWAP over the specified window and compares it with the current spot reserve price.
      • Reverts with PriceDeviationTooHigh if the spot vs. TWAP price deviation exceeds the configured threshold.
      • Reverts with TwapInsufficientHistory if the retained history does not cover the window (without silently truncating).
  • Configuration (contracts/open-market/src/config.rs):
    • Added twap_max_deviation_bps to contract Config (defaults to 1000 bps / 10%).
    • Added admin-guarded setter set_twap_max_deviation_bps (validating range 1..=10000 bps) and getter get_twap_max_deviation_bps.
  • Error Codes (contracts/open-market/src/errors.rs & ARCHITECTURE.md):
    • Registered PriceDeviationTooHigh = 113.
  • Unit Tests (contracts/open-market/tests/twap_validation_tests.rs):
    • Added 5 new regression tests verifying rejection of manipulated spot prices outside the deviation band, acceptance of normal prices within the band, configurable thresholds, administrative checks, and window insufficiency reverts.

Verification

  • cargo test --test twap_validation_tests passed (14 tests passed).
  • cargo test across contracts/open-market passed with 0 errors.

@vercel

vercel Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
insight-arena-4rll Error Error Oct 3, 2026 3:37am UTC

This branch had an error being deployed

1 failed deployment
Preview — 42c307a9 Deployed Oct 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Contracts] open-market: TWAP guard against settlement-price manipulation

1 participant