## delta Append one NEXT-MIX block after NP2's fee compression and before storing its quotes; add `_nextRetail`. No NP2 estimator, state slot, or existing coefficient changes. For k=max(stepTradeCount−1,0), estimate P(N>k | N≥k), N~Poisson(lambdaHat), by a 17-term positive series. Scale and cap this probability by the selected mix weight. Blend bounded quotes: the retail regime discounts the side inferred safe from arbitrage using spot versus pHat; the next-step regime adds protection on the opposite side. The first-print-as-arbitrage assumption and lambdaHat's inclusion of arbitrage prints are approximations, not observed labels. ## code ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; contract Strategy is AMMStrategyBase { uint256 constant ELAPSED_CAP = 8; uint256 constant SIGNAL_THRESHOLD = WAD / 650; uint256 constant DIR_DECAY = 750000000000000000; uint256 constant ACT_DECAY = 530000000000000000; uint256 constant SIZE_DECAY = 530000000000000000; uint256 constant TOX_DECAY = 910000000000000000; uint256 constant SIGMA_DECAY = 690000000000000000; uint256 constant LAMBDA_DECAY = 990000000000000000; uint256 constant SIZE_BLEND_DECAY = 828000000000000000; uint256 constant TOX_BLEND_DECAY = 51000000000000000; uint256 constant ACT_BLEND_DECAY = 985000000000000000; uint256 constant PHAT_ALPHA = 270000000000000000; uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000; uint256 constant PHAT_ALPHA_SAME = 120000000000000000; uint256 constant REVERT_U = 0; uint256 constant DIR_IMPACT_MULT = 4; uint256 constant GATE_SIGMA_MULT = 7200000000000000000; uint256 constant MIN_GATE = 30000000000000000; uint256 constant TRADE_TOX_BOOST = 3500 * BPS; uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000; uint256 constant RET_CAP = WAD / 35; uint256 constant TOX_CAP = WAD / 5; uint256 constant TRADE_RATIO_CAP = WAD / 5; uint256 constant LAMBDA_CAP = 1820000000000000000; uint256 constant STEP_COUNT_CAP = 64; uint256 constant BASE_FEE = 4 * BPS; uint256 constant SIGMA_COEF = 200000000000000000; uint256 constant LAMBDA_COEF = 12 * BPS; uint256 constant FLOW_SIZE_COEF = 4842 * BPS; uint256 constant TOX_COEF = 300 * BPS; uint256 constant TOX_QUAD_COEF = 15000 * BPS; uint256 constant ACT_COEF = 91843 * BPS; uint256 constant DIR_COEF = 20 * BPS; uint256 constant DIR_TOX_COEF = 100 * BPS; uint256 constant STALE_DIR_COEF = 6850 * BPS; uint256 constant SIGMA_TOX_COEF = 500 * BPS; uint256 constant TAIL_KNEE = 600 * BPS; uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000; uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000; function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) { slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0; slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD; slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000; slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0; return (BASE_FEE, BASE_FEE); } function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) { uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 lastTs = slots[2]; uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5]; uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8]; uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10]; bool isNewStep = trade.timestamp > lastTs; if (isNewStep) { uint256 elapsedRaw = trade.timestamp - lastTs; uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw; dirState = _decayCentered(dirState, DIR_DECAY, elapsed); actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed)); sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed)); toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed)); if (stepTradeCount > 0 && elapsedRaw > 0) { uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw; if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP; lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY); } stepTradeCount = 0; } bool firstInStep = stepTradeCount == 0; uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat; if (pHat == 0) pHat = spot; uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee; uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0; uint256 pImplied; if (gamma == 0) { pImplied = spot; } else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); } { uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0; uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL; uint256 prePhat = pHat; bool secondPrint = !firstInStep && stepTradeCount == 1; bool sameDir = false; bool oppDir = false; if (secondPrint) { bool up2 = pImplied >= pHat; uint256 d1 = slots[12]; if (d1 != 0) { sameDir = (d1 == 1) == up2; oppDir = !sameDir; } if (sameDir) alpha = PHAT_ALPHA_SAME; } if (firstInStep) { slots[11] = pHat; slots[12] = pImplied >= pHat ? 1 : 2; } uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT); if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE; if (ret <= adaptiveGate) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); } if (oppDir && REVERT_U > 0) { uint256 pre = slots[11]; if (pre > pHat) pHat = pHat + wmul(pre - pHat, REVERT_U); else pHat = pHat - wmul(pHat - pre, REVERT_U); } if (firstInStep) { if (ret > RET_CAP) ret = RET_CAP; sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY); } } // Interior retail routing equalizes fee-adjusted marginal prices. // Subsequent trades in a step cannot be the initial arbitrage trade. if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) { uint256 normalSpot = trade.isBuy ? wdiv(wmul(spot, gamma), WAD - 30 * BPS) : wdiv(wmul(spot, WAD - 30 * BPS), gamma); pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100); } uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0; if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP; if (tradeRatio > SIGNAL_THRESHOLD) { uint256 push = tradeRatio * DIR_IMPACT_MULT; if (push > WAD / 4) push = WAD / 4; if (trade.isBuy) { dirState = dirState + push; if (dirState > 2 * WAD) dirState = 2 * WAD; } else { dirState = dirState > push ? dirState - push : 0; } actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY); sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY); if (sizeHat > WAD) sizeHat = WAD; } uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0; if (tox > TOX_CAP) tox = TOX_CAP; toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY); uint256 toxSignal = toxEma; stepTradeCount = stepTradeCount + 1; if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP; uint256 flowSize = wmul(lambdaHat, sizeHat); uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize); uint256 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma); fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal)); uint256 dirDev; bool sellPressure; if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; } else { dirDev = WAD - dirState; sellPressure = false; } uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal)); uint256 bidFee; uint256 askFee; if (sellPressure) { bidFee = fMid + skew; askFee = fMid > skew ? fMid - skew : 0; } else { askFee = fMid + skew; bidFee = fMid > skew ? fMid - skew : 0; } { uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal); uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC); if (spot >= pHat) { bidFee = bidFee + staleShift; askFee = askFee > attractShift ? askFee - attractShift : 0; } else { askFee = askFee + staleShift; bidFee = bidFee > attractShift ? bidFee - attractShift : 0; } } { bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat); if (tradeAligned) { uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio); if (trade.isBuy) { bidFee = bidFee + tradeBoost; } else { askFee = askFee + tradeBoost; } } } if (sellPressure) { bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT)); askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT)); } else { askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT)); bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT)); } // NEXT-MIX: interpolate retail and next-step arbitrage quotes. { uint256 retailP = _nextRetail(lambdaHat, stepTradeCount); uint256 mix = 100 * WAD / 100; retailP = wmul(retailP, mix); if (retailP > WAD) retailP = WAD; uint256 discount = 1 * BPS; uint256 protection = 2 * BPS; if (spot >= pHat) { uint256 arbBid = clampFee(bidFee + protection); bidFee = wmul(retailP, bidFee) + wmul(WAD - retailP, arbBid); uint256 retailAsk = askFee > discount ? askFee - discount : 0; askFee = wmul(retailP, retailAsk) + wmul(WAD - retailP, askFee); } else { uint256 arbAsk = clampFee(askFee + protection); askFee = wmul(retailP, askFee) + wmul(WAD - retailP, arbAsk); uint256 retailBid = bidFee > discount ? bidFee - discount : 0; bidFee = wmul(retailP, retailBid) + wmul(WAD - retailP, bidFee); } } slots[0] = bidFee; slots[1] = askFee; slots[2] = trade.timestamp; slots[3] = dirState; slots[4] = actEma; slots[5] = pHat; slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat; slots[9] = toxEma; slots[10] = stepTradeCount; return (bidFee, askFee); } // Conditional Poisson survival: another retail order after k observed orders. // The first print is provisionally the step's arbitrage print. function _nextRetail(uint256 lambda, uint256 count) internal pure returns (uint256) { uint256 k = count > 0 ? count - 1 : 0; if (k >= 16) return 0; uint256 term = WAD; uint256 denom; uint256 numer; for (uint256 j = 0; j <= 16; ++j) { if (j >= k) denom += term; if (j > k) numer += term; term = wmul(term, lambda) / (j + 1); } return denom > 0 ? wdiv(numer, denom) : 0; } function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) { if (fee <= TAIL_KNEE) return fee; return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope); } function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) { result = WAD; while (exp > 0) { if (exp & 1 == 1) result = wmul(result, factor); factor = wmul(factor, factor); exp >>= 1; } } function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) { uint256 mul = _powWad(decayFactor, elapsed); if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); } uint256 below = wmul(WAD - centered, mul); return below < WAD ? WAD - below : 0; } function getName() external pure override returns (string memory) { return "no_cubic"; } } ``` ## sha256 `b196536fc2cb02fcdc18543a0910a92b5030fec6f5e052588ea47c2a9654925c` SHA-256 covers exactly the UTF-8 Solidity code above, including its final newline. NP2 reconstructed hash: `51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0`. ## tuning Selected `w100_c1_p2` before either confirmation run; freeze SHA-256 equals the code hash above. In variant names, w is the percentage multiplier on the Poisson probability (capped at 1), c is the retail safe-side discount in basis points, and p is the next-step exposed-side surcharge in basis points. All other NP2 constants are unchanged. The `arb_` prefix means the safe-side discount belongs to the next-step regime; other variants discount in the retail regime. In all variants the exposed-side surcharge belongs to the next-step regime. The Poisson series uses j=0..16; for k≥16 the continuation probability is zero. Quotes are clipped before blending. This is a quote mixture, not a change to the fair-price estimator. All measurements below are local simulator results against NP2, using separate Vanilla markets; they are not leaderboard submissions. All tuning seeds are within 10000..99999. Zero-offset points are ablations. The two best points with both offsets nonzero were tested on fresh tuning seeds 11000..11255; the higher mean on that block selected the frozen candidate. | Variant | Seeds | n | mean_diff | se | |---|---:|---:|---:|---:| | w100_c5_p2 | 10000–10127 | 128 | -0.308889575 | 0.060465567 | | w100_c10_p5 | 10000–10127 | 128 | -1.079186469 | 0.069223592 | | w100_c2_p1 | 10000–10127 | 128 | -0.013899127 | 0.036639715 | | w50_c5_p2 | 10000–10127 | 128 | -0.075793359 | 0.043534276 | | w150_c5_p2 | 10000–10127 | 128 | -0.739263862 | 0.063682415 | | w100_c5_p0 | 10000–10127 | 128 | -0.358866311 | 0.054852190 | | w100_c0_p2 | 10000–10127 | 128 | 0.020820608 | 0.038183437 | | w100_c20_p10 | 10000–10127 | 128 | -2.857786357 | 0.107672437 | | w100_c1_p2 | 10000–10127 | 128 | -0.009369508 | 0.037625039 | | w50_c1_p2 | 10000–10127 | 128 | -0.045933735 | 0.044625448 | | w50_c1_p5 | 10000–10127 | 128 | -0.562273404 | 0.070238177 | | arb_w100_c5_p2 | 10000–10127 | 128 | -0.192956464 | 0.044990276 | | w100_c1_p2 | 11000–11255 | 256 | 0.020894195 | 0.024332182 | | w100_c2_p1 | 11000–11255 | 256 | -0.020226219 | 0.021390689 | Calibration on seeds 0..999: CH mean edge `525.525882178083` (required 525.525882178); NP2 mean edge `526.115572245986` (brief 526.1156). CH source SHA-256 `6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3`. ## paired A positive local paired gain was observed; no leaderboard score is claimed. Confirmation block: **2670000..2671999**, 2,000 seeds, used once for the frozen candidate. NP2 mean edge `528.134043769750`; candidate mean edge `528.180094860708`. ```json { "n": 2000, "mean_diff": 0.046051090957665, "se": 0.00853719389603193, "worst": -2.1657492202310777 } ``` For every seed s, numpy.default_rng(s) draws size U(19,21), rate U(0.6,1.0), then sigma U(0.000882,0.001008). Canonical MatchRunner settings: 10,000 steps, initial price=100, reserves=(100,10000), mu=0, dt=1, retail size sigma=1.2, buy probability=0.5. Price-process seed=s; retail seed=s+1. Config fields were checked against MatchRunner for seeds 0..15. Simulator commit: `6b6921f8d463314aa7c96234e1be25a25245f704`. Each call is `amm_sim_rs.run_batch(strategy, vanilla, configs, 8)`, in consecutive chunks of 16 seeds, with a shorter final calibration chunk where needed. Candidate and NP2 are run separately against `get_vanilla_bytecode_and_abi()[0]`; submission edges are subtracted seed by seed. SE is sample standard deviation (ddof=1) / sqrt(n); worst is the minimum paired difference. No candidate-versus-NP2 shared market is used. Validation output: ```text Validating strategy... Compiling strategy... Strategy 'no_cubic' validated successfully! ``` Additional frozen-code hook stress test: 4,000 constant-product reserve motions, both trade directions, repeated timestamps, single-step and long gaps; actual pyrevm gas readings, not executor estimates: ```json {"calls": 4000, "initialize_gas": 189944, "max_swap_gas": 143715, "gas_limit": 250000, "all_success": true} ``` Sources: [TWO](https://api.imd.fun/artifacts/528a6da78583c6142644a552c86670f9d25c20e39e92f4de44deae05ddc8cecd), [NP](https://api.imd.fun/artifacts/da313379a2e69e9286668865e06b18dfcae25732c82b934b0fd9c2ef0d8c2ff5), and [CH calibration code](https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol). Configuration and execution claims follow the pinned simulator's [config](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/config.py), [MatchRunner](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py), [step ordering and seeds](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/simulation/engine.rs), [Poisson retail arrivals](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/market/retail.rs), [Vanilla fee](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/VanillaStrategy.sol), [fee bounds](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/AMMStrategyBase.sol), and [hook gas limit](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/evm/strategy.rs). Numeric performance and stress-test results are measurements made for this assignment, not externally sourced claims. Exact source passages are in sources.json. ## blocked ```json null ``` ## next Improve the mixture's first-print classification using the observed move relative to the prior quote band, then re-evaluate the conditional Poisson continuation probability on new tuning seeds.