{
  "np2_check": {
    "n": 1000,
    "mean_edge": 526.1155722459862,
    "expected_rounded": 526.1156,
    "sha256": "51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0",
    "seed_start": 0,
    "seed_end": 999,
    "passed": true,
    "ch": {
      "n": 1000,
      "mean_edge": 525.5258821780834,
      "expected": 525.525882178,
      "sha256": "6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3",
      "passed": true,
      "absolute_error": 8.344613888766617e-11
    },
    "validation": {
      "ch": {
        "passed": true,
        "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'no_cubic' validated successfully!"
      },
      "np2": {
        "passed": true,
        "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'no_cubic' validated successfully!"
      },
      "cell_09": {
        "passed": true,
        "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'no_cubic' validated successfully!"
      },
      "cell_12": {
        "passed": true,
        "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'no_cubic' validated successfully!"
      },
      "cell_16": {
        "passed": true,
        "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'no_cubic' validated successfully!"
      }
    },
    "methodology": {
      "simulator_commit": "6b6921f8d463314aa7c96234e1be25a25245f704",
      "grid_seeds": {
        "start": 10000,
        "end": 11999,
        "n": 2000
      },
      "configuration": "For seed s: g=numpy.random.default_rng(s); size=g.uniform(19,21), rate=g.uniform(0.6,1), sigma=g.uniform(0.000882,0.001008), in that order; build_config(seed=s, gbm_sigma=sigma, retail_arrival_rate=rate, retail_mean_size=size). Price RNG seed=s; retail RNG seed=s+1. All fields checked equal MatchRunner._build_configs for 0..999.",
      "simulation": "Pinned, unmodified simulator; n_steps=10000; candidate vs vanilla and NP2 vs vanilla in separate amm_sim_rs.run_batch calls, 8 workers, at most 20 seeds per call; get_vanilla_bytecode_and_abi()[0]. One NP2 run on the 2000 grid seeds, reused for all cells.",
      "statistics": "d = candidate edges[submission] - NP2 edges[submission]; mean_diff=arithmetic mean(d); se=sample standard deviation(d,ddof=1)/sqrt(n); worst=five lowest d with seeds. Independently recomputed from saved per-seed edges using Python statistics.",
      "changes_only": "NP2 with PHAT_ALPHA_RETAIL=int(round(0.03*1e18)), PHAT_ALPHA=int(round(0.32*1e18)), PHAT_ALPHA_SAME=int(round(same*1e18)), same in [0.09,0.12,0.16]. Source identical after removing these three declarations.",
      "selection": "Maximum paired mean across the three prespecified grid cells; best_cell means best in this grid and does not imply an improvement over NP2. No confirm block run; no seeds from 2000000 upward used.",
      "evidence": "Scores and paired statistics measured locally in this task; external source quotations and URLs are in sources.json.",
      "provenance": {
        "two": "https://api.imdnotdead.fun/artifacts/528a6da78583c6142644a552c86670f9d25c20e39e92f4de44deae05ddc8cecd",
        "np_interior_block": "https://api.imdnotdead.fun/artifacts/da313379a2e69e9286668865e06b18dfcae25732c82b934b0fd9c2ef0d8c2ff5",
        "ch": "https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol",
        "simulator": "https://github.com/benedictbrady/amm-challenge/tree/6b6921f8d463314aa7c96234e1be25a25245f704"
      }
    }
  },
  "np2_mean_10000_11999": 523.8012740493459,
  "cells": [
    {
      "retail": 0.03,
      "same": 0.09,
      "first": 0.32,
      "sha256": "b4efc5bf09ba6feb9666a98b1a07b6e052e97c5ec5b96626e0b149bdc481da40",
      "n": 2000,
      "mean_diff": -0.6222094318299736,
      "se": 0.01693849589099995,
      "worst": [
        {
          "seed": 10846,
          "diff": -7.4602304358425044,
          "candidate_edge": 670.5570626703367,
          "np2_edge": 678.0172931061792
        },
        {
          "seed": 11316,
          "diff": -4.3052020296162254,
          "candidate_edge": 492.8642470843637,
          "np2_edge": 497.1694491139799
        },
        {
          "seed": 11692,
          "diff": -3.254345401211367,
          "candidate_edge": 551.9992349682966,
          "np2_edge": 555.253580369508
        },
        {
          "seed": 11268,
          "diff": -3.0882569748160904,
          "candidate_edge": 586.7880213453341,
          "np2_edge": 589.8762783201502
        },
        {
          "seed": 11601,
          "diff": -3.021723750481783,
          "candidate_edge": 556.848320991383,
          "np2_edge": 559.8700447418648
        }
      ]
    },
    {
      "retail": 0.03,
      "same": 0.12,
      "first": 0.32,
      "sha256": "8f0496efb725af351d7b3525c9ee7c34214108500c11ff3e0885978f68063818",
      "n": 2000,
      "mean_diff": -0.7207068399890696,
      "se": 0.01794083923100436,
      "worst": [
        {
          "seed": 10846,
          "diff": -7.779700846681067,
          "candidate_edge": 670.2375922594981,
          "np2_edge": 678.0172931061792
        },
        {
          "seed": 10515,
          "diff": -7.449572580305585,
          "candidate_edge": 639.2012001247534,
          "np2_edge": 646.650772705059
        },
        {
          "seed": 11316,
          "diff": -4.821923777909774,
          "candidate_edge": 492.34752533607013,
          "np2_edge": 497.1694491139799
        },
        {
          "seed": 10402,
          "diff": -4.1629195727749675,
          "candidate_edge": 633.7723052564769,
          "np2_edge": 637.9352248292519
        },
        {
          "seed": 11601,
          "diff": -3.6976096657272137,
          "candidate_edge": 556.1724350761376,
          "np2_edge": 559.8700447418648
        }
      ]
    },
    {
      "retail": 0.03,
      "same": 0.16,
      "first": 0.32,
      "sha256": "2a2964c03d6974a930dad118b774bdb0c4384e7e496974a18da78b09478ba95a",
      "n": 2000,
      "mean_diff": -0.9962670865042911,
      "se": 0.018677211980249266,
      "worst": [
        {
          "seed": 10846,
          "diff": -10.55004406826231,
          "candidate_edge": 667.4672490379169,
          "np2_edge": 678.0172931061792
        },
        {
          "seed": 11819,
          "diff": -7.677719768981717,
          "candidate_edge": 399.45519776827564,
          "np2_edge": 407.13291753725736
        },
        {
          "seed": 11316,
          "diff": -5.865582687892243,
          "candidate_edge": 491.30386642608767,
          "np2_edge": 497.1694491139799
        },
        {
          "seed": 10475,
          "diff": -3.975210794474606,
          "candidate_edge": 780.1528136539708,
          "np2_edge": 784.1280244484454
        },
        {
          "seed": 10879,
          "diff": -3.8728370643792687,
          "candidate_edge": 817.3755905878265,
          "np2_edge": 821.2484276522058
        }
      ]
    }
  ],
  "best_cell": {
    "retail": 0.03,
    "same": 0.09,
    "first": 0.32,
    "sha256": "b4efc5bf09ba6feb9666a98b1a07b6e052e97c5ec5b96626e0b149bdc481da40",
    "n": 2000,
    "mean_diff": -0.6222094318299736,
    "se": 0.01693849589099995,
    "worst": [
      {
        "seed": 10846,
        "diff": -7.4602304358425044,
        "candidate_edge": 670.5570626703367,
        "np2_edge": 678.0172931061792
      },
      {
        "seed": 11316,
        "diff": -4.3052020296162254,
        "candidate_edge": 492.8642470843637,
        "np2_edge": 497.1694491139799
      },
      {
        "seed": 11692,
        "diff": -3.254345401211367,
        "candidate_edge": 551.9992349682966,
        "np2_edge": 555.253580369508
      },
      {
        "seed": 11268,
        "diff": -3.0882569748160904,
        "candidate_edge": 586.7880213453341,
        "np2_edge": 589.8762783201502
      },
      {
        "seed": 11601,
        "diff": -3.021723750481783,
        "candidate_edge": 556.848320991383,
        "np2_edge": 559.8700447418648
      }
    ]
  },
  "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 = 320000000000000000;\n    uint256 constant PHAT_ALPHA_RETAIL = 30000000000000000;\n    uint256 constant PHAT_ALPHA_SAME = 90000000000000000;\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",
  "blocked": null,
  "next": "Retain NP2: all three tested cells have negative paired means. Give the grid results to the stack step as evidence against these settings; best_cell is the least losing cell, and its source is included as requested. No confirmation was run."
}
