memoscan
← all memos

Memo 0x5fc60587…ede2da on Ethereum

0x68575b07…67c7·#25,913,248·view on Etherscan
isabled=true; let accs; try{accs=await rpc("eth_requestAccounts",[])} catch(e){err(e);return} if(!accs[0])return; let chainId=await rpc(I,[]); if(!CHAINS[+chainId]){ try{await rpc("wallet_switchEthereumChain",[{chainId:"0x1"}])} catch(e){stat.textContent=isRejection(e)?"":"Switch to a supported network.";return} chainId=await rpc(I,[]); if(!CHAINS[+chainId]){stat.textContent="Switch to a supported network.";return} } setChain(+chainId);showNet(); sessionStorage.removeItem("dc"); account=accs[0];sOpen(); const keepPair=pairAddrs(); showAddr(); if(!listLive){ swap.textContent="Loading tokens\u2026"; await loadTokenList(); } adoptList(keepPair); refreshBalance(); update(); fcMine(); loadPos(); syncChart(); if(lnMode)lnKindQuote(); }finally{connecting=false;swap.disabled=false; lnGo.disabled=false;lnGo.textContent=account?"Launch":"Connect Wallet"; wnGo.disabled=false; if(wnMode){wnPaint();wnQuote();wnRoll()}else wnGo.textContent="Register"; if(pvMode)pvWake();render()} } const lqReserveGas=()=>lqList.querySelector(".lqnew")?LQ_CREATE_GAS+300000n:400000n; const lqFillMax=(tok,max)=>{ const box=lqList.querySelector(".lqadd:not(.hide)"); const pair=chPair(); if(!box||!pair)return false; const side=pair[0].addr.toLowerCase()===tok.addr.toLowerCase()?"0":"1"; const el=box.querySelector(`.lqin[data-side="${side}"]`); if(!el)return false; el.value=formatUnits(max,tok.dec); el.dispatchEvent(new Event("input",{bubbles:true})); return true; }; bal1.addEventListener("click",async e=>{ if(e.target.tagName!=="A")return; e.preventDefault(); const t=TOKENS[toSel.value]; if(!t)return; let reserve=0n; if(t.addr===ZERO){ const g=lqReserveGas(); try{reserve=BigInt(await rpc("eth_gasPrice",[]))*g}catch{} if(reserve<=0n)reserve=GAS_FLOOR*g; } const max=toBalance>reserve?toBalance-reserve:0n; if(lqFillMax(t,max))return; outAmt.value=formatUnits(max,t.dec); fitFont(outAmt); lqPreviewSoon(); }); bal.addEventListener("click",async e=>{ if(e.target.tagName!=="A")return; e.preventDefault(); const f=TOKENS[fromSel.value]; if(!f)return; let reserve=0n; if(f.addr===ZERO){ const g=lqMode?lqReserveGas():tab==="swap"?900000n:tab==="book"?350000n :+dly.value?(tipCk.checked?200000n+TIP_GAS:200000n):30000n; try{reserve=BigInt(await rpc("eth_gasPrice",[]))*g}catch{} if(reserve<=0n)reserve=GAS_FLOOR*g; } const max=fromBalance>reserve?fromBalance-reserve:0n; if(lqMode&&lqFillMax(f,max))return; amt.value=formatUnits(max,f.dec); fitFont(amt); if(lqMode){lqPreviewSoon();return} setMode("in"); update(); }); let seq=0; const REVERTS={ "1ab7da6b":"That took too long and the deadline passed, so nothing was placed. Try again — you were not charged for an order.", "98c2c368":"The order was refused as malformed. Check the amounts and the expiry.", "7939f424":"The token transfer was refused — usually a missing or spent approval. Try again and approve first.", "90b8ec18":"The token transfer was refused by the token itself.", "3e3f8f73":"The approval was refused by the token.", "6fc70753":"That score name is not a valid label.", "9c9cef39":"That score is too long to record.", "7b2002ce":"Somebody already claimed that name — play again for a new one.", "845eadf1":"The score minter is not holding arcade.wei.", "b12d13eb":"Ether could not be sent to the recipient — a contract there may not accept it.", "ab143c06":"That call re-entered and was stopped.", }; const explain=e=>{ if(e?.noWallet)return NO_WALLET; const str=v=>typeof v==="string"?v :v&&typeof v.data==="string"?v.data :v&&typeof v.message==="string"?v.message:""; const s=str(e?.data)||str(e)||str(e?.error)||""; const m=/0x([0-9a-fA-F]{8})\b/.exec(s); const hit=m&&REVERTS[m[1].toLowerCase()]; if(hit)return hit; if(!s)return "The wallet returned an error with no message"+(e&&e.code!==undefined?" (code "+e.code+")":"")+"."; return "Error: "+s.slice(0,110); }; const err=e=>stat.textContent=isRejection(e)?"":explain(e); const settle=async(tx,quiet)=>{ if(!tx){stat.textContent="Done";return} if(!/^0x[0-9a-f]{64}$/i.test(tx))throw Error("bad tx"); stat.innerHTML=txLink(tx,"Sent"); try{await waitTx(tx)}catch(e){sFail();throw e} stat.innerHTML=txLink(tx,"Done");if(!quiet)sGot()}; const txLink=(h,label)=>`${label} <a href="${chainCfg().explorer}/tx/${h}" target="_blank" rel="noreferrer">${h.slice(0,10)}…${h.slice(-6)}</a>`; let autoQ=0,rankOpen=false; async function update(auto){ if(!auto)autoQ=0; syncIcons(); fcSync(); cbSync(); const my=++seq; quoting=false; if(wnMode||lnMode||pvMode)return; if(lqMode)return loadLq(); if(tab==="book")return render(); if(tab==="send")return updateSend(); last=null; rate.textContent=""; if(!lqMode&&!lnMode)rcvHdr.textContent="You receive"; render(); const f=TOKENS[fromSel.value], t=TOKENS[toSel.value]; if(!f||!t)return; if(f.addr===t.addr){stat.textContent="Pick different tokens.";return} stat.textContent=""; const isIn=mode==="in"; const inEl=isIn?amt:outAmt, outEl=isIn?outAmt:amt, inTok=isIn?f:t; outEl.value=""; fitFont(outEl); if(!inEl.value.trim())return; let swapAmt; try{swapAmt=parseUnits(inEl.value.trim(),inTok.dec)} catch(e){const q=inEl.value.trim(); if(q&&!/\.$/.test(q))stat.textContent=/^unclear comma/.test(e.message)?e.message:/decimals/.test(e.message)?`${inTok.sym} has ${inTok.dec} decimals`:"Invalid amount"; return} if(swapAmt<=0n)return; const rv=rc.value.trim(); let rcv=""; if(rv){ const fail=m=>{rc.classList.add("bad");rcvEl.textContent=""; outEl.value="";fitFont(outEl);rankBox.textContent="";rankBox.classList.add("hide");stat.textContent=m}; const bad=rcErr(rv); if(bad){fail(bad);return} if(isAddr(rv))rcv=rv; else{ rcvEl.textContent="resolving..."; try{rcv=await rcvOf(rv)} catch(e){if(my!==seq)return;fail(e&&e.offchain?e.message:"Name not registered");return} if(my!==seq)return; if(!rcv){fail("Name not registered");return} } rcvEl.textContent=rcv; }else rcvEl.textContent=""; rc.classList.remove("bad"); if(!rv&&f.addr===WETH&&t.addr===ZERO){ outEl.value=inEl.value;fitFont(outEl); rate.textContent=`1 ${f.sym} = 1 ${t.sym}`; last={callData:"0x2e1a7d4d"+encUint(swapAmt),msgValue:0n,isIn:true,amountIn:swapAmt,from:f,to:WETH,recipient:"",exp:Date.now()+QUOTE_TTL}; render();return; } if(!rv&&f.addr===ZERO&&t.addr===WETH){ outEl.value=inEl.value;fitFont(outEl); rate.textContent=`1 ${f.sym} = 1 ${t.sym}`; last={callData:"0xd0e30db0",msgValue:swapAmt,isIn:true,amountIn:swapAmt,from:f,to:WETH,recipient:"",exp:Date.now()+QUOTE_TTL}; render();return; } const pct=Math.min(10,Math.max(.01,+slip.value||.5)); const slipBps=BigInt(Math.round(pct*100)); const deadline=BigInt(Math.floor(Date.now()/1e3)+1800); outEl.value="...";fitFont(outEl);quoting=true;render(); const acct=account; const gaveUp0=mcGaveUp,ok0=mcOk,failed0=mcFailed; try{ const a=encAddr(rcv||acct||ZERO),rf=encAddr(acct||ZERO),tl=encBool(!isIn)+encAddr(f.addr)+encAddr(t.addr)+encUint(swapAmt)+encUint(slipBps)+encUint(deadline); const _rq=[]; const Q=(x,d,u,v,S,sb,mv)=>{_rq.push({to:ZQUOTER,data:"0x"+x+d,u,v,S,sb,mv});return _rq.length-1}; let r=null; const candKey=f.addr.toLowerCase()+":"+t.addr.toLowerCase(); const cachedCandidates=candCache[candKey]; const quoteBlock=cachedCandidates&&Date.now()-cachedCandidates.at<15000 ?cachedCandidates.block:await blockNow(); const candJob=acct?swapCandidates(f.addr,t.addr,quoteBlock).catch(()=>{ const x=[];x.unavailable=true;return x }):Promise.resolve([]); const gasPriceJob=acct?rpc("eth_gasPrice",[]).then(BigInt).catch(()=>0n):Promise.resolve(0n); let venueLost=false; const v4Job=isIn?quoteV4Pools(f,t,swapAmt,quoteBlock).catch(()=>{venueLost=true;return null}):Promise.resolve(null); const preJob=isIn?quotePrecision(f,t,swapAmt,PROUTE,quoteBlock).catch(()=>{venueLost=true;return null}):Promise.resolve(null); let curveOnlyOut=false; const exactOutSafe=y=>{if(isIn||!y.sources||!y.sources.includes(SRC_CURVE))return true;curveOnlyOut=true;return false}; const pick=y=>{if(!y)return;if(!exactOutSafe(y))return;const v=isIn?y.best.amountOut:y.best.amountIn;if(!v)return; if(!r||(isIn?v>r.best.amountOut:v<r.best.amountIn))r=y}; const tl0=encBool(!isIn)+encAddr(f.addr)+encAddr(t.addr)+encUint(swapAmt)+encUint(0n)+encUint(deadline); const jobs=[ [Q("e7798987",a+tl,0,4,0,0,6)], [Q("e453166e",a+rf+tl0,4,9),Q("e453166e",a+rf+tl,4,9)], [Q("4c464f59",a+tl0,8,13),Q("4c464f59",a+tl,8,13)]]; let splitSlip=0n; if(isIn){ const w=slipBps*3n,SS=w<150n?150n:w>500n?500n:w; splitSlip=SS; const ps=encAddr(f.addr)+encAddr(t.addr)+encUint(swapAmt)+encUint(SS)+encUint(deadline); for(const x of["892af013","85f86a90"])jobs.push([Q(x,a+ps,4,8,1,SS)]); } const tailStep=swapAmt/100n||1n,tailAmt=swapAmt>tailStep?swapAmt-tailStep:0n; const tailJobs=[]; if(tailAmt){ const z=encBool(!isIn)+encAddr(f.addr)+encAddr(t.addr)+encUint(tailAmt)+encUint(0n)+encUint(deadline); tailJobs.push(Q("e453166e",a+rf+z,4,9),Q("4c464f59",a+z,8,13)); if(isIn)for(const x of["892af013","85f86a90"]){ const ps=encAddr(f.addr)+encAddr(t.addr)+encUint(tailAmt)+encUint(splitSlip)+encUint(deadline); tailJobs.push(Q(x,a+ps,4,8,1,splitSlip)); } } const refAmt=swapAmt/REF_N; const iRef=refAmt>0n ?Q("e453166e",a+rf+encBool(!isIn)+encAddr(f.addr)+encAddr(t.addr)+encUint(refAmt)+encUint(0n)+encUint(deadline),4,9) :-1; const mcRaw=(await Promise.all(mc3Split(_rq,QPAIR) .map(g=>mc3Deep(g.map(q=>({to:q.to,data:q.data})),quoteBlock)))).flat(); if(my!==seq)return; const dec=_rq.map((q,i)=>{try{return mcRaw[i]?decQ(mcRaw[i],q.sb||slipBps,!isIn,q.u,q.v,q.S,q.mv):null}catch{return null}}); for(const g of jobs)pick(g.length>1?merge(dec[g[0]],dec[g[1]]):dec[g[0]]); const zr=r; if(isIn&&!(dec[jobs[1][0]]||dec[jobs[1][1]]||dec[jobs[2][0]]||dec[jobs[2][1]])){ const hq=await hubRoute(f,t,swapAmt,slipBps,deadline,rcv||acct||ZERO,acct||ZERO,quoteBlock).catch(()=>null); if(my!==seq)return; if(hq)pick(hq); } let tail=null; for(const i of tailJobs){const y=dec[i];if(!y)continue; const v=isIn?y.best.amountOut:y.best.amountIn; if(v&&(!tail||(isIn?v>tail.best.amountOut:v<tail.best.amountIn)))tail=y; } if(my!==seq)return; const v4q=await v4Job; let v4c=null; if(v4q&&v4q.amountOut>0n){ const v4Min=v4q.amountOut-(v4q.amountOut*slipBps)/10000n; const p=v4q.pool; v4c={best:{source:4,amountIn:v4q.amountIn,amountOut:v4q.amountOut,feeBps:p.fee>=8388608n?0n:BigInt(p.fee)/100n}, callData:"0x"+SEL_V4SWAP+encPoolKey(p)+encBool(p.zeroForOne)+encUint(v4q.amountIn) +encUint(v4Min)+encAddr(rcv||acct||ZERO)+encUint(deadline), fundedCallData:null,amountLimit:v4Min,msgValue:f.addr===ZERO?swapAmt:0n, to:V4PORT,spender:f.addr===ZERO?null:V4PORT}; if(!r)r=v4c; } const pq=await preJob; if(my!==seq)return; let pc=null; if(pq&&pq.out>0n){ const pMin=pq.out-(pq.out*slipBps)/10000n; const pNative=pq.tokenIn===ZERO; const pWethIn=pNative&&(pq.unwrap||0n)>0n; const pTo=rcv||acct||ZERO; const pFund=pWethIn?WETH:pq.tokenIn; const routeData=pWethIn ?encPRouteWeth(pq.pools,pq.tokenOut,swapAmt,pMin,pTo) :encPRoute(pq.pools,pq.tokenIn,pq.tokenOut,swapAmt,pMin,pTo); const routedLeg=encSnwap(pFund,swapAmt,pTo,pq.tokenOut,pMin,PROUTE,routeData); const checkpointLeg=encSnwap(ZERO,0n,acct||ZERO,ZERO,0n,PROUTE, "0x"+SEL_PCHECKPOINT+encAddr(pFund)+strip0x(keccak(hexToBytes(routeData)))+encAddr(acct||ZERO)); const pCall=(pNative&&!pWethIn)?routedLeg:encCalls([checkpointLeg,routedLeg]); const pFunded=(pNative&&!pWethIn)?null:encCalls([ checkpointLeg, encSweep(pFund,swapAmt,PROUTE), encSnwap(ZERO,0n,pTo,pq.tokenOut,pMin,PROUTE,routeData), ]); pc={best:{source:SRC_PRECISION,amountIn:swapAmt,amountOut:pq.out,feeBps:pq.fee/100n}, callData:pCall, fundedCallData:pFunded,amountLimit:pMin,msgValue:(pNative&&!pWethIn)?swapAmt:0n, wrap:pq.wrap||0n,outDiffers:!!pq.outDiffers,poolOut:pq.tokenOut, to:null,spender:(pNative&&!pWethIn)?null:ZROUTER,pools:pq.pools}; if(!r)r=pc; } if(!r&&curveOnlyOut)throw Object.assign(Error(`exact-output is unavailable for this pair — enter an amount to PAY instead`),{exactOut:1}); if(!r)throw Error("bad quote"); if(r.best.amountIn<=0n||r.best.amountOut<=0n||(!isIn&&r.amountLimit<r.best.amountIn))throw Error("bad quote"); let dc=null; if(isIn&&CHAIN_ID===4663&&f.addr!==ZERO&&t.addr!==ZERO){ const dq=await deepQuote(f.addr,t.addr,swapAmt,r&&r.best?r.best.amountOut:0n); if(my!==seq)return; if(dq&&dq.out>0n){ const dMin=dq.out-(dq.out*slipBps)/10000n; const dTo=rcv||acct||ZERO; const leg="0x"+SEL_SWAPDEEP+encAddr(dTo)+encAddr(dq.t0)+encAddr(dq.t1)+encUint(dq.epoch) +strip0x(dq.order)+encUint(dq.isBid?1:0)+encUint(swapAmt)+encUint(dMin)+encUint(deadline); dc={best:{source:SRC_DEEP,amountIn:dq.inUsed,amountOut:dq.out,feeBps:0n}, callData:encCalls([leg,encSweep(f.addr,0n,dTo)]), fundedCallData:null,amountLimit:dMin,msgValue:0n, wrap:0n,outDiffers:false,poolOut:t.addr,to:null,spender:ZROUTER}; } } for(const c of [v4c,pc,dc]){ if(!c||c===r)continue; if(isIn?c.best.amountOut>r.best.amountOut:c.best.amountIn<r.best.amountIn)r=c; } let hybrid=null,bookIncomplete=false,bookUnavailable=false,plannerBounded=false,rank=null; if(acct){ const candidates=await candJob; bookIncomplete=!!candidates.incomplete; bookUnavailable=!!candidates.unavailable; if(my!==seq)return; const quoteRem=async(x,eo)=>{ const qs=[],groups=[],add=(sel,data,u,v,S,sb)=>{ qs.push({data:"0x"+sel+data,u,v,S,sb});return qs.length-1}; const z0=encBool(eo)+encAddr(f.addr)+encAddr(t.addr)+encUint(x)+encUint(0n)+encUint(deadline); const zs=encBool(eo)+encAddr(f.addr)+encAddr(t.addr)+encUint(x)+encUint(slipBps)+encUint(deadline); groups.push([add("e453166e",a+rf+z0,4,9),add("e453166e",a+rf+zs,4,9)]); groups.push([add("4c464f59",a+z0,8,13),add("4c464f59",a+zs,8,13)]); if(!eo){ const w=slipBps*3n,ss=w<150n?150n:w>500n?500n:w; const ps=encAddr(f.addr)+encAddr(t.addr)+encUint(x)+encUint(ss)+encUint(deadline); for(const sel of["892af013","85f86a90"])groups.push([add(sel,a+ps,4,8,1,ss)]); } const raw=await mc3Deep(qs.map(q=>({to:ZQUOTER,data:q.data})),quoteBlock); const ds=qs.map((q,i)=>{try{return raw[i]?decQ(raw[i],q.sb||slipBps,eo,q.u,q.v,q.S):null}catch{return null}}); let best=null; for(const g of groups){ const y=g.length>1?merge(ds[g[0]],ds[g[1]]):ds[g[0]]; if(!y)continue; if(eo&&y.sources&&y.sources.includes(SRC_CURVE))continue; const v=eo?y.best.amountIn:y.best.amountOut; if(v&&(!best||(eo?v<best.best.amountIn:v>best.best.amountOut)))best=y; } return best; }; const gasValue=async wei=>{ const token=isIn?t.addr:f.addr; if(token===ZERO||token.toLowerCase()===WETH.toLowerCase())return wei; const eo=!isIn,ti=isIn?ZERO:token,to=isIn?token:ZERO; const z=encBool(eo)+encAddr(ti)+encAddr(to)+encUint(wei)+encUint(0n)+encUint(deadline); try{ const raw=await rpc(C,[{to:ZQUOTER,data:"0xe453166e"+a+rf+z},quoteBlock]); const q=decQ(raw,0n,eo,4,9); const v=isIn?q.best.amountOut:q.best.amountIn; if(v>0n)return v; }catch{} const ethSide=f.addr===ZERO||f.addr.toLowerCase()===WETH.toLowerCase(); const tokSide=t.addr===ZERO||t.addr.toLowerCase()===WETH.toLowerCase(); const inA=r.best.amountIn,outA=r.best.amountOut; if(inA>0n&&outA>0n){ if(isIn&&ethSide)return wei*outA/inA; if(!isIn&&tokSide)return wei*inA/outA; } return null; }; const tailDelta=swapAmt-tailAmt; const tailFits=!!tail&&r===zr; const seedOut=isIn&&tailFits&&r.best.amountOut>tail.best.amountOut ?r.best.amountOut-tail.best.amountOut:r.best.amountOut; const seedIn=!isIn&&tailFits&&r.best.amountIn>tail.best.amountIn ?r.best.amountIn-tail.best.amountIn:r.best.amountIn; let p=isIn ?planBookExactIn(candidates,swapAmt,seedOut,tailFits&&seedOut!==r.best.amountOut?tailDelta:r.best.amountIn) :planBookExactOut(candidates,swapAmt,seedIn,tailFits&&seedIn!==r.best.amountIn?tailDelta:r.best.amountOut); plannerBounded=!!(p&&p.bounded); let amm=null,ammFor=null; for(let round=0;p&&(isIn?p.ammIn:p.ammOut)&&round<2;round++){ const rem=isIn?p.ammIn:p.ammOut; amm=await quoteRem(rem,!isIn).catch(()=>null);ammFor=rem; if(!amm)break; const next=isIn ?planBookExactIn(candidates,swapAmt,amm.best.amountOut,rem) :planBookExactOut(candidates,swapAmt,amm.best.amountIn,rem); if(!next){p=null;break} const stable=(isIn?next.ammIn===p.ammIn:next.ammOut===p.ammOut); p=next;plannerBounded=plannerBounded||!!p.bounded;if(stable)break; } if(p){ const rem=isIn?p.ammIn:p.ammOut; if(!rem)amm=null; else if(!(amm&&ammFor===rem))amm=await quoteRem(rem,!isIn).catch(()=>null); if(!rem||amm){ const recipient=rcv||acct; const ammBudget=amm?(isIn?rem:amm.amountLimit):0n; const totalBudget=p.bookIn+ammBudget; const expectedIn=p.bookIn+(amm?amm.best.amountIn:0n); const expectedOut=p.bookOut+(amm?amm.best.amountOut:0n); const saving=isIn ?(expectedOut>r.best.amountOut?expectedOut-r.best.amountOut:0n) :(expectedIn<r.best.amountIn?r.best.amountIn-expectedIn:0n); let gasFloor=null; if(saving){ const gasPrice=await gasPriceJob; if(gasPrice){ const extraWei=gasPrice*(120000n+105000n*BigInt(p.fills.length)); gasFloor=await gasValue(extraWei); } } const pricedGas=gasFloor!==null&&gasFloor>0n; const wins=saving>0n&&pricedGas&&saving*10n>gasFloor*12n; if(my!==seq)return; if(wins){ const fp=encFillPlanAndSwap( p.fills,f.addr,t.addr,recipient,acct,deadline,ammBudget,amm?amm.callData:"0x" ); const minOut=isIn?p.bookOut+(amm?amm.amountLimit:0n):swapAmt; const snwapIn=totalBudget===0n?ZERO:f.addr; const planCall=encSnwap(snwapIn,totalBudget,recipient,t.addr,minOut,SWAPBOL,fp); const callData=f.addr===ZERO?planCall:encCalls([ encCheckpoint(SWAPBOL,f.addr,acct),planCall ]); let fundedCallData=null; if(f.addr!==ZERO){ fundedCallData=encPermit2Hybrid( amm?amm.callData:"0x",p.fills,f.addr,t.addr,recipient,acct,deadline,p.bookIn,p.bookOut ); } r={...r,best:{...r.best,amountIn:expectedIn,amountOut:expectedOut},callData, fills:p.fills,pools:undefined, fundedCallData,amountLimit:isIn?minOut:totalBudget,msgValue:f.addr===ZERO?totalBudget:0n, slipBps:amm&&amm.slipBps, to:null,spender:f.addr===ZERO?null:ZROUTER, pool:null,wrap:0n,outDiffers:false,poolOut:undefined}; hybrid=isIn ?{bookIn:p.bookIn,bookOut:p.bookOut,ammIn:p.ammIn} :{bookIn:p.bookIn,bookOut:p.bookOut,ammOut:p.ammOut}; plannerBounded=plannerBounded||!!p.bounded; } } } } const pin=lanePin(); if(!isIn&&r&&r.amountLimit>0n){ try{ await loadSolvers(); const lanes=(solverLanes||[]).filter(x=>!laneDown(x.name)&&SOLVER_SHAPES_OUT[x.name]); if(lanes.length&&solverFill&&solverExec){ const dl=new Promise(res=>setTimeout(()=>res(null),SOLVER_DEADLINE)); const ck=`o|${f.addr}|${t.addr}|${swapAmt}|${slipBps}`; const tried=await Promise.all(lanes.map(x=>Promise.race([ laneCached(`${x.name}|${ck}`, ()=>SOLVER_SHAPES_OUT[x.name](x.endpoint,f.addr,t.addr,swapAmt,Number(slipBps),SOLVER_DEADLINE,f.dec,t.dec)) .then(q=>q?{...q,lane:x}:null).catch(()=>null),dl]))); const laneCeil=c=>{const d=c.inp+(c.inp*slipBps)/10000n; return c.cap&&c.cap>0n?bmin(c.cap,d):d}; const chainCeil=r.amountLimit; let win=null; for(const c of tried){ if(!laneOk(c)||!c.inp)continue; const ceil=laneCeil(c); if(pin){ if(c.lane.name!==pin)continue; win={...c,ceil,forced:1};break; } if(ceil*(10000n+c.lane.handicapBps)>=chainCeil*10000n)continue; if(!win||ceil<win.ceil)win={...c,ceil}; } rank=[{n:"On-chain",f:chainCeil}]; for(const c of tried){ if(!laneOk(c)||!c.inp)continue; rank.push({n:c.lane.name,f:laneCeil(c)}); } rank.sort((a,b)=>a.f>b.f?1:a.f<b.f?-1:0); if(my!==seq)return; if(win){ const ethIn=f.addr===ZERO; r={best:{source:SRC_SOLVER,amountIn:win.inp,amountOut:swapAmt,feeBps:0n, lane:win.lane.name,forced:win.forced}, callData:"0x"+SEL_FILL+encAddr(win.target)+encAddr(win.spender)+encAddr(f.addr) +encUint(win.ceil)+encAddr(t.addr)+encUint(swapAmt)+encAddr(rcv||acct||ZERO) +encUint(256n)+encUint(BigInt(strip0x(win.data).length/2)) +strip0x(win.data)+"0".repeat((64-(strip0x(win.data).length%64))%64), fundedCallData:null,amountLimit:win.ceil, msgValue:ethIn?win.ceil:0n, to:solverFill,spender:ethIn?null:solverFill}; hybrid=null; } } }catch{} } const pin2=lanePin(); if(isIn&&r&&r.amountLimit>0n){ try{ await loadSolvers(); const lanes=(solverLanes||[]).filter(x=>!laneDown(x.name)); if(lanes.length&&solverFill&&solverExec){ const deadline_=new Promise(res=>setTimeout(()=>res(null),SOLVER_DEADLINE)); const ck=`${f.addr}|${t.addr}|${swapAmt}|${slipBps}`; const tried=await Promise.all(lanes.map(x=>Promise.race([ laneCached(`${x.name}|${ck}`, ()=>SOLVER_SHAPES[x.name](x.endpoint,f.addr,t.addr,swapAmt,Number(slipBps),SOLVER_DEADLINE,f.dec,t.dec)) .then(q=>q?{...q,lane:x}:null).catch(()=>null), deadline_]))); const chainFloor=r.amountLimit; let win=null; for(const c of tried){ if(!laneOk(c)||!c.out)continue; const floor=c.out-(c.out*slipBps)/10000n; if(pin2){ if(c.lane.name!==pin2)continue; win={...c,floor,forced:1}; break; } if(floor*10000n<=chainFloor*(10000n+c.lane.handicapBps))continue; if(!win||floor>win.floor)win={...c,floor}; } rank=[{n:"On-chain",f:chainFloor}]; for(const c of tried){ if(!laneOk(c)||!c.out)continue; rank.push({n:c.lane.name,f:c.out-(c.out*slipBps)/10000n}); } rank.sort((a,b)=>a.f<b.f?1:a.f>b.f?-1:0); if(my!==seq)return; if(win){ const ethIn=f.addr===ZERO; r={best:{source:SRC_SOLVER,amountIn:swapAmt,amountOut:win.out,feeBps:0n,lane:win.lane.name,forced:win.forced}, callData:"0x"+SEL_FILL+encAddr(win.target)+encAddr(win.spender)+encAddr(f.addr) +encUint(swapAmt)+encAddr(t.addr)+encUint(win.floor)+encAddr(rcv||acct||ZERO) +encUint(256n)+encUint(BigInt(strip0x(win.data).length/2)) +strip0x(win.data)+"0".repeat((64-(strip0x(win.data).length%64))%64), fundedCallData:null,amountLimit:win.floor, msgValue:ethIn?swapAmt:0n, to:solverFill,spender:ethIn?null:solverFill}; hybrid=null; } } }catch{} } if(my!==seq)return; const finalIn=r.best.amountIn; const finalOut=r.best.amountOut; if(isIn){outAmt.value=trimAmt(finalOut,t.dec);fitFont(outAmt)} else{amt.value=trimAmt(finalIn,f.dec);fitFont(amt)} const rx=(+formatUnits(finalOut,t.dec))/(+formatUnits(finalIn,f.dec)); const s=r.best.source,raw=SOURCES[s]||"?"; const src=r.best.source===SRC_SOLVER?`${r.best.lane} solver${r.best.forced?" \u00b7 pinned":""}`:hybrid?bookShare(hybrid,raw):(s===3||s===4||s===SRC_PRECISION)&&r.best.feeBps?`${raw}\u00a0${Number(r.best.feeBps)/100}%`:raw; rate.textContent=isFinite(rx)?`1 ${f.sym} ≈ ${rx>=1?rx.toLocaleString("en-US",{maximumFractionDigits:4}):rx.toPrecision(4)} ${t.sym} · ${src}`:""; rankBox.textContent=""; if(rank&&rank.length>1){ const top=rank[0].f; const worse=isIn?"-":"+"; const used=r.best.source===SRC_SOLVER?r.best.lane:"On-chain"; for(const x of rank){ const row=document.createElement("i"); if(x.n===used)row.className="w"; const pinNow=lanePin(); if(x.n===pinNow)row.className+=" pin"; row.title=x.n===pinNow?"Pinned \u2014 click again for the best floor" :x.f===top&&x.n!==used?"Best floor \u2014 click to use it":"Use this venue"; const pick=()=>{const on=lanePin()===x.n;laneOverride=on?null:x.n;laneOverrideFor=on?"":laneScope();update()}; row.onclick=pick; row.setAttribute("role","button"); row.tabIndex=0; row.onkeydown=e=>{if(e.key==="Enter"||e.key===" "){e.preventDefault();pick()}}; const n=document.createElement("span"); n.textContent=safeSym(x.n).slice(0,16); const v=document.createElement("u"); const gap=top>0n?Number((x.f>top?x.f-top:top-x.f)*10000n/top)/100:0; v.textContent=`${sigAmt(x.f,isIn?t.dec:f.dec)}${gap>0.004?` \u00b7 ${worse}${gap.toFixed(2)}%`:""}`; row.append(n,v); rankBox.appendChild(row); } } rankBox.classList.toggle("hide",!(rankOpen&&rankBox.firstChild)); const paysSym=r.poolOut===ZERO?"ETH":"WETH"; rcvHdr.textContent=r.outDiffers?`You receive \u2014 as ${paysSym}`:"You receive"; if(r.outDiffers&&rate.textContent)rate.textContent+=` \u00b7 pays ${paysSym}`; rate.title=""; if(venueLost||mcGaveUp>gaveUp0||(mcFailed>failed0&&mcOk===ok0)) rate.textContent+=" \u00b7 some venues unreachable"; if(isIn)rate.textContent+=` · Min ${trimAmt(r.amountLimit,t.dec)} ${t.sym}`; else{ const exactMax=formatUnits(r.amountLimit,f.dec); rate.textContent+=` · Max ${maxAmt(r.amountLimit,f.dec)} ${f.sym}`; rate.title=`Exact maximum: ${exactMax} ${f.sym}`; } if(r.s