Skip to content

feat: build circuit breaker status indicator on buy form for price im… - #1039

Merged
Chucks1093 merged 4 commits into
accesslayerorg:devfrom
Unclebaffa:feat/1034-circuit-breaker-status-indicator
Sep 28, 2026
Merged

Chucks1093 merged 4 commits into
accesslayerorg:devfrom
Unclebaffa:feat/1034-circuit-breaker-status-indicator

Conversation

@Unclebaffa

@Unclebaffa Unclebaffa commented Sep 27, 2026 •

Copy link
Copy Markdown

Closes #1034


Pull Request: feat: build circuit breaker status indicator on buy form for price impact warnings (#1034)

Target Branch: dev
Closes: #1034


Summary

This PR implements a real-time price impact indicator and circuit breaker protection system across the key purchasing interfaces (TradeDialog modal and BuySellKeyFlow tab). When traders enter an order amount on a bonding curve, the UI dynamically assesses price impact against the key's configured circuit breaker threshold to protect buyers from predatory front-running and sudden volatility.

Key Capabilities & Architectural Additions

  • Real-Time Price Impact Evaluation:

    • Evaluates marginal price impact $\left(\frac{\text{quotePrice} - \text{spotPrice}}{\text{spotPrice}} \times 100\right)$ reactively upon amount changes within $< 300\text{ ms}$.
    • Modularized mathematical utilities in src/utils/circuitBreaker.utils.ts via evaluateCircuitBreakerStatus and resolveCircuitBreakerThreshold.
    • Flexible threshold configuration: parses circuitBreakerThresholdPercent, falls back to circuitBreakerThresholdBps / 100, or defaults to 15.0%. Proximity warning triggers at $80%$ of threshold (DEFAULT_CIRCUIT_BREAKER_PROXIMITY_RATIO = 0.8).
  • Three-Tier Visual State Indicator (CircuitBreakerStatusIndicator):

    State Condition Visual Representation Buy Action
    Normal $\text{Impact} < 80%$ threshold Emerald badge with percentage & info tooltip Enabled
    Approaching $80% \le \text{Impact} < 100%$ threshold Amber alert banner warning of high price impact near circuit limit Enabled
    Breached $\text{Impact} \ge 100%$ threshold Critical red alert banner with explicit reduction instructions Disabled ("Circuit Breaker Tripped")
  • Hard Protection (Non-Bypassable):

    • Unlike configurable user slippage tolerances, circuit breaker limits represent hard safety boundaries.
    • When breached, the buy button is disabled, styled with disabled:cursor-not-allowed, changes text to "Circuit Breaker Tripped", and prevents submission even if triggered programmatically.
  • Accessible Tooltip & Responsive Design:

    • Includes an accessible tooltip (aria-label="Circuit breaker protection info", role, keyboard focusable via Tab/Escape) detailing the automated circuit breaker mechanism.
    • Placed directly beneath the amount input on buy forms; hidden on sell forms where slippage/impact mechanics differ.
  • Data Models & Page Propagation:

    • Extended Course and KeyConfig in src/services/course.service.ts to support optional circuitBreakerThresholdPercent and circuitBreakerThresholdBps.
    • Wired key configurations in CreatorDetailPage.tsx into TradeDialog.

Changed Files

File Type Description
src/utils/circuitBreaker.utils.ts Created Threshold resolution, proximity evaluation, status typing, and tooltip constants.
src/utils/__tests__/circuitBreaker.utils.test.ts Created 13 unit tests for threshold precedence, boundary conditions, and proximity math.
src/components/common/CircuitBreakerStatusIndicator.tsx Created Reusable presentation component for impact badges, tooltips, and warning/breach banners.
src/components/common/__tests__/CircuitBreakerStatusIndicator.test.tsx Created 7 component tests verifying badge styles, alert levels, tooltips, and zero-state handling.
src/components/common/TradeDialog.tsx Modified Placed indicator below amount input, disabled confirm button, updated button text, and blocked submit.
src/components/common/__tests__/TradeDialog.circuitBreaker.test.tsx Created 6 integration tests verifying reactive updates ($< 300\text{ ms}$), approaching banners, button blocking, and exclusion from sell mode.
src/components/common/BuySellKeyFlow.tsx Modified Embedded indicator in buy tab, disabled review button with "Circuit Breaker Tripped", and blocked submit.
src/components/common/__tests__/BuySellKeyFlow.circuitBreaker.test.tsx Created 5 integration tests testing threshold breach and approaching states on the key swap flow.
src/pages/CreatorDetailPage.tsx Modified Forwarded circuitBreakerThresholdPercent and circuitBreakerThresholdBps to TradeDialog.
src/services/course.service.ts Modified Added threshold properties to Course and KeyConfig.
src/test/setup.ts Modified Added Node 22 localStorage mock fallback for jsdom test runner environments.

Testing

  • pnpm lint (0 errors, 0 warnings)
  • pnpm build (clean production bundle generated via tsc -b && vite build)
  • pnpm vitest run circuitBreaker TradeDialog.circuitBreaker BuySellKeyFlow.circuitBreaker (31 tests passed across 4 test suites)
  • pnpm vitest run TradeDialog.a11y.test.tsx TradeDialog.test.tsx (17 tests passed)

Checklist

  • Scope is limited to the stated change
  • Updated docs if behavior or setup changed
  • Added screenshots for UI changes when relevant (or test-covered UI DOM assertions)

…pact warnings (accesslayerorg#1034)

Show real-time price impact indicator on buy forms (TradeDialog and BuySellKeyFlow), display approaching and breached warning banners when impact reaches threshold, and disable buy submission with circuit breaker warning when threshold breached.

Closes accesslayerorg#1034
@drips-wave

drips-wave Bot commented Sep 27, 2026

Copy link
Copy Markdown

@Unclebaffa Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

…esslayerorg#1015 holding cap PR

After the large dev rebase (20+ PRs merged including holding cap accesslayerorg#1015, batch trade accesslayerorg#1044, cooldown accesslayerorg#1043), several issues were introduced in BuySellKeyFlow and TradeDialog:

- TradeDialog.tsx: priceImpactPercent guard was changed to currentSupply <= 0, incorrectly blocking valid impact calculations at supply=0 (new bonding curves). Reverted to < 0 to match calculateTradePriceImpact's own guard. Fixes 4 failing circuit breaker tests.
- BuySellKeyFlow.tsx: Unused HoldingCapIndicator import and dead isCapLimitReached/isCapBreached/isCapExceeded useMemo blocks added but never rendered. Removed to fix ESLint no-unused-vars errors.
- TradeDialog.tsx: Unused isCapExceeded combined variable removed (isCapLimitReached and isCapBreached used directly).
- course.service.ts: holdingCap and maxHoldingCap properties missing from KeyConfig interface despite being accessed via keyConfig?.holdingCap in both BuySellKeyFlow and TradeDialog. Added with correct JSDoc (accesslayerorg#1015).

Result: 31/31 circuit breaker tests pass, lint 0 errors, 0 TypeScript errors.
- course.service.ts: Restore WhitelistEntry and Course whitelist properties dropped during merge (accesslayerorg#1031)
- BuySellKeyFlow.tsx: Re-integrate HoldingCapIndicator and cap limit/breach checks to satisfy holding cap tests (accesslayerorg#1015)
- TradeDialog.tsx: Clean up dataset access, redundant jumps, Number.NaN, nullish coalescing, and ambiguous spacing
- TradeDialog.sellPayoutDisplay.test.tsx: Acknowledge high price impact override checkbox on 5-key sell
- TradeDialog.mobileBottomSheet.test.tsx: Provide safe supply in mobile trade confirmation test to avoid tripping circuit breaker
@Chucks1093
Chucks1093 merged commit c261b72 into accesslayerorg:dev Sep 28, 2026
1 check passed
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.

Build circuit breaker status indicator on buy form for price impact warnings

2 participants