# ep5-mech merge 2 Four reported results pass mean_diff >= 2*se: prespot, silence, EV-fee and nextmix. Prespot ranks first by t; silence has the largest mean gain. Arbfirst is flat; Kalman lacks confirmation. No combined strategy or additive gain is established. All performance figures below are item-reported, not independently rerun. Sources are the six reports linked below (`paired`, `tuning`, `delta`). ## Confirmation ranking Descending t=mean_diff/se (merge calculation, six decimals). Mean_diff is candidate minus NP2 edge; worst is the minimum paired difference. “Valid” means separate candidate/Vanilla and NP2/Vanilla markets as reported, not independently certified execution. Seed endpoints are inclusive. | Rank | Item/source | Mechanism | Confirm seeds | n | mean_diff | se | worst | t | Market method | Blocked step | |---:|---|---|---|---:|---:|---:|---:|---:|---|---| | 1 | [prespot-b][prespot-b] | Large-first-print alpha cut | 2650000..2651999 | 2000 | 0.3945690120943292 | 0.010624604223147479 | -2.814939168815499 | 37.137290 | Valid: separate Vanilla | None reported | | 2 | [silence-b][silence-b] | Silent-band pull and shrink | 2630000..2631999 | 2000 | 0.4104324429272151 | 0.01466784136652623 | -5.991013150057938 | 27.981789 | Valid: separate Vanilla | None reported | | 3 | [evfee-b][evfee-b] | Band-masked EV blend | 2660000..2661999 | 2000 | 0.2109 | 0.0085 | -2.1987 | 24.811765 | Valid: separate Vanilla | None reported | | 4 | [nextmix-b][nextmix-b] | Poisson quote mixture | 2670000..2671999 | 2000 | 0.046051090957665 | 0.00853719389603193 | -2.1657492202310777 | 5.394172 | Valid: separate Vanilla | None reported | | 5 | [arbfirst-b][arbfirst-b] | Escape/size posterior alpha | 2620000..2621999 | 2000 | -0.00021707923826105004 | 0.0036910362339696895 | -0.8421897563255243 | -0.058813 | Valid: separate Vanilla | None reported | | — | [kalman-b][kalman-b] | Kalman alpha | 2640000..2641999 planned, incomplete | Missing | Missing | Missing | Missing | — | Unknown | Confirmation: time limit | No item explicitly reports the invalid candidate-versus-NP2 shared-market method. Kalman's “paired vs NP2” does not specify its market opponent; it cannot be classified as valid or shared-market invalid. Excluded: no completed confirmation. Its frozen tuning-only estimate on 10000..10127 is n=128, mean_diff=-8.87, se=0.16, worst=-16.4. [kalman-b][kalman-b] ## Method and evidence qualifications - **Prespot:** Explicit separate Vanilla calls; CH=525.525882178 and NP2=526.1156 on 0..999, validation and 16-seed exact-match check reported. No gas measurement. The winner cuts alpha to 0.20 above amountY/reserveY=0.0025; it does not recover pre-spot. Tested pre-spot targets underperformed this cut. Tuning used 10000..10699. [prespot-b][prespot-b] - **Silence:** Explicit separate Vanilla arms and seed pairing; CH=525.5258821780834 and NP2=526.1155722459862 on 0..999 reported. Tuning blocks 10000..10255 and 11000..11511 froze w1=0.2, cap=1, shrink=1. Band-centre pull is heuristic. Reports validation, no-silence equivalence and stress checks. Its worst observed difference is more negative than other retained items', on different blocks. [silence-b][silence-b] - **EV-fee:** Official method specifies separate Vanilla runs; confirmation is presented under it. **CH calibration was explicitly not run**: full protocol compliance is incomplete. NP2=526.1155722 on 0..999 and final validation reported. Rounded metrics make t approximate. The own-RNG numba pre-screen is not official confirmation. The winning variant adds a 20..50 bps mask, fixed 20 Y order, interpolated split and pHat proxy for the other pool. Gas checks concern test variants; a larger-grid out-of-gas explanation was not directly measured. [evfee-b][evfee-b] - **Nextmix:** Explicit separate Vanilla calls; CH=525.525882178083 and NP2=526.115572245986 on 0..999, validation and stress checks reported. Tuning blocks 10000..10127 and 11000..11255 froze w100_c1_p2. First-print-as-arbitrage and lambdaHat including arbitrage prints are approximations. Tuning says “before either confirmation run,” but `paired` says the named block was used once. No second confirmation result is supplied; only the named block is ranked. [nextmix-b][nextmix-b] - **Arbfirst:** Explicit separate Vanilla calls; CH=525.5258821780834 and NP2=526.1155722459862 on 0..999, validation and callback checks reported. Its rational escape proxy is not an exact Gaussian posterior. Larger adjustments mostly lost; the frozen near-zero result fails the threshold. [arbfirst-b][arbfirst-b] - **Kalman:** All listed tuning points lost; NP2 reproduction reported, but no CH check, opponent specification or completed confirmation. Suggested bounded alpha modulation is future work. [kalman-b][kalman-b] The five ranked reports describe frozen candidates on disjoint 2,000-seed blocks above 2000000. Reuse outside these reports cannot be audited. Absolute scores from different blocks are not interchangeable; no combination was tested. [arbfirst-b][arbfirst-b], [silence-b][silence-b], [prespot-b][prespot-b], [evfee-b][evfee-b], [nextmix-b][nextmix-b] ## Source failures and missing evidence The requester flags failed source rechecks for arbfirst-b (2), silence-b (1), prespot-b (1), evfee-b (1), nextmix-b (2). Failed URLs/passages and exact errors are not identified in the supplied material. No particular URL can be labelled disproven. No warning is listed for Kalman; that is not independent verification. Retain local measurements as attributed reports with reduced confidence. External links do not prove execution. Baseline provenance and simulator-behavior support remain unresolved; the escape prior, silent-band inference, pre-spot interpretation, EV/router approximations and Poisson/ordering rationale are not independently verified here. Unidentified citation failures neither disprove nor validate paired results. These caveats apply to every flagged item and its retained code. Only the six Markdown results were supplied at the named paths. Raw per-seed outputs, runner scripts and nextmix's cited `sources.json` were not supplied there. SE/worst, execution, seed freshness and failed passages cannot be independently audited. Source links identify original report paths; inputs are not delivered separately. Essential results and qualifying code are embedded here. ## Blocked and incomplete work All six formal `blocked` fields are null; no structured step/error object is supplied. Kalman nevertheless reports: “Confirm block 2640000..2641999: NOT COMPLETED (the run exceeded the time limit; no confirm number exists).” Completed confirmation n is missing. EV-fee's missing CH check is a protocol omission, not a runtime block. Prespot dropped tuning point `add='p', a=1.0` after command timeout but completed confirmation. [kalman-b][kalman-b], [evfee-b][evfee-b], [prespot-b][prespot-b] ## Qualifying implementations Four entries pass the threshold under the reported separate-Vanilla criterion; EV-fee remains conditional because CH was omitted. Original delta, full code and sha256 sections follow verbatim. Code hashes cover fenced Solidity bytes including final newlines. These remain separate variants, not a combined executable strategy. ### prespot-b Source: [prespot-b][prespot-b], `delta`, `code`, `sha256`. ## delta Two constants and one line added to NP2; no slots, nothing else touched: ``` uint256 constant PHAT_ALPHA_LARGE = 200000000000000000; // 0.20 uint256 constant LARGE_FIRST_RATIO = 2500000000000000; // 0.0025 ... uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL; if (firstInStep && trade.reserveY > 0 && wdiv(trade.amountY, trade.reserveY) > LARGE_FIRST_RATIO) alpha = PHAT_ALPHA_LARGE; ``` A first print with amountY/reserveY > 0.0025 moves pHat at 0.20 instead of 0.27. Target stays pImplied. The contract does not compute a pre-trade spot: every variant that did scored below this (see tuning). ## code Full contract. The sha256 below is of this text, including its final newline. ```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_LARGE = 200000000000000000; uint256 constant LARGE_FIRST_RATIO = 2500000000000000; 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; // A large first print is often retail, so its pImplied carries its own impact: trust it less. if (firstInStep && trade.reserveY > 0 && wdiv(trade.amountY, trade.reserveY) > LARGE_FIRST_RATIO) alpha = PHAT_ALPHA_LARGE; 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)); } 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"; } } ``` ## sha256 `21d89fc048cf7f56701a64ef3f1707602d5bfa92b68735cb15f27609886b814d` NP2 base rebuilt from the two artifacts: `51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0`, which matches the prefix in the brief. ### silence-b Source: [silence-b][silence-b], `delta`, `code`, `sha256`. ## delta 24 lines added to NP2 for SILENCE only: add SILENCE_W1, SILENCE_CAP, SILENCE_SHRINK; initialize slot 13 to initial spot, then save each print's post-trade spot there. Before a new print, g=min(cap,number of fully silent steps): timestamp-lastTs-1 after a print, timestamp before the first. For g>0 use the stored spot and previous fees to form the old no-arbitrage band's arithmetic centre; pull pHat with w=1-(1-w1)^g. Multiply that print's gate and alpha by 1-shrink*w. No change to NP2's other constants or fee equations. No silence update on consecutive or same-step prints. ## 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 SILENCE_W1 = 200000000000000000; uint256 constant SILENCE_CAP = 1; uint256 constant SILENCE_SHRINK = 1000000000000000000; 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; slots[13] = slots[5]; 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]; // No swaps in intervening steps: the old spot and fee band persisted. uint256 silenceWeight; if (trade.timestamp > lastTs) { uint256 silent = trade.timestamp - lastTs; if (stepTradeCount > 0) --silent; // exclude the current print's step if (silent > SILENCE_CAP) silent = SILENCE_CAP; if (silent > 0 && prevAskFee < WAD) { silenceWeight = WAD - _powWad(WAD - SILENCE_W1, silent); uint256 oldSpot = slots[13]; uint256 centre = (wmul(oldSpot, WAD - prevBidFee) + wdiv(oldSpot, WAD - prevAskFee)) / 2; pHat = wmul(pHat, WAD - silenceWeight) + wmul(centre, silenceWeight); } } 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 (silenceWeight > 0) { uint256 shrinkFactor = WAD - wmul(SILENCE_SHRINK, silenceWeight); adaptiveGate = wmul(adaptiveGate, shrinkFactor); alpha = wmul(alpha, shrinkFactor); } 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[13] = spot; 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"; } } ``` ## sha256 `ffdf23dd87a4e101f80ac7c54eadda6ecc5808668c500ce704f2eb1f7a12fc25` Hash covers exactly the UTF-8 Solidity code text above, including its trailing newline(s), excluding Markdown fences. ### evfee-b Source: [evfee-b][evfee-b], `delta`, `code`, `sha256`. ## delta NP2's external body becomes internal `_npSwap`; dead `REVERT_U`/`oppDir`/`prePhat` code removed so it fits the stack (w=0 gave identical edges on 16 seeds). New `afterSwap` calls `_npSwap`, then for each side whose NP2 fee is in 20..50 bps: f* = argmax over 24,30..66 bps of 0.4*(f-g)*input(f) - loss*(ry/4)*s2*G((g-f)/sig), with input(f) the router split for a 20 Y order vs the 30 bps pool at pHat, g the spot-to-pHat gap on that side, s2 = sigmaHat^2 + (5 bps)^2, G(a) = E[(a+Z)+^2] (P(arb)*E[arb loss] in one closed form). Fee = 0.85*NP2 + 0.15*f*; slots[0..1] rewritten. Added: EV_* constants, `_evFee`, `_evInput`, `_evValue`, `_evG`, `_evT`. ## 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 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; // EV-FEE: per side, argmax f*share(f)*flow - P(arb)*E[arb loss], blended into NP2 by EV_W. uint256 constant EV_W = 1500 * BPS; uint256 constant EV_LOSS = 300 * BPS; uint256 constant EV_NP_MIN = 20 * BPS; uint256 constant EV_NP_MAX = 50 * BPS; uint256 constant EV_RATE = 400000000000000000; uint256 constant EV_SIZE = 20 * WAD; uint256 constant EV_SIG_FLOOR = 5 * BPS; uint256 constant EV_GN = WAD - 30 * BPS; int256 constant EV_IW = 1e18; uint256 constant EV_G0 = 155531932593661535509439217633609764404057129223931873107340792336220163; uint256 constant EV_G1 = 19756060248278132793504906396444574602741627683706482581582301539109711339; uint256 constant EV_G2 = 195427288969396225601208343948908710188678607846069930557563910332624134121; uint256 constant EV_G3 = 23694537670257982128393850425900606629390772774217; 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 bidFee, uint256 askFee) = _npSwap(trade); bool evAsk = askFee >= EV_NP_MIN && askFee <= EV_NP_MAX; bool evBid = bidFee >= EV_NP_MIN && bidFee <= EV_NP_MAX; if (evAsk || evBid) { uint256 pHat = slots[5]; uint256 sigmaHat = slots[6]; uint256 spot = wdiv(trade.reserveY, trade.reserveX); uint256 s2 = wmul(sigmaHat, sigmaHat) + wmul(EV_SIG_FLOOR, EV_SIG_FLOOR); uint256 sig = sqrt(s2 * WAD); uint256 lossK = wmul(wmul(EV_LOSS, trade.reserveY / 4), s2); uint256 k = wmul(trade.reserveX, trade.reserveY); int256 g = (2 * (int256(pHat) - int256(spot)) * EV_IW) / int256(pHat + spot); if (evAsk) { uint256 fa = _evFee(g, trade.reserveY, sqrt(wmul(k, pHat) * WAD), EV_SIZE, WAD, lossK, sig); askFee = wmul(askFee, WAD - EV_W) + wmul(fa, EV_W); slots[1] = askFee; } if (evBid) { uint256 fb = _evFee(-g, trade.reserveX, sqrt(wdiv(k, pHat) * WAD), wdiv(EV_SIZE, pHat), pHat, lossK, sig); bidFee = wmul(bidFee, WAD - EV_W) + wmul(fb, EV_W); slots[0] = bidFee; } } return (bidFee, askFee); } function _npSwap(TradeInfo calldata trade) internal 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; bool secondPrint = !firstInStep && stepTradeCount == 1; bool sameDir = false; if (secondPrint) { bool up2 = pImplied >= pHat; uint256 d1 = slots[12]; if (d1 != 0) { sameDir = (d1 == 1) == up2; } 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 (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); } /// Grid argmax of EV per step. own/other are this side's input reserve in the two pools /// (other = 30 bps pool at pHat, same k); size is the mean order in input units, px converts to Y. /// The router split is exact at both grid ends and linear between them (error under 0.01 Y). function _evFee(int256 g, uint256 own, uint256 other, uint256 size, uint256 px, uint256 lossK, uint256 sig) internal pure returns (uint256 bestFee) { int256 d0 = _evInput(24 * BPS, own, other, size); int256 step = (_evInput(66 * BPS, own, other, size) - d0) / 7; int256 best = type(int256).min; unchecked { for (uint256 i = 0; i < 8; i++) { uint256 f = (24 + i * 6) * BPS; int256 o = _evValue(f, g, d0 + int256(i) * step, size, px) - int256((lossK * _evG(((g - int256(f)) * EV_IW) / int256(sig))) / WAD); if (o > best) { best = o; bestFee = f; } } } } /// Exact unclipped router split, r = sqrt(gm / EV_GN) as a third-order series in f - 30 bps. function _evInput(uint256 f, uint256 own, uint256 other, uint256 size) internal pure returns (int256) { uint256 gm = WAD - f; int256 d = ((int256(gm) - int256(EV_GN)) * EV_IW) / int256(EV_GN); int256 d2 = (d * d) / EV_IW; uint256 r = uint256(EV_IW + d / 2 - d2 / 8 + ((d2 * d) / EV_IW) / 16); return ((int256(wmul(r, other + wmul(EV_GN, size))) - int256(own)) * EV_IW) / int256(gm + wmul(r, EV_GN)); } function _evValue(uint256 f, int256 g, int256 inp, uint256 size, uint256 px) internal pure returns (int256) { unchecked { if (inp < 0) inp = 0; if (inp > int256(size)) inp = int256(size); return ((((int256(f) - g) * ((inp * int256(px)) / EV_IW)) / EV_IW) * int256(EV_RATE)) / EV_IW; } } /// G(a) = E[(a + Z)_+^2] for standard normal Z: 0.25-step table, linear interpolation. function _evG(int256 a) internal pure returns (uint256) { if (a <= -4e18) return 0; if (a >= 4e18) return WAD + uint256((a * a) / int256(WAD)); unchecked { uint256 u = uint256(a + 4e18); uint256 i = u / 25e16; uint256 t = (u % 25e16) * 4; return ((WAD - t) * _evT(i) + t * _evT(i + 1)) / 1e6; } } function _evT(uint256 i) internal pure returns (uint256) { uint256 w = i / 9; uint256 word = w == 0 ? EV_G0 : w == 1 ? EV_G1 : w == 2 ? EV_G2 : EV_G3; return (word >> (28 * (i % 9))) & 0xFFFFFFF; } 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 `39b8552b89e220bda4cde6a2a64d9feee3544d07a9b41fa134cbdc54e741c743` (sha256 of the code text above, without the fence; the file ends with one newline) ### nextmix-b Source: [nextmix-b][nextmix-b], `delta`, `code`, `sha256`. ## 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`. ## Provenance and merge checks All six input identities and fenced code hashes matched their supplied values. Ranking and threshold arithmetic were recomputed. No compilation or simulation was run for this documentary merge. Input SHA-256 identities: - [arbfirst-b][arbfirst-b]: `dd785f4741360f111acb8d82ae1475984409e41915ba44269ac4c57ba7e0fd73`; `.imd/reads/artifacts/item-arbfirst-b`. - [silence-b][silence-b]: `2bedf7e371f3bae0b981d140075d55be3abb68786ad091ad2d5b0057cdd91338`; `.imd/reads/artifacts/item-silence-b`. - [kalman-b][kalman-b]: `d490644213c3dd450735c7a7be6b17d03b105ba035b49dc9b4d215d8daf02dd0`; `.imd/reads/artifacts/item-kalman-b`. - [prespot-b][prespot-b]: `887c370d93ef9d91b30103e52f6a214fc357d142d81dd2b1a5b43ab3b62e8bc8`; `.imd/reads/artifacts/item-prespot-b`. - [evfee-b][evfee-b]: `3dc54f060cbf6e800a6ec8e35118f485574ab1ebd64ad8a7520ef2a3fae65138`; `.imd/reads/artifacts/item-evfee-b`. - [nextmix-b][nextmix-b]: `61fd84282441186887f9db7f8459b86ebde4425e8b5b59be35342c40ad41f37a`; `.imd/reads/artifacts/item-nextmix-b`. [arbfirst-b]: ../../items/arbfirst-b/result.md [silence-b]: ../../items/silence-b/result.md [kalman-b]: ../../items/kalman-b/result.md [prespot-b]: ../../items/prespot-b/result.md [evfee-b]: ../../items/evfee-b/result.md [nextmix-b]: ../../items/nextmix-b/result.md