{
  "np2_check": {
    "n": 1000,
    "mean": 526.1155722459862,
    "target": 526.1156,
    "error": -2.7754013785852294e-05,
    "sha256": "51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0"
  },
  "variants": [
    {
      "name": "BASE",
      "w": 0.1,
      "gate_bps": 30,
      "n": 2000,
      "mean_diff": 0.10312176914142748,
      "se": 0.01267534949762333,
      "worst": [
        {
          "seed": 10870,
          "diff": -12.63679623677757
        },
        {
          "seed": 10846,
          "diff": -8.169419708641499
        },
        {
          "seed": 11968,
          "diff": -2.845424403297386
        },
        {
          "seed": 11316,
          "diff": -2.830900426198923
        },
        {
          "seed": 11268,
          "diff": -2.2323983818620263
        }
      ],
      "sha256": "abee910ebd6e8f88b55482702858e7065171136363feec0ae11b5865cfaa0fb1"
    },
    {
      "name": "W15",
      "w": 0.15,
      "gate_bps": 30,
      "n": 2000,
      "mean_diff": 0.06043978648054212,
      "se": 0.011386984170832672,
      "worst": [
        {
          "seed": 11268,
          "diff": -3.0288060223706452
        },
        {
          "seed": 11907,
          "diff": -2.700684186176659
        },
        {
          "seed": 11316,
          "diff": -2.537839139055052
        },
        {
          "seed": 11240,
          "diff": -1.8234340226263157
        },
        {
          "seed": 11392,
          "diff": -1.8200021969652198
        }
      ],
      "sha256": "30e7a95f46f572be12b623eebcbe15e13bb8cf82b34348f1a39eabd445335beb"
    },
    {
      "name": "W20",
      "w": 0.2,
      "gate_bps": 30,
      "n": 2000,
      "mean_diff": -0.17831414453017447,
      "se": 0.01613346475537521,
      "worst": [
        {
          "seed": 11392,
          "diff": -8.006867539991504
        },
        {
          "seed": 10475,
          "diff": -5.569579232831984
        },
        {
          "seed": 11907,
          "diff": -5.0998168869436995
        },
        {
          "seed": 10609,
          "diff": -3.811525048829594
        },
        {
          "seed": 11815,
          "diff": -2.9533234002281006
        }
      ],
      "sha256": "b8e58387ccb2749377bbd69829ccaf01d646165c79d3b26273115f7709e0479f"
    },
    {
      "name": "W30",
      "w": 0.3,
      "gate_bps": 30,
      "n": 2000,
      "mean_diff": -1.2275708007414097,
      "se": 0.03000391033718654,
      "worst": [
        {
          "seed": 11392,
          "diff": -13.534007963899285
        },
        {
          "seed": 10775,
          "diff": -10.006550837354666
        },
        {
          "seed": 11907,
          "diff": -8.627577665320587
        },
        {
          "seed": 10475,
          "diff": -8.465596508830572
        },
        {
          "seed": 10159,
          "diff": -7.727233449805794
        }
      ],
      "sha256": "849e3bdca63e694f56f588a5d9bbe7d0a27b91db70c944b099ce07c0743ea5a4"
    },
    {
      "name": "G20",
      "w": 0.1,
      "gate_bps": 20,
      "n": 2000,
      "mean_diff": 0.06464950584616366,
      "se": 0.012541840659872443,
      "worst": [
        {
          "seed": 10870,
          "diff": -12.641889336110182
        },
        {
          "seed": 10846,
          "diff": -8.081386972697828
        },
        {
          "seed": 11240,
          "diff": -1.851536485853103
        },
        {
          "seed": 10609,
          "diff": -1.7894067979111696
        },
        {
          "seed": 10839,
          "diff": -1.6502112772932378
        }
      ],
      "sha256": "985e4a5855f4004b27c3957bb00ddc341554af4b3584b2da4c4e7aa0247a1270"
    }
  ],
  "chosen": "BASE",
  "paired": {
    "n": 2000,
    "mean_diff": 0.10764996146159848,
    "se": 0.009847618419198933,
    "worst": [
      {
        "seed": 2700867,
        "diff": -2.6045141393083213
      },
      {
        "seed": 2701030,
        "diff": -1.8113880894445629
      },
      {
        "seed": 2701174,
        "diff": -1.6798430530761834
      },
      {
        "seed": 2700012,
        "diff": -1.619681276368965
      },
      {
        "seed": 2700973,
        "diff": -1.5967145897195678
      }
    ]
  },
  "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 = 30000000000000000;\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\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 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma);\n        fMid = fMid + 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        if (sellPressure) {\n            bidFee = fMid + skew;\n            askFee = fMid > skew ? fMid - skew : 0;\n        } else {\n            askFee = fMid + skew;\n            bidFee = fMid > skew ? fMid - skew : 0;\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 _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": "abee910ebd6e8f88b55482702858e7065171136363feec0ae11b5865cfaa0fb1",
  "blocked": null,
  "next": "Retain BASE as the small measured improvement from this item. Do not increase the routing weight or lower the fee gate on the evidence here; pursue other mechanisms for larger gains. No holdout retuning or leaderboard submission was performed.",
  "methodology": {
    "simulator_commit": "6b6921f8d463314aa7c96234e1be25a25245f704",
    "tuning_seeds": [
      10000,
      11999
    ],
    "confirmation_seeds": [
      2700000,
      2701999
    ],
    "batch_size": 20,
    "workers": 8,
    "comparison": "Separate candidate-vs-vanilla and NP2-vs-vanilla markets; subtract submission edge per seed.",
    "se": "sample std(ddof=1)/sqrt(n)",
    "validation": "amm-match validate passed for NP2, CH and all five variants.",
    "retail_alpha_wad": 30000000000000000,
    "wad_conversion": "int(round(v * 1e18)); the requested values are represented exactly by the integer literals in the code.",
    "draws": "numpy.random.default_rng(seed): size uniform(19,21), rate uniform(0.6,1), sigma uniform(0.000882,0.001008), in that order.",
    "market_seeds": "build_config(seed=s); pinned engine uses price seed=s and retail seed=s+1.",
    "fresh_block": "2700000..2701999 was unused in this run before freezing; both reference and chosen candidate were evaluated once on it.",
    "selection_rule": "Highest tuning mean only if its paired gain over BASE exceeds twice the SE of candidate-minus-BASE; otherwise BASE. Every alternative had a lower tuning mean than BASE.",
    "baseline": "NP2 reconstructed from the two specified artifacts; exact SHA-256 matches the supplied prefix. All five variants change PHAT_ALPHA_RETAIL from 0.06 to 0.03."
  },
  "ch_check": {
    "n": 1000,
    "mean": 525.5258821780834,
    "target": 525.525882178,
    "error": 8.344613888766617e-11,
    "sha256": "6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3"
  },
  "selection": {
    "chosen": "BASE",
    "sha256": "abee910ebd6e8f88b55482702858e7065171136363feec0ae11b5865cfaa0fb1",
    "fresh_start": 2700000,
    "selection": "Highest mean among variants beating BASE by two SE of paired variant-minus-BASE differences; otherwise BASE.",
    "base_comparisons": {
      "W15": {
        "n": 2000,
        "mean_diff": -0.042681982660885354,
        "se": 0.012120603428298165,
        "worst": [
          {
            "seed": 11907,
            "diff": -2.7536431983679677
          },
          {
            "seed": 11757,
            "diff": -2.5603881372023807
          },
          {
            "seed": 11147,
            "diff": -2.190836494541827
          },
          {
            "seed": 11163,
            "diff": -1.949760965793189
          },
          {
            "seed": 11815,
            "diff": -1.6091071089369962
          }
        ]
      },
      "W20": {
        "n": 2000,
        "mean_diff": -0.281435913671602,
        "se": 0.01739817360651589,
        "worst": [
          {
            "seed": 11392,
            "diff": -7.318939738678409
          },
          {
            "seed": 11907,
            "diff": -5.152775899135008
          },
          {
            "seed": 10475,
            "diff": -4.373181636655886
          },
          {
            "seed": 10609,
            "diff": -4.0243891559754275
          },
          {
            "seed": 11163,
            "diff": -3.4353907315022525
          }
        ]
      },
      "W30": {
        "n": 2000,
        "mean_diff": -1.3306925698828367,
        "se": 0.031110792135153536,
        "worst": [
          {
            "seed": 11392,
            "diff": -12.84608016258619
          },
          {
            "seed": 10775,
            "diff": -9.72606416526844
          },
          {
            "seed": 11907,
            "diff": -8.680536677511896
          },
          {
            "seed": 10789,
            "diff": -7.702469577214856
          },
          {
            "seed": 10193,
            "diff": -7.5905269851355115
          }
        ]
      },
      "G20": {
        "n": 2000,
        "mean_diff": -0.03847226329526381,
        "se": 0.006652140623197343,
        "worst": [
          {
            "seed": 10839,
            "diff": -2.9517751307942035
          },
          {
            "seed": 10609,
            "diff": -2.002270905057003
          },
          {
            "seed": 10645,
            "diff": -1.6000196862977418
          },
          {
            "seed": 11669,
            "diff": -1.5745536116568246
          },
          {
            "seed": 11502,
            "diff": -1.2073123464745095
          }
        ]
      }
    }
  },
  "confirmation_scores": {
    "NP2": 525.7962807344512,
    "BASE": 525.9039306959129
  }
}
