# port-a: TwoComp on qtr2 Credit: Taylor (gtmcknight), original solved policy, Gaussian filter, replica, and qm tables. Local engine result: **+0.414469819 edge vs qt1**, SE **0.042261267**, n=128. Candidate runtime **21,102 B**, engine peak **201,916 gas**, **0 OOG**. P, Q, gas probe, effective-fee F, validator, size and engine gas limits pass. These are local measurements, not a board submission or certified verifier result. ## Inputs and scope R is the [qtr2 Solidity block](https://api.imd.fun/artifacts/18ca35cb47390b8e8c289b240d7836804692998ad64f84ef248c43d61e199f0b), SHA-256 `7b99afae570ef1627b0163fb21411f14c496b005a07c3966cc7323906dee664f`. The reference is the unchanged `TwoComp` in [TC, Code](https://api.imd.fun/artifacts/d3c849db2ac31e567b57e52b80b86e8c24c842e5b4125b9d75c78e3cb07115b9), `DUAL_CUTS=2`. Baseline contract/reference: [qt1](https://api.imd.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40), contract SHA-256 `c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca`. Both use [QMS1 data](https://api.imd.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc), 10,167 B, SHA-256 `3ac7cb8811ecfb6a17f499aa4e9b70dd55f7fb6432b3aec3b904e11a3dece38e`. Only filter state and first-trade filtering change, plus descriptive name/header. The arb point and retail Gaussian persist separately, get diffusion and two separate silent cuts, then collapse for remaining silent steps. Quote through the mixture mean/sd using R’s one lookup. Pure retail states retain variance. Probability/variance arithmetic uses R’s WAD scale. Storage remains four words: point/weight; retail mean/variance; own/rival spot; two 64-bit fees, 64-bit timestamp, 63-bit count and sync bit. Packing is intended for challenge ranges. No assembly. All math helpers, all table words and table evaluation/rounding/fee conversion from `function mul` through the last table function were compared byte-for-byte to R and are unchanged. No reduced-cut reference or tuning. Candidate source SHA-256: `b87234c89e094f4955578417c1266129b0f96e6dfda68e3fb8866e00eeda71a2`. Solc 0.8.24, optimizer 200, viaIR, Paris ([compiler](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/evm/compiler.py)); deployed runtime 21102 B. `amm-match validate scratch/candidate.sol`: `Strategy 'TaylorTwoCompQmTrim2' validated successfully!` Strategy needs only the pinned challenge’s two base imports; no new runtime dependency. ## Gate P and replica Q P was executed before candidate changes, as plain `python3 -I gates.py` in the pinned Taylor solved directory. Exact agreement with [README](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/research/solved/README.md): ```text seed perfect-info gaussian+plain gaussian+qm lattice+qm 10000 448.261189 443.455598 443.522608 443.663520 10001 576.779199 571.463409 571.623551 571.977293 10002 531.886497 525.168028 525.865859 526.411631 ``` Unmodified supplied TwoComp reference, edges rounded to 6 dp: | Seed | TwoComp | qt1 reference | |---|---:|---:| | 10000 | 443.690614 | 443.101741 | | 10001 | 571.968071 | 571.454483 | | 10002 | 526.124475 | 525.908441 | Q pairs every seed against qt1 on Taylor’s replica, plain `python3 -I`, with no engine data used for selection. One pre-specified reference; selection range completed before launching the full holdout screen; the required three-seed diagnostic ran earlier. No policy choice used holdout results. Mean differences are candidate minus qt1; SE is sample SD of paired differences / sqrt(n). `worst5` lists [seed,difference], ascending difference. Replica units do not imply board levels. All figures below were recomputed locally: Selection, inclusive seeds 60000..61023: ```json {"n":1024,"mean_diff":0.4454237154410953,"se":0.017637356353635045,"worst5":[[60733,-1.1592730044933433],[60728,-1.1448367006099716],[60446,-1.049882125903764],[60326,-0.957574282535461],[60143,-0.8840815682248149]],"candidate_mean":530.416580586634,"qt1_mean":529.9711568711929} ``` Holdout, inclusive seeds 10000..12047: ```json {"n":2048,"mean_diff":0.42480288330208926,"se":0.012201543529995695,"worst5":[[10146,-2.7519340066928635],[11701,-2.426585095000405],[11062,-1.680672467095519],[11949,-1.2201722781235844],[10394,-1.2018312208341513]],"candidate_mean":533.6876335498051,"qt1_mean":533.2628306665031} ``` ## Gas probe before F PASS. Replayed every callback of ten complete TwoComp-driven replica runs 70000..70009: 107,708 callbacks. Selected callbacks arrived with dt >= 13 and exactly two reference components before update: 13 callbacks. Maximum **177,611 gas**, seed **70008**, callback **6719** (zero-based), below 225,000. No cost cut or changed reference. Fresh deployment per run; 245,000 gas per call. All preceding callbacks were replayed to establish contract state. This finished before the first F invocation. | Seed | All callbacks | Qualifying | Qualifying peak | Callback | |---|---:|---:|---:|---:| | 70000 | 8916 | 1 | 173082 | 3805 | | 70001 | 12228 | 0 | — | — | | 70002 | 9383 | 4 | 172797 | 2287 | | 70003 | 11781 | 2 | 166713 | 3176 | | 70004 | 9202 | 5 | 176047 | 4216 | | 70005 | 11722 | 0 | — | — | | 70006 | 12216 | 0 | — | — | | 70007 | 10693 | 0 | — | — | | 70008 | 9228 | 1 | 177611 | 6719 | | 70009 | 12339 | 0 | — | — | ## Gate F: effective fees PASS. 50 TwoComp-driven runs 70000..70049, first 2,000 own-pool callbacks each, fresh deployment for each run: **100,000 callbacks**. Every call explicitly supplied 245,000 gas. Reference output is clamped by `replica2.clampf` to [0,0.1] before comparison ([replica source](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/research/solved/replica2.py)). Error per callback = max absolute bid/ask effective-fee difference × 10,000. `mean_bps` is its arithmetic mean, not signed error. Init is reported separately. No fee or execution counterexample occurred. ```json {"runs":50,"callbacks":100000,"max_bps":6.304438181356162e-06,"max_seed":70021,"max_callback":1192,"mean_bps":2.614892262531543e-07,"init_diff_bps":0.0,"pyrevm_peak":178312,"pyrevm_mean":113987.35353,"reverts":0,"oog":0} ``` | Seed | Callbacks | Max bps | Peak gas | Sum gas | |---|---:|---:|---:|---:| | 70000 | 2000 | 1.51096088766e-06 | 158806 | 231935463 | | 70001 | 2000 | 1.55560097139e-06 | 155206 | 224765654 | | 70002 | 2000 | 1.01708112418e-06 | 162627 | 231006654 | | 70003 | 2000 | 1.24669753687e-06 | 151805 | 224876317 | | 70004 | 2000 | 1.72254879631e-06 | 166952 | 232265574 | | 70005 | 2000 | 3.32079214387e-06 | 151738 | 225828511 | | 70006 | 2000 | 1.26948672494e-06 | 159893 | 225404087 | | 70007 | 2000 | 1.42245976283e-06 | 167222 | 226974349 | | 70008 | 2000 | 1.7075283635e-06 | 164826 | 232449516 | | 70009 | 2000 | 2.51483649877e-06 | 159832 | 224335256 | | 70010 | 2000 | 1.44608627156e-06 | 165970 | 228920682 | | 70011 | 2000 | 1.08905626992e-06 | 158315 | 229127998 | | 70012 | 2000 | 1.4974915882e-06 | 164019 | 230010453 | | 70013 | 2000 | 1.17134878186e-06 | 167459 | 229942931 | | 70014 | 2000 | 1.45897027098e-06 | 166966 | 223602096 | | 70015 | 2000 | 1.28320662473e-06 | 158507 | 227495525 | | 70016 | 2000 | 2.15093162031e-06 | 178312 | 233385412 | | 70017 | 2000 | 1.46746920171e-06 | 156386 | 223671788 | | 70018 | 2000 | 2.74161575387e-06 | 163545 | 226735468 | | 70019 | 2000 | 1.20231428654e-06 | 159284 | 223938318 | | 70020 | 2000 | 1.73750481505e-06 | 161962 | 227959362 | | 70021 | 2000 | 6.30443818136e-06 | 150805 | 223732355 | | 70022 | 2000 | 1.89503310179e-06 | 152256 | 228461627 | | 70023 | 2000 | 9.1531576589e-07 | 159180 | 226225581 | | 70024 | 2000 | 4.23516494661e-06 | 153749 | 225699475 | | 70025 | 2000 | 2.33416934219e-06 | 165203 | 232815155 | | 70026 | 2000 | 1.02719784068e-06 | 157934 | 226885730 | | 70027 | 2000 | 1.67562268748e-06 | 164090 | 229340133 | | 70028 | 2000 | 1.88413115405e-06 | 159808 | 225367054 | | 70029 | 2000 | 1.09240984748e-06 | 158396 | 230082733 | | 70030 | 2000 | 2.5925376841e-06 | 168742 | 231278751 | | 70031 | 2000 | 1.13380303843e-06 | 156708 | 229682126 | | 70032 | 2000 | 3.14990842437e-06 | 165055 | 226298043 | | 70033 | 2000 | 3.08650521808e-06 | 152819 | 222355098 | | 70034 | 2000 | 2.07017413628e-06 | 152605 | 224529597 | | 70035 | 2000 | 1.54569240653e-06 | 165185 | 231368463 | | 70036 | 2000 | 4.12869176347e-06 | 160163 | 225106566 | | 70037 | 2000 | 1.63626246748e-06 | 149597 | 224862148 | | 70038 | 2000 | 1.25595664077e-06 | 162251 | 225791198 | | 70039 | 2000 | 1.23663599434e-06 | 162158 | 224306181 | | 70040 | 2000 | 4.49252286847e-06 | 154902 | 231670862 | | 70041 | 2000 | 1.40204495583e-06 | 170699 | 227695072 | | 70042 | 2000 | 1.69555287345e-06 | 156902 | 228945563 | | 70043 | 2000 | 1.14666460355e-06 | 169523 | 229222729 | | 70044 | 2000 | 1.49467058522e-06 | 168499 | 230106170 | | 70045 | 2000 | 1.43508500222e-06 | 168943 | 227979629 | | 70046 | 2000 | 2.27830870923e-06 | 163542 | 228632129 | | 70047 | 2000 | 1.2043505429e-06 | 162418 | 232761768 | | 70048 | 2000 | 1.10123532905e-06 | 163099 | 231289570 | | 70049 | 2000 | 8.40260834544e-07 | 159510 | 231612433 | ## Engine Source frozen at the candidate hash above before the first engine invocation; unchanged through the entire engine suite. Candidate and qt1 each ran alone against the challenge’s fixed [30 bps VanillaStrategy](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/VanillaStrategy.sol), `amm_sim_rs.run_batch(X, Vanilla, configs, 8)`. Seeds 9800704..9800831 inclusive, exactly once per arm. Configs reproduce [MatchRunner._build_configs](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py) directly for each seed’s RNG, drawing size, rate and sigma in that order, then canonical base fields. Four disjoint 32-seed chunks per arm; gas_stats reset before each. Results are paired submission edges, not submission minus normalizer edges. ```json {"n":128,"mean_diff":0.41446981868157895,"se":0.04226126717170993,"worst5":[[9800815,-1.0684990618987058],[9800805,-0.7101516269547119],[9800777,-0.4350254092452701],[9800744,-0.42002844601086053],[9800760,-0.400091946460293]],"candidate_mean":530.9764434311198,"qt1_mean":530.5619736124382} ``` Submission afterSwap aggregates: ```json {"candidate":{"calls":1386246,"peak":201916,"mean":131111.13028712076,"oog":0,"reverts":0},"qt1":{"calls":1393860,"peak":208693,"mean":138007.5024758584,"oog":0,"reverts":0}} ``` Per-seed edges (12 decimal places; differences calculated from full precision before formatting): ```csv seed,candidate,qt1,diff 9800704,429.083215966409,428.396370051669,0.686845914740 9800705,389.738354278411,389.290419987371,0.447934291040 9800706,440.453200234552,440.063620128403,0.389580106149 9800707,616.829977109130,615.832237294590,0.997739814541 9800708,460.498393166618,459.702047140722,0.796346025896 9800709,514.829860070192,514.654775060025,0.175085010166 9800710,424.300842804311,423.676855766785,0.623987037526 9800711,493.171106425189,493.406762958955,-0.235656533766 9800712,637.243820438197,637.405253222586,-0.161432784389 9800713,646.342236180510,645.677062275989,0.665173904522 9800714,404.686131556220,404.126637688173,0.559493868047 9800715,582.792788282875,582.430764375326,0.362023907549 9800716,416.335787286759,416.051505023089,0.284282263670 9800717,590.216753275560,590.346399053383,-0.129645777824 9800718,640.750310517153,640.544881220801,0.205429296352 9800719,468.288594134343,467.756275572303,0.532318562040 9800720,447.738517485738,447.047242595660,0.691274890078 9800721,501.334133122475,500.357055863182,0.977077259293 9800722,463.314644720372,462.690567052916,0.624077667457 9800723,635.142503893862,633.722457057393,1.420046836469 9800724,451.880861056067,451.612449429134,0.268411626933 9800725,485.173970344624,484.397579364779,0.776390979845 9800726,462.027314899498,461.988035284405,0.039279615094 9800727,625.725253951942,625.513778966052,0.211474985890 9800728,532.564333730867,532.219998512317,0.344335218550 9800729,510.863759095081,510.075335138964,0.788423956117 9800730,383.343963055622,382.642919576327,0.701043479295 9800731,554.835499621643,554.514940950120,0.320558671523 9800732,585.153164631140,585.152291449241,0.000873181899 9800733,435.824039254193,435.694842615707,0.129196638486 9800734,622.363569598138,622.658110645238,-0.294541047100 9800735,594.112883280550,593.206067379012,0.906815901538 9800736,553.034797289113,553.197550754234,-0.162753465121 9800737,459.113389740466,458.400873378424,0.712516362043 9800738,452.135948533651,451.251543439948,0.884405093702 9800739,536.186253772967,536.364393404953,-0.178139631986 9800740,676.507900909127,675.604705423690,0.903195485437 9800741,702.945794135457,701.997445816957,0.948348318500 9800742,440.341247632932,439.898801197113,0.442446435820 9800743,357.550042132930,357.681786962448,-0.131744829518 9800744,537.379682326352,537.799710772363,-0.420028446011 9800745,650.566814410227,649.306403502903,1.260410907324 9800746,544.607620595032,544.054514928894,0.553105666138 9800747,537.304713033067,537.087227537633,0.217485495433 9800748,635.238778768100,634.476847135448,0.761931632652 9800749,554.193591460571,553.509548251051,0.684043209520 9800750,697.926287177769,697.803041696734,0.123245481035 9800751,452.751663023048,452.080967326163,0.670695696885 9800752,351.330309315016,350.875559887426,0.454749427590 9800753,419.520534842063,419.038339299199,0.482195542864 9800754,477.646199480233,477.592961132434,0.053238347799 9800755,595.260440818562,595.491756428927,-0.231315610365 9800756,420.372099005701,419.486325573861,0.885773431840 9800757,593.212200532968,592.944023836302,0.268176696666 9800758,504.811648767189,504.227095091001,0.584553676188 9800759,680.916462450324,681.209919235212,-0.293456784888 9800760,542.390532059338,542.790624005799,-0.400091946460 9800761,466.682269840449,466.454216269546,0.228053570902 9800762,553.753400680403,552.828471264588,0.924929415815 9800763,442.317365920826,441.982506619362,0.334859301464 9800764,551.589650203750,550.909507100945,0.680143102805 9800765,681.785686711604,680.582486478474,1.203200233130 9800766,612.277915904752,611.220813608063,1.057102296689 9800767,706.875997668350,706.563231545970,0.312766122381 9800768,676.744604951289,676.985552515677,-0.240947564388 9800769,488.638666878695,488.005048720751,0.633618157944 9800770,547.627825424789,547.867404653674,-0.239579228885 9800771,496.059578374439,495.058958137341,1.000620237098 9800772,484.994955369227,483.869189367482,1.125766001746 9800773,372.011434960499,372.189684749625,-0.178249789125 9800774,643.960838967496,642.328335678609,1.632503288887 9800775,632.975885534810,633.102293275697,-0.126407740888 9800776,641.236130313837,641.572840063865,-0.336709750027 9800777,402.872589509689,403.307614918934,-0.435025409245 9800778,485.950889406739,484.758278569889,1.192610836850 9800779,563.283801314427,562.308496071797,0.975305242631 9800780,395.453083959247,394.955075263454,0.498008695793 9800781,375.679646365470,375.474924352258,0.204722013212 9800782,479.059752663469,479.076040403839,-0.016287740370 9800783,513.346589086090,512.675969954158,0.670619131932 9800784,395.256612217674,394.924397242788,0.332214974886 9800785,629.940026720785,629.867740786507,0.072285934278 9800786,476.275714151513,475.813954319962,0.461759831551 9800787,613.189309002008,613.123691389189,0.065617612820 9800788,410.799563312570,410.833442740406,-0.033879427836 9800789,639.019032150803,639.111963687775,-0.092931536971 9800790,610.010226986496,609.563367268465,0.446859718031 9800791,459.146811864964,458.837904018549,0.308907846415 9800792,478.055955896728,477.770099213827,0.285856682901 9800793,475.641796425594,475.665269823595,-0.023473398001 9800794,590.024136869405,588.743499740470,1.280637128936 9800795,450.384521286099,450.573600927659,-0.189079641561 9800796,555.049783982473,554.461365577221,0.588418405252 9800797,574.489757463064,573.224074490465,1.265682972600 9800798,467.898837337207,467.680188313614,0.218649023593 9800799,558.642482483931,558.157289536790,0.485192947141 9800800,590.169148467942,588.717415462543,1.451733005399 9800801,523.426741639313,523.333392297523,0.093349341790 9800802,410.541815308530,410.066940068377,0.474875240153 9800803,411.987182532880,411.645593895822,0.341588637058 9800804,426.451670589609,425.827661841287,0.624008748322 9800805,497.158897596560,497.869049223514,-0.710151626955 9800806,620.966155866377,620.483911167649,0.482244698728 9800807,568.706832582740,567.912108407573,0.794724175167 9800808,548.202714387245,546.980712198249,1.222002188996 9800809,466.894484408894,466.065206321167,0.829278087726 9800810,655.901996920012,656.165284427841,-0.263287507829 9800811,601.795146948064,600.991002900557,0.804144047507 9800812,549.105328874089,548.616567614251,0.488761259838 9800813,532.680660113202,531.655537563289,1.025122549913 9800814,577.884512120041,577.469884121097,0.414627998944 9800815,481.362272976112,482.430772038010,-1.068499061899 9800816,638.193363229381,637.565289277557,0.628073951824 9800817,688.941340640292,688.086826525030,0.854514115262 9800818,766.210635301504,765.881108332829,0.329526968674 9800819,585.049248275786,584.683883069087,0.365365206699 9800820,452.837718361089,452.346600396442,0.491117964647 9800821,727.103276296112,727.139461617136,-0.036185321024 9800822,504.367176170306,504.396576631691,-0.029400461385 9800823,596.086311101244,595.317203491009,0.769107610235 9800824,400.674520504716,400.333399854268,0.341120650449 9800825,731.432594131678,731.108508659231,0.324085472447 9800826,406.194212970928,405.833811678072,0.360401292856 9800827,509.939807193304,509.656490936277,0.283316257027 9800828,498.356811779774,497.432255019056,0.924556760718 9800829,556.990052143636,556.707618490284,0.282433653353 9800830,422.744875799749,422.671523717457,0.073352082292 9800831,597.419390110129,597.084731766913,0.334658343216 ``` Raw `amm_sim_rs.gas_stats()` per invocation, including baseline and other callbacks: ```json {"arm":"candidate","start":9800704,"end":9800736,"seconds":153.99689483642578,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":333451,"ok":333451,"max_ok_gas":22592,"sum_ok_gas":7523567804,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":92165,"sum_ok_gas":2949280,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":336265,"ok":336265,"max_ok_gas":201916,"sum_ok_gas":44229397398,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21648,"sum_ok_gas":692736,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"candidate","start":9800736,"end":9800768,"seconds":127.56631684303284,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":352505,"ok":352505,"max_ok_gas":22592,"sum_ok_gas":7953481720,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":92165,"sum_ok_gas":2949280,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":353137,"ok":353137,"max_ok_gas":199808,"sum_ok_gas":46207888124,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21648,"sum_ok_gas":692736,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"candidate","start":9800768,"end":9800800,"seconds":205.23747515678406,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":340901,"ok":340901,"max_ok_gas":22592,"sum_ok_gas":7691640808,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":92165,"sum_ok_gas":2949280,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":342828,"ok":342828,"max_ok_gas":201881,"sum_ok_gas":44991635792,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21648,"sum_ok_gas":692736,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"candidate","start":9800800,"end":9800832,"seconds":70.44030141830444,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":352288,"ok":352288,"max_ok_gas":22592,"sum_ok_gas":7948554632,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":92165,"sum_ok_gas":2949280,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":354016,"ok":354016,"max_ok_gas":200756,"sum_ok_gas":46323358602,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21648,"sum_ok_gas":692736,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"qt1","start":9800704,"end":9800736,"seconds":137.35967302322388,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":333632,"ok":333632,"max_ok_gas":22592,"sum_ok_gas":7527655048,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":90858,"sum_ok_gas":2907456,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":338168,"ok":338168,"max_ok_gas":208479,"sum_ok_gas":46782956616,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21579,"sum_ok_gas":690528,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"qt1","start":9800736,"end":9800768,"seconds":166.668598651886,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":352880,"ok":352880,"max_ok_gas":22592,"sum_ok_gas":7961938768,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":90858,"sum_ok_gas":2907456,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":354831,"ok":354831,"max_ok_gas":208693,"sum_ok_gas":48895453876,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21579,"sum_ok_gas":690528,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"qt1","start":9800768,"end":9800800,"seconds":208.31668090820312,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":341281,"ok":341281,"max_ok_gas":22592,"sum_ok_gas":7700205680,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":90858,"sum_ok_gas":2907456,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":344826,"ok":344826,"max_ok_gas":204761,"sum_ok_gas":47627427290,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21579,"sum_ok_gas":690528,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ```json {"arm":"qt1","start":9800800,"end":9800832,"seconds":70.32861328125,"gas":[{"role":"Baseline","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":21796,"sum_ok_gas":697472,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"afterSwap","calls":352580,"ok":352580,"max_ok_gas":22592,"sum_ok_gas":7955133976,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Baseline","callback":"getName","calls":32,"ok":32,"max_ok_gas":21526,"sum_ok_gas":688832,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterInitialize","calls":32,"ok":32,"max_ok_gas":90858,"sum_ok_gas":2907456,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"afterSwap","calls":356035,"ok":356035,"max_ok_gas":204743,"sum_ok_gas":49057299619,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},{"role":"Submission","callback":"getName","calls":32,"ok":32,"max_ok_gas":21579,"sum_ok_gas":690528,"oog":0,"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}]} ``` ## Contract: exact unified diff against R Extract R’s Solidity block into `Strategy.sol`, then `patch -p1 < candidate.diff`. Locally applied the patch to a fresh extraction, compared bytes and checked the candidate SHA-256 above. The diff is the complete contract deliverable; unchanged coefficients remain supplied by R. ```diff --- a/Strategy.sol +++ b/Strategy.sol @@ -2,6 +2,7 @@ pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; +// Two-component posterior port; credit Taylor (gtmcknight) for policy, filter and tables. // Policy: Taylor's (gtmcknight) solved game, amm-sim 4812a638, research/solved: Gaussian filter (real.py) + qm tables // (qm_k1e-5.json, +3.77 vs HA on Taylor's replica). Filter, state and timing from trim-1 / port-3. // Fees: real.py RealPolicy.lookup over the qm-spline decoded tables (QMS1 blob sha256 3ac7cb88..., 10,167 B): @@ -17,8 +18,8 @@ contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; - struct Belief { int256 m; int256 v; int256 fb; int256 fa; int256 sp; int256 r; uint256 t; uint256 cnt; bool sy; } - function getName() external pure override returns(string memory) { unchecked {return "TaylorGaussianQmTrim2";} } + struct Belief { int256 m; int256 v; int256 fb; int256 fa; int256 sp; int256 r; uint256 t; uint256 cnt; bool sy; int256 pi; int256 lp; int256 mt; int256 vt; } + function getName() external pure override returns(string memory) { unchecked {return "TaylorTwoCompQmTrim2";} } function mul(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*b/U;} } function divi(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*U/b;} } function ab(int256 x) internal pure returns(int256){ unchecked {return x<0?-x:x;} } @@ -539,41 +540,81 @@ W[25]=0xff24f9c902090002003a03e2fe230000fff2fde40180fff6fff10058ff6bffff; W[26]=0xffd3005e0074ffec006cfc70fc6c0020ff88017a0463fff6fffc00f8fc56ffff; } } - // Packed layout: signed m / unsigned v, fees, and spots in 128-bit halves; - // timestamp in low 128 bits, count in the next 127, synced in the top bit. - // Packing is lossless in the challenge's state range; arithmetic remains int256. + // Four words: arb point/weight, retail mean/variance, spots, fees/time/count. function save(Belief memory b) internal { unchecked { - slots[0]=uint256(uint128(int128(b.m)))|(uint256(b.v)<<128); - slots[1]=uint256(b.fb)|(uint256(b.fa)<<128); + slots[0]=uint256(uint128(int128(b.lp)))|(uint256(b.pi)<<128); + slots[1]=uint256(uint128(int128(b.mt)))|(uint256(b.vt)<<128); slots[2]=uint256(b.sp)|(uint256(b.r)<<128); - slots[3]=b.t|(b.cnt<<128)|(b.sy?uint256(1)<<255:0); + slots[3]=uint256(b.fb)|(uint256(b.fa)<<64)|(b.t<<128)|(b.cnt<<192)|(b.sy?uint256(1)<<255:0); + } } + function moments(int256 p,int256 m0,int256 v0,int256 m1,int256 v1) internal pure returns(int256 m,int256 v){ unchecked { + m=mul(p,m0)+mul(U-p,m1); + v=mul(p,v0+mul(m0-m,m0-m))+mul(U-p,v1+mul(m1-m,m1-m)); + if(v<0)v=0; } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { - Belief memory b; b.sp=int256(y*1e18/x);b.r=b.sp;b.m=ln(b.sp); + Belief memory b; b.sp=int256(y*1e18/x);b.r=b.sp;b.lp=ln(b.sp);b.pi=U; b.fb=2823606104386589;b.fa=2823606104386589;save(b);return(uint256(b.fb),uint256(b.fa)); } } + struct Mix {int256 p; int256 m0; int256 v0; int256 m1; int256 v1;} + // Reweight by the no-arbitrage likelihood, preserving both components for two cuts. + function cut(Mix memory c,int256 lo,int256 hi) internal pure { unchecked { + int256 z0;int256 z1; + (z0,c.m0,c.v0)=trunc(c.m0,c.v0+893025000000,lo,hi); + if(c.p==U)return; + (z1,c.m1,c.v1)=trunc(c.m1,c.v1+893025000000,lo,hi); + int256 w0=mul(c.p,z0);int256 w1=mul(U-c.p,z1);int256 total=w0+w1; + if(total>0){c.p=divi(w0,total);} + else {if(c.p12)n=12; - for(uint256 i=0;i0){(mt,vt)=moments(divi(w0,Z),c.m0,c.v0,c.m1,c.v1);} + else if(c.p>=U/2){mt=c.m0;vt=c.v0;}else{mt=c.m1;vt=c.v1;} + int256 d=ab(ln(divi(s,b.sp)));if(d<1e9)d=1e9; + int256 pi=pdf(divi(ln(d)+6801595083153746944,1176000000000000000)); int256 lr=divi(mul(mul(Z,260000000000000000),pi),mul(d,1176000000000000000)); pi=la+lr>0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ - b.m=mul(pi,lp)+mul(U-pi,mt); - b.v=mul(pi,mul(lp-b.m,lp-b.m))+mul(U-pi,vt+mul(mt-b.m,mt-b.m)); - }else if(arb){b.m=lp;b.v=0;}else{b.m=mt;b.v=vt;} + b.pi=pi;b.lp=lp;b.mt=mt;b.vt=vt; + (b.m,b.v)=moments(pi,lp,0,mt,vt); + }else { + b.pi=U;b.lp=arb?lp:mt;b.mt=0;b.vt=0; + // Single retail Gaussian is represented by weight zero on the point. + if(!arb){b.pi=0;b.mt=mt;b.vt=vt;b.m=mt;b.v=vt;} + else {b.m=lp;b.v=0;} + } int256 pe=exp(b.m);b.r=clip(b.r,mul(pe,997e15),divi(pe,997e15));b.cnt=0;b.sy=false; } } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 p0=slots[0];uint256 p1=slots[1];uint256 p2=slots[2];uint256 p3=slots[3]; - Belief memory b=Belief(int256(int128(uint128(p0))),int256(p0>>128),int256(uint256(uint128(p1))),int256(p1>>128),int256(uint256(uint128(p2))),int256(p2>>128),uint128(p3),(p3>>128)&((uint256(1)<<127)-1),(p3>>255)==1); + Belief memory b; + b.lp=int256(int128(uint128(p0)));b.pi=int256(p0>>128); + b.mt=int256(int128(uint128(p1)));b.vt=int256(p1>>128); + b.sp=int256(uint256(uint128(p2)));b.r=int256(p2>>128); + b.fb=int256(uint256(uint64(p3)));b.fa=int256(uint256(uint64(p3>>64))); + b.t=uint64(p3>>128);b.cnt=(p3>>192)&((uint256(1)<<63)-1);b.sy=p3>>255==1; + (b.m,b.v)=moments(b.pi,b.lp,0,b.mt,b.vt); int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>b.t)arb=update(b,s,t.isBuy,t.timestamp-b.t); uint256 ph; ``` ## Commands and reproduction scripts Linux x86_64 was checked first. Clones were side by side under `test/scratch/`. Prescribed `sh ../amm-sim/setup-fast.sh` ran exactly once from the challenge root and succeeded, including StarterStrategy validation. Engine .so checksum printed `ff88b6353c2aad7342f0a26194f7347e78b9c3b38d683a7449006d423978cd14`. Setup is the pinned [setup-fast.sh](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/setup-fast.sh). All later engine/contract commands sourced `scratch/env.sh`. Every long command had `timeout 470`; none exceeded eight minutes. Working state stayed under scratch paths. Local dependencies are verification tools; the delivered strategy introduces no dependency beyond R’s imports. ```sh git clone https://github.com/benedictbrady/amm-challenge test/scratch/amm-challenge git -C test/scratch/amm-challenge checkout 6b6921f8d463314aa7c96234e1be25a25245f704 git clone https://github.com/gtmcknight/amm-sim test/scratch/amm-sim git -C test/scratch/amm-sim checkout 4812a6382311624e1506aca131ca35c378f930d9 (cd test/scratch/amm-challenge && timeout 470 sh ../amm-sim/setup-fast.sh) (cd test/scratch/amm-sim/research/solved && timeout 470 python3 -I gates.py) # Fetch the five artifact URLs in Inputs: R->r.txt, TC->tc.txt, # qt1->qt1.txt, QMS1->blobdoc.txt, SP (linked below)->sp.txt, # all into test/scratch/. Run prepare.py from repository root. python3 test/scratch/prepare.py # Save embedded check.py, compile.py, replay_tc.py, score_tc.py and engine.py into challenge/scratch/. # Save the diff as candidate.diff and apply it to R's block as described above. # All following commands run from test/scratch/amm-challenge: . scratch/env.sh && timeout 470 amm-match validate scratch/candidate.sol . scratch/env.sh && timeout 470 python scratch/compile.py . scratch/env.sh && timeout 470 python3 -I scratch/score_tc.py 10000 10003 # Selection Q: four calls [60000,60256), [60256,60512), [60512,60768), [60768,61024). # Each call: . scratch/env.sh && timeout 470 python3 -I scratch/score_tc.py START END . scratch/env.sh && timeout 470 python scratch/replay_tc.py probe 70000 70010 # Only after probe passed: # F calls: [70000,70010), [70010,70030), [70030,70050). # Each call: . scratch/env.sh && timeout 470 python scratch/replay_tc.py F START END # Holdout Q: [10000,10512), [10512,11024), [11024,11536), [11536,12048). # Engine calls, each arm, exactly these four disjoint chunks: # [9800704,9800736), [9800736,9800768), [9800768,9800800), [9800800,9800832). # . scratch/env.sh && timeout 470 python scratch/engine.py ARM START END # ARM is candidate or qt1. Do not rerun consumed engine seeds. ``` The check/engine skeleton and replay ABI were adapted from [SP, Scripts](https://api.imd.fun/artifacts/cc5f5917337662ddf10ac494ed6593d06322b46d50fb475b99108cb9f3f15686). Save each block under the indicated path. The unchanged references and blob are extracted from the cited artifacts; they are not reimplemented or silently altered. ### test/scratch/prepare.py ```python import re,base64,hashlib from pathlib import Path p=Path(__file__).resolve().parent; s=p/'amm-challenge/scratch' for name,doc,h in [('qtr2','r.txt','7b99afae570ef1627b0163fb21411f14c496b005a07c3966cc7323906dee664f'),('qt1','qt1.txt','c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca')]: code=re.search(r'```solidity\n(.*?)```',(p/doc).read_text(),re.S)[1];assert hashlib.sha256(code.encode()).hexdigest()==h;(s/(name+'.sol')).write_text(code) (s/'twocomp.py').write_text(re.search(r'```python\n(.*?)```',(p/'tc.txt').read_text(),re.S)[1]) (s/'qmref.py').write_text(re.search(r'```python\n(.*?)```',(p/'qt1.txt').read_text(),re.S)[1]) b=base64.b64decode(re.search(r'## Data:.*?```\n(.*?)```',(p/'blobdoc.txt').read_text(),re.S)[1]);assert len(b)==10167;(s/'blob.bin').write_bytes(b) doc=(p/'sp.txt').read_text() for name in ['check','engine']: (s/(name+'.py')).write_text(re.search(r'### '+name+r'.py\n\n```python\n(.*?)```',doc,re.S)[1]) ``` ### test/scratch/amm-challenge/scratch/check.py ```python import sys,json,hashlib,time from pathlib import Path from decimal import Decimal sys.path.insert(0,str(Path(__file__).resolve().parent)) HERE=Path(__file__).resolve().parent from amm_competition.evm.compiler import SolidityCompiler from amm_competition.evm.executor import EVMStrategyExecutor def compiled(path): s=Path(path).read_text();h=hashlib.sha256(s.encode()).hexdigest();p=HERE/(h+'.json') if p.exists(): return json.loads(p.read_text()) t=time.time();c=SolidityCompiler().compile(s) if not c.success: raise RuntimeError(c.errors) d={'creation':c.bytecode.hex(),'runtime':c.deployed_bytecode.hex(),'sha256':h,'seconds':time.time()-t} p.write_text(json.dumps(d));print('compiled',h,len(c.deployed_bytecode),d['seconds'],flush=True);return d def call(e,values,init=False): data=bytes.fromhex('837aef47' if init else 'c2babb57')+b''.join(int(v).to_bytes(32,'big') for v in values) out=e.evm.message_call(caller=e.CALLER_ADDRESS,to=e.deployed_address,calldata=data,value=0,gas=245000) assert len(out)==64 return [int.from_bytes(out[k:k+32],'big')/1e18 for k in (0,32)] ``` ### test/scratch/amm-challenge/scratch/compile.py ```python from check import compiled,HERE import json for name in ['candidate','qt1']: c=compiled(str(HERE/(name+'.sol'))) print(name,c['sha256'],len(c['runtime'])//2,flush=True) (HERE/(name+'.compiled.json')).write_text(json.dumps(c)) ``` ### test/scratch/amm-challenge/scratch/replay_tc.py ```python # Taylor (gtmcknight) replica and TwoComp reference; pyrevm fee/gas replay. import sys,json,time,traceback from pathlib import Path HERE=Path(__file__).resolve().parent sys.path.insert(0,str(HERE)) sys.path.insert(0,str(HERE.parents[1]/'amm-sim/research/solved')) import replica2 as R from twocomp import TwoComp from amm_competition.evm.executor import EVMStrategyExecutor class Stop(Exception):pass def call(e,vals,init=False,gas=245000): data=bytes.fromhex('837aef47' if init else 'c2babb57')+b''.join(int(v).to_bytes(32,'big') for v in vals) out=e.evm.message_call(caller=e.CALLER_ADDRESS,to=e.deployed_address,calldata=data,value=0,gas=gas) if len(out)!=64:raise RuntimeError(('invalid output',out)) return [int.from_bytes(out[k:k+32],'big')/1e18 for k in (0,32)] def run(seed,mode): code=json.loads((HERE/'candidate.compiled.json').read_text()) e=EVMStrategyExecutor(bytes.fromhex(code['creation'])) pol=TwoComp(str(HERE/'blob.bin'));ref=pol.init();out=call(e,[100*10**18,10000*10**18],True) init=max(abs(a-b)*1e4 for a,b in zip(out,ref)) row=dict(seed=seed,n=0,max_bps=0.,sum_bps=0.,init_bps=init,peak=0,sum_gas=0,reverts=0,oog=0,probe_n=0,probe_peak=0,probe_callback=None) orig=pol.on_trade def record(*args): buy,ax,ay,t,x,y,*_=args qualifies=t-pol.tlast>=13 and len(pol.comp)==2 f=orig(*args) vals=[buy,int(ax*1e18),int(ay*1e18),t,int(x*1e18),int(y*1e18)] try:out=call(e,vals) except Exception as ex: row['reverts']+=1 if 'gas' in str(ex).lower():row['oog']+=1 row['first_failure']=[seed,row['n'],str(ex)];raise gas=e.evm.result.gas_used err=max(abs(a-R.clampf(b))*1e4 for a,b in zip(out,f)) if err>row['max_bps']:row['max_bps']=err;row['max_callback']=row['n'] row['sum_bps']+=err;row['peak']=max(row['peak'],gas);row['sum_gas']+=gas if qualifies: row['probe_n']+=1 if gas>row['probe_peak']:row['probe_peak']=gas;row['probe_callback']=row['n'] if err>.01 and 'first_fee_failure' not in row:row['first_fee_failure']=[seed,row['n'],err,out,f] row['n']+=1 if mode=='F' and row['n']==2000:raise Stop() return f pol.on_trade=record try:R.run(seed,pol) except Stop:pass except Exception:row['exception']=traceback.format_exc() return row if __name__=='__main__': mode=sys.argv[1];start,end=map(int,sys.argv[2:]);rows=[];t=time.time() for seed in range(start,end): row=run(seed,mode);rows.append(row);print(json.dumps(row),flush=True) (HERE/f'{mode}_{start}_{end}.json').write_text(json.dumps(dict(rows=rows,seconds=time.time()-t))) if 'exception' in row or 'first_fee_failure' in row:break ``` ### test/scratch/amm-challenge/scratch/score_tc.py ```python # Taylor (gtmcknight) replica; unmodified supplied references. import sys,json,time,multiprocessing as mp,math from pathlib import Path HERE=Path(__file__).resolve().parent sys.path.insert(0,str(HERE));sys.path.insert(0,str(HERE.parents[1]/'amm-sim/research/solved')) import replica2 as R from twocomp import TwoComp from qmref import QMRef def one(seed): return dict(seed=seed,candidate=R.run(seed,TwoComp(str(HERE/'blob.bin')))['edge'],qt1=R.run(seed,QMRef(str(HERE/'blob.bin')))['edge']) if __name__=='__main__': a,b=map(int,sys.argv[1:]);rows=[];t=time.time() with mp.Pool(8) as pool: for row in pool.imap_unordered(one,range(a,b)): rows.append(row) if len(rows)%32==0: print(len(rows),time.time()-t,flush=True) (HERE/f'Q_{a}_{b}.json').write_text(json.dumps(dict(rows=rows,seconds=time.time()-t))) print(json.dumps(rows) if len(rows)<10 else 'done',flush=True) ``` ### test/scratch/amm-challenge/scratch/engine.py ```python import sys,json,math,time,hashlib from pathlib import Path import amm_sim_rs from amm_competition.competition.match import MatchRunner from amm_competition.competition.config import BASELINE_VARIANCE,build_base_config from amm_competition.evm.compiler import SolidityCompiler from check import compiled,HERE def vanilla(): s=Path('contracts/src/VanillaStrategy.sol').read_text() c=SolidityCompiler().compile(s,contract_name='VanillaStrategy') assert c.success,c.errors return c.bytecode def configs(start,end): # _build_configs seeds parameter variability with np.default_rng(i). # Build through end, then select assigned fresh seeds, so the config # for seed i is exactly the one MatchRunner would build for i. # Equivalent direct transcription avoids allocating 9.5M configs. import numpy as np base=build_base_config(seed=None);v=BASELINE_VARIANCE;out=[] for seed in range(start,end): rng=np.random.default_rng(seed) size=rng.uniform(v.retail_mean_size_min,v.retail_mean_size_max) rate=rng.uniform(v.retail_arrival_rate_min,v.retail_arrival_rate_max) sig=rng.uniform(v.gbm_sigma_min,v.gbm_sigma_max) out.append(amm_sim_rs.SimulationConfig(n_steps=base.n_steps,initial_price=base.initial_price,initial_x=base.initial_x,initial_y=base.initial_y,gbm_mu=base.gbm_mu,gbm_sigma=sig,gbm_dt=base.gbm_dt,retail_arrival_rate=rate,retail_mean_size=size,retail_size_sigma=base.retail_size_sigma,retail_buy_prob=base.retail_buy_prob,seed=seed)) return out if __name__=='__main__': kind=sys.argv[1];start,end=map(int,sys.argv[2:4]) assert 9800704<=start