# ep5-mech merge 01: six mechanism items vs NP2 **Bottom line:** Two of the six mechanisms gave a confirmed positive paired gain over NP2: **SILENCE** (+0.219, t=20.3) and **NEXT-MIX** (+0.128, t=8.1). Both are tiny next to the stated targets: top 10 needs +5.8 and 1st needs +9.6. None of the six moves NP2 meaningfully toward 10th place. PRE-SPOT came out positive but weak (t=1.71, below 2 se). ARB-FIRST, EV-FEE and KALMAN all lost to NP2. Each item's result file is under `.imd/reads/artifacts/` (item-arbfirst-a, item-silence-a, item-kalman-a, item-prespot-a, item-evfee-a, item-nextmix-a). All six were present and non-empty. Their SHA-256 values matched the ones given in the merge brief. Every number below is copied from those files. The only exceptions are **t**, which I computed as mean_diff/se from the reported values, and the code-hash check described below. The numbers are the items' own local measurements on the pinned simulator (benedictbrady/amm-challenge @ 6b6921f8d463314aa7c96234e1be25a25245f704). I did not re-run any simulation for this merge. ## Ranking (by t = mean_diff / se on each item's confirmation block) | Rank | Item | Mechanism | Confirm seeds | n | mean_diff | se | t | worst | Method valid? | blocked | |---:|---|---|---|---:|---:|---:|---:|---:|---|---| | 1 | silence-a | SILENCE: after a timestamp gap of g silent steps (cap 4), move pHat toward the centre of the previous fee band, weight w=1-(1-0.075)^g. pHat alpha and gate are shrunk by 1-0.30w | 2570000..2571999 | 2000 | 0.21866808751973402 | 0.010767301406119376 | 20.31 | -1.7520790263144477 | valid (separate vs Vanilla) | null | | 2 | nextmix-a | NEXT-MIX: for each side, arb-vs-retail share of the next trade, w=pA/(pA+pR). Adds +60bps·(w-0.5) when w≥0.5, else subtracts 0.5bps·(0.5-w); applied before tail compression | 2610000..2611999 | 2000 | 0.127518 | 0.01567 | 8.14 | -25.342129 | valid (separate vs Vanilla); see platform caveat | null (caveats) | | 3 | prespot-a | PRE-SPOT: for prints with amountY/reserveY ≥1%, recover the pre-trade reserves and blend pHat 95/5 toward the pre-trade spot | 2590000..2591999 | 2000 | 0.02476925897607873 | 0.014470095602418815 | 1.71 | -5.140715602119258 | valid (separate vs Vanilla) | null | | 4 | arbfirst-a | ARB-FIRST: first-in-step pHat alpha set from a posterior-like arb probability (diffusion-escape prior × Cauchy size likelihood); A1=1, A0=0.2, odds 0.5, k=0.15 | 2560000..2561999 | 2000 | -0.14056490017037462 | 0.016574418190467805 | -8.48 | -5.795228468953155 | valid (separate vs Vanilla) | null | | 5 | evfee-a | EV-FEE: one-step expected-value fee on an 8-point grid, applied as a delta W·(fEV(gap)−fEV(0)); W=0.3, LOSS=8, SIGSC=2 | 2600000..2601999 | 2000 | -0.07057953261128873 | 0.004943029783569504 | -14.28 | -1.855760044241606 | valid (separate vs Vanilla) | null | | 6 | kalman-a | KALMAN: replaces NP2's pHat alpha logic with a log-price Kalman filter; R_arb=16σ̂², R_retail=64(halfband+impact)², V floor 1e-8 | 2580000..2581999 | 2000 | -3.221992582358308 | 0.03807168294784598 | -84.63 | -8.853330939894477 | valid (separate vs Vanilla) | null | **Method validity.** All six items say they ran the candidate and NP2 against Vanilla in **separate** `amm_sim_rs.run_batch` calls on the same config draws, with d = candidate `edges['submission']` − NP2 `edges['submission']`: - arbfirst-a: "There was no candidate-vs-NP2 shared market." - silence-a: "Candidate and NP2 each ran separately against Vanilla with identical configs." - kalman-a: "candidate and NP2 never share a market." - prespot-a: "separate candidate-versus-Vanilla and NP2-versus-Vanilla `run_batch` calls." - evfee-a: "NP2 run separately." - nextmix-a: "`run_batch(cand, vanilla, cfgs, 8)` and `run_batch(np2, vanilla, cfgs, 8)` run separately." None used the invalid candidate-vs-NP2 single-market setup, so none is left out of the ranking. Every confirmation block starts at or above 2,000,000, has exactly 2,000 seeds, and is reported as run once after freezing. The six blocks do not overlap. **Blocked items:** none. Every item reports `blocked: null`. nextmix-a gave `null` but listed environment caveats that matter for reproducibility (next section). ## Caveats that affect how far to trust each result - **nextmix-a ran on a different platform and compiler.** The item says its machine was **Linux aarch64**, so "the objective's setup script exits at its platform gate (`BLOCKED platform`)". It then "used the item's own setup script, which has no platform gate". Compilation used "a local aarch64 **solc 0.8.26**" in place of 0.8.24, with `SOLC_VERSION` edited in its scratch checkout. It also did **not** run the CH = 525.525882178 check ("the brief does not say which contract CH is"). It did reproduce NP2 at 526.1155722 on seeds 0..999, which matches the 526.1156 reference, and `amm-match validate` passed. The method was still separate runs against Vanilla, so I count it as valid. But its +0.128 should be re-confirmed under the reference x86_64 / solc 0.8.24 setup before anything is promoted. Its first confirm run was killed by a tool timeout before printing anything. The item reports the rerun as the only confirm result it ever saw. - **silence-a** is the strongest and cleanest result. It reproduced CH at 525.525882178083 and recorded NP2's sha256 as 51f1c1a9…. However, its tuning picked the highest observed training mean across 15 points. The confirm value (+0.219) is close to the 512-seed training value (+0.249), so any selection bias looks small. - **prespot-a** is +0.025 at 1.71 se. The item itself says this "does not establish a reliable gain". Its tuning sweep had more than 50 points on 256-seed blocks, so a small positive selection is to be expected. - **evfee-a** tuned only on seeds 10000..10299 ("not the full 10000..99999 range (time)"). That does not change its negative confirmation. - **arbfirst-a, kalman-a, evfee-a** each say plainly not to promote their candidate. ### Sources that did not hold on re-read The task notes say some cited sources could not be read again, or did not say what they were quoted for: arbfirst-a (2), silence-a (2), kalman-a (1), prespot-a (3). The notes do not say **which** URLs failed, and I have not re-fetched them, so I cannot name them. Here is what those sources were used for in each item: - **arbfirst-a:** TWO and NP artifact URLs, simulator `match.py`, `config.py`, `engine.rs`, `arbitrageur.rs`, `VanillaStrategy.sol`. - **silence-a:** TWO and NP artifacts, a CH source at `raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199…/contracts/src/Strategy.sol`, simulator `match.py` and `engine.rs`. - **kalman-a:** TWO and NP artifacts, the same ChengHua926 CH URL, simulator `compiler.py`, `match.py`, `config.py`, `engine.rs`, `evm/strategy.rs`, `AMMStrategyBase.sol`. - **prespot-a:** TWO and NP artifacts, simulator `IAMMStrategy.sol`, `cfmm.rs`, `match.py`, `config.py`, `engine.rs`, and the ChengHua926 CH URL. These sources only back **method and provenance claims**: how NP2 and CH were rebuilt, the config-draw order, the gas limits, and the CFMM reserve maths. They do not back the measured paired numbers, which are the items' own simulator outputs. So the ranking does not rest on them. There are three places where they do matter: 1. **CH source.** silence-a, kalman-a and prespot-a cite a third-party CH file (ChengHua926). That file is not one of the brief's named artifacts. arbfirst-a instead derived CH by removing the routing block from NP. All four report the same CH sha256, `6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3`, and the same CH mean, 525.5258821780834. The CH calibration therefore holds by agreement between items, even if the ChengHua926 URL is one of the sources that failed. 2. **NP2 reconstruction.** arbfirst-a, silence-a, kalman-a and nextmix-a each state NP2's sha256 as `51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0`, matching the brief's prefix 51f1c1a9…. All five items that report an NP2 baseline give 526.1155722459862 on 0..999. evfee-a only says "51f1c1a9d499…, reproduced". prespot-a does not print NP2's hash. Its baseline matches, which is indirect support for its NP2 being correct. 3. **prespot-a's pre-trade reserve formulas** rest on `IAMMStrategy.sol` (post-trade reserves) and `cfmm.rs` (fee-adjusted reserve changes). If those are among its 3 failed sources, the claim that the code exactly "undoes" the trade is unverified. Its measured result (+0.025, t=1.71) stands either way, and it is not copied here because it is below 2 se. ## Code hashes checked I extracted each item's ```solidity block and computed SHA-256 over the UTF-8 text between the fences. Each one matches the sha256 the item reports: | Item | sha256 (computed = reported) | |---|---| | arbfirst-a | 22390cf5b7b83c8bc6abab6eaa099dfb6ce0b9e50eb96fc0ff94982577f38d29 | | silence-a | 22958b18bc780e722b6e13f16da6a70c9b61e54b429c8f65e0046e44fd6ce379 | | kalman-a | 5a75d3b274c99beadb7396b5a97e75ffe25ea34456c5855aaa425af9a90ec69a | | prespot-a | 3ce21d811b5440456116b7d848e36c7938863ababf10dd028d710ed35c633e3d | | evfee-a | 252d2fc7a97902f244057faf7f62fb90f9dd08544107bd950bafbb0984d46dec | | nextmix-a | 8d9bc6c3884259f922b1015ac8b5a4b5ce60f7b45677b67dd1e3c3be56b0adc3 | ## Gap to target The two confirmed gains together come to about +0.35. That assumes they would add up if combined, which **has not been tested**: SILENCE changes pHat and NEXT-MIX changes fees, so they touch different parts of the code. +0.35 is about 6% of the +5.8 needed for top 10. On this evidence, none of these six mechanism families closes the gap. Possible next steps: - Test SILENCE + NEXT-MIX together against NP2 on a fresh block. - Re-confirm NEXT-MIX on x86_64 with solc 0.8.24. - Try the follow-ups the items themselves suggest: separate silence curves for gate and alpha (silence-a), and gating NEXT-MIX on the per-side fee gap to Vanilla's 30 bps (nextmix-a). These are suggestions only; none of them has been measured. ## Copied candidates (valid, mean_diff ≥ 2·se) ### 1. silence-a **delta** (verbatim from item): Added a local silence count `g = min(timestamp - lastTimestamp - 1, 4)` on the first print after a timestamp gap. For `g > 0`, reconstruct pre-trade spot, find the geometric center of the previous bid/ask fee band, and move `pHat` toward it by `w = 1 - (1 - 0.075)^g`. Scale the pHat update alpha and gate by `1 - 0.30w`. Same-timestamp later prints use `g=0`; no extra storage slot was added. The base TWO code and NP routing insertion are preserved. **sha256:** `22958b18bc780e722b6e13f16da6a70c9b61e54b429c8f65e0046e44fd6ce379` **code** (verbatim): ```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 SILENCE_W1 = 75000000000000000; uint256 constant SILENCE_CAP = 4; uint256 constant SILENCE_GATE_SHRINK = 300000000000000000; uint256 constant SILENCE_ALPHA_SHRINK = 300000000000000000; 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]; uint256 silenceSteps = 0; bool isNewStep = trade.timestamp > lastTs; if (isNewStep) { uint256 elapsedRaw = trade.timestamp - lastTs; silenceSteps = elapsedRaw > 1 ? elapsedRaw - 1 : 0; if (silenceSteps > SILENCE_CAP) silenceSteps = SILENCE_CAP; 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 silenceWeight = WAD - _powWad(WAD - SILENCE_W1, silenceSteps); if (silenceWeight > 0) { uint256 spotBefore; if (trade.isBuy) { uint256 priorX = trade.reserveX - wmul(trade.amountX, WAD - prevBidFee); uint256 priorY = trade.reserveY + trade.amountY; spotBefore = priorX > 0 ? wdiv(priorY, priorX) : spot; } else { uint256 priorX = trade.reserveX + trade.amountX; uint256 netY = wmul(trade.amountY, WAD - prevAskFee); uint256 priorY = trade.reserveY > netY ? trade.reserveY - netY : 0; spotBefore = priorX > 0 && priorY > 0 ? wdiv(priorY, priorX) : spot; } uint256 bandScale = sqrt(wdiv(WAD - prevBidFee, WAD - prevAskFee) * WAD); uint256 bandCenter = wmul(spotBefore, bandScale); pHat = wmul(pHat, WAD - silenceWeight) + wmul(bandCenter, silenceWeight); } 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; } alpha = wmul(alpha, WAD - wmul(SILENCE_ALPHA_SHRINK, silenceWeight)); 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; adaptiveGate = wmul(adaptiveGate, WAD - wmul(SILENCE_GATE_SHRINK, silenceWeight)); 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)); } 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); } 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"; } } ``` ### 2. nextmix-a **delta** (verbatim from item): One call is added just before NP2's tail compression and clamp: `(bidFee, askFee) = _nextMix(bidFee, askFee, spot, pHat, sigmaHat, lambdaHat);` It also adds 8 constants and the helpers `_nextMix`, `_tail` (normal tail, A&S 26.2.19) and `_nmShift`. For each side, pA = P(fair next step crosses that side's band edge spot·(1−bid) or spot/(1−ask)), using N(pHat, sigmaHat). pR = λ̂/(1+λ̂), the chance of another retail order. w = pA/(pA+pR), and fee += 60bps·(w−0.5) when w≥0.5, else fee −= 0.5bps·(0.5−w). NM_FRAC=0, so the protective-target branch is disabled. **sha256:** `8d9bc6c3884259f922b1015ac8b5a4b5ce60f7b45677b67dd1e3c3be56b0adc3` **code** (verbatim): ```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; uint256 constant NM_FRAC = 0; uint256 constant NM_K = 1000000000000000000; uint256 constant NM_MIX_W = 1000000000000000000; int256 constant NM_PROT = 6000000000000000; int256 constant NM_CHEAP = 50000000000000; uint256 constant NM_W0 = 500000000000000000; uint256 constant NM_SIG_MULT = 1000000000000000000; uint256 constant NM_SIG_FLOOR = 300000000000000; 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; } } } // Next-mix: the fee quoted now serves the next trade (arb next step or retail this step). (bidFee, askFee) = _nextMix(bidFee, askFee, spot, pHat, sigmaHat, lambdaHat); 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)); } 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); } // Next-mix: the fee returned after a print serves the next trade, which is either the // arbitrageur next step (if fair leaves the no-arb band on that side) or another retail // order this step (Poisson(lambdaHat)). Weight each side by the arb share of that mix. function _nextMix(uint256 bidFee, uint256 askFee, uint256 spot, uint256 pHat, uint256 sigmaHat, uint256 lambdaHat) internal pure returns (uint256, uint256) { if (pHat == 0 || spot == 0) return (bidFee, askFee); uint256 sig = wmul(sigmaHat, NM_SIG_MULT); if (sig < NM_SIG_FLOOR) sig = NM_SIG_FLOOR; uint256 pR = wmul(wdiv(lambdaHat, WAD + lambdaHat), NM_MIX_W); uint256 wB; uint256 wA; { uint256 edgeB = wmul(spot, bidFee < WAD ? WAD - bidFee : 0); uint256 pAB = edgeB >= pHat ? WAD - _tail(wdiv(wdiv(edgeB - pHat, pHat), sig)) : _tail(wdiv(wdiv(pHat - edgeB, pHat), sig)); wB = pAB + pR > 0 ? wdiv(pAB, pAB + pR) : 0; } { uint256 edgeA = askFee < WAD ? wdiv(spot, WAD - askFee) : 1e36; uint256 pAA = edgeA <= pHat ? WAD - _tail(wdiv(wdiv(pHat - edgeA, pHat), sig)) : _tail(wdiv(wdiv(edgeA - pHat, pHat), sig)); wA = pAA + pR > 0 ? wdiv(pAA, pAA + pR) : 0; } if (NM_FRAC > 0) { uint256 kd = wmul(sig, NM_K); uint256 lo = wmul(pHat, WAD > kd ? WAD - kd : 0); uint256 fPB = lo < spot ? WAD - wdiv(lo, spot) : 0; uint256 hi = wmul(pHat, WAD + kd); uint256 fPA = hi > spot ? WAD - wdiv(spot, hi) : 0; if (fPB > bidFee) bidFee = bidFee + wmul(wmul(fPB - bidFee, wB), NM_FRAC); if (fPA > askFee) askFee = askFee + wmul(wmul(fPA - askFee, wA), NM_FRAC); } return (_nmShift(bidFee, wB), _nmShift(askFee, wA)); } // upper normal tail P(Z > x), x in WAD (Abramowitz-Stegun 26.2.19) function _tail(uint256 x) internal pure returns (uint256) { if (x > 8 * WAD) return 0; uint256 p = WAD + wmul(x, 196854000000000000 + wmul(x, 115194000000000000 + wmul(x, 344000000000000 + wmul(x, 19527000000000000)))); uint256 p2 = wmul(p, p); return wdiv(WAD / 2, wmul(p2, p2)); } function _nmShift(uint256 fee, uint256 w) internal pure returns (uint256) { int256 d; if (w >= NM_W0) d = (NM_PROT * int256(w - NM_W0)) / int256(WAD); else d = -(NM_CHEAP * int256(NM_W0 - w)) / int256(WAD); int256 f = int256(fee) + d; return f > 0 ? uint256(f) : 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"; } } ```