@anywalletkit/protocol-swap2-kyberswap 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -3
- package/dist/activity.cjs +25 -0
- package/dist/activity.cjs.map +1 -1
- package/dist/activity.js +2 -2
- package/dist/{chunk-WOVGN3FL.js → chunk-D4UIDHM2.js} +37 -1
- package/dist/chunk-D4UIDHM2.js.map +1 -0
- package/dist/{chunk-FWC6K553.js → chunk-HHZEL5IJ.js} +2 -2
- package/dist/{chunk-PIC4PYGN.js → chunk-LIEIWIYI.js} +3 -3
- package/dist/{chunk-DEWODPQQ.js → chunk-NFMHUXU6.js} +59 -12
- package/dist/chunk-NFMHUXU6.js.map +1 -0
- package/dist/index.cjs +87 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +18 -4
- package/dist/protocol.cjs +80 -8
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.js +4 -4
- package/dist/provider.cjs +83 -8
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.cts +32 -2
- package/dist/provider.d.ts +32 -2
- package/dist/provider.js +9 -3
- package/dist/router.cjs +36 -0
- package/dist/router.cjs.map +1 -1
- package/dist/router.d.cts +27 -1
- package/dist/router.d.ts +27 -1
- package/dist/router.js +9 -1
- package/package.json +3 -3
- package/dist/chunk-DEWODPQQ.js.map +0 -1
- package/dist/chunk-WOVGN3FL.js.map +0 -1
- /package/dist/{chunk-FWC6K553.js.map → chunk-HHZEL5IJ.js.map} +0 -0
- /package/dist/{chunk-PIC4PYGN.js.map → chunk-LIEIWIYI.js.map} +0 -0
package/dist/protocol.cjs
CHANGED
|
@@ -67,6 +67,34 @@ var KYBER_CHAINS = Object.freeze({
|
|
|
67
67
|
5042: { slug: "arc", name: "Arc", build: "lite" },
|
|
68
68
|
99901: { slug: "elysium", name: "Elysium", build: "lite" }
|
|
69
69
|
});
|
|
70
|
+
var KYBER_LIMIT_ORDER_SOURCES = Object.freeze(["kyberswap-limit-order-v2"]);
|
|
71
|
+
var KYBER_RFQ_SOURCES = Object.freeze({
|
|
72
|
+
1: ["bebop", "clear-rfq", "hashflow-v3", "native-v1", "native-v2", "pmm-1", "pmm-10", "pmm-11", "pmm-12", "pmm-18", "pmm-2", "pmm-20", "pmm-3", "pmm-4", "pmm-5", "pmm-6", "pmm-7", "pmm-8", "swaap-v2"],
|
|
73
|
+
8453: ["bebop", "hashflow-v3", "native-v1", "native-v2", "pmm-1", "pmm-13", "pmm-14", "pmm-18", "pmm-19", "pmm-2", "pmm-20", "pmm-21", "pmm-4", "pmm-5", "pmm-8"],
|
|
74
|
+
56: ["bebop", "hashflow-v3", "native-v1", "native-v2", "pmm-1", "pmm-10", "pmm-13", "pmm-18", "pmm-19", "pmm-2", "pmm-20", "pmm-5", "pmm-8", "swaap-v2"],
|
|
75
|
+
42161: ["bebop", "hashflow-v3", "native-v1", "native-v2", "pmm-13", "pmm-2", "pmm-21", "pmm-5", "pmm-6", "pmm-7", "pmm-8", "swaap-v2"],
|
|
76
|
+
10: ["bebop", "pmm-13", "swaap-v2"],
|
|
77
|
+
137: ["bebop", "native-v1", "pmm-5", "pmm-8", "swaap-v2"],
|
|
78
|
+
43114: ["bebop", "dexalot", "hashflow-v3", "native-v1", "pmm-4", "swaap-v2"],
|
|
79
|
+
59144: ["native-v1", "swaap-v2"],
|
|
80
|
+
146: ["swaap-v2"],
|
|
81
|
+
80094: ["bebop"],
|
|
82
|
+
130: [],
|
|
83
|
+
999: ["bebop"],
|
|
84
|
+
9745: [],
|
|
85
|
+
143: ["native-v2", "swaap-v2"],
|
|
86
|
+
4326: [],
|
|
87
|
+
4663: ["bebop", "native-v2", "pmm-18", "pmm-19", "pmm-20", "swaap-v2"],
|
|
88
|
+
5042: ["pmm-13"],
|
|
89
|
+
99901: []
|
|
90
|
+
});
|
|
91
|
+
var RFQ_LIKE = /^(?:pmm-\d+|kyber-pmm|native-v\d+|bebop|hashflow(?:-v\d+)?|swaap-v\d+|clipper|dexalot|mx-trading|[a-z0-9-]*-rfq|rfq-[a-z0-9-]*)$/;
|
|
92
|
+
function kyberSourceKind(chainId, exchange, poolType) {
|
|
93
|
+
const id = typeof exchange === "string" ? exchange.split("/")[0] : "";
|
|
94
|
+
if (poolType === "limit-order" || /^kyberswap-limit-order(?:-v\d+)?$/.test(id)) return "limit-order";
|
|
95
|
+
const listed = Object.prototype.hasOwnProperty.call(KYBER_RFQ_SOURCES, chainId) ? KYBER_RFQ_SOURCES[chainId] : [];
|
|
96
|
+
return poolType === "orderbook" || listed.includes(id) || RFQ_LIKE.test(id) ? "rfq" : "pool";
|
|
97
|
+
}
|
|
70
98
|
function kyberChain(chainId) {
|
|
71
99
|
return Object.prototype.hasOwnProperty.call(KYBER_CHAINS, chainId) ? KYBER_CHAINS[chainId] : void 0;
|
|
72
100
|
}
|
|
@@ -194,9 +222,13 @@ function kyberVerifyQuote(params, quote) {
|
|
|
194
222
|
function routerFor(chain) {
|
|
195
223
|
return kyberChain(chainIdOf(chain)) ? [KYBER_ROUTER_V2] : [];
|
|
196
224
|
}
|
|
225
|
+
function kyberExactAllowanceOnly(chain) {
|
|
226
|
+
return kyberChain(chainIdOf(chain))?.build !== "lite";
|
|
227
|
+
}
|
|
197
228
|
var kyberPins = Object.freeze({
|
|
198
229
|
callTargets: routerFor,
|
|
199
230
|
spenders: routerFor,
|
|
231
|
+
exactAllowanceOnly: kyberExactAllowanceOnly,
|
|
200
232
|
verifyQuote: kyberVerifyQuote,
|
|
201
233
|
reasonOf: (refusal2) => notes.reasonOf(refusal2)
|
|
202
234
|
});
|
|
@@ -234,13 +266,20 @@ var ERROR_REASONS = {
|
|
|
234
266
|
4227: "simulation_failed"
|
|
235
267
|
};
|
|
236
268
|
var errorReason = (code) => typeof code === "number" && Object.prototype.hasOwnProperty.call(ERROR_REASONS, code) ? ERROR_REASONS[code] : void 0;
|
|
269
|
+
var KYBER_RFQ_TTL_MS = 1e4;
|
|
270
|
+
var KYBER_LIMIT_ORDER_TTL_MS = 3e4;
|
|
237
271
|
var uint = (v) => typeof v === "string" && /^\d{1,78}$/.test(v) ? BigInt(v) : typeof v === "number" && Number.isSafeInteger(v) && v >= 0 ? BigInt(v) : null;
|
|
238
272
|
var isAddr2 = (a) => typeof a === "string" && /^0x[0-9a-fA-F]{40}$/.test(a);
|
|
239
273
|
var same2 = (a, b) => isAddr2(a) && a.toLowerCase() === b.toLowerCase();
|
|
240
274
|
var sameText = (a, b) => a.length === b.length && a.toLowerCase() === b.toLowerCase();
|
|
241
275
|
var sameAmount = (a, b) => /^\d{1,80}$/.test(a) && /^\d{1,80}$/.test(b) && BigInt(a) === BigInt(b);
|
|
242
276
|
var apiToken = (t) => t === ZERO2 ? KYBER_NATIVE : t;
|
|
277
|
+
var offchainName = (offchain) => offchain.includes("rfq") && offchain.includes("limit-order") ? "market maker's quote or limit order" : offchain.includes("rfq") ? "market maker's quote" : "limit order";
|
|
243
278
|
var short = (s) => String(s ?? "").replace(/\s+/g, " ").slice(0, 120);
|
|
279
|
+
function kyberGasEstimate(routeGas) {
|
|
280
|
+
const gas = uint(routeGas);
|
|
281
|
+
return gas !== null && gas > 0n && gas < 10000000000n ? gas.toString() : void 0;
|
|
282
|
+
}
|
|
244
283
|
var ERC20_READS = (0, import_viem2.parseAbi)(["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)", "function approve(address, uint256) returns (bool)"]);
|
|
245
284
|
function revertData(e) {
|
|
246
285
|
const err = e;
|
|
@@ -267,6 +306,14 @@ function revertString(e) {
|
|
|
267
306
|
return m ? m[1] : null;
|
|
268
307
|
}
|
|
269
308
|
function kyberswapProvider(config = {}) {
|
|
309
|
+
const legacy = config.excludeRfqSources === false ? "include" : "exclude";
|
|
310
|
+
const setting = (v, name) => {
|
|
311
|
+
if (v === void 0) return legacy;
|
|
312
|
+
if (v !== "exclude" && v !== "include") throw new TypeError(`kyberswap: ${name} must be "exclude" or "include"`);
|
|
313
|
+
return v;
|
|
314
|
+
};
|
|
315
|
+
const rfqSetting = setting(config.rfq, "rfq");
|
|
316
|
+
const limitOrderSetting = setting(config.limitOrders, "limitOrders");
|
|
270
317
|
const baseUrl = (config.baseUrl ?? (config.apiKey ? GATEWAY_BASE_URL : PUBLIC_BASE_URL)).replace(/\/+$/, "");
|
|
271
318
|
const clientId = config.clientId ?? "anywalletkit";
|
|
272
319
|
const slippageDefault = config.defaultSlippageBps ?? DEFAULT_SLIPPAGE_BPS;
|
|
@@ -325,10 +372,17 @@ function kyberswapProvider(config = {}) {
|
|
|
325
372
|
if (!body?.data || typeof body.data !== "object") return { ok: false, why: "malformed API response", info: "upstream_unavailable" };
|
|
326
373
|
return { ok: true, data: body.data };
|
|
327
374
|
}
|
|
375
|
+
function sourceFilters(q, chainId) {
|
|
376
|
+
const excluded = [];
|
|
377
|
+
if (rfqSetting === "exclude" && limitOrderSetting === "exclude") q.set("excludeRFQSources", "true");
|
|
378
|
+
else if (rfqSetting === "exclude") excluded.push(...Object.prototype.hasOwnProperty.call(KYBER_RFQ_SOURCES, chainId) ? KYBER_RFQ_SOURCES[chainId] : []);
|
|
379
|
+
else if (limitOrderSetting === "exclude") excluded.push(...KYBER_LIMIT_ORDER_SOURCES);
|
|
380
|
+
for (const s of config.excludedSources ?? []) if (!excluded.includes(s)) excluded.push(s);
|
|
381
|
+
if (excluded.length) q.set("excludedSources", excluded.join(","));
|
|
382
|
+
}
|
|
328
383
|
async function routes(chainId, tokenIn, tokenOut, amountIn, signal) {
|
|
329
384
|
const q = new URLSearchParams({ tokenIn: apiToken(tokenIn), tokenOut: apiToken(tokenOut), amountIn: amountIn.toString(), gasInclude: "true" });
|
|
330
|
-
|
|
331
|
-
if (config.excludedSources?.length) q.set("excludedSources", config.excludedSources.join(","));
|
|
385
|
+
sourceFilters(q, chainId);
|
|
332
386
|
const r = await api(`${baseUrl}/${kyberChain(chainId).slug}/api/v1/routes?${q}`, { method: "GET" }, signal);
|
|
333
387
|
if (!r.ok) return r;
|
|
334
388
|
const summary = r.data.routeSummary;
|
|
@@ -345,16 +399,19 @@ function kyberswapProvider(config = {}) {
|
|
|
345
399
|
}
|
|
346
400
|
const amountOut = uint(summary.amountOut);
|
|
347
401
|
if (amountOut === null || amountOut <= 0n) return { ok: false, why: "the route pays nothing", info: "no_route" };
|
|
348
|
-
const
|
|
349
|
-
|
|
402
|
+
const hops = Array.isArray(summary.route) ? summary.route.flat().filter((h) => h && typeof h === "object") : [];
|
|
403
|
+
const sources = [...new Set(hops.map((h) => h.exchange).filter((x) => typeof x === "string"))].slice(0, 6).map((s) => s.slice(0, 40));
|
|
404
|
+
const kinds = new Set(hops.map((h) => kyberSourceKind(chainId, h.exchange, h.poolType)));
|
|
405
|
+
return { ok: true, summary, amountOut, sources, offchain: { rfq: kinds.has("rfq"), limitOrder: kinds.has("limit-order") } };
|
|
350
406
|
}
|
|
351
|
-
async function revertReason(chainId, sender, quoted, e) {
|
|
407
|
+
async function revertReason(chainId, sender, quoted, e, offchain) {
|
|
352
408
|
const text = revertString(e);
|
|
353
409
|
if (text && /return amount is not enough|min return/i.test(text)) {
|
|
354
410
|
return (0, import_errors2.refusal)("price_check_failed", "kyberswap: the price moved past your minimum. Nothing was sent; refresh the quote.");
|
|
355
411
|
}
|
|
356
412
|
const lack = await inputShortfall(chainId, sender, quoted).catch(() => null);
|
|
357
413
|
if (lack) return (0, import_errors2.refusal)("simulation_failed", lack);
|
|
414
|
+
if (offchain) return (0, import_errors2.refusal)("quote_expired", `kyberswap: the ${offchainName(offchain)} in this route no longer fills (expired or taken). Nothing was sent; refresh the quote.`);
|
|
358
415
|
return (0, import_errors2.refusal)("simulation_failed", `kyberswap: the swap would revert now (${short(text ?? (e instanceof Error ? e.message : "revert"))}). Nothing was sent; refresh the quote.`);
|
|
359
416
|
}
|
|
360
417
|
async function inputShortfall(chainId, sender, quoted) {
|
|
@@ -419,6 +476,8 @@ function kyberswapProvider(config = {}) {
|
|
|
419
476
|
supportsSameChain: true,
|
|
420
477
|
// The account's own estimate of the exact call, never the API's gas model.
|
|
421
478
|
ownGasEstimate: true,
|
|
479
|
+
// On a generic-build router (an owner-managed swapGeneric whitelist) no allowance may outlive the swap.
|
|
480
|
+
exactAllowanceOnly: kyberExactAllowanceOnly,
|
|
422
481
|
enabled: () => config.enabled ?? true,
|
|
423
482
|
supportsChain: (caip2) => chainIdOf2(caip2) !== null,
|
|
424
483
|
supportsChainPair: (from, to) => from === to && chainIdOf2(from) !== null,
|
|
@@ -487,6 +546,9 @@ function kyberswapProvider(config = {}) {
|
|
|
487
546
|
if (apiBps === null) return refuse("the output is too small to carry a minimum", "amount_too_small");
|
|
488
547
|
const minOut = kyberMinimum(r.amountOut, apiBps);
|
|
489
548
|
if (minOut < floor) return refuse(`floor breach (min=${minOut} < floor=${floor})`, "price_check_failed");
|
|
549
|
+
const gasEstimate = kyberGasEstimate(r.summary.gas);
|
|
550
|
+
const offchain = [...r.offchain.rfq ? ["rfq"] : [], ...r.offchain.limitOrder ? ["limit-order"] : []].join(",");
|
|
551
|
+
const life = Math.min(ttl, ...r.offchain.rfq ? [KYBER_RFQ_TTL_MS] : [], ...r.offchain.limitOrder ? [KYBER_LIMIT_ORDER_TTL_MS] : []);
|
|
490
552
|
const base = {
|
|
491
553
|
provider: "kyberswap",
|
|
492
554
|
intentId: `kyberswap:${chainId}`,
|
|
@@ -497,16 +559,18 @@ function kyberswapProvider(config = {}) {
|
|
|
497
559
|
destAmount: r.amountOut.toString(),
|
|
498
560
|
minDestAmount: minOut.toString(),
|
|
499
561
|
feeUsd: 0,
|
|
562
|
+
...gasEstimate !== void 0 ? { gasEstimate, gasEstimateSource: "venue" } : {},
|
|
500
563
|
etaSeconds: 15,
|
|
501
564
|
reliability: 0.95,
|
|
502
|
-
validUntilMs: Date.now() +
|
|
565
|
+
validUntilMs: Date.now() + life
|
|
503
566
|
};
|
|
504
567
|
const providerData = {
|
|
505
568
|
amountOut: r.amountOut.toString(),
|
|
506
569
|
minOut: minOut.toString(),
|
|
507
570
|
apiSlippageBps: String(apiBps),
|
|
508
571
|
...typeof r.summary.routeID === "string" ? { routeId: r.summary.routeID.slice(0, 80) } : {},
|
|
509
|
-
...r.sources.length ? { sources: r.sources.join(",") } : {}
|
|
572
|
+
...r.sources.length ? { sources: r.sources.join(",") } : {},
|
|
573
|
+
...offchain ? { offchain } : {}
|
|
510
574
|
};
|
|
511
575
|
const lossBps = kyberValueLossBps(r.summary.amountInUsd, r.summary.amountOutUsd);
|
|
512
576
|
if (lossBps !== null) providerData.priceImpactBps = String(lossBps);
|
|
@@ -530,6 +594,13 @@ function kyberswapProvider(config = {}) {
|
|
|
530
594
|
if (!built.ok) return refuse(`build: ${built.why}`, built.info);
|
|
531
595
|
const b = built.data;
|
|
532
596
|
if (uint(b.amountIn) !== amountIn) return refuse("the build spends another amount", "price_check_failed");
|
|
597
|
+
let destAmount = r.amountOut;
|
|
598
|
+
if (offchain) {
|
|
599
|
+
const builtOut = uint(b.amountOut);
|
|
600
|
+
if (builtOut === null) return refuse("malformed build response", "upstream_unavailable");
|
|
601
|
+
if (builtOut < minOut || builtOut < floor) return refuse(`the ${offchainName(offchain)} came in under the minimum when the route was built (${builtOut} < ${minOut}); refresh the quote`, "price_check_failed");
|
|
602
|
+
if (builtOut < destAmount) destAmount = builtOut;
|
|
603
|
+
}
|
|
533
604
|
if (same2(b.routerAddress, KYBER_ROUTER_V3)) return refuse("Kyber router V3 not supported yet", "policy_refused");
|
|
534
605
|
const value = uint(b.transactionValue ?? "0");
|
|
535
606
|
if (value === null || typeof b.data !== "string") return refuse("malformed build response", "upstream_unavailable");
|
|
@@ -563,6 +634,7 @@ function kyberswapProvider(config = {}) {
|
|
|
563
634
|
}
|
|
564
635
|
return {
|
|
565
636
|
...base,
|
|
637
|
+
destAmount: destAmount.toString(),
|
|
566
638
|
minDestAmount: decoded.minReturnAmount.toString(),
|
|
567
639
|
txRequest: {
|
|
568
640
|
to: KYBER_ROUTER_V2,
|
|
@@ -590,7 +662,7 @@ function kyberswapProvider(config = {}) {
|
|
|
590
662
|
try {
|
|
591
663
|
await rpc(chainId, "eth_call", [{ from: sender, to: quoted.to, data: quoted.data, value: `0x${BigInt(quoted.value ?? "0").toString(16)}` }, "latest"]);
|
|
592
664
|
} catch (e) {
|
|
593
|
-
if (e.rpc) throw await revertReason(chainId, sender, quoted, e);
|
|
665
|
+
if (e.rpc) throw await revertReason(chainId, sender, quoted, e, typeof quote.providerData?.offchain === "string" ? quote.providerData.offchain : void 0);
|
|
594
666
|
throw (0, import_errors2.withReason)(new Error("kyberswap: the swap cannot be simulated (no RPC answered). Nothing was sent; try again."), (0, import_errors2.refusalOf)(e) ?? "upstream_unavailable");
|
|
595
667
|
}
|
|
596
668
|
},
|