# delta Added lines; leading spaces omitted. ```solidity uint256 constant SIDE_CAP = 12 * BPS; uint256 constant STUCK_K = 20; uint256 constant PULL = 100000000000000000; slots[13] = 0; slots[14] = 0; if (trade.isBuy) slots[13] = trade.timestamp; else slots[14] = trade.timestamp; bidFee = _pull(bidFee, askFee, trade.timestamp - slots[13]); askFee = _pull(askFee, bidFee, trade.timestamp - slots[14]); uint256 feeSum = bidFee + askFee; if (bidFee > askFee + SIDE_CAP) { bidFee = (feeSum + SIDE_CAP) / 2; askFee = feeSum - bidFee; } if (askFee > bidFee + SIDE_CAP) { askFee = (feeSum + SIDE_CAP) / 2; bidFee = feeSum - askFee; } function _pull(uint256 fee, uint256 other, uint256 age) internal pure returns (uint256) { if (age <= STUCK_K) return fee; uint256 gap = wmul(absDiff(fee, other), _powWad(WAD - PULL, age - STUCK_K)); return fee > other ? other + gap : other - gap; } ``` # 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 SIDE_CAP = 12 * BPS; uint256 constant STUCK_K = 20; uint256 constant PULL = 100000000000000000; 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] = 0; slots[14] = 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)); } if (trade.isBuy) slots[13] = trade.timestamp; else slots[14] = trade.timestamp; bidFee = _pull(bidFee, askFee, trade.timestamp - slots[13]); askFee = _pull(askFee, bidFee, trade.timestamp - slots[14]); uint256 feeSum = bidFee + askFee; if (bidFee > askFee + SIDE_CAP) { bidFee = (feeSum + SIDE_CAP) / 2; askFee = feeSum - bidFee; } if (askFee > bidFee + SIDE_CAP) { askFee = (feeSum + SIDE_CAP) / 2; bidFee = feeSum - askFee; } 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 _pull(uint256 fee, uint256 other, uint256 age) internal pure returns (uint256) { if (age <= STUCK_K) return fee; uint256 gap = wmul(absDiff(fee, other), _powWad(WAD - PULL, age - STUCK_K)); return fee > other ? other + gap : other - gap; } 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 `df5cdad23036fe4176f2a0898ffc5cd1a2911f1d90be84d52936f954dc6be5b2` SHA-256 covers exactly the Solidity text inside the code fence, including its final newline. # tuning Rejected mechanism; retain NP2. This candidate caps the final bid–ask gap by moving both fees toward their midpoint, preserving their sum at the cap stage (integer rounding makes the gap at most the cap). Slots 13/14 track last bid/ask trade timestamps. Once age exceeds k, the inactive side’s raw fee distance to the other fee is multiplied by (1-pull)^(age-k), in either direction. The active side has age zero. Work occurs only on afterSwap callbacks; elapsed timestamps account for silent steps. All NP2 constants and prior logic remain unchanged. Grid: cap ∈ {4,8,12} bps; k ∈ {3,10,20} timestamps; pull ∈ {0.1,0.3,0.5}. Each point used seeds 10000–10199, n=200, direct `amm_sim_rs.run_batch(candidate, NP2, configs, 8)`. Selection maximized the paired mean. No confirmation seed was used for tuning. | Cap bps | k | Pull | Mean diff | SE | |---:|---:|---:|---:|---:| | 4 | 3 | 0.1 | -59.054690 | 1.166023 | | 4 | 3 | 0.3 | -66.088261 | 1.195049 | | 4 | 3 | 0.5 | -72.145679 | 1.249463 | | 4 | 10 | 0.1 | -55.495905 | 1.141180 | | 4 | 10 | 0.3 | -55.718853 | 1.135442 | | 4 | 10 | 0.5 | -55.863515 | 1.133339 | | 4 | 20 | 0.1 | -55.407903 | 1.143937 | | 4 | 20 | 0.3 | -55.408888 | 1.143915 | | 4 | 20 | 0.5 | -55.410696 | 1.143863 | | 8 | 3 | 0.1 | -54.779485 | 1.137486 | | 8 | 3 | 0.3 | -62.248590 | 1.168090 | | 8 | 3 | 0.5 | -68.509072 | 1.228688 | | 8 | 10 | 0.1 | -51.242823 | 1.116358 | | 8 | 10 | 0.3 | -51.470557 | 1.112629 | | 8 | 10 | 0.5 | -51.630720 | 1.111738 | | 8 | 20 | 0.1 | -51.133706 | 1.118428 | | 8 | 20 | 0.3 | -51.135632 | 1.118319 | | 8 | 20 | 0.5 | -51.139448 | 1.118142 | | 12 | 3 | 0.1 | -50.908986 | 1.106549 | | 12 | 3 | 0.3 | -58.521471 | 1.134006 | | 12 | 3 | 0.5 | -65.187262 | 1.191072 | | 12 | 10 | 0.1 | -47.263252 | 1.083965 | | 12 | 10 | 0.3 | -47.547789 | 1.079840 | | 12 | 10 | 0.5 | -47.725790 | 1.076856 | | 12 | 20 | 0.1 | -47.170443 | 1.086496 | | 12 | 20 | 0.3 | -47.173670 | 1.086417 | | 12 | 20 | 0.5 | -47.174741 | 1.086368 | Chosen: cap=12 bps, k=20, pull=0.1; tuning mean_diff=-47.170443. This is the least damaging tested grid point, not a recommendation to promote it. Earlier discarded pilots on the same tuning seeds only: lower the expensive fee to the cheaper fee plus cap, and pull only an expensive inactive fee (cap=4, k=3): | Pull | n | Mean diff | SE | |---:|---:|---:|---:| | 0.1 | 200 | -218.024213 | 3.483746 | | 0.3 | 200 | -218.025138 | 3.484726 | | 0.5 | 200 | -218.163726 | 3.484920 | An intermediate midpoint cap with an expensive-side-only pull (12 bps, k=20, pull=0.1) gave -47.190359 ± 1.093261 SE, n=200. The delivered mechanism pulls either inactive side toward the active side, as requested; the full grid above tests that version. Partial interrupted pilot runs were discarded and did not inform selection. Calibration against the simulator’s VanillaStrategy (30 bps), seeds 0–999: CH SHA-256 `6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3`, mean 525.5258821780834 (matches 525.525882178); NP2 SHA-256 `51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0`, mean 526.1155722459862 (matches 526.1156). Configuration for every run: 10,000 steps; initial price 100, reserves X=100/Y=10,000; μ=0, dt=1; retail buy probability 0.5, size σ=1.2. For each seed s, `rng=np.random.default_rng(s)` draws size uniform(19,21), then rate uniform(0.6,1.0), then price σ uniform(0.000882,0.001008); cfg.seed=s. The pinned engine supplies price seed s and retail seed s+1. Results were processed in chunks of 20 solely to bound memory. The calibration uses the 30 bps normalizer; all tuning/confirmation comparisons directly pit the candidate against NP2, so their edge levels are not the calibration score. Self-pair control, NP2 versus NP2, seeds 12000–12199, n=200: mean difference 0.000080573, SE 0.000316412; no adjustment was applied to candidate results. # paired One confirmation only, frozen candidate, fresh seeds 2520000–2521999. Difference is candidate edge minus NP2 edge in the same simulation; SE is sample standard deviation (ddof=1)/sqrt(n). Worst lists the five smallest (seed, difference) pairs. ```json { "n": 2000, "mean_diff": -47.28558852638282, "se": 0.4403751565283165, "worst": [ [ 2520916, -516.6694164959474 ], [ 2521443, -262.4079120428237 ], [ 2521475, -241.99845630758637 ], [ 2520462, -220.2151914884122 ], [ 2520275, -148.38392749433422 ] ], "mean_candidate": 485.8784553113997, "mean_np2": 533.1640438377826 } ``` # fee_stats Confirmation simulations, same 2,000 pairs as above. Bid/ask are time averages of the simulator’s step-end fee quotes, then equal-weight averages over seeds, in bps. “Runs with bid>40” counts runs whose mean step-end bid exceeds 40 bps. Mid slope is a descriptive cross-run OLS slope of each run’s mean (bid+ask)/2 in bps against that strategy’s total retail Y/10,000, including an intercept; it is not a causal estimate or a time trend. | Strategy | Mean bid bps | Mean ask bps | Bid−ask bps | Mid slope bps/10k retail Y | Runs bid>40 | Mean retail Y | |---|---:|---:|---:|---:|---:|---:| | NP2 | 36.461417 | 35.458166 | 1.003251 | 1.761969 | 215/2000 | 76033.433 | | candidate | 31.983270 | 32.017128 | -0.033858 | 1.328761 | 4/2000 | 83600.338 | NP2 calibration against 30 bps, seeds 0–999: 103/1000 runs have mean bid>40 bps. This does not reproduce the brief’s count of 90, although the score reproduces. Seed 476 mean bid=33.966428, ask=35.532165, bid−ask=-1.565737 bps. Pairing against an adaptive opponent changes routing, so confirmation counts need not match the 30 bps calibration. Checks: pinned simulator setup and starter validation passed; CH and NP2 calibration reproduced; final `amm-match validate scratch/final.sol` passed. Final candidate passed 239 scripted callback checks spanning both trade sides, repeated timestamps, long inactive gaps, and one-sided flow; all checked fee gaps met the chosen cap. Exact helper boundary/exponential checks, 1,000-config draw parity checks, and 50,000 simulated step-end cap checks (seeds 12000–12004) also passed. These are local measurements, not leaderboard results. No public-board submission was made. Source provenance: NP2 combines the TWO artifact’s code with the NP artifact’s interior retail-routing block. The pinned simulator’s match/config/engine/interface sources define draws, run lengths, fee averaging and side semantics; CH comes from its pinned contract URL. Exact passages and URLs are in sources.json. All numeric experiment results above were measured locally, not copied from source claims. # blocked `null` # next Retain NP2. Test a targeted cap only when persistent inactivity and fee/price dislocation agree; avoid globally truncating the protective skew. Use a new confirmation block for any revised mechanism.