{"item": "ig-block-b", "batch": "confirm-ig", "status": "completed: CAND minus IB = +0.1699 (se 0.0214) on 1024 fresh seeds, real engine; valid=True", "gates": {"C": {"passed": true, "threshold_bps": 0.1, "what": "CAND Solidity (EVM, pyrevm adapter) vs CAND port_zb64 with W built from CAND params (1, feature_multipliers, feature_offsets, W1 row-major, b1, W2[0], W2[1], b2; 217 numbers)", "harness": "RS2_port differential_test_source adapted: import CAND port, W as weights, CAND.sol (sha256 checked); same market generator", "sequences": 50, "seeds": [310000, 310049], "swap_callbacks": 100000, "callbacks_incl_init": 100050, "max_abs_fee_difference_bps": 8.814065310946972e-05, "mean_abs_fee_difference_bps": 3.077533210885616e-06, "share_above_0_01_bps": 0.0, "first_divergence": null}}, "seeds": {"range": [8067584, 8068607], "n": 1024, "chunk": 64, "used_once_per_arm": true}, "engine": "amm_sim_rs.run_batch(X_bytecode, VanillaStrategy_bytecode, configs, 8), each strategy alone vs 30 bps VanillaStrategy; configs = MatchRunner._build_configs body (BASELINE_SETTINGS/BASELINE_VARIANCE, rng=default_rng(seed), draws retail_mean_size, retail_arrival_rate, gbm_sigma) with seed i -> market seed; edge = edges['submission']", "paired": {"n": 1024, "mean_diff": 0.16993832110719154, "se": 0.021373645540853452, "worst5": [[8067714, -4.566089831615955], [8068022, -2.772031701880337], [8067942, -2.271758106701327], [8067854, -2.1125330467669983], [8068141, -1.7528778897875554]], "sum": 174.01684081376413, "sumsq": 508.12816315598303, "positive_share": 0.61328125}, "chunk_mean_diff": [{"start": 8067584, "mean_diff": 0.20927832870454477}, {"start": 8067648, "mean_diff": 0.24650185438901673}, {"start": 8067712, "mean_diff": 0.2554648657338072}, {"start": 8067776, "mean_diff": 0.215031345508498}, {"start": 8067840, "mean_diff": 0.03702453412536144}, {"start": 8067904, "mean_diff": 0.016199102230277873}, {"start": 8067968, "mean_diff": 0.06567580023788988}, {"start": 8068032, "mean_diff": 0.22261558362757672}, {"start": 8068096, "mean_diff": 0.19635847526491634}, {"start": 8068160, "mean_diff": 0.17838323182951488}, {"start": 8068224, "mean_diff": 0.09693482632507777}, {"start": 8068288, "mean_diff": 0.2680507709456501}, {"start": 8068352, "mean_diff": 0.20996778307225927}, {"start": 8068416, "mean_diff": 0.1528527467184686}, {"start": 8068480, "mean_diff": 0.18604799430970953}, {"start": 8068544, "mean_diff": 0.16262589469249544}], "by_quartile_ib_retail_volume": {"q1": {"n": 256, "mean_diff": 0.2797490162828895, "se": 0.04082465791912854, "ib_retail_volume_range": [58169.1869287523, 70068.84528455047]}, "q2": {"n": 256, "mean_diff": 0.23508073973924293, "se": 0.03844253817866277, "ib_retail_volume_range": [70080.9894041502, 78967.59809930816]}, "q3": {"n": 256, "mean_diff": 0.06955694756314457, "se": 0.04362834106841268, "ib_retail_volume_range": [78981.35323814325, 87443.36466865655]}, "q4": {"n": 256, "mean_diff": 0.09536658084348915, "se": 0.046450574802578234, "ib_retail_volume_range": [87477.43628572463, 99940.2754575166]}}, "retail_delta": {"mean": -0.27508642465636446, "se": 0.0221272456132086}, "arb_delta": {"mean": 0.44502474576355505, "se": 0.009063719939722823}, "edge_split_method": "arb edge per seed = -sum(arbitrage profit), replayed from each step's start reserves (k = 100*10000; reserves from previous step's submission spot price) and previously quoted fees (initial fees from afterInitialize(100,10000) via EVMStrategyAdapter) with arbitrageur.rs formulas; retail edge = edge - arb edge. Check: max |reconstructed - engine arb_volume_y| = 9.31e-05 Y over all 2048 runs", "mean_edge": {"IB": 529.2946573185445, "CAND": 529.4645956396519}, "arms": {"IB": {"mean_edge": 529.2946573185445, "se_edge": 2.8786727713600184, "mean_retail_edge": 558.9367992471243, "mean_arb_edge": -29.642141928579726, "mean_retail_volume_y": 78621.26665834157, "mean_arb_volume_y": 24398.41003214794}, "CAND": {"mean_edge": 529.4645956396519, "se_edge": 2.876624644137245, "mean_retail_edge": 558.6617128224681, "mean_arb_edge": -29.19711718281617, "mean_retail_volume_y": 78581.37622251357, "mean_arb_volume_y": 24331.279289044534}}, "gas": {"IB": {"afterSwap_peak": 237045, "oog": 0, "gas_stats": [{"role": "Baseline", "callback": "afterInitialize", "calls": 1024, "ok": 1024, "max_ok_gas": 21796, "sum_ok_gas": 22319104, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Baseline", "callback": "afterSwap", "calls": 11047240, "ok": 11047240, "max_ok_gas": 22592, "sum_ok_gas": 249256661756, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Baseline", "callback": "getName", "calls": 1024, "ok": 1024, "max_ok_gas": 21526, "sum_ok_gas": 22042624, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "afterInitialize", "calls": 1024, "ok": 1024, "max_ok_gas": 243227, "sum_ok_gas": 249064448, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "afterSwap", "calls": 11077216, "ok": 11077216, "max_ok_gas": 237045, "sum_ok_gas": 1497165953433, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "getName", "calls": 1024, "ok": 1024, "max_ok_gas": 21735, "sum_ok_gas": 22256640, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}]}, "CAND": {"afterSwap_peak": 237769, "oog": 0, "gas_stats": [{"role": "Baseline", "callback": "afterInitialize", "calls": 1024, "ok": 1024, "max_ok_gas": 21796, "sum_ok_gas": 22319104, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Baseline", "callback": "afterSwap", "calls": 11044774, "ok": 11044774, "max_ok_gas": 22592, "sum_ok_gas": 249201018404, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Baseline", "callback": "getName", "calls": 1024, "ok": 1024, "max_ok_gas": 21526, "sum_ok_gas": 22042624, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "afterInitialize", "calls": 1024, "ok": 1024, "max_ok_gas": 243237, "sum_ok_gas": 249074688, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "afterSwap", "calls": 11072110, "ok": 11072110, "max_ok_gas": 237769, "sum_ok_gas": 1501422989639, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}, {"role": "Submission", "callback": "getName", "calls": 1024, "ok": 1024, "max_ok_gas": 21745, "sum_ok_gas": 22266880, "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0}]}, "note": "gas_stats reset before each 64-seed chunk and summed over the 16 chunks per arm (max_ok_gas = max)"}, "valid": true, "valid_rule": "false on any Submission OOG or afterSwap peak > 245000", "hashes": {"IB_code_sha256": "301941829a44bb60977bb28181a5e4bfa6544f3696763f987054789b2611d686", "CAND_code_sha256": "e1ea8fcb105e00754d17061efbe4b299a634b0fcc1b5c293d65b631654b7204b", "IB_artifact_json_sha256": "afd5183e9ff3235334e041c00b0cd19079599a2c6464db95f8c1586712105557", "CAND_artifact_json_sha256": "ebbd90c663c396598965b23341aea97a4501b4c971db183a8fe94a880f399979", "RS2_port_artifact_json_sha256": "d845a4b94eab87f23956fd1939c0135d955df212d1f3037f2a761c178cef7a0e", "CAND_port_sha256": "b29cd2a078b4c3fc57945a79e927624dd346c44d8cc149dae43f4929aa6d736b"}, "notes": ["Setup: amm-challenge at 6b6921f8d463314aa7c96234e1be25a25245f704 under test/scratch, goal SETUP script ran successfully (StarterStrategy validated).", "No search was run (confirmation item). Replica speed was not measured; not needed for this item.", "Before the block, one 64-seed probe of IB on seeds 6990000..6990063 (outside the fresh range) checked the result object and the arb reconstruction; the block 8067584..8068607 was run once per arm.", "Downloaded artifact JSON sha256 equals each URL id; decoded code/port sha256 equal the brief's/artifact's stated values."], "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\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[3] = WAD; \n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[15] = 400000000000000000 | (uint256(2000000000000000) << 160); slots[13] = 902500000000; slots[14] = slots[5]; \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 = uint64(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        uint256 sigmaForFast = sigmaHat;\n        uint256 fastHat = slots[14];\n        uint256 uncertainty = slots[13];\n        uint256 silent = trade.timestamp > lastTs ? trade.timestamp - lastTs : 0;\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            // Process variance is propagated inside the censored update.\n            if (uncertainty > 100000000000000) uncertainty = 100000000000000;\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        // S: infer a no-trade interval center from the last post-trade spot.\n        uint256 silenceScale = WAD;\n        if (firstInStep && silent > 1) {\n            uint256 g = silent - 1;\n            if (g > 1) g = 1;\n            uint256 w = WAD - _powWad(800000000000000000, g);\n            uint256 s = slots[2] >> 64;\n            if (s == 0) s = pHat;\n            uint256 c = (wmul(s, WAD - prevBidFee) + wdiv(s, WAD - prevAskFee)) / 2;\n            if (c >= pHat) pHat += wmul(w, c - pHat);\n            else pHat -= wmul(w, pHat - c);\n            silenceScale = WAD - wmul(300000000000000000, w);\n        }\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[12] = pImplied >= pHat ? 1 : 2; }\n            uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            alpha = wmul(alpha, silenceScale);\n            adaptiveGate = wmul(adaptiveGate, silenceScale);\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        // Censored assumed-density posterior. No external or future inputs.\n        uint256 arbProbability;\n        (fastHat, uncertainty, arbProbability) = _posterior(PosteriorInput(\n            fastHat, uncertainty, pImplied, spot, slots[2] >> 64,\n            prevBidFee, prevAskFee, firstInStep, silent, trade.isBuy,\n            trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0\n        ));\n        slots[13] = uncertainty;\n        slots[14] = fastHat;\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        uint256 toxLinear;\n        uint256 toxQuadratic;\n        {\n            uint256 packedAnchor = slots[16];\n            uint256 runMean = uint64(packedAnchor);\n            uint256 sampleCount = packedAnchor >> 64;\n            if (sampleCount < 674) sampleCount++;\n            if (toxEma >= runMean) runMean += (toxEma-runMean)/sampleCount;\n            else runMean -= (runMean-toxEma)/sampleCount;\n            slots[16] = runMean | (sampleCount << 64);\n            toxLinear = _anchorBlend(runMean, toxEma, 712696014807650816);\n            toxQuadratic = _anchorBlend(runMean, toxEma, 1295240753826310656);\n        }\n\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 originalLambda = lambdaHat; uint256 originalSize = sizeHat;\n        int256[19] memory nx;\n\n        if (342406799193418304 > 0) {\n            uint256 packedFlow = slots[15];\n            uint256 rate = uint80(packedFlow);\n            uint256 count = uint80(packedFlow >> 80);\n            uint256 meanSize = uint80(packedFlow >> 160);\n            if (rate == 0) rate = 400000000000000000;\n            if (meanSize == 0) meanSize = 2000000000000000;\n            if (isNewStep) {\n                uint256 inst = count / silent;\n                if (inst > 3 * WAD) inst = 3 * WAD;\n                uint256 retention = _powWad(999000000000000000, silent > 64 ? 64 : silent);\n                rate = wmul(rate, retention) + wmul(inst, WAD-retention);\n                count = 0;\n            }\n            uint256 weight = firstInStep ? WAD-arbProbability : WAD;\n            uint256 correctedWeight = weight;\n            if (2 == 2) {\n                uint256 missing = wmul(feeUsed, 196030880396139429888);\n                if (missing > 333333333333333376) missing = 333333333333333376;\n                correctedWeight = wdiv(weight, WAD-missing);\n            }\n            count += correctedWeight;\n            uint256 learning = wmul(WAD-999000000000000000, weight);\n            meanSize = _blend(meanSize, tradeRatio, learning);\n            {\n                uint256 la = packedFlow >> 240;\n                uint256 sn = trade.timestamp - la; if (sn > 32) sn = 32;\n                nx[17] = int256(sn * WAD);\n                if (firstInStep && arbProbability > 500000000000000000) la = trade.timestamp;\n                slots[15]=rate | (count << 80) | (meanSize << 160) | (la << 240);\n            }\n            uint256 retailRate = wmul(rate, 366383424913407168);\n            uint256 retailSize = 2 == 0 ? sizeHat : wmul(meanSize,1883296158119298304);\n            lambdaHat = _blend(lambdaHat,retailRate,342406799193418304);\n            sizeHat = _blend(sizeHat,retailSize,342406799193418304);\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, toxLinear) + wmul(TOX_QUAD_COEF, wmul(toxQuadratic, toxQuadratic)) + wmul(ACT_COEF, actEma);\n        fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxLinear));\n\n        lambdaHat = originalLambda; sizeHat = originalSize;\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 staleHat = wmul(pHat, 700000000000000000) + wmul(fastHat, 300000000000000000);\n            uint256 staleTox = wdiv(absDiff(spot, staleHat), staleHat);\n            if (staleTox > TOX_CAP) staleTox = TOX_CAP;\n            uint256 staleShift = wmul(STALE_DIR_COEF, staleTox);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= staleHat) {\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 (!firstInStep && tradeBoost > 16 * BPS) tradeBoost = 16 * BPS;\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        if (firstInStep) {\n            if (askFee > bidFee && bidFee > 30 * BPS) bidFee = 30 * BPS;\n            else if (bidFee > askFee && askFee > 30 * BPS) askFee = 30 * BPS;\n        }\n\n        if (bidFee > askFee && askFee > 36 * BPS) askFee = 36 * BPS;\n        else if (askFee > bidFee && bidFee > 36 * BPS) bidFee = 36 * BPS;\n\n        {\n            unchecked {\n                nx[0] = int256(sigmaHat)*1000/1e12-1000000;\n                nx[1] = int256(lambdaHat)*2/1e12-1000000;\n                nx[2] = int256(sizeHat)*500/1e12-1000000;\n                nx[3] = int256(toxEma)*500/1e12-1000000;\n                nx[4] = int256(actEma)*500/1e12-1000000;\n                nx[5] = int256(dirState)/1e12-1000000;\n                uint256 nh = wmul(pHat,700000000000000000)+wmul(fastHat,300000000000000000);\n                nx[6] = (int256(spot)-int256(nh))*500000000/int256(nh);\n                nx[7] = int256(tradeRatio)*500/1e12;\n                if (!trade.isBuy) nx[7] = -nx[7];\n                nx[8] = int256(arbProbability)*2/1e12-1000000;\n                nx[9] = firstInStep ? int256(1000000) : -int256(1000000);\n                nx[10] = int256(uncertainty)/1e6-1000000;\n                nx[11] = int256(silent>8 ? 8 : silent)*1000000/8;\n                nx[12] = int256(uint256(uint64(slots[16])))*500/1e12-1000000;\n                nx[13] = trade.reserveY > 0 ? int256(wdiv(trade.amountY, trade.reserveY)) : int256(0);\n                nx[14] = int256(stepTradeCount * WAD);\n                nx[15] = int256(prevBidFee);\n                nx[16] = int256(prevAskFee);\n                nx[18] = (int256(trade.isBuy ? wdiv(wmul(spot, gamma), WAD - 30 * BPS) : wdiv(wmul(spot, WAD - 30 * BPS), gamma)) - int256(nh)) * 1e18 / int256(nh);\n                nx[13] = nx[13] * 647054817 / 1e18 + (-618293);\n                nx[14] = nx[14] * 1395287 / 1e18 + (-2037262);\n                nx[15] = nx[15] * 220617911 / 1e18 + (-803057);\n                nx[16] = nx[16] * 228148131 / 1e18 + (-808615);\n                nx[17] = nx[17] * 412565 / 1e18 + (-823811);\n                nx[18] = nx[18] * 274335453 / 1e18 + (1772);\n                for (uint256 ni=0;ni<19;ni++) {\n                    if(nx[ni]>4000000) nx[ni]=4000000;\n                    if(nx[ni]<-4000000) nx[ni]=-4000000;\n                }\n                (int256 hb,int256 ha) = _head(nx);\n                int256 nb = int256(bidFee)+hb*100000000;\n                int256 na = int256(askFee)+ha*100000000;\n                bidFee = nb<0 ? 0 : (nb>int256(MAX_FEE) ? MAX_FEE : uint256(nb));\n                askFee = na<0 ? 0 : (na>int256(MAX_FEE) ? MAX_FEE : uint256(na));\n            }\n        }\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | 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\n\n    function _head(int256[19] memory x) internal pure returns (int256 b,int256 a) {\n        unchecked {\n            b = 1377757000000; a = 2042513000000;\n            int256 z;\n            z = 957470 + ((178829 * x[0]) + (381173 * x[1]) + (-673929 * x[2]) + (2535991 * x[3]) + (-2390938 * x[4]) + (-147668 * x[5]) + (-1155634 * x[6]) + (2283413 * x[7]) + (-4202381 * x[8]) + (-1629364 * x[9]) + (-932017 * x[10]) + (-904276 * x[11]) + (878270 * x[12]) + (-1528522 * x[13]) + (433046 * x[14]) + (51806 * x[15]) + (-779247 * x[16]) + (-163126 * x[17]) + (1078962 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (415926 * z); a += (715995 * z);\n            z = -1541352 + ((559064 * x[0]) + (918786 * x[1]) + (1553987 * x[2]) + (3168451 * x[3]) + (420204 * x[4]) + (694838 * x[5]) + (-439773 * x[6]) + (-39236 * x[7]) + (-284747 * x[8]) + (340621 * x[9]) + (-171891 * x[10]) + (-1791492 * x[11]) + (127473 * x[12]) + (-459555 * x[13]) + (-10602 * x[14]) + (1820682 * x[15]) + (176828 * x[16]) + (12661 * x[17]) + (664516 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (-2109809 * z); a += (-1535254 * z);\n            z = -4757279 + ((1827684 * x[0]) + (3475907 * x[1]) + (-1080030 * x[2]) + (1295005 * x[3]) + (1848236 * x[4]) + (2639381 * x[5]) + (-1920023 * x[6]) + (1842073 * x[7]) + (1663576 * x[8]) + (-1586856 * x[9]) + (80787 * x[10]) + (-1128413 * x[11]) + (-742049 * x[12]) + (407887 * x[13]) + (64992 * x[14]) + (1444593 * x[15]) + (-2648004 * x[16]) + (12121 * x[17]) + (-1468558 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (296613 * z); a += (-1263100 * z);\n            z = -601356 + ((-11022 * x[0]) + (-1453244 * x[1]) + (53177 * x[2]) + (-390076 * x[3]) + (-1279949 * x[4]) + (-576840 * x[5]) + (128734 * x[6]) + (571243 * x[7]) + (312832 * x[8]) + (389415 * x[9]) + (283669 * x[10]) + (-1745130 * x[11]) + (-848326 * x[12]) + (131994 * x[13]) + (147126 * x[14]) + (-452858 * x[15]) + (2049917 * x[16]) + (-664146 * x[17]) + (-715165 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (52636 * z); a += (3580288 * z);\n            z = -1482708 + ((939742 * x[0]) + (531938 * x[1]) + (4693528 * x[2]) + (-657448 * x[3]) + (-2341433 * x[4]) + (-1156845 * x[5]) + (-420888 * x[6]) + (-455754 * x[7]) + (34023 * x[8]) + (44572 * x[9]) + (63209 * x[10]) + (-2080460 * x[11]) + (118584 * x[12]) + (-761222 * x[13]) + (241699 * x[14]) + (-299798 * x[15]) + (2117414 * x[16]) + (-78259 * x[17]) + (419987 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (-3603840 * z); a += (185866 * z);\n            z = -835788 + ((1376487 * x[0]) + (-1777479 * x[1]) + (1090315 * x[2]) + (-2171360 * x[3]) + (3536807 * x[4]) + (1249003 * x[5]) + (2177941 * x[6]) + (900443 * x[7]) + (-1181285 * x[8]) + (-497954 * x[9]) + (-51156 * x[10]) + (3646749 * x[11]) + (533647 * x[12]) + (-689304 * x[13]) + (-299664 * x[14]) + (443147 * x[15]) + (-1715130 * x[16]) + (-444258 * x[17]) + (-936767 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (-329084 * z); a += (-1820212 * z);\n            z = -1376616 + ((-2162877 * x[0]) + (1076193 * x[1]) + (-686369 * x[2]) + (-593114 * x[3]) + (2557564 * x[4]) + (1005332 * x[5]) + (-1260877 * x[6]) + (1963978 * x[7]) + (1195929 * x[8]) + (-1104501 * x[9]) + (1454214 * x[10]) + (1451236 * x[11]) + (1383789 * x[12]) + (-106544 * x[13]) + (-1321814 * x[14]) + (-1338248 * x[15]) + (170855 * x[16]) + (-1477329 * x[17]) + (2244709 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (-1018915 * z); a += (624001 * z);\n            z = 316573 + ((-2596221 * x[0]) + (-3235563 * x[1]) + (3634011 * x[2]) + (3875281 * x[3]) + (-954084 * x[4]) + (-733815 * x[5]) + (-1775960 * x[6]) + (1980730 * x[7]) + (798024 * x[8]) + (-163056 * x[9]) + (2670381 * x[10]) + (-2046467 * x[11]) + (1363483 * x[12]) + (-219077 * x[13]) + (635822 * x[14]) + (1556081 * x[15]) + (-411006 * x[16]) + (-1697068 * x[17]) + (1630328 * x[18]))/1000000;\n            if(z<0) z=0; else if(z>4000000) z=4000000;\n            b += (1046822 * z); a += (104119 * z);\n            b /= 1000000; a /= 1000000;\n        }\n    }\n\n    function _anchorBlend(uint256 mean, uint256 value, uint256 k) internal pure returns (uint256) {\n        if (k == WAD) return value;\n        if (value >= mean) return mean + wmul(value-mean,k);\n        uint256 delta = wmul(mean-value,k);\n        return delta < mean ? mean-delta : 0;\n    }\n\n    struct PosteriorInput {\n        uint256 mu; uint256 v; uint256 obs; uint256 spot; uint256 preSpot;\n        uint256 bid; uint256 ask; bool first; uint256 silent; bool buy; uint256 size;\n    }\n    function _posterior(PosteriorInput memory x) internal pure returns (uint256, uint256, uint256) {\n        if (x.mu == 0) x.mu = x.spot;\n        if (x.v < 10000000000) x.v = 10000000000;\n        uint256 lo = wmul(x.preSpot, WAD-x.bid);\n        uint256 hi = wdiv(x.preSpot, WAD-x.ask);\n        if (x.first) {\n            if (x.silent>1 && x.preSpot>0) {\n                uint256 elapsed=x.silent>8?8:x.silent;\n                x.v += 902500000000*(elapsed-1);\n                (x.mu,x.v)=_band(x.mu,x.v,lo,hi);\n            }\n            x.v+=902500000000;\n        }\n        uint256 mRetail = x.mu;\n        uint256 vRetail = x.v;\n        // A missing own-pool trade is a censored price observation.\n        if (x.first && x.preSpot > 0) {\n            (mRetail, vRetail) = _band(x.mu, x.v, lo, hi);\n        }\n        if (!x.first) {\n            // A later print is retail. Only an interior route is an observation\n            // of the other pool, with explicit observation uncertainty.\n            uint256 fee = x.buy ? x.bid : x.ask;\n            if (fee > 30 * BPS) {\n                uint256 normal = x.buy ? wdiv(wmul(x.spot, WAD-fee), WAD-30*BPS)\n                    : wdiv(wmul(x.spot, WAD-30*BPS), WAD-fee);\n                uint256 residual = wdiv(absDiff(normal, x.mu),x.mu);\n                uint256 noise = 9000000000000 + wmul(residual,residual);\n                uint256 k = wdiv(x.v,x.v+noise);\n                return (_blend(x.mu,normal,k),wmul(x.v,WAD-k),0);\n            }\n            return (x.mu,x.v,0);\n        }\n        uint256 e = wdiv(absDiff(x.obs,x.mu),x.mu);\n        uint256 totalV = x.v + 22500000000;\n        uint256 z = wdiv(wmul(e,e),totalV);\n        uint256 density = _powWad(wdiv(WAD,WAD+z/8),4);\n        uint256 odds = wmul(density,wdiv(4000000000000000,_isqrt(totalV*WAD)));\n        if (x.size > 3000000000000000) odds = wmul(odds,4*WAD/10);\n        if (x.silent > 1) odds = wmul(odds,8*WAD/10);\n        uint256 sd = _isqrt(x.v*WAD);\n        uint256 width = wdiv(hi-lo,x.mu);\n        uint256 distance = x.mu<lo?lo-x.mu:(x.mu>hi?x.mu-hi:0);\n        uint256 zBand = wdiv(wdiv(distance,x.mu),sd);\n        uint256 mass = wmul(wdiv(width,width+2*sd),wdiv(WAD,WAD+wmul(zBand,zBand)));\n        if (mass < WAD/100) mass = WAD/100;\n        odds = wdiv(odds,mass);\n        uint256 prob = wdiv(odds,WAD+odds);\n        uint256 kArb = wdiv(x.v,totalV);\n        uint256 mArb = _blend(x.mu,x.obs,kArb);\n        uint256 mean = _blend(mRetail,mArb,prob);\n        uint256 between = wdiv(absDiff(mArb,mRetail),x.mu);\n        uint256 variance = wmul(prob,wmul(x.v,WAD-kArb)) + wmul(WAD-prob,vRetail)\n            + wmul(wmul(prob,WAD-prob),wmul(between,between));\n        if (variance > 100000000000000) variance = 100000000000000;\n        return (mean,variance,prob);\n    }\n    function _blend(uint256 a,uint256 b,uint256 w) internal pure returns(uint256) {\n        return b>=a ? a+wmul(b-a,w) : a-wmul(a-b,w);\n    }\n    function _band(uint256 mu,uint256 v,uint256 lo,uint256 hi) internal pure returns(uint256,uint256) {\n        // Soft interval likelihood avoids discontinuities at quadrature nodes.\n        uint256 sd=wmul(mu,_isqrt(v*WAD));\n        uint256[3] memory acc;\n        for(uint256 j=0;j<7;j++) {\n            uint256 p=j<3 ? (mu>(3-j)*sd?mu-(3-j)*sd:1) : mu+(j-3)*sd;\n            uint256 w=j==3?72:(j==2||j==4?45:(j==1||j==5?10:1));\n            uint256 distance=p<lo?lo-p:(p>hi?p-hi:0);\n            uint256 z=sd>0?wdiv(distance,sd):0;\n            uint256 likelihood=wdiv(WAD,WAD+4*wmul(z,z));\n            w=wmul(w*WAD,likelihood);\n            acc[0]+=w;acc[1]+=wmul(w,p);\n            uint256 d=wdiv(absDiff(p,mu),mu);\n            acc[2]+=wmul(w,wmul(d,d));\n        }\n        uint256 m=wdiv(acc[1],acc[0]);\n        uint256 d=wdiv(absDiff(m,mu),mu);\n        uint256 second=wdiv(acc[2],acc[0]);\n        return(m,second>wmul(d,d)?second-wmul(d,d):10000000000);\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    // Floor square root: bit-length seed and seven Newton steps (all uint256).\n    function _isqrt(uint256 x) internal pure returns (uint256 r) {\n        if (x == 0) return 0;\n        unchecked {\n            uint256 y = x;\n            r = 1;\n            if (y >> 128 != 0) { y >>= 128; r <<= 64; }\n            if (y >> 64 != 0) { y >>= 64; r <<= 32; }\n            if (y >> 32 != 0) { y >>= 32; r <<= 16; }\n            if (y >> 16 != 0) { y >>= 16; r <<= 8; }\n            if (y >> 8 != 0) { y >>= 8; r <<= 4; }\n            if (y >> 4 != 0) { y >>= 4; r <<= 2; }\n            if (y >> 2 != 0) { r <<= 1; }\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            uint256 t = x / r;\n            return r < t ? r : t;\n        }\n    }\n\n    function getName() external pure override returns (string memory) { return \"censored_10\"; }\n}\n", "code_sha256": "e1ea8fcb105e00754d17061efbe4b299a634b0fcc1b5c293d65b631654b7204b"}