feat(predict): EWMA gas-price trade penalty for mint/redeem#1063
Merged
Conversation
0xaslan
approved these changes
Jun 2, 2026
Port DeepBook core's gas-price EWMA penalty into Predict's mint and live redeem flows. Each ExpiryMarket keeps a smoothed (mean, variance) of ctx.gas_price(); when enabled and gas is a high outlier (z-score above the configured threshold), a flat additional_fee * quantity surcharge is added to the stake-discounted trading fee and retained as pool surplus. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
cbf62ea to
8b5c40b
Compare
Address review: remove the #[test_only] mean/variance/last_updated_timestamp_ms getters from the ewma module and verify the EWMA entirely through its observable penalty_fee output, driving the per-tx gas price and bracketing the z-score with the penalty threshold instead of reading internal state. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mintand liveredeemflows.ExpiryMarketnow keeps a smoothed(mean, variance)ofctx.gas_price()(sources/ewma.move), seeded atcreate_and_share. On every mint/live-redeem it folds in the current gas price.z_score_thresholdstandard deviations, a flatadditional_fee × quantitysurcharge is added on top of the (stake-discounted) trading fee.EwmaConfig(sources/config/ewma_config.move) insideProtocolConfig:alpha,z_score_threshold,additional_fee,enabled. New admin entrypointsprotocol_config::set_ewma_paramsandset_ewma_enabled(AdminCap-gated, bounds-checked). Disabled by default.OrderMinted/LiveOrderRedeemedevents gain apenalty_feefield; newEwmaConfigUpdatedevent on admin changes.Key decisions
ctx.gas_price()exactly like DeepBook core (not oracle price), with the same mean/variance/z-score math.ExpiryMarketstate, not global: the market is already&mutin mint/redeem, so this adds zero new shared-object contention — the analog of DeepBook's per-pool EWMA. Tradeoff: each market cold-starts (variance = 0), so the penalty can't fire until a couple of differently-priced trades accumulate variance.EwmaConfig(read immutably in the flow), evolving estimate inExpiryMarket(mutated in place).additional_fee × quantity, independent of the fee, applied after the stake discount — stakers pay the full surcharge, mirroring DeepBook.cash_balance, excluded from the builder cut, the rebate fee basis, and gross P&L. On redeem it is capped atredeem_amount − fee − builder_feeso it can never over-withhold the payout.Test plan
sui move test --path packages/predict --gas-limit 100000000000— 483 tests pass (21 new EWMA tests).ewma_tests): hand-derived mean/variance, z-score crossing (fires >1σ, not at default 3σ), disabled / zero-variance / below-mean → 0, same-ms no-op.ewma_config_tests,admin_setters_tests): defaults, every bound, all three new abort codes, setter forwarding, enable toggle.expiry_market_tests): a real 1000→2000→3000 gas spike throughmint(extra cost equals the surcharge; penalty is not recorded as a trading fee) and throughredeem(two identical positions; the penalized one pays exactly the surcharge less)..min()cap (deep-OTM low-value redeem where penalty ≥ remaining payout).scripts/to mirror).🤖 Generated with Claude Code