forked from subnet112/minotaur-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathking_solver.py
More file actions
547 lines (507 loc) · 30.9 KB
/
Copy pathking_solver.py
File metadata and controls
547 lines (507 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
"""apex-split-router — thin subclass of the CURRENT champion (king_base.py).
Design: king_base.py is the reigning champion's solver.py copied verbatim. THIS
file subclasses its MinerSolver and adds ONE thing — never-drop blind-spot cover
for tokens the champion's engine + hardcode genuinely cannot route (champ delivers
0). For every other order we defer entirely to the champion, so we match it
byte-for-byte (0 regressions). A covered token delivers where the champion delivers
nothing = a clean "new" win; below-min delivery just skips (== champ's 0), so it
can never regress.
Re-fork onto a new champion = copy its solver.py to king_base.py. This file is
fixed (no re-editing the champion's evolving code) — that's the whole point.
"""
from __future__ import annotations
import logging
import os
import time
from king_base import MinerSolver as _Base
from minotaur_subnet.sdk.intent_solver import SolverMetadata
from minotaur_subnet.shared.types import ExecutionPlan, Interaction
logger = logging.getLogger(__name__)
SOLVER_NAME = os.environ.get("MINOTAUR_SOLVER_NAME", "viking-mino-solver")
SOLVER_VERSION = os.environ.get("MINOTAUR_SOLVER_VERSION", "96.0.0")
SOLVER_AUTHOR = os.environ.get("MINOTAUR_SOLVER_AUTHOR", "martindev0207")
_BASE = 8453
_WETH = "0x4200000000000000000000000000000000000006"
_MAVERICK_ROUTER = "0x5eDEd0d7E76C563FF081Ca01D9d12D6B404Df527" # MaverickV2Router
_UNIV2_ROUTER = "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24" # Uniswap V2 Router02
_VIRTUAL = "0x0b3e328455c4059eeb9e3f84b5543f74e24e7e1b" # VIRTUAL hub
# ── FRONTIER EDGE: venues the champion (king) does NOT generically quote ───────
# From champion analysis: pancake-edge wins fresh tokens via Sushi V3 / SushiV2 /
# AlienBase V2 — venues king lacks. We quote them live and override the champion
# ONLY when the extra venue's quote strictly beats king's reachable best by a safe
# margin AND clears min_out (quote-gated => never regresses on the quote side).
_FRONTIER_ON = os.environ.get("APEX_FRONTIER", "1") == "1" # kill switch (ON: validated reachable==0 => win-or-skip)
_FRONTIER_MARGIN = 1.02 # +2% over reachable (covers king split/2hop edge)
_SUSHI_V3_QUOTER = "0xb1E835Dc2785b52265711e17fCCb0fd018226a6e" # Sushi V3 QuoterV2
_SUSHI_V3_ROUTER = "0xFB7eF66a7e61224DD6FcD0D7d9C3be5C8B049b9f" # Sushi V3 SwapRouter (deadline 0x414bf389)
_SUSHI_V2_ROUTER = "0x6BDED42c6DA8FBf0d2bA55B2fa120C5e0c8D7891" # SushiSwap V2 router
_ALIEN_V2_ROUTER = "0x8c1A3cF8f83074169FE5D7aD50B978e1cD6b37c7" # AlienBase V2 router
# King's OTHER generic venues — quoted as 'reachable' so we never fire on a token
# king can already route (fire only when EVERY reachable venue returns 0).
_PANCAKE_V2_ROUTER = "0x8cFe327CEc66d1C090Dd72bd0FF11d690C33a2Eb"
_AERO_V2_ROUTER = "0xcF77a3Ba9A5CA399B7c97c74d54e5b1Beb874E43"
_AERO_V2_FACTORY = "0x420DD381b31aEf6683db6B902084cB0FFECe40Da"
# QuickSwap V4 (Algebra Integral) — where corpus blind-spots like RYFT live and where
# ms-lab dethroned king. King hardcodes some (skipped by _apex_champ_hardcodes); we win
# the FRESH ones via pool-existence (no quoter needed; safe under reachable==0).
_QS_ALGEBRA_ROUTER = "0xe6c9bb24ddB4aE5c6632dbE0DE14e3E474c6Cb04" # SwapRouter (sel 0x1679c792, deployer=0)
_QS_ALGEBRA_FACTORY = "0xc5396866754799b9720125b104ae01d935ab9c7b" # poolByPair(a,b)
_ZERO_ADDR = "0x0000000000000000000000000000000000000000"
# Deep majors king dominates — skip the sweep (cost + never a win there).
_FRONTIER_MAJORS = {
"0x4200000000000000000000000000000000000006", # WETH
"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913", # USDC
"0xd9aaec86b65d86f6a7b5b1b0c42ffa531710b6ca", # USDbC
"0x50c5725949a6f0c72e6c4a641f24049a917db0cb", # DAI
"0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf", # cbBTC
"0x2ae3f1ec7f1f5012cfeab0185bfc7aa3cf0dec22", # cbETH
"0x940181a94a35a4569e4529a3cdfb74e38fd98631", # AERO
"0x0b3e328455c4059eeb9e3f84b5543f74e24e7e1b", # VIRTUAL
}
# Tokens the current champion delivers 0 on (venues its enum + hardcode can't reach:
# Maverick-general, Uni V2, the VIRTUAL hub). ONLY put a token here if king_base
# genuinely can't route it — verify against king_base._HOLE_ROUTES + its enum before
# adding, else we'd pre-empt the champion's better route and regress.
# ("uni_mav", (maverick_pool, tokenAIn)) = Uni V3 tin->WETH + Maverick WETH->token
# ("uni_v2_via", (mid_hub, v2_router)) = Uni V3 tin->mid + V2 router mid->token
# ("v2", v2_router) = V2 router tin->WETH->token (FoT)
_APEX_HOLE_ROUTES = {
# GPUS — only on Maverick V2 (king's enum has no general Maverick).
"0x8189910840771050bf9ed268abfc9c0882137029":
("uni_mav", ("0x77aa9de2695c28ddd5831c33bf7021e9aa2db23f", True)),
# WAGMI — only via the VIRTUAL hub then Uni V2 (champion lacks Uni V2).
# (MANEKI removed — the champion has since absorbed it -> now matched, not a win.)
"0x2ce1340f1d402ae75afeb55003d7491645db1857":
("uni_v2_via", (_VIRTUAL, _UNIV2_ROUTER)),
# (0dca08cf uni_mav BENCHED: /score 0.0 twice with IDENTICAL gas 311859 =
# deterministic revert in _uni_mav_plan for this pool (not RPC noise).
# tokenAIn=False per tokenA()=token; suspect flag semantics or tickLimit.
# Debug offline before shipping — never ship unverified.)
}
def _load_dynamic_holes():
"""Holes the bot's detector confirmed this round (structural, champion can't route,
Uni V3-routable) — baked in via a committed apex_holes.json so the benchmark sees
them. Format: {"0xtoken": {"kind": "uni_v3"}}. Only kinds we can build are honored.
"""
import json as _json
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "apex_holes.json")
try:
data = _json.load(open(path)) or {}
except Exception:
return {}
out = {}
for tok, spec in data.items():
try:
kind = (spec or {}).get("kind", "uni_v3")
if kind == "uni_v3":
out[str(tok).lower()] = ("uni_v3", None)
except Exception:
continue
return out
_APEX_HOLE_ROUTES.update(_load_dynamic_holes())
class MinerSolver(_Base):
"""Champion base + never-drop blind-spot cover (apex-split-router)."""
def metadata(self): # type: ignore[override]
base = super().metadata()
return SolverMetadata(
name=SOLVER_NAME, version=SOLVER_VERSION, author=SOLVER_AUTHOR,
description=("Current-champion base + never-drop blind-spot cover for "
"tokens it can't route (Maverick / Uni V2 / VIRTUAL hub)"),
supported_chains=base.supported_chains,
supported_intent_types=base.supported_intent_types)
def _generate_plan_impl(self, intent, state, snapshot=None): # type: ignore[override]
try:
p = self._normalized_swap_params(intent, state)
except Exception:
p = {}
# FRONTIER: venues king lacks (Sushi V3 / SushiV2 / AlienBase). Overrides the
# champion ONLY when an extra venue's live quote beats king's reachable best by
# +margin AND clears min_out — a quote-verified win, never a blind override.
try:
edge = self._apex_frontier_sweep(intent, state, snapshot, p)
if edge is not None:
return edge
except Exception:
logger.exception("[apex] frontier sweep failed")
# FILL-ONLY-EMPTY: run the champion; if it produced ANY plan, defer to it
# (never override -> cannot regress). Only when it's empty do we fill a hole.
champ = super()._generate_plan_impl(intent, state, snapshot)
if champ is not None and getattr(champ, "interactions", None):
return champ
try:
if str(p.get("output_token", "") or "").lower() in _APEX_HOLE_ROUTES:
plan = self._apex_hole_plan(intent, state, snapshot, p)
if plan is not None:
return plan
except Exception:
logger.exception("[apex] hole fill failed; using champion path")
return champ
def _apex_hole_plan(self, intent, state, snapshot, params):
try:
tin = str(params.get("input_token", "") or "")
tout = str(params.get("output_token", "") or "")
amount_in = int(params.get("input_amount", 0) or 0)
amount_in = self._effective_swap_amount(self._fee_params(state, params), tin, amount_in)
chain_id = int(state.chain_id or (snapshot.chain_id if snapshot else 0) or 0)
if chain_id != _BASE or amount_in <= 0 or not tin or not tout:
return None
kind, param = _APEX_HOLE_ROUTES[tout.lower()]
if kind == "uni_mav":
pool, token_a_in = param
return self._apex_uni_mav(intent, state, snapshot, pool, bool(token_a_in),
tin, tout, amount_in, chain_id)
if kind == "uni_v3":
return self._apex_uni_v3(intent, state, snapshot, tin, tout, amount_in, chain_id)
if kind == "uni_v2_via":
mid, v2_router = param
return self._apex_uni_v2_via(intent, state, snapshot, mid, v2_router,
tin, tout, amount_in, chain_id)
if kind == "v2":
mid = _WETH
path = ([tin, tout] if mid in (tin.lower(), tout.lower()) else [tin, mid, tout])
return self._apex_v2(intent, state, snapshot, param, path, amount_in, chain_id)
except Exception:
logger.exception("[apex] hole plan build failed")
return None
# ── builders (named _apex_* to avoid clobbering champion methods) ──────────
def _apex_recipient(self, state, params):
return state.contract_address or params.get("receiver") or state.owner
def _apex_deadline(self, snapshot):
ts = getattr(snapshot, "timestamp", None) if snapshot else None
return int(ts or time.time()) + 300
def _apex_v2(self, intent, state, snapshot, router, path, amount_in, chain_id):
from common.abi_utils import encode_approve
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck
params = self._normalized_swap_params(intent, state)
recipient = self._apex_recipient(state, params); deadline = self._apex_deadline(snapshot)
call = "0x5c11d795" + _enc( # swapExactTokensForTokensSupportingFeeOnTransferTokens
["uint256", "uint256", "address[]", "address", "uint256"],
[int(amount_in), 0, [_ck(p) for p in path], _ck(recipient), int(deadline)]).hex()
ix = [Interaction(target=path[0], value="0", call_data=encode_approve(router, amount_in), chain_id=chain_id),
Interaction(target=router, value="0", call_data=call, chain_id=chain_id)]
return ExecutionPlan(intent_id=intent.app_id, interactions=ix, deadline=deadline,
nonce=state.nonce, metadata={"solver": "apex-hole-v2", "chain_id": chain_id})
def _apex_uni_v3(self, intent, state, snapshot, tin, tout, amount_in, chain_id):
# Plain single-hop Uni V3 exactInputSingle — the venue every structural hole
# used (VU, Cookie, ...). Champion delivers 0 (token not in its enum), so any
# positive delivery is a clean win and 0-quote just falls through (== its 0).
from common.abi_utils import encode_approve
from strategies.dex_aggregator.swap_solver import UNISWAP_V3_ROUTERS
from strategies.dex_aggregator.v3_codec import encode_exact_input_single
w3 = self._get_web3(int(chain_id))
uni_router = UNISWAP_V3_ROUTERS.get(int(chain_id))
if w3 is None or not uni_router:
return None
best_out, best_fee = 0, 3000
for fee in (3000, 500, 10000, 100):
try:
q = int(self._quote_one(w3, "uniswap_v3", fee, tin, tout, amount_in))
except Exception:
q = 0
if q > best_out:
best_out, best_fee = q, fee
if best_out <= 0:
return None
params = self._normalized_swap_params(intent, state)
recipient = self._apex_recipient(state, params); deadline = self._apex_deadline(snapshot)
call = encode_exact_input_single(token_in=tin, token_out=tout, fee=int(best_fee),
recipient=recipient, deadline=deadline, amount_in=amount_in, amount_out_minimum=0, chain_id=chain_id)
ix = [Interaction(target=tin, value="0", call_data=encode_approve(uni_router, amount_in), chain_id=chain_id),
Interaction(target=uni_router, value="0", call_data=call, chain_id=chain_id)]
return ExecutionPlan(intent_id=intent.app_id, interactions=ix, deadline=deadline,
nonce=state.nonce, metadata={"solver": "apex-hole-uni-v3", "chain_id": chain_id})
def _apex_uni_mav(self, intent, state, snapshot, pool, token_a_in, tin, tout, amount_in, chain_id):
from common.abi_utils import encode_approve
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck
from strategies.dex_aggregator.swap_solver import UNISWAP_V3_ROUTERS
from strategies.dex_aggregator.v3_codec import encode_exact_input_single
w3 = self._get_web3(int(chain_id))
uni_router = UNISWAP_V3_ROUTERS.get(int(chain_id))
if w3 is None or not uni_router:
return None
weth_out, best_fee = 0, 500
for fee in (500, 3000, 100, 10000):
try:
q = int(self._quote_one(w3, "uniswap_v3", fee, tin, _WETH, amount_in))
except Exception:
q = 0
if q > weth_out:
weth_out, best_fee = q, fee
if weth_out <= 0:
return None
mav_in = weth_out * 995 // 1000 # buffer vs quote/exec drift
params = self._normalized_swap_params(intent, state)
recipient = self._apex_recipient(state, params); deadline = self._apex_deadline(snapshot)
leg1 = encode_exact_input_single(token_in=tin, token_out=_WETH, fee=int(best_fee),
recipient=recipient, deadline=deadline, amount_in=amount_in, amount_out_minimum=0, chain_id=chain_id)
mav = "0x" + ("a3b105ca" + _enc(["address", "address", "bool", "uint256", "uint256"],
[_ck(recipient), _ck(pool), bool(token_a_in), int(mav_in), 0]).hex())
ix = [Interaction(target=tin, value="0", call_data=encode_approve(uni_router, amount_in), chain_id=chain_id),
Interaction(target=uni_router, value="0", call_data=leg1, chain_id=chain_id),
Interaction(target=_WETH, value="0", call_data=encode_approve(_MAVERICK_ROUTER, mav_in), chain_id=chain_id),
Interaction(target=_MAVERICK_ROUTER, value="0", call_data=mav, chain_id=chain_id)]
return ExecutionPlan(intent_id=intent.app_id, interactions=ix, deadline=deadline,
nonce=state.nonce, metadata={"solver": "apex-hole-uni-mav", "chain_id": chain_id})
def _apex_uni_v2_via(self, intent, state, snapshot, mid, v2_router, tin, tout, amount_in, chain_id):
from common.abi_utils import encode_approve
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck
from strategies.dex_aggregator.swap_solver import UNISWAP_V3_ROUTERS
from strategies.dex_aggregator.v3_codec import encode_exact_input_single
w3 = self._get_web3(int(chain_id))
uni_router = UNISWAP_V3_ROUTERS.get(int(chain_id))
if w3 is None or not uni_router:
return None
mid_out, best_fee = 0, 3000
for fee in (3000, 10000, 500, 100):
try:
q = int(self._quote_one(w3, "uniswap_v3", fee, tin, mid, amount_in))
except Exception:
q = 0
if q > mid_out:
mid_out, best_fee = q, fee
if mid_out <= 0:
return None
v2_in = mid_out * 995 // 1000
params = self._normalized_swap_params(intent, state)
recipient = self._apex_recipient(state, params); deadline = self._apex_deadline(snapshot)
leg1 = encode_exact_input_single(token_in=tin, token_out=mid, fee=int(best_fee),
recipient=recipient, deadline=deadline, amount_in=amount_in, amount_out_minimum=0, chain_id=chain_id)
leg2 = "0x5c11d795" + _enc(["uint256", "uint256", "address[]", "address", "uint256"],
[int(v2_in), 0, [_ck(mid), _ck(tout)], _ck(recipient), int(deadline)]).hex()
ix = [Interaction(target=tin, value="0", call_data=encode_approve(uni_router, amount_in), chain_id=chain_id),
Interaction(target=uni_router, value="0", call_data=leg1, chain_id=chain_id),
Interaction(target=mid, value="0", call_data=encode_approve(v2_router, v2_in), chain_id=chain_id),
Interaction(target=v2_router, value="0", call_data=leg2, chain_id=chain_id)]
return ExecutionPlan(intent_id=intent.app_id, interactions=ix, deadline=deadline,
nonce=state.nonce, metadata={"solver": "apex-hole-uni-v2-via", "chain_id": chain_id})
# ── FRONTIER EDGE: quote venues king lacks; override only when strictly better ──
def _apex_champ_hardcodes(self, tin, tout):
"""True if the champion base already special-cases this token/pair (its own
_HOLE_ROUTES / _STATIC_EXOTIC_ROUTES). We must NOT run the frontier there — the
champion may deliver via a venue our 'reachable' estimate misses, so overriding
risks a regression. Defer to the champion for anything it hardcodes."""
try:
import king_base as kb
except Exception:
return False
tinL, toutL = tin.lower(), tout.lower()
hole = getattr(kb, "_HOLE_ROUTES", None)
if isinstance(hole, dict) and toutL in {str(k).lower() for k in hole}:
return True
exotic = getattr(kb, "_STATIC_EXOTIC_ROUTES", None)
if isinstance(exotic, dict):
for k in exotic:
if isinstance(k, tuple) and len(k) == 2 and str(k[0]).lower() == tinL and str(k[1]).lower() == toutL:
return True
return False
def _q1(self, w3, venue, param, tin, tout, amount):
try:
return int(self._quote_one(w3, venue, param, tin, tout, amount))
except Exception:
return 0
def _fx_v3_quote(self, w3, quoter, tin, tout, fee, amount):
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck
try:
data = "0xc6a5026a" + _enc(["(address,address,uint256,uint24,uint160)"],
[(_ck(tin), _ck(tout), int(amount), int(fee), 0)]).hex()
r = bytes(w3.eth.call({"to": _ck(quoter), "data": data}))
return int.from_bytes(r[:32], "big") if len(r) >= 32 else 0
except Exception:
return 0
def _fx_v2_quote(self, w3, router, path, amount):
from eth_abi import encode as _enc, decode as _dec
from eth_utils import to_checksum_address as _ck
try:
data = "0xd06ca61f" + _enc(["uint256", "address[]"],
[int(amount), [_ck(p) for p in path]]).hex()
r = bytes(w3.eth.call({"to": _ck(router), "data": data}))
amounts = _dec(["uint256[]"], r)[0]
return int(amounts[-1]) if amounts else 0
except Exception:
return 0
def _fx_aerov2_quote(self, w3, tin, tout, amount):
from eth_abi import encode as _enc, decode as _dec
from eth_utils import to_checksum_address as _ck, keccak as _kk
sel = "0x" + _kk(text="getAmountsOut(uint256,(address,address,bool,address)[])")[:4].hex()
best = 0
for stable in (False, True):
try:
data = sel + _enc(["uint256", "(address,address,bool,address)[]"],
[int(amount), [(_ck(tin), _ck(tout), stable, _ck(_AERO_V2_FACTORY))]]).hex()
r = bytes(w3.eth.call({"to": _ck(_AERO_V2_ROUTER), "data": data}))
amounts = _dec(["uint256[]"], r)[0]
best = max(best, int(amounts[-1]) if amounts else 0)
except Exception:
continue
return best
def _fx_qs_pool(self, w3, a, b):
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck, keccak as _kk
try:
sel = "0x" + _kk(text="poolByPair(address,address)")[:4].hex()
r = bytes(w3.eth.call({"to": _ck(_QS_ALGEBRA_FACTORY),
"data": sel + _enc(["address", "address"], [_ck(a), _ck(b)]).hex()}))
addr = "0x" + r[-20:].hex()
return addr if len(r) >= 20 and int(addr, 16) != 0 else None
except Exception:
return None
def _apex_qs_candidate(self, w3, tin, tout, wi):
# QuickSwap V4 (Algebra) route by pool existence — no quoter; safe under reachable==0
# (any delivery is a win vs king's 0, a revert is a skip). Direct first, else via WETH.
if self._fx_qs_pool(w3, tin, tout):
return ("qs_direct", None)
if wi > 0 and tout.lower() != _WETH.lower() and self._fx_qs_pool(w3, _WETH, tout):
return ("qs_weth", None)
return None
def _apex_frontier_sweep(self, intent, state, snapshot, params):
"""Quote Sushi V3 / SushiV2 / AlienBase (venues king lacks) vs king's reachable
best; override king ONLY when an extra venue beats reachable*margin AND clears
min_out. Quote-gated => never regresses on the quote side. Bounded + concurrent."""
if not _FRONTIER_ON:
return None
from concurrent.futures import ThreadPoolExecutor
tin = str(params.get("input_token", "") or "")
tout = str(params.get("output_token", "") or "")
if not tin or not tout or tout.lower() in _FRONTIER_MAJORS or tin.lower() == tout.lower():
return None
if self._apex_champ_hardcodes(tin, tout):
return None # champion special-cases it -> defer (avoid mis-estimating its route)
# If the champion base ALREADY has the generic multi-venue sweep (pancake-edge-
# router: _sweep_plan/_sweep_quotes), it covers Sushi/AlienBase itself — and
# BETTER than us — so our 'reachable' (uni/pancake/aero) would underestimate its
# true delivery and we'd override + regress (the USDC->OGN lesson: it delivered
# 1289 via its own sushi, we'd have delivered 120). Only run the frontier when the
# champion genuinely LACKS these venues (king / top-miner / putty).
if any(hasattr(self, m) for m in ("_sweep_plan", "_sweep_quotes", "_sweep_sushi_plan")):
return None
chain_id = int(state.chain_id or (snapshot.chain_id if snapshot else 0) or 0)
amount_in = int(params.get("input_amount", 0) or 0)
amount_in = self._effective_swap_amount(self._fee_params(state, params), tin, amount_in)
min_out = int(params.get("min_output_amount", 0) or 0)
if chain_id != _BASE or amount_in <= 0:
return None
w3 = self._get_web3(chain_id)
if w3 is None:
return None
wethL = _WETH.lower()
via_weth = tin.lower() != wethL and tout.lower() != wethL
# phase 1: best tin->WETH (feeds both reachable-2hop and extra-2hop)
weth_fee, weth_out = 500, 0
if via_weth:
with ThreadPoolExecutor(max_workers=6) as ex:
fs = {ex.submit(self._q1, w3, "uniswap_v3", f, tin, _WETH, amount_in): f for f in (500, 3000, 100, 10000)}
for fut, f in fs.items():
o = fut.result()
if o > weth_out:
weth_out, weth_fee = o, f
wi = weth_out * 995 // 1000 if weth_out > 0 else 0
# phase 2: flat concurrent task list — tag R (reachable) / E (extra, with spec)
tasks = [] # (tag, spec, callable)
for f in (100, 500, 3000, 10000):
tasks.append(("R", None, lambda f=f: self._q1(w3, "uniswap_v3", f, tin, tout, amount_in)))
tasks.append(("R", None, lambda f=f: self._q1(w3, "pancake_v3", f, tin, tout, amount_in)))
tasks.append(("E", ("sushi_v3_direct", f), lambda f=f: self._fx_v3_quote(w3, _SUSHI_V3_QUOTER, tin, tout, f, amount_in)))
for t in (1, 50, 100, 200, 2000):
tasks.append(("R", None, lambda t=t: self._q1(w3, "aerodrome_slipstream", t, tin, tout, amount_in)))
for rtr in (_UNIV2_ROUTER, _PANCAKE_V2_ROUTER): # king's generic V2 venues (reachable)
tasks.append(("R", None, lambda rtr=rtr: self._fx_v2_quote(w3, rtr, [tin, tout], amount_in)))
tasks.append(("R", None, lambda: self._fx_aerov2_quote(w3, tin, tout, amount_in)))
for rtr in (_SUSHI_V2_ROUTER, _ALIEN_V2_ROUTER):
tasks.append(("E", ("v2fot_direct", rtr), lambda rtr=rtr: self._fx_v2_quote(w3, rtr, [tin, tout], amount_in)))
if wi > 0:
for f in (100, 500, 3000, 10000):
tasks.append(("R", None, lambda f=f: self._q1(w3, "uniswap_v3", f, _WETH, tout, wi)))
tasks.append(("E", ("sushi_v3_weth", f), lambda f=f: self._fx_v3_quote(w3, _SUSHI_V3_QUOTER, _WETH, tout, f, wi)))
for t in (1, 50, 100, 200):
tasks.append(("R", None, lambda t=t: self._q1(w3, "aerodrome_slipstream", t, _WETH, tout, wi)))
for rtr in (_UNIV2_ROUTER, _PANCAKE_V2_ROUTER): # king's generic V2 venues via WETH (reachable)
tasks.append(("R", None, lambda rtr=rtr: self._fx_v2_quote(w3, rtr, [_WETH, tout], wi)))
tasks.append(("R", None, lambda: self._fx_aerov2_quote(w3, _WETH, tout, wi)))
for rtr in (_SUSHI_V2_ROUTER, _ALIEN_V2_ROUTER):
tasks.append(("E", ("v2fot_weth", rtr), lambda rtr=rtr: self._fx_v2_quote(w3, rtr, [_WETH, tout], wi)))
reachable, extra = 0, (0, None)
with ThreadPoolExecutor(max_workers=16) as ex:
futs = [(tag, spec, ex.submit(fn)) for tag, spec, fn in tasks]
for tag, spec, fut in futs:
try:
out = int(fut.result(timeout=6))
except Exception:
out = 0
if tag == "R":
reachable = max(reachable, out)
elif out > extra[0]:
extra = (out, spec)
# Fire ONLY when every reachable (king) venue returns 0 => king delivers 0 => we are
# strictly win-or-skip (a revert == king's 0 == skip, never a regression).
if reachable > 0:
return None
out, spec = extra # best QUOTED extra (Sushi/AlienBase)
if out > 0 and spec is not None and (min_out <= 0 or out >= min_out):
return self._apex_build_frontier(intent, state, snapshot, params, tin, tout, amount_in, wi, chain_id, spec)
# Sushi didn't cover it -> QuickSwap V4 (Algebra) by pool existence (this is where
# RYFT-class corpus blind-spots live; win-or-skip so safe even without a quote).
qs = self._apex_qs_candidate(w3, tin, tout, wi)
if qs is not None:
return self._apex_build_frontier(intent, state, snapshot, params, tin, tout, amount_in, wi, chain_id, qs)
return None
def _apex_build_frontier(self, intent, state, snapshot, params, tin, tout, amount_in, wi, chain_id, spec):
from common.abi_utils import encode_approve
from eth_abi import encode as _enc
from eth_utils import to_checksum_address as _ck
from strategies.dex_aggregator.swap_solver import UNISWAP_V3_ROUTERS
from strategies.dex_aggregator.v3_codec import encode_exact_input_single
recipient = self._apex_recipient(state, params)
deadline = self._apex_deadline(snapshot)
kind, par = spec
def sushi_v3_leg(_in, _out, fee, amt):
call = "0x414bf389" + _enc( # Sushi V3 SwapRouter exactInputSingle (deadline-style)
["address", "address", "uint24", "address", "uint256", "uint256", "uint256", "uint160"],
[_ck(_in), _ck(_out), int(fee), _ck(recipient), int(deadline), int(amt), 0, 0]).hex()
return [Interaction(target=_in, value="0", call_data=encode_approve(_SUSHI_V3_ROUTER, amt), chain_id=chain_id),
Interaction(target=_SUSHI_V3_ROUTER, value="0", call_data=call, chain_id=chain_id)]
def v2fot_leg(router, path, amt):
call = "0x5c11d795" + _enc( # swapExactTokensForTokensSupportingFeeOnTransferTokens
["uint256", "uint256", "address[]", "address", "uint256"],
[int(amt), 0, [_ck(p) for p in path], _ck(recipient), int(deadline)]).hex()
return [Interaction(target=path[0], value="0", call_data=encode_approve(router, amt), chain_id=chain_id),
Interaction(target=router, value="0", call_data=call, chain_id=chain_id)]
def qs_leg(_in, _out, amt):
# QuickSwap V4 (Algebra) exactInputSingle — king's proven encoding (deployer=0, no fee)
call = "0x1679c792" + _enc(
["(address,address,address,address,uint256,uint256,uint256,uint160)"],
[(_ck(_in), _ck(_out), _ck(_ZERO_ADDR), _ck(recipient), int(deadline), int(amt), 0, 0)]).hex()
return [Interaction(target=_in, value="0", call_data=encode_approve(_QS_ALGEBRA_ROUTER, amt), chain_id=chain_id),
Interaction(target=_QS_ALGEBRA_ROUTER, value="0", call_data=call, chain_id=chain_id)]
def uni_weth_leg(amt):
uni = UNISWAP_V3_ROUTERS.get(chain_id)
best_fee, best = 500, 0
w3 = self._get_web3(chain_id)
for fee in (500, 3000, 100, 10000):
q = self._q1(w3, "uniswap_v3", fee, tin, _WETH, amt)
if q > best:
best, best_fee = q, fee
leg = encode_exact_input_single(token_in=tin, token_out=_WETH, fee=int(best_fee),
recipient=recipient, deadline=deadline, amount_in=amt, amount_out_minimum=0, chain_id=chain_id)
return [Interaction(target=tin, value="0", call_data=encode_approve(uni, amt), chain_id=chain_id),
Interaction(target=uni, value="0", call_data=leg, chain_id=chain_id)]
if kind == "sushi_v3_direct":
ix = sushi_v3_leg(tin, tout, par, amount_in)
elif kind == "v2fot_direct":
ix = v2fot_leg(par, [tin, tout], amount_in)
elif kind == "sushi_v3_weth":
ix = uni_weth_leg(amount_in) + sushi_v3_leg(_WETH, tout, par, wi)
elif kind == "v2fot_weth":
ix = uni_weth_leg(amount_in) + v2fot_leg(par, [_WETH, tout], wi)
elif kind == "qs_direct":
ix = qs_leg(tin, tout, amount_in)
elif kind == "qs_weth":
ix = uni_weth_leg(amount_in) + qs_leg(_WETH, tout, wi)
else:
return None
return ExecutionPlan(intent_id=intent.app_id, interactions=ix, deadline=deadline,
nonce=state.nonce, metadata={"solver": "apex-frontier", "chain_id": chain_id})
SOLVER_CLASS = MinerSolver