{"delta": "NP2 + EV fee. Slots/shifts/skew unchanged. fBase replaced per side by mix (1-W)*fBase+W*f*, f*=argmax over grid {2,6,11,16,22,30,50,120}bps of f*share(f)*T - C*0.5*E[max(s*d+z*sigmaHat-f,0)^2]; share per brief with R=1 (reserve-normalised), T=flowSize*TS, 997bps-normalizer r=sqrt((1-f)/.997); 5pt Gauss-Hermite; d=spot/pHat-1 signed per side (bid +d, ask -d), one-sided arb. Tox/act/sigma-tox extras kept. Final (best found, ~null): C=0, W=0.2, TS=1.", "code": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport {AMMStrategyBase} from \"./AMMStrategyBase.sol\";\nimport {TradeInfo} from \"./IAMMStrategy.sol\";\n\ncontract Strategy is AMMStrategyBase {\n    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 650;\n    uint256 constant DIR_DECAY = 750000000000000000;\n    uint256 constant ACT_DECAY = 530000000000000000;\n    uint256 constant SIZE_DECAY = 530000000000000000;\n    uint256 constant TOX_DECAY = 910000000000000000;\n    uint256 constant SIGMA_DECAY = 690000000000000000;\n    uint256 constant LAMBDA_DECAY = 990000000000000000;\n    uint256 constant SIZE_BLEND_DECAY = 828000000000000000;\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000;\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000;\n    uint256 constant PHAT_ALPHA = 270000000000000000;\n    uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000;\n    uint256 constant PHAT_ALPHA_SAME = 120000000000000000;\n    uint256 constant REVERT_U = 0;\n    uint256 constant DIR_IMPACT_MULT = 4;\n    uint256 constant GATE_SIGMA_MULT = 7200000000000000000;\n    uint256 constant MIN_GATE = 30000000000000000;\n    uint256 constant TRADE_TOX_BOOST = 3500 * BPS;\n    uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000;\n    uint256 constant RET_CAP = WAD / 35;\n    uint256 constant TOX_CAP = WAD / 5;\n    uint256 constant TRADE_RATIO_CAP = WAD / 5;\n    uint256 constant LAMBDA_CAP = 1820000000000000000;\n    uint256 constant STEP_COUNT_CAP = 64;\n    uint256 constant BASE_FEE = 4 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000;\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 300 * BPS;\n    uint256 constant TOX_QUAD_COEF = 15000 * BPS;\n    uint256 constant ACT_COEF = 91843 * BPS;\n    uint256 constant DIR_COEF = 20 * BPS;\n    uint256 constant DIR_TOX_COEF = 100 * BPS;\n    uint256 constant STALE_DIR_COEF = 6850 * BPS;\n    uint256 constant SIGMA_TOX_COEF = 500 * BPS;\n    uint256 constant TAIL_KNEE = 600 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000;\n    uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000;\n    uint256 constant EV_C = 0;\n    uint256 constant EV_W = 200000000000000000;\n    uint256 constant EV_TS = 1000000000000000000;\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0;\n        return (BASE_FEE, BASE_FEE);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 lastTs = slots[2];\n        uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10];\n\n        bool isNewStep = trade.timestamp > lastTs;\n        if (isNewStep) {\n            uint256 elapsedRaw = trade.timestamp - lastTs;\n            uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw;\n            dirState = _decayCentered(dirState, DIR_DECAY, elapsed);\n            actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed));\n            sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed));\n            toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed));\n            if (stepTradeCount > 0 && elapsedRaw > 0) {\n                uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw;\n                if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP;\n                lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY);\n            }\n            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\n        uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat;\n        if (pHat == 0) pHat = spot;\n\n        uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee;\n        uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0;\n        uint256 pImplied;\n        if (gamma == 0) { pImplied = spot; }\n        else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); }\n\n        {\n            uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0;\n            uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL;\n            uint256 prePhat = pHat;\n            bool secondPrint = !firstInStep && stepTradeCount == 1;\n            bool sameDir = false; bool oppDir = false;\n            if (secondPrint) {\n                bool up2 = pImplied >= pHat;\n                uint256 d1 = slots[12];\n                if (d1 != 0) { sameDir = (d1 == 1) == up2; oppDir = !sameDir; }\n                if (sameDir) alpha = PHAT_ALPHA_SAME;\n            }\n            if (firstInStep) { slots[11] = pHat; slots[12] = pImplied >= pHat ? 1 : 2; }\n            uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            if (ret <= adaptiveGate) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); }\n            if (oppDir && REVERT_U > 0) {\n                uint256 pre = slots[11];\n                if (pre > pHat) pHat = pHat + wmul(pre - pHat, REVERT_U);\n                else pHat = pHat - wmul(pHat - pre, REVERT_U);\n            }\n            if (firstInStep) {\n                if (ret > RET_CAP) ret = RET_CAP;\n                sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY);\n            }\n        }\n\n        // Interior retail routing equalizes fee-adjusted marginal prices.\n        // Subsequent trades in a step cannot be the initial arbitrage trade.\n        if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) {\n            uint256 normalSpot = trade.isBuy\n                ? wdiv(wmul(spot, gamma), WAD - 30 * BPS)\n                : wdiv(wmul(spot, WAD - 30 * BPS), gamma);\n            pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100);\n        }\n\n        uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0;\n        if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP;\n\n        if (tradeRatio > SIGNAL_THRESHOLD) {\n            uint256 push = tradeRatio * DIR_IMPACT_MULT;\n            if (push > WAD / 4) push = WAD / 4;\n            if (trade.isBuy) {\n                dirState = dirState + push;\n                if (dirState > 2 * WAD) dirState = 2 * WAD;\n            } else {\n                dirState = dirState > push ? dirState - push : 0;\n            }\n            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\n            sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY);\n            if (sizeHat > WAD) sizeHat = WAD;\n        }\n\n        uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0;\n        if (tox > TOX_CAP) tox = TOX_CAP;\n        toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY);\n        uint256 toxSignal = toxEma;\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 flowSize = wmul(lambdaHat, sizeHat);\n        uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize);\n        uint256 extra = wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma);\n        extra = extra + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));\n\n        uint256 dirDev; bool sellPressure;\n        if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; }\n        else { dirDev = WAD - dirState; sellPressure = false; }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n        uint256 bidFee; uint256 askFee;\n        {\n            (uint256 evB, uint256 evA) = _evFees(wmul(flowSize, EV_TS), sigmaHat, spot, pHat);\n            uint256 midB = _mix(fBase, evB) + extra;\n            uint256 midA = _mix(fBase, evA) + extra;\n            if (sellPressure) {\n                bidFee = midB + skew;\n                askFee = midA > skew ? midA - skew : 0;\n            } else {\n                askFee = midA + skew;\n                bidFee = midB > skew ? midB - skew : 0;\n            }\n        }\n\n        {\n            uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= pHat) {\n                bidFee = bidFee + staleShift;\n                askFee = askFee > attractShift ? askFee - attractShift : 0;\n            } else {\n                askFee = askFee + staleShift;\n                bidFee = bidFee > attractShift ? bidFee - attractShift : 0;\n            }\n        }\n\n        {\n            bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat);\n            if (tradeAligned) {\n                uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);\n                if (trade.isBuy) { bidFee = bidFee + tradeBoost; }\n                else { askFee = askFee + tradeBoost; }\n            }\n        }\n\n        if (sellPressure) {\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT));\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT));\n        } else {\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT));\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT));\n        }\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = trade.timestamp;\n        slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[9] = toxEma; slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n    function _mix(uint256 a, uint256 b) internal pure returns (uint256) {\n        return wmul(a, WAD - EV_W) + wmul(b, EV_W);\n    }\n\n    function _grid(uint256 k) internal pure returns (uint256) {\n        if (k == 0) return 2 * BPS;\n        if (k == 1) return 6 * BPS;\n        if (k == 2) return 11 * BPS;\n        if (k == 3) return 16 * BPS;\n        if (k == 4) return 22 * BPS;\n        if (k == 5) return 30 * BPS;\n        if (k == 6) return 50 * BPS;\n        return 120 * BPS;\n    }\n\n    // Expected-value fee: maximise f*share(f)*T - c*arbLoss(f) on an 8-point grid.\n    function _evFees(uint256 T, uint256 sigma, uint256 spot, uint256 pHat) internal pure returns (uint256, uint256) {\n        if (T == 0 || pHat == 0) return (BASE_FEE, BASE_FEE);\n        int256 d = int256(wdiv(spot, pHat)) - int256(WAD);\n        int256 bestB = type(int256).min; int256 bestA = type(int256).min;\n        uint256 fB = BASE_FEE; uint256 fA = BASE_FEE;\n        uint256 tg = wmul(997000000000000000, T);\n        for (uint256 k = 0; k < 8; k++) {\n            uint256 f = _grid(k);\n            uint256 rev;\n            {\n                uint256 g = WAD - f;\n                uint256 v = (g * WAD) / 997000000000000000;\n                uint256 r = WAD;\n                for (uint256 i = 0; i < 5; i++) { r = (r + (v * WAD) / r) / 2; }\n                uint256 a = wmul(r, WAD + tg);\n                if (a > WAD) {\n                    uint256 sh = wdiv(a - WAD, wmul(g + wmul(997000000000000000, r), T));\n                    if (sh > WAD) sh = WAD;\n                    rev = wmul(wmul(f, sh), T);\n                }\n            }\n            int256 vb = int256(rev) - int256(wmul(EV_C, _loss(d, sigma, f)));\n            int256 va = int256(rev) - int256(wmul(EV_C, _loss(-d, sigma, f)));\n            if (vb > bestB) { bestB = vb; fB = f; }\n            if (va > bestA) { bestA = va; fA = f; }\n        }\n        return (fB, fA);\n    }\n\n    // 0.5 * E[max(m + z*sigma - f, 0)^2], z ~ N(0,1) by 5-point Gauss-Hermite.\n    function _loss(int256 m, uint256 sigma, uint256 f) internal pure returns (uint256 acc) {\n        int256 zs0 = int256(wmul(1355626179974265600, sigma));\n        int256 zs1 = int256(wmul(2856970013872805800, sigma));\n        int256 base = m - int256(f);\n        acc = wmul(533333333333333333, _sq(base));\n        acc += wmul(222075922005612600, _sq(base + zs0) + _sq(base - zs0));\n        acc += wmul(11257411327720700, _sq(base + zs1) + _sq(base - zs1));\n        acc = acc / 2;\n    }\n\n    function _sq(int256 x) internal pure returns (uint256) {\n        if (x <= 0) return 0;\n        return wmul(uint256(x), uint256(x));\n    }\n\n    function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) {\n        if (fee <= TAIL_KNEE) return fee;\n        return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope);\n    }\n\n    function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) {\n        result = WAD;\n        while (exp > 0) {\n            if (exp & 1 == 1) result = wmul(result, factor);\n            factor = wmul(factor, factor);\n            exp >>= 1;\n        }\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    function getName() external pure override returns (string memory) { return \"no_cubic\"; }\n}\n", "sha256": "6b8fcb6fa7dc3865a874e85cf9a47c05122824f48422b6aacf97e689e40ddcb4", "np2_check": "NP2 rebuilt sha256 51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0; seeds 0..999 mean 526.1155722 (matches 526.1156). CH not separately run.", "tune": {"seeds": "10000..10239 (240 paired seeds, NP2 mean 522.853)", "best_mean_diff": -0.053, "grid": "(C,W,TS)->diff: C=0.01..100,W=1: -112..-133; C=1e-3,W=1:-59.7; 1e-4:-38; 1e-5:-24; C=0,W=1:-3.44; C=1e-7,W=.5:-3.47; C=0,W=.5:-0.75; C=0,W=.2:-0.053(se .034). Not positive anywhere; any C>0 hurts."}, "paired": {"n": 0, "mean_diff": null, "se": null, "worst": null, "note": "Confirm block 2320000+ NOT run: no tuned config beat NP2, and time was short. Delivered code is not shown to improve on NP2."}, "arb_volume": "not measured vs NP2 beyond one seed (10000): C=0,W=1 arb 28.9k vs NP2 24.5k; retail 80.1k vs 75.3k (single seed, Y units)", "retail_volume": "see arb_volume", "gas": "validate passes; harness gas counter reports flat 125000 (<250k), unreliable; grid loop is 8 sqrt-newton x 5 iters + 80 loss terms", "blocked": null, "next": "EV fee as specified does not help: loss term is mis-scaled vs sim (any C>0 hurts sharply); TWO's tox/shift terms already price stale flow. Try EV only to modulate fBase by +-few bps, or fit C via regression of realized arb loss."}