**The strongest new contract measured directly against both required controls is AGE: +0.223992329 engine edge over lb-4 (SE 0.018530803) and +3.446088217 over qt1 (SE 0.045177687), on 512 paired fresh seeds.** Its observed `afterSwap` peak was **199,567 gas**, with **0 OOG and 0 callback failures**, **21,507 deployed bytes**, and **44,616 source characters**. Its incremental gain over LEAN was only **+0.032245553 (SE 0.008394521)**. AGE did not clear the requester's later +0.05-over-LEAN threshold, and its P/Q gates are missing. It is the best *directly measured* artifact here, not an eligible #1 submission. [AGE engine][AGE-E], [AGE source and T/F/G][AGE-S]. The latest requested candidate, **QLX5**, added **+0.030493957 (SE 0.005098025), n=1,024**, over LEAN on two disjoint engine blocks; peak **208,366**, **0 OOG**, **22,440 bytes**, **47,230 characters**. There is **no direct paired QLX5−lb-4 or QLX5−qt1 measurement**. Two later audits passed P/T/F on unchanged Solidity under a disclosed quote-feedback protocol. Full Q and the ordered final G/E chain remain incomplete. Neither candidate establishes first place, and this goal made no leaderboard submission. [QLX block A][QLX-A], [QLX block B][QLX-B], [certificate A][CERT-A], [certificate B][CERT-B]. Credit: Taylor (gtmcknight), [amm-sim@4812a6382311624e1506aca131ca35c378f930d9, research/solved][TAYLOR], for the solved policy, replica, filter, tables and tells underlying every variant. Source licenses and credit are preserved. AGE's complete, unchanged **Strategy.sol** follows. SHA-256: `8cb006fa6376b0f5b2ac1c80627ce55f6b52ceef19d22a80687a87247431b135`. Extract the Solidity block exactly, including its final newline. The two imported interface/base files are included at the end of this report. Compiler settings are solc **0.8.24**, optimizer **200**, **viaIR**, **Paris**. The source still returns `TellMinLookup` from `getName()`; identify variants by their hashes, not this shared name. [Frozen AGE source][AGE-S].
Complete AGE Strategy.sol — 44,616 ASCII bytes ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; // Solved AMM policy by Taylor (gtmcknight), amm-sim 4812a638 research/solved: Gaussian filter + qm tables (qm_k1e-5.json), // fee rule of real.py's qm variant (sd level, phase, synced flag). All credit for the policy, filter and tables: Taylor. // Original tables compressed by qm-spline (cubic B-splines, QMS1 blob sha256 3ac7cb88...); filter, 3-slot state, math and fee conversion // from trim-2 (port-3's filter); synced flag stored in bit 255 of slot 2. // lookup-headroom: C0 (sha256 c68887cf...) with coefficient words loaded once per callback into memory and the // second knot-span search of each axis walking forward from the first; same arithmetic, same fees. // give-u1: lookup-u1 (Strategy.sol sha256 69c361ef...) plus one rule, the constant rival pull of GivePolicy (give.py sha256 8d871a6b...). // lean-sol: give-u1 (Strategy.sol sha256 99d031f4...) with the exact-arithmetic isArb replaced by lb-4's LEAN TELL (bounded // sell_one_division / buy_grid, budget 4): a first own trade is read as arbitrage iff the lean label is positive; negative, over // budget and unsupported are misses; chance, filter, tables, pull, fee guard and storage slots unchanged (LEANBASE variant A). // age-sol: lean-sol (Strategy.sol sha256 a6dfc3ba...) with one rule changed, the AGE pull of e03-cheap-ref/age-thr (frozen pick AGE): // a bound that does not bind pulls ln r by 0.7*age/(age+1) instead of 0.35, age = steps since the last binding router bound, whose // timestamp rt is kept in bits 224..252 of slot 2; bits 253..254 record what the rule did in the last callback (k: 0 read as arb, // rule not run; 1 binding bound; 2 pull; 3 neither). Filter, tell, tables, fee guard and slots 0..5 are otherwise LEAN's. // Credit: Taylor (gtmcknight), github.com/gtmcknight/amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved. // Taylor source: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; uint256 constant M64=0xffffffffffffffff; // slots[0] = m (int128) | v (int128)<<128 ; slots[1] = sp (int128) | r (int128)<<128 ; slots[2] = fb | fa<<64 | t<<128 | cnt<<192 | rt<<224 | k<<253 | syn<<255 function getName() external pure override returns(string memory) { unchecked {return "TellMinLookup";} } 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;} } function clip(int256 x,int256 a,int256 b) internal pure returns(int256){ unchecked {return xb?b:x);} } function ln(int256 x) internal pure returns(int256 z){ unchecked { require(x>0); int256 k; while(x< U){x*=2;k--;} while(x>=2*U){x/=2;k++;} int256 y=divi(x-U,x+U); int256 y2=mul(y,y); int256 q=y; z=y; for(int256 n=3;n<=29;n+=2){q=mul(q,y2);z+=q/n;} return 2*z+k*LN2; } } function exp(int256 x) internal pure returns(int256){ unchecked { if(x< -42*U)return 0; require(x<60*U); int256 k=(x>=0?x+LN2/2:x-LN2/2)/LN2; int256 r=x-k*LN2; int256 r2=mul(r,r); int256 even=665280*U+mul(r2,75600*U+mul(r2,840*U+r2)); int256 odd=mul(r,332640*U+mul(r2,10080*U+mul(r2,42*U))); int256 s=divi(even+odd,even-odd); return k>=0?s*int256(uint256(1)<>128>0){z>>=128;r<<=64;} if(z>>64>0){z>>=64;r<<=32;} if(z>>32>0){z>>=32;r<<=16;} if(z>>16>0){z>>=16;r<<=8;} if(z>>8>0){z>>=8;r<<=4;} if(z>>4>0){z>>=4;r<<=2;} if(z>>2>0){r<<=1;} for(uint256 i=0;i<7;i++)r=(r+n/r)/2; return int256(r10*U)return 0;return divi(exp(-mul(x,x)/2),2506628274631000200);} } // Hart rational normal tail approximation; erf(x)=2*Phi(x*sqrt(2))-1. function cdf(int256 x,int256 density) internal pure returns(int256){ unchecked { int256 y=ab(x);if(y>8*U)return x>0?U:int256(0); int256 n=35262496599891000; n=mul(n,y)+700383064443688000;n=mul(n,y)+6373962203531650000; n=mul(n,y)+33912866078383000000;n=mul(n,y)+112079291497871000000; n=mul(n,y)+221213596169931000000;n=mul(n,y)+220206867912376000000; int256 d=88388347648318400; d=mul(d,y)+1755667163182640000;d=mul(d,y)+16064177579207000000; d=mul(d,y)+86780732202946100000;d=mul(d,y)+296564248779674000000; d=mul(d,y)+637333633378831000000;d=mul(d,y)+793826512519948000000; d=mul(d,y)+440413735824752000000; int256 tail=mul(mul(density,2506628274631000200),divi(n,d));return x>0?U-tail:tail; } } function trunc(int256 m,int256 v,int256 lo,int256 hi) internal pure returns(int256 Z,int256 mt,int256 vt){ unchecked { if(v<=0)return (lo<=m&&m<=hi?U:int256(0),m,0); int256 sd=root(v);int256 a=divi(lo-m,sd);int256 b=divi(hi-m,sd); int256 pa=pdf(a);int256 pb=pdf(b); Z=cdf(b,pb)-cdf(a,pa); if(Z<1e6)return(Z,m>(16*(i%16)))&65535;}} function spanR(QT memory t,uint256 x) internal pure returns(uint256 a){unchecked{ uint256 hi=t.nr-1;while(hi-a>1){uint256 m=(a+hi)/2;if(rk(t,m)<=x)a=m;else hi=m;} }} function spanC(QT memory t,uint256 x) internal pure returns(uint256 a){unchecked{ uint256 hi=t.nc-1;while(hi-a>1){uint256 m=(a+hi)/2;if(((t.ck>>(8*m))&255)<=x)a=m;else hi=m;} }} function record(bytes memory b,QT memory t,uint256 l,uint256 r,uint256 c) internal pure returns(int256 v,int256 vn){unchecked{ uint256 o=t.off+(l*t.nr+r)*(t.nc+1); v=int256(int16(uint16(uint8(b[o]))|(uint16(uint8(b[o+1]))<<8))); for(uint256 n=0;n319)i=319;int256 f=pw-int256(i)*U; uint256 j=uint256(p2/U);if(j>jm)j=jm;int256 g=p2-int256(j)*U; uint256 a;while(a>(16*(a+1)))&65535)<=k)a++; uint256 r=spanR(t,i);uint256 c=spanC(t,j); int256[8] memory v; for(uint256 l=0;l<2;l++)for(uint256 row=0;row<2;row++)(v[l*4+row*2],v[l*4+row*2+1])=record(b,t,a+l,r+row,c); int256[2] memory lr;lr[0]=int256((t.lk>>(16*a))&65535);lr[1]=int256((t.lk>>(16*(a+1)))&65535); int256 rlo=int256(rk(t,r));int256 rd=int256(rk(t,r+1))-rlo; int256 clo=int256((t.ck>>(8*c))&255);int256 cd=int256((t.ck>>(8*(c+1)))&255)-clo; int256[2] memory levels; for(uint256 l=0;l<2;l++){ int256 u=(int256(k+l)-lr[0])*U/(lr[1]-lr[0]);int256[4] memory q; for(uint256 z=0;z<4;z++)q[z]=lerp(v[z],v[z+4],u); int256[4] memory nodes; for(uint256 row=0;row<2;row++){ int256 rf=(int256(i+row)-rlo)*U/rd; int256 x0=lerp(q[0],q[2],rf);int256 x1=lerp(q[1],q[3],rf); for(uint256 col=0;col<2;col++){ int256 cf=(int256(j+col)-clo)*U/cd; int256 n=lerp(x0,x1,cf);nodes[row*2+col]=n>0?n:int256(0); } } levels[l]=lerp(lerp(nodes[0],nodes[2],f),lerp(nodes[1],nodes[3],f),g); } return lerp(levels[0],levels[1],tt); }} function w2l(uint256 L) internal pure returns(int256){ unchecked { uint256 K=L<16?0x7ff77ff67ff57ff47ff27fef7fec7fe77fe27fd87fce7fc47fb07f9c7f7e7f60:(L<32?0x800780068005800480038002800180007fff7ffe7ffd7ffc7ffb7ffa7ff97ff8:(L<48?0x808280648050803c80328028801e801980148011800e800c800b800a80098008:0x00000000000000000000000000000000000000000000000000000000000080a0)); return int256((K>>(16*(L&15)))&0xffff)-32768; } } // real.py/evalpol _pos2 over pol_mid's W2L (grid units of 5 bps), WAD in and out function pos2(int256 v) internal pure returns(int256){ unchecked { if(v<=-160*U)return 0; if(v>=160*U)return 48*U; uint256 lo=0;uint256 hi=48; while(hi-lo>1){uint256 md=(lo+hi)>>1;if(w2l(md)*U<=v)lo=md;else hi=md;} int256 a=w2l(lo);return int256(lo)*U+(v-a*U)/(w2l(hi)-a); } } function sdl(uint256 k) internal pure returns(int256){ unchecked { return int256((0x221a14100c080400>>(8*k))&0xff); } } // qm fee rule: w, w2 offsets (WAD bps), phase, synced flag, belief variance v (WAD, ln-price^2) function fees(int256 w,int256 w2,uint256 ph,bool syn,int256 v) internal pure returns(int256 fb,int256 fa){ unchecked { int256 sd=root(v)*10000;uint256 k; while(k<6&&sd>=sdl(k+1)*U)k++; int256 tt=clip((sd-sdl(k)*U)/(sdl(k+1)-sdl(k)),0,U); int256 pw=clip(w/5+160*U,0,320*U);int256 wq=(pw-160*U)*5; uint256 tb;int256 p2;int256 nc1; if(ph==0){tb=0;p2=clip(w2/5,-6*U,6*U)+6*U;nc1=12;} else{tb=(ph==1?1:3)+(syn?0:1);p2=pos2(w2/5);nc1=48;} QT memory t=qt(tb);bytes memory b=QPL; int256 xa=surf(t,b,pw,p2,k,tt,uint256(nc1)-1)+wq-w; int256 xb=surf(t,b,320*U-pw,nc1*U-p2,k,tt,uint256(nc1)-1)+w-wq; fb=xb>0?U-exp(-xb/10000):int256(0);fa=xa>0?U-exp(-xa/10000):int256(0); fb=clip(fb,0,U/10);fa=clip(fa,0,U/10); } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { int256 sp=int256(y*1e18/x); slots[0]=uint256(uint128(int128(ln(sp)))); slots[1]=uint256(uint128(int128(sp)))*(1+(uint256(1)<<128)); slots[3]=x;slots[4]=y;slots[5]=0; slots[2]=2823606104386589*(1+(uint256(1)<<64)); return(2823606104386589,2823606104386589); } } // filter update over a gap of dt steps; returns new m, v, rival spot r and the arb flag function update(int256 m,int256 v,int256 fb,int256 fa,int256 sp,int256 r,int256 s,bool buy,uint256 dt,bool hit,int256 chance) internal pure returns(int256,int256,int256,bool arb){ unchecked { int256 lo=ln(mul(sp,U-fb));int256 hi=ln(divi(sp,U-fa)); uint256 n=dt-1;if(n>12)n=12; for(uint256 i=0;i0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ m=mul(pi,lp)+mul(U-pi,mt); v=mul(pi,mul(lp-m,lp-m))+mul(U-pi,vt+mul(mt-m,mt-m)); }else if(arb){m=lp;v=0;}else{m=mt;v=vt;} int256 pe=exp(m);r=clip(r,mul(pe,997e15),divi(pe,997e15)); return(m,v,r,arb); } } // AGE pull weight (WAD): 0.7*age/(age+1), age = steps since the last binding router bound; 0 on the step of a binding bound function pullW(uint256 age) public pure returns(int256){ unchecked {return int256(7e17*age/(age+1));} } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 s0=slots[0];uint256 s1=slots[1];uint256 s2=slots[2]; int256 m=int256(int128(uint128(s0)));int256 v=int256(int128(uint128(s0>>128))); int256 sp=int256(int128(uint128(s1)));int256 r=int256(int128(uint128(s1>>128))); int256 fb=int256(s2&M64);int256 fa=int256(s2>>64&M64);uint256 cnt=s2>>192&0xffffffff;bool syn=s2>>255==1; uint256 rt=s2>>224&0x1fffffff;uint256 k; int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>(s2>>128&M64)){ bool hit=TellMath.ltTell(slots[3],slots[4],uint256(t.isBuy?fb:fa),t.amountX,t.amountY,t.reserveY,t.isBuy)==1; int256 chance=hit?int256(TellMath.ulpOverGridWad(t.isBuy?t.amountX:t.amountY,t.isBuy?slots[3]:slots[4],t.isBuy?t.reserveX:t.reserveY)):int256(0); (m,v,r,arb)=update(m,v,fb,fa,sp,r,s,t.isBuy,t.timestamp-(s2>>128&M64),hit,chance);cnt=0;syn=false; } slots[3]=t.reserveX;slots[4]=t.reserveY; uint256 ph; if(!arb){ int256 c=t.isBuy?divi(mul(s,U-fb),997e15):divi(mul(s,997e15),U-fa); // binding router bound: r = c and its timestamp rt is kept (age 0) if(t.isBuy?cr){r=c;syn=true;rt=t.timestamp&0x1fffffff;k=1;} // AGE: bound did not bind -> pull ln r alpha of the way to ln c, alpha = 0.7*age/(age+1), age = t - rt (LEAN: alpha 0.35) else if(c!=r){int256 al=pullW(t.timestamp-rt);if(al>0){int256 lr=ln(r);r=exp(lr+mul(al,ln(c)-lr));}syn=true;k=2;} else k=3; cnt++;ph=cnt==1?1:2; } int256 w=(ln(s)-m)*10000;int256 rr=(ln(r)-m)*10000; (fb,fa)=fees(w,rr,ph,syn,v); fb=guardFee(fb);fa=guardFee(fa); slots[0]=uint256(uint128(int128(m)))|uint256(uint128(int128(v)))<<128; slots[1]=uint256(uint128(int128(s)))|uint256(uint128(int128(r)))<<128; slots[2]=uint256(fb)|uint256(fa)<<64|t.timestamp<<128|cnt<<192|rt<<224|k<<253|(syn?uint256(1)<<255:0); return(uint256(fb),uint256(fa)); } } // Complete integer preimage of Vanilla gamma under IEEE binary64 conversion. // Closest integer outside that interval; ties select the lower fee. function guardFee(int256 f) internal returns(int256) { if(f>=2999999999999948&&f<=3000000000000057){slots[5]++;return f-2999999999999947<=3000000000000058-f?int256(2999999999999947):int256(3000000000000058);}return f; } function guardShifts() external view returns(uint256) {return slots[5];} // lean label of a trade: 0 unsupported, 1 positive, 2 negative, 3 over budget; isArb = (label == 1). x (reserveX) is not used. function leanLabel(bool b,uint256 ax,uint256 ay,uint256,uint256 y,uint256 px,uint256 py,uint256 fb,uint256 fa) public pure returns(uint256) {return TellMath.ltTell(px,py,b?fb:fa,ax,ay,y,b);} function isArb(bool b,uint256 ax,uint256 ay,uint256 x,uint256 y,uint256 px,uint256 py,uint256 fb,uint256 fa) external pure returns(bool) {return leanLabel(b,ax,ay,x,y,px,py,fb,fa)==1;} } library TellMath { uint256 internal constant WAD = 1e18; uint256 internal constant TWO53 = 1 << 53; uint256 internal constant TWO52 = 1 << 52; uint256 internal constant LIM = 1 << 128; /// floor(log2(x)) + 1, 0 for x = 0, by binary normalization. function bitLen(uint256 x) internal pure returns (uint256 n) { unchecked { if (x >= 1 << 128) { x >>= 128; n += 128; } if (x >= 1 << 64) { x >>= 64; n += 64; } if (x >= 1 << 32) { x >>= 32; n += 32; } if (x >= 1 << 16) { x >>= 16; n += 16; } if (x >= 1 << 8) { x >>= 8; n += 8; } if (x >= 1 << 4) { x >>= 4; n += 4; } if (x >= 1 << 2) { x >>= 2; n += 2; } if (x >= 1 << 1) { x >>= 1; n += 1; } if (x != 0) n += 1; } } /// num/den (both > 0) rounded to nearest-even double m*2^e, 2^52 <= m < 2^53. /// One division: the bit-length estimate is off by at most one binade, fixed by a shift. function rn(uint256 num, uint256 den) internal pure returns (uint256 q, int256 e) { e = int256(bitLen(num)) - int256(bitLen(den)) - 53; uint256 sn; uint256 sd; if (e < 0) { sn = num << uint256(-e); sd = den; } else { sn = num; sd = den << uint256(e); } q = sn / sd; // estimate gives q in [2^52, 2^54): at most one step down in precision if (q >= TWO53) { e += 1; if (e <= 0) { sn = num << uint256(-e); } else { sn = num; sd = den << uint256(e); } q = sn / sd; } uint256 r = sn - q * sd; uint256 twice = 2 * r; if (twice > sd || (twice == sd && (q & 1) == 1)) { q += 1; if (q == TWO53) { q = TWO52; e += 1; } } } /// e with 2^e * 1e18 <= w < 2^(e+1) * 1e18 (w > 0). function binade(uint256 w) internal pure returns (int256 e) { e = int256(bitLen(w)) - 60; // bitLen(1e18) = 60 if (!_atLeast(w, e)) e -= 1; else if (_atLeast(w, e + 1)) e += 1; } function _atLeast(uint256 w, int256 e) private pure returns (bool) { return e >= 0 ? (WAD << uint256(e)) <= w : WAD <= (w << uint256(-e)); } function _min(int256 a, int256 b) private pure returns (int256) { return a < b ? a : b; } /// log2 of the grid: min(binade(before), binade(after)) - 52. function gridExp(uint256 beforeW, uint256 afterW) internal pure returns (int256) { return _min(binade(beforeW), binade(afterW)) - 52; } /// min(1, ulp(paid)/grid) scaled by 1e18, paid = fl(paidW/1e18) (the double behind paidW). /// Returns 0 for out-of-bounds inputs. 1e18 means the paid amount is no finer than the grid. function ulpOverGridWad(uint256 paidW, uint256 beforeW, uint256 afterW) internal pure returns (uint256) { if (paidW == 0 || paidW >= LIM || beforeW == 0 || beforeW >= LIM || afterW == 0 || afterW >= LIM) return 0; (, int256 pe) = rn(paidW, WAD); int256 d = gridExp(beforeW, afterW) - pe; if (d <= 0) return WAD; if (d > 64) return 0; return WAD >> uint256(d); } // ---- LEAN TELL of result lb-4 (scratch/LeanTell.sol, functions verbatim): buy-grid's bounded sell_one_division / buy_grid tests, // budget 4, in exact integer binary64 (round-to-nearest-even) arithmetic. A double is m*2^e with 2^52 <= m <= 2^53; 1e18 = LT_C*2^7. // ltTell returns 0 unsupported, 1 positive, 2 negative, 3 over budget. Mirrors lean_tell.py bit for bit. Credit Taylor (gtmcknight). uint256 constant LT_C=7812500000000000; function ltMsb(uint256 x) internal pure returns(uint256 r){unchecked{ if(x>>128!=0){x>>=128;r=128;}if(x>>64!=0){x>>=64;r+=64;}if(x>>32!=0){x>>=32;r+=32;}if(x>>16!=0){x>>=16;r+=16;} if(x>>8!=0){x>>=8;r+=8;}if(x>>4!=0){x>>=4;r+=4;}if(x>>2!=0){x>>=2;r+=2;}if(x>>1!=0)r++; }} // n / 2^s to nearest, ties to even; s >= 1. function ltRound(uint256 n,uint256 s) internal pure returns(uint256 q){unchecked{ q=n>>s;uint256 rem=n&((uint256(1)<half||(rem==half&&(q&1)!=0))q++; }} // Wad::from_f64 of the double m*2^e: trunc(RN(m*2^e * 1e18)). function ltWad(uint256 m,int256 e) internal pure returns(uint256){unchecked{ uint256 n=m*LT_C;uint256 s=n>>105!=0?53:52;uint256 p=ltRound(n,s);int256 t=e+7+int256(s); if(t>=0)return t>128?type(uint256).max:p<>uint256(-t); }} // Wad of RN(d*2^e / g), g = G*2^-53, 0 < d < 2^53 exact. function ltDivWad(uint256 d,int256 e,uint256 G) internal pure returns(uint256){unchecked{ uint256 n=(d<<110)/G;if(n*G!=d<<110)n|=1; uint256 s=ltMsb(n)-52;return ltWad(ltRound(n,s),e+int256(s)-57); }} // G = 2^53 * RN(1 - RN(RN(fee)/1e18)), fee <= 1e17. function ltGamma(uint256 f) internal pure returns(uint256){unchecked{ if(f==0)return 1<<53; uint256 sf=ltMsb(f); if(sf>52){sf-=52;f=ltRound(f,sf);}else sf=0; uint256 n=(f<<110)/LT_C;if(n*LT_C!=f<<110)n|=1; uint256 s=ltMsb(n)-52;uint256 r=64-s-sf; return ltRound((uint256(1)<<(53+r))-ltRound(n,s),r); }} // Every double whose wad is w (2^60 <= w < 2^120): mantissas a, b (0 = absent), exponent e. function ltPre(uint256 w) internal pure returns(uint256 a,uint256 b,int256 e){unchecked{ uint256 n=(w<<64)/1e18;uint256 h=ltMsb(n);uint256 m=n>>(h-52);e=int256(h)-116; if(ltWad(m,e)==w)a=m; if(ltWad(m+1,e)==w)b=m+1; }} // Virtual reserves xv = (m+k)*2^e > rx whose amount wad is ax. function ltGrid(uint256 m,int256 e,uint256 k0,uint256 G,uint256 ax) internal pure returns(uint256 hits){unchecked{ if(m==0)return 0; uint256 lo=k0;uint256 hi=k0+1;if(k0>>48!=0){lo--;hi++;} if(lo==0)lo=1; for(uint256 k=lo;k<=hi;k++){ uint256 t=m+k;if(t>>53!=0&&(t&1)!=0)continue; if(ltDivWad(k,e,G)==ax)hits++; } }} // px, py: previous callback's reserve wads; fee: the fee wad returned then for this side // (bid when the pool buys X); ax, ay, cy: this trade's amountX, amountY, reserveY. function ltTell(uint256 px,uint256 py,uint256 fee,uint256 ax,uint256 ay,uint256 cy,bool buy) internal pure returns(uint256){unchecked{ if(fee>1e17)return 0; uint256 G=ltGamma(fee); if(buy){ if(px>>60==0||px>>120!=0||ax==0||ax>px>>1)return 0; (uint256 a,uint256 b,int256 e)=ltPre(px); if((a|b)==0)return 0; uint256 k0=ax*G/(uint256(1e18)<4)return 3; return hits!=0?1:2; } if(py>>60==0||py>>120!=0||cy>>60==0||cy>>120!=0||ay==0||ay>>120!=0)return 0; (uint256 y0,uint256 y1,int256 ey)=ltPre(py);(uint256 z0,uint256 z1,int256 ez)=ltPre(cy); if((y0|y1)==0||(z0|z1)==0)return 0; int256 de=ez-ey;if(de>1||de<-1)return 0; if(de==1){z0<<=1;z1<<=1;}else if(de==-1){y0<<=1;y1<<=1;ey=ez;} for(uint256 i=0;i<4;i++){ uint256 y=i<2?y0:y1;uint256 z=(i&1)==0?z0:z1; if(y==0||z<=y)continue; z-=y;if(z>>53!=0)return 0; if(ltDivWad(z,ey,G)==ay)return 1; } return 2; }} } ```
**Engine results.** Each arm ran alone against the 30 bps VanillaStrategy for 10,000 steps per seed. SE is sample standard deviation of paired seed differences divided by sqrt(n). These are simulation edge differences, not leaderboard scores; SE does not account for searching many policies. | Candidate | Comparator | n | Mean gain | SE | Inclusive engine seeds | |---|---|---:|---:|---:|---| | AGE | lb-4 | 512 | +0.223992329 | 0.018530803 | 10016072..10016583 | | AGE | qt1 | 512 | +3.446088217 | 0.045177687 | same | | AGE | LEAN | 512 | +0.032245553 | 0.008394521 | same | | QLX5, A | LEAN | 512 | +0.025802473 | 0.007294233 | 10010000..10010511 | | QLX5, B | LEAN | 512 | +0.035185441 | 0.007125250 | 10010512..10011023 | | QLX5, pooled | LEAN | 1,024 | +0.030493957 | 0.005098025 | 10010000..10011023 | The write-up recalculated AGE from all unrounded rows in [AGE-E], and QLX from all rows in [QLX-A]/[QLX-B], without averaging SEs or selecting favorable subsets. Block B stores six-decimal edges, so its recalculated and pooled last digits are approximate; its original unrounded summary reports +0.035185, SE 0.007125. Both engine artifacts embed byte-identical QLX5 source matching the later certificates. No new simulation was run here. AGE had 5,581,936 `afterSwap` calls, no missing/burned seeds and zero failure counters in all arms. Most of its lb-4 gain was already in LEAN: +0.191746776 (SE 0.016891799) on these same seeds. [AGE-E]. QLX5's block peaks were 207,838 and 208,366, with zero failures. QLX3 pooled +0.026952222 (SE 0.005080780) over LEAN; QLX5−QLX3 was +0.003541735 (SE 0.002394412), not separated at two SE. QLX5 was picked in replica research before E. There is no paired AGE−QLX5 measurement and their gains cannot be added. [QLX-A], [QLX-B]. Limits: 50,000 characters, 24,576 deployed bytes, 245,000 peak gas. AGE has 5,384 characters / 3,069 bytes / 45,433 observed gas headroom; QLX5 has 2,770 / 2,136 / 36,634. Gas headroom is not a universal bound. Local compilation reproduced both recorded runtime byte hashes. **Baseline changes.** [lb-4][LB4]'s historical +3.20-over-qt1 on about 1,850 seeds is supplied context. The base became [TellMinLookup (TML)][TML], with U1 tables and constant 0.35 rival pull, then [LEAN][LEAN], substituting lb-4's cheaper bounded tell. The linked bundles and engine results pin the complete control-source hashes; qt1 is the kit's materialized reference. On 512 engine seeds 10003536..10004047, LEAN−lb-4 was +0.1637 (SE 0.0161) and LEAN−qt1 +3.391 (SE 0.051). Later LEAN/TML edges were exactly equal on **1,008 completed seeds**, 768+240, not the planned 1,536; the incomplete second lease had 192 burned and 336 untouched seeds. Their equality-test peaks were at most 199,412/209,166. This is finite-sample agreement, not universal equivalence. [R2], b18-qlite-a/b34-tml-a; [R3], b34-tml-b. **BASE/LEANBASE research frequently used full qm tables; the production contracts use U1/QPL1.** Gains on those research policies do not automatically transfer to the contracts. **Gate ledger, in the required P → Q → T → F → G → E order.** No candidate completed the whole ordered chain. Passes describe the cited worker's scope. | Gate | AGE, 8cb006fa… | QLX5, e183e5bc… | |---|---|---| | P: Taylor gates.py | Not run for this package. | Both e07 auditors reproduce all 12 canonical values. This checks the reference environment, not candidate performance. | | Q: full 1,024 choose / 2,048 report, paired lb-4/qt1 | Missing for production policy. Earlier AGE research is not full Q. | Missing. Prior full-qm reference differs from production U1; largest report only 1,024 seeds. | | T: 50 full replica seeds | Pass: 549,505 contract-driven callbacks, zero tell/rule mismatches; separate open-loop label check on 352,817 first trades. | Both audits pass with actual prior-quote feedback: 549,802 callbacks. Copy B also checks 549,733 fixed-input callbacks. | | F: 50×2,000, <=0.01 bps | Pass: maximum 7.48403075623294e-11 bps. | Pass with repaired tables: copy A maximum 1.8001225510211327e-10 bps; copy B fixed-input maximum 5.273786e-08 bps. | | G: size/characters/engine gas | Eight seeds 10009056..10009063 pass, peak 197,103; later E observes 199,567. | Historical compile/engine observations fit limits. Neither e07 audit runs G; ordered post-Q G absent. | | E: fresh paired candidate/lb-4/qt1 | Complete four-arm 512-seed measurement, but P/Q missing upstream. | Only historical QLX3/QLX5/LEAN experiment; required direct lb-4/qt1 pairing and final E absent. | T/F seeds were 74200..74249. QLX T/F used replica_e, not Taylor's original replica2 or the Rust engine. AGE's `all_pass=true` means T/F/G only. Its F shadow receives actual prior fee wads and uses decoded production U1. Its neither-bound-nor-pull case, over-budget label and fee-guard shifts were not exercised; its packed timestamp is 29 bits versus 10,000-step engine runs. [AGE-S], [CERT-A], [CERT-B]. **QLX certification: a repaired comparator, unchanged Solidity.** At seed 74200 callback 0, full-qm Python returned bid 2956545026965740 versus Solidity's 2951324332807182 wei, a 0.05220694158558 bps difference; ask differed by 0.02125451091531 bps. At callback 1 that prior-quote mismatch flipped the lean label and arb/tested branch. The offset search did not cause that first mismatch. Both auditors repaired Python to decode the contract's 10,160-byte QPL1 blob, hash `830868f9b3e5ad162c4f2070e6faed0782c7b4cc308ab69044b2cf7306f68099`. [CERT-A], [CERT-B]. They also matched Solidity's raw `qlx.hit_mku`, keeping the original exponent, instead of binade-safe `qlx.hit`. Raw/safe synthetic differences were 191/6,000 and 493/50,000; no audited real seed crossed that boundary. QLX5 tries `[m0,m0+1,m0-1,m0+2,m0-2]` on non-arb callbacks with `dt<=6`, updating on acceptance. A binade normalization change would require a new source hash and validation. [CERT-A], [CERT-B]. In the passing protocol, Python maintains independent filter, rival estimate, phase and offset; its output is computed and compared **before** the contract's quote is supplied as the next observable fee. There is no EVM storage injection or hidden truth input. Split-verdict observation partly uses an unchanged TellMath probe, cross-checked against Strategy's actual offset. Copy A observed 356,965 tests, 317,544 hits and 118,104 offset moves with no disagreement. Its 180,159 pyrevm message-gas peak is not engine gas. [CERT-A]. The “autonomous” reports use different definitions. Copy B's fresh stream is **contract-quote-driven** and passes. Copy A additionally uses a Python shadow with **no quote feedback**, on the same contract stream: it fails on all 50 seeds, 542,700/549,802 callbacks, worst fee error 486.2889 bps. The first offset divergence (−2 versus 0) follows a 284-wei prior-fee difference. Thus P/T/F pass under observable quote feedback; independently self-quoted Python trajectories do not match. Full production Q for structural search remains unestablished. [CERT-A], [CERT-B]. The two audit artifacts preserve exact reference patches, harnesses, manifests, hash recipes and reproduction commands. Their reference hashes differ because they package independent repairs; Solidity is identical. [CERT-A], [CERT-B]. Root 3's blanket statement that old Q/E is void overreaches: old **full-qm replica Q** cannot certify this U1 policy, but historical **engine measurements of unchanged e183e5bc… Solidity remain evidence for that source**. They still are not a complete E pass. Likewise, audit fee mismatch does not remeasure the original +0.111 edge loss of lb-4's different compressed tables. Root 3's stack recommendation is superseded by the final requester scope. [R3], [CERT-A], [QLX-A], [QLX-B]; goal episodes 6–7. **Lever results.** Comparisons differ in base and backend and are not additive. | Lever | Measured gain and outcome | |---|---| | Rival64 / few-point | Brief: rival64 +0.145, SE 0.015 on original no-pull policy. Later BASE-relative rival64 +0.04230, SE 0.00585, n=2,048 replica, requiring about 198 storage words. Few-point variants +0.04754 to +0.05400 on n=1,216; port T/F passed but peak 248,120 and **94,550 OOG / 94,578 calls**. No feasible full tracker. [R1], b07-few-point/b08-rival64/b11-few-point. | | q-lite / QLX5 | cap24+GATE +0.038568, SE 0.003636, n=2,048 replica; only +0.00361, SE 0.00365, n=512 engine over LEAN. Drift-tolerant QLX5 restores a +0.030494, SE 0.005098 engine gain, n=1,024, below +0.05. [R2], b16/b18; [QLX-A], [QLX-B]. | | AGE pull | `alpha=0.7*age/(age+1)` replaces constant 0.35, age since last binding bound. Replica +0.019710, SE 0.004066, n=2,048; production engine +0.032245553, SE 0.008394521, n=512. Cheap but insufficient alone; stacking unmeasured and outside latest scope. [R1], b07-age-alpha/b16-age-thr; [AGE-E]. | | Compression | Brief: FULL kappa 1e-5 beats old lb-4 compression by +0.111. Requester history: U1 already recovers +0.097, SE 0.008, n=1,024 engine. Later FULL−U1 choose ceilings +0.00079 to +0.00254 (SE about 0.006–0.007); another report ceiling +0.00618, SE 0.00411. No further positive size-compatible contract. Do not count +0.111 again. [R1], b12; goal episode 2. | | Refit | Brief: rp-3 about +0.05 engine on earlier base. Later U1 ridge refit −0.00171, SE 0.00197, n=2,048 report; visit refits −0.0047/−0.0023 on choose. No incremental U1 gain; original rp-3 engine claim not reproduced as the same experiment. [R1], b12-refit-visit/rp3. | | Price between pins | Brief's true-price oracle +1.19 and tell-flag +0.006 are context, not live policy gains. Selected two-component filter +0.0026, SE 0.0022, n=512; full lattice +0.0101, SE 0.0090, n=256, with prohibitive estimated gas. No observable policy captures the oracle gap. [R1], [R2], b19. | Engine recomputation of rival `k=x*y` from rounded reserves disrupted q-lite's exact split test: about 10% hits versus 78% on the replica. Three/five tracked denominators restored 81.5%/89.2%. Detection is imperfect: decoy false accepts were 0.28%, with rare retail split errors up to 14.39 bps in later audits. [R2], b26; [R3], b35. Other tested lines produced no advancing contract: - **Exact rival carry:** −1.4274 (SE 0.0237) and −1.4214 (0.0230) vs QLITE, n=1,024; oracle rival +0.1054 (0.0084) over QLITE. Unseen rival flow defeated these specific rules. [R3], b35. - **Split-derived price bands:** −137.11 (SE 1.78), and −151.63 (1.95) with an extra edge term, n=512; the split spot is after retail, not the preceding arb band. The separate arb-pin item ran nothing: untested, not negative. [R2], b19/b25. - **Learned fee heads:** linear/h4x4/h8x4/wide-linear, 100/100/100/141 generations, holdout n=2,048: +0.005755 (SE 0.004623), −0.000936 (0.004419), −0.001021 (0.005648), −0.020432 (0.005510). None cleared +0.05 or reached Solidity/E. [R2], b28; goal episode 6. - **Run-parameter oracles:** at most +0.0049 (SE 0.0027), n=2,048, with existing tables; no live estimator. Bayesian pull, soft clips and threshold/blend rules produced no eligible engine upgrade. [R2], b27; [R1], b07. A rare coverage issue persists: replica seed 10942 lost about 35.67 when an unsupported lean-tell label was treated as retail. Neutral likelihood fixed that Python case. The frozen AGE/QLX5 source still retains variant A (unsupported/over-budget labels are misses); no fix was silently inserted here. LEAN/TML's 1,008 engine seeds did not show the failure, and QLX's 50 audit seeds saw neither unsupported nor over-budget labels. Any change needs a new hash and affected gates repeated. [R2], b20; [CERT-A]; source listings. **Distance to #1 and missing work.** In the requester's 2026-11-10 snapshot, TML was fourth at **528.3025**, first **528.4152**: **0.1127** still to close on the board. Third was 528.3479 (gap 0.0454); lb-4 was 528.1088 (gap to first 0.3064). TML was a sibling goal's submission `9b220625-75c1-4f47-bc90-b0b123b1bafd` at 14:39Z. This is supplied history, not a fresh leaderboard check or this goal's submission. Neither new component clears +0.05 over LEAN. Their approximately +0.03 engine gains cannot be added to the board score, or to each other, to claim a rank. What remains is a larger observable improvement, a faithful production-policy Q evaluator, the full ordered gates, and fresh confirmation including TML, lb-4 and qt1. Oracle gaps are not executable strategies. The final structural search requested three families: phase/tell, rival bands, and synced-table/level switching. Goal state records **three failed items, zero accepted**, with no handed result or failure explanation. Their gains are unknown; the three ports and twelve engine shards were cancelled. The QLX5+AGE stack had source `c25a999646859d187f9a03655b4c7b3009a41b9cb20b4a7b56a19d612242ebc4`, 47,599 characters, 22,525 bytes, P and a four-seed smoke only. Its later pipeline was cancelled when the requester excluded AGE integration; it is not the selected next action. [R3], b38; goal e06/e07 batches. Retain TML as the already submitted entry and LEAN as its cheaper measured base. QLX5 is the latest modest improvement with targeted P/T/F evidence; AGE is the strongest direct lb-4/qt1 experiment. Neither is claimed fully eligible or first. **The requester must ask the user before any future leaderboard submission.** **Provenance and seed handoff.** No engine seeds were consumed here. Retire prior leases through 10019023 and the reserved e07 G 10020000..10020023 / E 10021000..10022535, including unused, interrupted or cancelled parts. **Next free: 10023000.** Avoid other goals' 9.70M, 9.978M, 9.995M and 13.99M..14.01M banks. Choose 64000..65023 / report 10000..12047 were repeatedly exposed; they are exploratory, not fresh confirmation. Engine: [amm-challenge@6b6921f8d463314aa7c96234e1be25a25245f704][ENGINE], `amm_sim_rs 0.1.0.post1`; [kit][KIT] `45e7563d1a29ffca9d608fdf60c90bf2624d2d01`; [fresh-seed driver][DRIVER] hash `d3f8646b541a76921a47e9d03247c8c9dfab594076a7c5dca79c36b6c048a282`. Later research also used Taylor snapshot `66604b3426d8e97a73e2c39940b9b4dece51cc73`. Final goal state has **trunk=null**, all 53 batches mode **research**: **no goal code repository or wave commit exists**. These linked repositories are dependencies. All five handed inputs were read and hash-checked. [R1]/[R2]/[R3] flag citation re-check failures, including AGE and both certificates, without identifying URLs. Re-fetched source/result hashes and recomputed rows support this report, but neither resolve every citation flag nor independently certify worker behavior. **Final decision record.** The sole output is report.md, so the reference-shaped object is embedded. `finish` closes this write-up, not the unmet #1 objective. No new batches. Historic briefs are truncated; episode 7 records one certificate/family/port/shard per item and required hash/gate/statistic outputs. ```json { "notes": "Credit Taylor (gtmcknight), amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved. Write-up complete; #1 unachieved. AGE has the strongest direct lb-4/qt1 experiment but lacks P/Q. Unchanged QLX5 e183e5bc3fb794b7067703b10a7da9920267aa5f4caa968e16ca101b02338f40 gains about 0.030494 SE 0.005098 over LEAN on 1024 historical engine seeds. P/T/F pass with actual prior-quote feedback; self-quoted Python diverges. Full production Q, direct final E and ordered eligibility remain missing. Three structural items failed without handed results. No goal trunk, new engine seeds or submission. Next free seed 10023000; preserve all prior leases. Requester must ask user before submission.", "spawn": [], "read": [], "cancel": [], "finish": true } ``` QLX5's complete, unchanged **Strategy.sol** follows: SHA-256 `e183e5bc3fb794b7067703b10a7da9920267aa5f4caa968e16ca101b02338f40`, byte-identical in [QLX-S], [QLX-A], [QLX-B] and as audited by [CERT-A]/[CERT-B]. Extract as an alternative Strategy.sol, not concatenated with AGE.
Complete QLX5 Strategy.sol — 47,230 ASCII bytes ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; // Solved AMM policy by Taylor (gtmcknight), amm-sim 4812a638 research/solved: Gaussian filter + qm tables (qm_k1e-5.json), // fee rule of real.py's qm variant (sd level, phase, synced flag). All credit for the policy, filter and tables: Taylor. // Original tables compressed by qm-spline (cubic B-splines, QMS1 blob sha256 3ac7cb88...); filter, 3-slot state, math and fee conversion // from trim-2 (port-3's filter); synced flag stored in bit 255 of slot 2. // lookup-headroom: C0 (sha256 c68887cf...) with coefficient words loaded once per callback into memory and the // second knot-span search of each axis walking forward from the first; same arithmetic, same fees. // give-u1: lookup-u1 (Strategy.sol sha256 69c361ef...) plus one rule, the constant rival pull of GivePolicy (give.py sha256 8d871a6b...). // lean-sol: give-u1 (Strategy.sol sha256 99d031f4...) with the exact-arithmetic isArb replaced by lb-4's LEAN TELL (bounded // sell_one_division / buy_grid, budget 4): a first own trade is read as arbitrage iff the lean label is positive; negative, over // budget and unsupported are misses; chance, filter, tables, pull, fee guard and storage slots unchanged (LEANBASE variant A). // qlite-a: LEAN (sha256 a6dfc3ba...) plus the frozen qlgate rule cap24+GATE (qlgate.py sha256 0823f2d2..., qlite_int.capped_int cap 24): // on a callback not read as arbitrage and at most 6 steps after the previous own callback, the capped router-quotient split test; // a hit sets the rival spot to the marginal-price candidate, synced, and skips the 0.35 pull; a miss leaves LEAN unchanged. // Credit: Taylor (gtmcknight), github.com/gtmcknight/amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved. // Taylor source: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; uint256 constant M64=0xffffffffffffffff; // slots[0] = m (int128) | v (int128)<<128 ; slots[1] = sp (int128) | r (int128)<<128 ; slots[2] = fb | fa<<64 | t<<128 | cnt<<192 function getName() external pure override returns(string memory) { unchecked {return "TellMinLookup";} } 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;} } function clip(int256 x,int256 a,int256 b) internal pure returns(int256){ unchecked {return xb?b:x);} } function ln(int256 x) internal pure returns(int256 z){ unchecked { require(x>0); int256 k; while(x< U){x*=2;k--;} while(x>=2*U){x/=2;k++;} int256 y=divi(x-U,x+U); int256 y2=mul(y,y); int256 q=y; z=y; for(int256 n=3;n<=29;n+=2){q=mul(q,y2);z+=q/n;} return 2*z+k*LN2; } } function exp(int256 x) internal pure returns(int256){ unchecked { if(x< -42*U)return 0; require(x<60*U); int256 k=(x>=0?x+LN2/2:x-LN2/2)/LN2; int256 r=x-k*LN2; int256 r2=mul(r,r); int256 even=665280*U+mul(r2,75600*U+mul(r2,840*U+r2)); int256 odd=mul(r,332640*U+mul(r2,10080*U+mul(r2,42*U))); int256 s=divi(even+odd,even-odd); return k>=0?s*int256(uint256(1)<>128>0){z>>=128;r<<=64;} if(z>>64>0){z>>=64;r<<=32;} if(z>>32>0){z>>=32;r<<=16;} if(z>>16>0){z>>=16;r<<=8;} if(z>>8>0){z>>=8;r<<=4;} if(z>>4>0){z>>=4;r<<=2;} if(z>>2>0){r<<=1;} for(uint256 i=0;i<7;i++)r=(r+n/r)/2; return int256(r10*U)return 0;return divi(exp(-mul(x,x)/2),2506628274631000200);} } // Hart rational normal tail approximation; erf(x)=2*Phi(x*sqrt(2))-1. function cdf(int256 x,int256 density) internal pure returns(int256){ unchecked { int256 y=ab(x);if(y>8*U)return x>0?U:int256(0); int256 n=35262496599891000; n=mul(n,y)+700383064443688000;n=mul(n,y)+6373962203531650000; n=mul(n,y)+33912866078383000000;n=mul(n,y)+112079291497871000000; n=mul(n,y)+221213596169931000000;n=mul(n,y)+220206867912376000000; int256 d=88388347648318400; d=mul(d,y)+1755667163182640000;d=mul(d,y)+16064177579207000000; d=mul(d,y)+86780732202946100000;d=mul(d,y)+296564248779674000000; d=mul(d,y)+637333633378831000000;d=mul(d,y)+793826512519948000000; d=mul(d,y)+440413735824752000000; int256 tail=mul(mul(density,2506628274631000200),divi(n,d));return x>0?U-tail:tail; } } function trunc(int256 m,int256 v,int256 lo,int256 hi) internal pure returns(int256 Z,int256 mt,int256 vt){ unchecked { if(v<=0)return (lo<=m&&m<=hi?U:int256(0),m,0); int256 sd=root(v);int256 a=divi(lo-m,sd);int256 b=divi(hi-m,sd); int256 pa=pdf(a);int256 pb=pdf(b); Z=cdf(b,pb)-cdf(a,pa); if(Z<1e6)return(Z,m>(16*(i%16)))&65535;}} function spanR(QT memory t,uint256 x) internal pure returns(uint256 a){unchecked{ uint256 hi=t.nr-1;while(hi-a>1){uint256 m=(a+hi)/2;if(rk(t,m)<=x)a=m;else hi=m;} }} function spanC(QT memory t,uint256 x) internal pure returns(uint256 a){unchecked{ uint256 hi=t.nc-1;while(hi-a>1){uint256 m=(a+hi)/2;if(((t.ck>>(8*m))&255)<=x)a=m;else hi=m;} }} function record(bytes memory b,QT memory t,uint256 l,uint256 r,uint256 c) internal pure returns(int256 v,int256 vn){unchecked{ uint256 o=t.off+(l*t.nr+r)*(t.nc+1); v=int256(int16(uint16(uint8(b[o]))|(uint16(uint8(b[o+1]))<<8))); for(uint256 n=0;n319)i=319;int256 f=pw-int256(i)*U; uint256 j=uint256(p2/U);if(j>jm)j=jm;int256 g=p2-int256(j)*U; uint256 a;while(a>(16*(a+1)))&65535)<=k)a++; uint256 r=spanR(t,i);uint256 c=spanC(t,j); int256[8] memory v; for(uint256 l=0;l<2;l++)for(uint256 row=0;row<2;row++)(v[l*4+row*2],v[l*4+row*2+1])=record(b,t,a+l,r+row,c); int256[2] memory lr;lr[0]=int256((t.lk>>(16*a))&65535);lr[1]=int256((t.lk>>(16*(a+1)))&65535); int256 rlo=int256(rk(t,r));int256 rd=int256(rk(t,r+1))-rlo; int256 clo=int256((t.ck>>(8*c))&255);int256 cd=int256((t.ck>>(8*(c+1)))&255)-clo; int256[2] memory levels; for(uint256 l=0;l<2;l++){ int256 u=(int256(k+l)-lr[0])*U/(lr[1]-lr[0]);int256[4] memory q; for(uint256 z=0;z<4;z++)q[z]=lerp(v[z],v[z+4],u); int256[4] memory nodes; for(uint256 row=0;row<2;row++){ int256 rf=(int256(i+row)-rlo)*U/rd; int256 x0=lerp(q[0],q[2],rf);int256 x1=lerp(q[1],q[3],rf); for(uint256 col=0;col<2;col++){ int256 cf=(int256(j+col)-clo)*U/cd; int256 n=lerp(x0,x1,cf);nodes[row*2+col]=n>0?n:int256(0); } } levels[l]=lerp(lerp(nodes[0],nodes[2],f),lerp(nodes[1],nodes[3],f),g); } return lerp(levels[0],levels[1],tt); }} function w2l(uint256 L) internal pure returns(int256){ unchecked { uint256 K=L<16?0x7ff77ff67ff57ff47ff27fef7fec7fe77fe27fd87fce7fc47fb07f9c7f7e7f60:(L<32?0x800780068005800480038002800180007fff7ffe7ffd7ffc7ffb7ffa7ff97ff8:(L<48?0x808280648050803c80328028801e801980148011800e800c800b800a80098008:0x00000000000000000000000000000000000000000000000000000000000080a0)); return int256((K>>(16*(L&15)))&0xffff)-32768; } } // real.py/evalpol _pos2 over pol_mid's W2L (grid units of 5 bps), WAD in and out function pos2(int256 v) internal pure returns(int256){ unchecked { if(v<=-160*U)return 0; if(v>=160*U)return 48*U; uint256 lo=0;uint256 hi=48; while(hi-lo>1){uint256 md=(lo+hi)>>1;if(w2l(md)*U<=v)lo=md;else hi=md;} int256 a=w2l(lo);return int256(lo)*U+(v-a*U)/(w2l(hi)-a); } } function sdl(uint256 k) internal pure returns(int256){ unchecked { return int256((0x221a14100c080400>>(8*k))&0xff); } } // qm fee rule: w, w2 offsets (WAD bps), phase, synced flag, belief variance v (WAD, ln-price^2) function fees(int256 w,int256 w2,uint256 ph,bool syn,int256 v) internal pure returns(int256 fb,int256 fa){ unchecked { int256 sd=root(v)*10000;uint256 k; while(k<6&&sd>=sdl(k+1)*U)k++; int256 tt=clip((sd-sdl(k)*U)/(sdl(k+1)-sdl(k)),0,U); int256 pw=clip(w/5+160*U,0,320*U);int256 wq=(pw-160*U)*5; uint256 tb;int256 p2;int256 nc1; if(ph==0){tb=0;p2=clip(w2/5,-6*U,6*U)+6*U;nc1=12;} else{tb=(ph==1?1:3)+(syn?0:1);p2=pos2(w2/5);nc1=48;} QT memory t=qt(tb);bytes memory b=QPL; int256 xa=surf(t,b,pw,p2,k,tt,uint256(nc1)-1)+wq-w; int256 xb=surf(t,b,320*U-pw,nc1*U-p2,k,tt,uint256(nc1)-1)+w-wq; fb=xb>0?U-exp(-xb/10000):int256(0);fa=xa>0?U-exp(-xa/10000):int256(0); fb=clip(fb,0,U/10);fa=clip(fa,0,U/10); } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { int256 sp=int256(y*1e18/x); slots[0]=uint256(uint128(int128(ln(sp)))); slots[1]=uint256(uint128(int128(sp)))*(1+(uint256(1)<<128)); slots[3]=x;slots[4]=y;slots[5]=0; slots[2]=2823606104386589*(1+(uint256(1)<<64)); return(2823606104386589,2823606104386589); } } // filter update over a gap of dt steps; returns new m, v, rival spot r and the arb flag function update(int256 m,int256 v,int256 fb,int256 fa,int256 sp,int256 r,int256 s,bool buy,uint256 dt,bool hit,int256 chance) internal pure returns(int256,int256,int256,bool arb){ unchecked { int256 lo=ln(mul(sp,U-fb));int256 hi=ln(divi(sp,U-fa)); uint256 n=dt-1;if(n>12)n=12; for(uint256 i=0;i0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ m=mul(pi,lp)+mul(U-pi,mt); v=mul(pi,mul(lp-m,lp-m))+mul(U-pi,vt+mul(mt-m,mt-m)); }else if(arb){m=lp;v=0;}else{m=mt;v=vt;} int256 pe=exp(m);r=clip(r,mul(pe,997e15),divi(pe,997e15)); return(m,v,r,arb); } } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 s0=slots[0];uint256 s1=slots[1];uint256 s2=slots[2]; int256 m=int256(int128(uint128(s0)));int256 v=int256(int128(uint128(s0>>128))); int256 sp=int256(int128(uint128(s1)));int256 r=int256(int128(uint128(s1>>128))); int256 fb=int256(s2&M64);int256 fa=int256(s2>>64&M64);uint256 cnt=s2>>192&0xffffffff;bool syn=s2>>255==1; int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>(s2>>128&M64)){ bool hit=TellMath.ltTell(slots[3],slots[4],uint256(t.isBuy?fb:fa),t.amountX,t.amountY,t.reserveY,t.isBuy)==1; int256 chance=hit?int256(TellMath.ulpOverGridWad(t.isBuy?t.amountX:t.amountY,t.isBuy?slots[3]:slots[4],t.isBuy?t.reserveX:t.reserveY)):int256(0); (m,v,r,arb)=update(m,v,fb,fa,sp,r,s,t.isBuy,t.timestamp-(s2>>128&M64),hit,chance);cnt=0;syn=false; } uint256 ph; if(!arb){ int256 c=t.isBuy?divi(mul(s,U-fb),997e15):divi(mul(s,997e15),U-fa); // counting variant qlx5 (copy-b, e04-split-engine): tracked-offset split test (see copy-b result). Credit Taylor (gtmcknight). bool qx;if(t.timestamp<(s2>>128&M64)+7){int256 m0=int256(slots[6]);int256 mm;(qx,mm)=TellMath.qHitX(t.isBuy?t.amountX:t.amountY,slots[3],slots[4],uint256(t.isBuy?fb:fa),t.isBuy,m0,5);if(mm!=m0)slots[6]=uint256(mm);}if(qx||(t.isBuy?cr)){r=c;syn=true;} // give: bound did not bind -> pull ln r 0.35 of the way to ln c (GivePolicy, alpha=alpha2=0.35) else if(c!=r){int256 lr=ln(r);r=exp(lr+mul(35e16,ln(c)-lr));syn=true;} cnt++;ph=cnt==1?1:2; } slots[3]=t.reserveX;slots[4]=t.reserveY; int256 w=(ln(s)-m)*10000;int256 rr=(ln(r)-m)*10000; (fb,fa)=fees(w,rr,ph,syn,v); fb=guardFee(fb);fa=guardFee(fa); slots[0]=uint256(uint128(int128(m)))|uint256(uint128(int128(v)))<<128; slots[1]=uint256(uint128(int128(s)))|uint256(uint128(int128(r)))<<128; slots[2]=uint256(fb)|uint256(fa)<<64|t.timestamp<<128|cnt<<192|(syn?uint256(1)<<255:0); return(uint256(fb),uint256(fa)); } } // Complete integer preimage of Vanilla gamma under IEEE binary64 conversion. // Closest integer outside that interval; ties select the lower fee. function guardFee(int256 f) internal returns(int256) { if(f>=2999999999999948&&f<=3000000000000057){slots[5]++;return f-2999999999999947<=3000000000000058-f?int256(2999999999999947):int256(3000000000000058);}return f; } function guardShifts() external view returns(uint256) {return slots[5];} // lean label of a trade: 0 unsupported, 1 positive, 2 negative, 3 over budget; isArb = (label == 1). x (reserveX) is not used. function leanLabel(bool b,uint256 ax,uint256 ay,uint256,uint256 y,uint256 px,uint256 py,uint256 fb,uint256 fa) public pure returns(uint256) {return TellMath.ltTell(px,py,b?fb:fa,ax,ay,y,b);} // qlite-a decision as afterSwap takes it: p paid wad, px/py previous reserves, f fee wad of the paid side, dt steps since the previous own callback function isArb(bool b,uint256 ax,uint256 ay,uint256 x,uint256 y,uint256 px,uint256 py,uint256 fb,uint256 fa) external pure returns(bool) {return leanLabel(b,ax,ay,x,y,px,py,fb,fa)==1;} } library TellMath { uint256 internal constant WAD = 1e18; uint256 internal constant TWO53 = 1 << 53; uint256 internal constant TWO52 = 1 << 52; uint256 internal constant LIM = 1 << 128; /// floor(log2(x)) + 1, 0 for x = 0, by binary normalization. function bitLen(uint256 x) internal pure returns (uint256 n) { unchecked { if (x >= 1 << 128) { x >>= 128; n += 128; } if (x >= 1 << 64) { x >>= 64; n += 64; } if (x >= 1 << 32) { x >>= 32; n += 32; } if (x >= 1 << 16) { x >>= 16; n += 16; } if (x >= 1 << 8) { x >>= 8; n += 8; } if (x >= 1 << 4) { x >>= 4; n += 4; } if (x >= 1 << 2) { x >>= 2; n += 2; } if (x >= 1 << 1) { x >>= 1; n += 1; } if (x != 0) n += 1; } } /// num/den (both > 0) rounded to nearest-even double m*2^e, 2^52 <= m < 2^53. /// One division: the bit-length estimate is off by at most one binade, fixed by a shift. function rn(uint256 num, uint256 den) internal pure returns (uint256 q, int256 e) { e = int256(bitLen(num)) - int256(bitLen(den)) - 53; uint256 sn; uint256 sd; if (e < 0) { sn = num << uint256(-e); sd = den; } else { sn = num; sd = den << uint256(e); } q = sn / sd; // estimate gives q in [2^52, 2^54): at most one step down in precision if (q >= TWO53) { e += 1; if (e <= 0) { sn = num << uint256(-e); } else { sn = num; sd = den << uint256(e); } q = sn / sd; } uint256 r = sn - q * sd; uint256 twice = 2 * r; if (twice > sd || (twice == sd && (q & 1) == 1)) { q += 1; if (q == TWO53) { q = TWO52; e += 1; } } } /// e with 2^e * 1e18 <= w < 2^(e+1) * 1e18 (w > 0). function binade(uint256 w) internal pure returns (int256 e) { e = int256(bitLen(w)) - 60; // bitLen(1e18) = 60 if (!_atLeast(w, e)) e -= 1; else if (_atLeast(w, e + 1)) e += 1; } function _atLeast(uint256 w, int256 e) private pure returns (bool) { return e >= 0 ? (WAD << uint256(e)) <= w : WAD <= (w << uint256(-e)); } function _min(int256 a, int256 b) private pure returns (int256) { return a < b ? a : b; } /// log2 of the grid: min(binade(before), binade(after)) - 52. function gridExp(uint256 beforeW, uint256 afterW) internal pure returns (int256) { return _min(binade(beforeW), binade(afterW)) - 52; } /// min(1, ulp(paid)/grid) scaled by 1e18, paid = fl(paidW/1e18) (the double behind paidW). /// Returns 0 for out-of-bounds inputs. 1e18 means the paid amount is no finer than the grid. function ulpOverGridWad(uint256 paidW, uint256 beforeW, uint256 afterW) internal pure returns (uint256) { if (paidW == 0 || paidW >= LIM || beforeW == 0 || beforeW >= LIM || afterW == 0 || afterW >= LIM) return 0; (, int256 pe) = rn(paidW, WAD); int256 d = gridExp(beforeW, afterW) - pe; if (d <= 0) return WAD; if (d > 64) return 0; return WAD >> uint256(d); } // ---- LEAN TELL of result lb-4 (scratch/LeanTell.sol, functions verbatim): buy-grid's bounded sell_one_division / buy_grid tests, // budget 4, in exact integer binary64 (round-to-nearest-even) arithmetic. A double is m*2^e with 2^52 <= m <= 2^53; 1e18 = LT_C*2^7. // ltTell returns 0 unsupported, 1 positive, 2 negative, 3 over budget. Mirrors lean_tell.py bit for bit. Credit Taylor (gtmcknight). uint256 constant LT_C=7812500000000000; function ltMsb(uint256 x) internal pure returns(uint256 r){unchecked{ if(x>>128!=0){x>>=128;r=128;}if(x>>64!=0){x>>=64;r+=64;}if(x>>32!=0){x>>=32;r+=32;}if(x>>16!=0){x>>=16;r+=16;} if(x>>8!=0){x>>=8;r+=8;}if(x>>4!=0){x>>=4;r+=4;}if(x>>2!=0){x>>=2;r+=2;}if(x>>1!=0)r++; }} // n / 2^s to nearest, ties to even; s >= 1. function ltRound(uint256 n,uint256 s) internal pure returns(uint256 q){unchecked{ q=n>>s;uint256 rem=n&((uint256(1)<half||(rem==half&&(q&1)!=0))q++; }} // Wad::from_f64 of the double m*2^e: trunc(RN(m*2^e * 1e18)). function ltWad(uint256 m,int256 e) internal pure returns(uint256){unchecked{ uint256 n=m*LT_C;uint256 s=n>>105!=0?53:52;uint256 p=ltRound(n,s);int256 t=e+7+int256(s); if(t>=0)return t>128?type(uint256).max:p<>uint256(-t); }} // Wad of RN(d*2^e / g), g = G*2^-53, 0 < d < 2^53 exact. function ltDivWad(uint256 d,int256 e,uint256 G) internal pure returns(uint256){unchecked{ uint256 n=(d<<110)/G;if(n*G!=d<<110)n|=1; uint256 s=ltMsb(n)-52;return ltWad(ltRound(n,s),e+int256(s)-57); }} // G = 2^53 * RN(1 - RN(RN(fee)/1e18)), fee <= 1e17. function ltGamma(uint256 f) internal pure returns(uint256){unchecked{ if(f==0)return 1<<53; uint256 sf=ltMsb(f); if(sf>52){sf-=52;f=ltRound(f,sf);}else sf=0; uint256 n=(f<<110)/LT_C;if(n*LT_C!=f<<110)n|=1; uint256 s=ltMsb(n)-52;uint256 r=64-s-sf; return ltRound((uint256(1)<<(53+r))-ltRound(n,s),r); }} // Every double whose wad is w (2^60 <= w < 2^120): mantissas a, b (0 = absent), exponent e. function ltPre(uint256 w) internal pure returns(uint256 a,uint256 b,int256 e){unchecked{ uint256 n=(w<<64)/1e18;uint256 h=ltMsb(n);uint256 m=n>>(h-52);e=int256(h)-116; if(ltWad(m,e)==w)a=m; if(ltWad(m+1,e)==w)b=m+1; }} // Virtual reserves xv = (m+k)*2^e > rx whose amount wad is ax. function ltGrid(uint256 m,int256 e,uint256 k0,uint256 G,uint256 ax) internal pure returns(uint256 hits){unchecked{ if(m==0)return 0; uint256 lo=k0;uint256 hi=k0+1;if(k0>>48!=0){lo--;hi++;} if(lo==0)lo=1; for(uint256 k=lo;k<=hi;k++){ uint256 t=m+k;if(t>>53!=0&&(t&1)!=0)continue; if(ltDivWad(k,e,G)==ax)hits++; } }} // px, py: previous callback's reserve wads; fee: the fee wad returned then for this side // (bid when the pool buys X); ax, ay, cy: this trade's amountX, amountY, reserveY. function ltTell(uint256 px,uint256 py,uint256 fee,uint256 ax,uint256 ay,uint256 cy,bool buy) internal pure returns(uint256){unchecked{ if(fee>1e17)return 0; uint256 G=ltGamma(fee); if(buy){ if(px>>60==0||px>>120!=0||ax==0||ax>px>>1)return 0; (uint256 a,uint256 b,int256 e)=ltPre(px); if((a|b)==0)return 0; uint256 k0=ax*G/(uint256(1e18)<4)return 3; return hits!=0?1:2; } if(py>>60==0||py>>120!=0||cy>>60==0||cy>>120!=0||ay==0||ay>>120!=0)return 0; (uint256 y0,uint256 y1,int256 ey)=ltPre(py);(uint256 z0,uint256 z1,int256 ez)=ltPre(cy); if((y0|y1)==0||(z0|z1)==0)return 0; int256 de=ez-ey;if(de>1||de<-1)return 0; if(de==1){z0<<=1;z1<<=1;}else if(de==-1){y0<<=1;y1<<=1;ey=ez;} for(uint256 i=0;i<4;i++){ uint256 y=i<2?y0:y1;uint256 z=(i&1)==0?z0:z1; if(y==0||z<=y)continue; z-=y;if(z>>53!=0)return 0; if(ltDivWad(z,ey,G)==ay)return 1; } return 2; }} // ---- qlite-a split test: qlgate.py cap24+GATE = qlite_int.capped_int(cap=24) in exact integer binary64 (round-to-nearest-even) // arithmetic, one candidate (preimage pair (0,0), z = r, k = n). Credit Taylor (gtmcknight), amm-sim 4812a638 research/solved. uint256 constant QG=8980177656976769;int256 constant QGE=-53; // 0.997 uint256 constant QK=8782888972198741;int256 constant QKE=-43; // sqrt(1e6*0.997) // n > 0 to the nearest double m*2^s, 2^52 <= m < 2^53. function qR(uint256 n) internal pure returns(uint256 m,int256 s){unchecked{ uint256 h=ltMsb(n); if(h<53)return(n<<(52-h),int256(h)-52); s=int256(h)-52;m=ltRound(n,uint256(s));if(m>>53!=0){m>>=1;s++;} }} // RN(a/b), a and b of 53 bits: m*2^s. function qD(uint256 a,uint256 b) internal pure returns(uint256 m,int256 s){unchecked{ uint256 n=(a<<110)/b;if(n*b!=a<<110)n|=1; (m,s)=qR(n);s-=110; }} // RN(RN(w)/1e18), w > 0: float(w)/1e18. function qW(uint256 w) internal pure returns(uint256 m,int256 e){unchecked{ int256 s;(m,e)=qR(w);(m,s)=qD(m,LT_C);e+=s-7; }} // pw: wad paid; rx, ry: previous callback's reserve wads; fee: fee wad returned then for the paid side; buy: the pool bought X. // Outside pw in (0, 2^128), rx, ry in [2^40, 2^128) the answer is a miss. function qHitX(uint256 pw,uint256 rx,uint256 ry,uint256 fee,bool buy,int256 m0,uint256 nc) internal pure returns(bool,int256){unchecked{ if(pw==0||pw>>128!=0||rx>>40==0||rx>>128!=0||ry>>40==0||ry>>128!=0)return(false,m0); uint256 G=ltGamma(fee>1e17?1e17:fee); // gamma = G*2^-53 (uint256 pm,int256 pe)=qW(pw); (uint256 am,int256 ae)=qW(buy?ry:rx);(uint256 vm,int256 ve)=qW(buy?rx:ry); // v: paid-side reserve, grid = ulp(v) = 2^ve int256 s; (am,s)=qR(am*G);ae+=s-53;(am,s)=qR(am*vm);ae+=s+ve; // a = (u*gamma)*v s=((ae-57)&1)!=0?int256(58):int256(57); uint256 M=am<>1;if(y>=q)break;q=y;} // floor sqrt (am,s)=qR(q<<1|(q*q!=M?1:0));ae+=s; // sqrt(a) (am,s)=qD(am,QK);ae+=s-QKE; // r (am,s)=qR(am*QG);ae+=s+QGE; // r*0.997 s=ae<-53?ae:-53; (uint256 dm,int256 de)=qR((G<
Unchanged shared imports follow, preserving upstream licenses. Together with either Strategy.sol they permit offline source extraction. Imports are outside the submitted Strategy.sol character count. **AMMStrategyBase.sol**, SHA-256 `3f4154a6c4bb8f1223c7fc0bde1f9cd9dba10cef502e300d0f6d229f610b8869`.
Complete AMMStrategyBase.sol ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {IAMMStrategy, TradeInfo} from "./IAMMStrategy.sol"; /// @title AMM Strategy Base Contract /// @notice Base contract that all user strategies must inherit from /// @dev Provides fixed storage slots, helper functions, and fee clamping abstract contract AMMStrategyBase is IAMMStrategy { /*////////////////////////////////////////////////////////////// CONSTANTS //////////////////////////////////////////////////////////////*/ /// @notice 1e18 - represents 100% in WAD precision uint256 public constant WAD = 1e18; /// @notice Maximum allowed fee: 10% (1e17) uint256 public constant MAX_FEE = WAD / 10; /// @notice Minimum allowed fee: 0 uint256 public constant MIN_FEE = 0; /// @notice 1 basis point in WAD (0.01% = 0.0001 = 1e14) uint256 public constant BPS = 1e14; /*////////////////////////////////////////////////////////////// STORAGE SLOTS //////////////////////////////////////////////////////////////*/ /// @notice Fixed storage array - strategies can only use these 32 slots /// @dev This provides 1KB of persistent storage per strategy /// @dev Slot access is validated at the EVM level (array bounds) uint256[32] public slots; /*////////////////////////////////////////////////////////////// HELPER FUNCTIONS //////////////////////////////////////////////////////////////*/ /// @notice Multiply two WAD values /// @param x First value (WAD) /// @param y Second value (WAD) /// @return Result in WAD precision function wmul(uint256 x, uint256 y) internal pure returns (uint256) { return (x * y) / WAD; } /// @notice Divide two WAD values /// @param x Numerator (WAD) /// @param y Denominator (WAD) /// @return Result in WAD precision function wdiv(uint256 x, uint256 y) internal pure returns (uint256) { return (x * WAD) / y; } /// @notice Clamp a value between min and max /// @param value Value to clamp /// @param minVal Minimum value /// @param maxVal Maximum value /// @return Clamped value function clamp(uint256 value, uint256 minVal, uint256 maxVal) internal pure returns (uint256) { if (value < minVal) return minVal; if (value > maxVal) return maxVal; return value; } /// @notice Convert basis points to WAD /// @param bps Basis points (1 bps = 0.01%) /// @return WAD value function bpsToWad(uint256 bps) internal pure returns (uint256) { return bps * BPS; } /// @notice Convert WAD to basis points /// @param wadValue WAD value /// @return Basis points function wadToBps(uint256 wadValue) internal pure returns (uint256) { return wadValue / BPS; } /// @notice Clamp fee to valid range [0, MAX_FEE] /// @param fee Fee value to clamp /// @return Clamped fee value function clampFee(uint256 fee) internal pure returns (uint256) { return clamp(fee, MIN_FEE, MAX_FEE); } /// @notice Calculate absolute difference between two values /// @param a First value /// @param b Second value /// @return Absolute difference function absDiff(uint256 a, uint256 b) internal pure returns (uint256) { return a > b ? a - b : b - a; } /// @notice Simple integer square root (Babylonian method) /// @param x Value to take sqrt of /// @return y Square root function sqrt(uint256 x) internal pure returns (uint256 y) { if (x == 0) return 0; uint256 z = (x + 1) / 2; y = x; while (z < y) { y = z; z = (x / z + z) / 2; } } /*////////////////////////////////////////////////////////////// SLOT HELPERS //////////////////////////////////////////////////////////////*/ /// @notice Read a slot value /// @param index Slot index (0-31) /// @return Value stored in the slot function readSlot(uint256 index) internal view returns (uint256) { require(index < 32, "Slot index out of bounds"); return slots[index]; } /// @notice Write a value to a slot /// @param index Slot index (0-31) /// @param value Value to store function writeSlot(uint256 index, uint256 value) internal { require(index < 32, "Slot index out of bounds"); slots[index] = value; } } ```
**IAMMStrategy.sol**, SHA-256 `85b2a0fce484b1c4f70c5bebe6c06042293b2099983f0b641c4a6656c7ea15ff`.
Complete IAMMStrategy.sol ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /// @title Trade information passed to AMM strategies /// @notice Contains all data about an executed trade that strategies can use to adjust fees struct TradeInfo { bool isBuy; // true if AMM bought X (trader sold X) uint256 amountX; // Amount of X traded (WAD precision, 1e18) uint256 amountY; // Amount of Y traded (WAD precision, 1e18) uint256 timestamp; // Simulation step number uint256 reserveX; // Post-trade X reserves (WAD precision) uint256 reserveY; // Post-trade Y reserves (WAD precision) } /// @title AMM Strategy Interface /// @notice Interface that all AMM fee strategies must implement /// @dev Fees are returned as WAD values (1e18 = 100%, 1e15 = 0.1% = 10bps) interface IAMMStrategy { /// @notice Initialize the strategy with starting reserves /// @param initialX Starting X reserve amount (WAD precision) /// @param initialY Starting Y reserve amount (WAD precision) /// @return bidFee Fee when AMM buys X (WAD precision, e.g., 30e14 = 30bps) /// @return askFee Fee when AMM sells X (WAD precision, e.g., 30e14 = 30bps) function afterInitialize(uint256 initialX, uint256 initialY) external returns (uint256 bidFee, uint256 askFee); /// @notice Called after each trade to update fees /// @param trade Information about the just-executed trade /// @return bidFee Updated fee when AMM buys X (WAD precision) /// @return askFee Updated fee when AMM sells X (WAD precision) function afterSwap(TradeInfo calldata trade) external returns (uint256 bidFee, uint256 askFee); /// @notice Get the strategy name for display /// @return Strategy name string function getName() external view returns (string memory); } ```
**Write-up checks.** Input and source SHA-256 hashes and ASCII character counts were verified. The two embedded strategies were compiled separately with the pinned solc 0.8.24 settings and shared imports, reproducing 21,507 and 22,440 runtime bytes with zero compiler errors. All 512 AGE rows and both 512-row QLX blocks were decoded, checked for complete unique seed ranges and paired statistics recomputed. Markdown extraction checks ensure the four source blocks preserve the original bytes and the decision block parses as one JSON object. These are local packaging, compilation and arithmetic checks; P/Q/T/F/G/E simulations were not rerun. The platform verifier checks paths and bytes only. [TAYLOR]: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved [ENGINE]: https://github.com/benedictbrady/amm-challenge/tree/6b6921f8d463314aa7c96234e1be25a25245f704 [KIT]: https://github.com/identity-md-launches/goal-3178-build-shared-evaluation-kit/tree/45e7563d1a29ffca9d608fdf60c90bf2624d2d01 [DRIVER]: https://api.imd.fun/artifacts/764c9461a9f3bab6e277178f4bb48553d1b97c6cca33bed016f7c9bfd611cc42 [R1]: https://api.imd.fun/artifacts/6e49ac8603cf53f5ad5f5cd15171566ee820fa707140592b70992360eb1c1799 [R2]: https://api.imd.fun/artifacts/7ee5203bc3dd1ce86a3c50e9310a0bb4d909eb95ec89de5fbe7ae55bca7132fa [R3]: https://api.imd.fun/artifacts/9607fe6b57cca321e4fcc8005371a94d9d2964b3d7e87adbba3c69ceebba00bf [CERT-A]: https://api.imd.fun/artifacts/0e7b77ee3947fe43bedef9f98e24715c61b35338a9f30d581ee084334d44e0a9 [CERT-B]: https://api.imd.fun/artifacts/d73c1749574cce67042552e9278be2ba20c8fedf9720bdee71c27d4019f21f02 [AGE-S]: https://api.imd.fun/artifacts/0343e8049294da6680c2ee1ad413fa5dc95843b1893e96c3b173df49b850b3e1 [AGE-E]: https://api.imd.fun/artifacts/87dfbac60f00625d53477537621d5d15b2b95529011ed061eab3bb64452d677c [QLX-S]: https://api.imd.fun/artifacts/84a14d5edf847f83dfdd8f6a9c3e034a97004dcae6c069c11b4f6a2ffa7c517c [QLX-A]: https://api.imd.fun/artifacts/8f25be1e7a92585f01095429712ee5b25435abb73a2756bc6d5b47ac58421e4a [QLX-B]: https://api.imd.fun/artifacts/1ef80bfb6d6791b877b1f4048a5e63cf2ae54153e6e8763ae3f8ab13132dfb15 [LB4]: https://api.imd.fun/artifacts/93cc15561f172c49c8bce174143333ede904fdbfcf51276cbbeb514d4f5b614e [TML]: https://api.imd.fun/artifacts/fd2f147e94f3523e4c4f91e43dc98b88e9ce8ef1060d2939367445711154a16a [LEAN]: https://api.imd.fun/artifacts/a79a3606add967bb8f8ec2af8ef3d61bafb597419c488133597c8af5b9d34505