@anywalletkit/protocol-swap2-router 0.0.0-stage → 0.2.2
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/LICENSE +202 -0
- package/README.md +70 -2
- package/dist/chunk-6WXEWTIF.js +296 -0
- package/dist/chunk-6WXEWTIF.js.map +1 -0
- package/dist/chunk-ATQMEXTG.js +98 -0
- package/dist/chunk-ATQMEXTG.js.map +1 -0
- package/dist/chunk-VRUHVDAX.js +78 -0
- package/dist/chunk-VRUHVDAX.js.map +1 -0
- package/dist/index.cjs +487 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/router.cjs +316 -0
- package/dist/router.cjs.map +1 -0
- package/dist/router.d.cts +118 -0
- package/dist/router.d.ts +118 -0
- package/dist/router.js +7 -0
- package/dist/router.js.map +1 -0
- package/dist/swap2/router.cjs +386 -0
- package/dist/swap2/router.cjs.map +1 -0
- package/dist/swap2/router.d.cts +100 -0
- package/dist/swap2/router.d.ts +100 -0
- package/dist/swap2/router.js +8 -0
- package/dist/swap2/router.js.map +1 -0
- package/dist/value.cjs +118 -0
- package/dist/value.cjs.map +1 -0
- package/dist/value.d.cts +51 -0
- package/dist/value.d.ts +51 -0
- package/dist/value.js +9 -0
- package/dist/value.js.map +1 -0
- package/module.json +20 -0
- package/package.json +69 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/value.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/value.ts
|
|
21
|
+
var value_exports = {};
|
|
22
|
+
__export(value_exports, {
|
|
23
|
+
quoteValues: () => quoteValues,
|
|
24
|
+
usdValue: () => usdValue
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(value_exports);
|
|
27
|
+
var import_core = require("@anywalletkit/core");
|
|
28
|
+
var import_swap2_assets = require("@anywalletkit/swap2-assets");
|
|
29
|
+
var PAR = /^(USDC|USDT|USDT0|USD₮0|DAI|USDC\.E)$/i;
|
|
30
|
+
function priceOf(prices, symbol) {
|
|
31
|
+
const tries = [symbol, symbol.toUpperCase()];
|
|
32
|
+
if (/^w/i.test(symbol) && symbol.length > 2) tries.push(symbol.slice(1), symbol.slice(1).toUpperCase());
|
|
33
|
+
for (const t of tries) {
|
|
34
|
+
const p = prices[t];
|
|
35
|
+
if (typeof p === "number" && Number.isFinite(p) && p > 0) return p;
|
|
36
|
+
}
|
|
37
|
+
return PAR.test(symbol) ? 1 : null;
|
|
38
|
+
}
|
|
39
|
+
var units = (amount, decimals) => (0, import_core.bigintToUnits)(BigInt(amount), decimals);
|
|
40
|
+
function usdValue(prices, options) {
|
|
41
|
+
return (quote, params) => {
|
|
42
|
+
const dest = (0, import_swap2_assets.ccAssetByToken)(params.destChain, params.destToken);
|
|
43
|
+
if (!dest) return null;
|
|
44
|
+
const destPrice = priceOf(prices, dest.symbol);
|
|
45
|
+
if (destPrice === null) return null;
|
|
46
|
+
let value = units(quote.minDestAmount, dest.decimals) * destPrice;
|
|
47
|
+
const nativeFee = quote.txRequest?.nativeFee;
|
|
48
|
+
if (nativeFee && BigInt(nativeFee) > 0n) {
|
|
49
|
+
const native = (0, import_swap2_assets.ccAssetByToken)(params.sourceChain, "");
|
|
50
|
+
const nativePrice = native ? priceOf(prices, native.symbol) : null;
|
|
51
|
+
if (!native || nativePrice === null) return null;
|
|
52
|
+
value -= units(nativeFee, native.decimals) * nativePrice;
|
|
53
|
+
}
|
|
54
|
+
if (options?.gasUsd) {
|
|
55
|
+
const gas = options.gasUsd(quote, params);
|
|
56
|
+
if (gas === null || !Number.isFinite(gas)) return null;
|
|
57
|
+
value -= gas;
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function assetFor(chain, token) {
|
|
63
|
+
const byToken = (0, import_swap2_assets.ccAssetByToken)(chain, token);
|
|
64
|
+
if (byToken) return byToken;
|
|
65
|
+
const want = token.toLowerCase();
|
|
66
|
+
return (0, import_swap2_assets.ccChain)(chain)?.assets.find((a) => a.symbol.toLowerCase() === want);
|
|
67
|
+
}
|
|
68
|
+
function quoteValues(prices, params, quotes, options = {}) {
|
|
69
|
+
const betterBy = options.betterByPct ?? 2;
|
|
70
|
+
const src = assetFor(params.sourceChain, params.sourceToken);
|
|
71
|
+
const dest = assetFor(params.destChain, params.destToken);
|
|
72
|
+
const srcPrice = src ? priceOf(prices, src.symbol) : null;
|
|
73
|
+
const destPrice = dest ? priceOf(prices, dest.symbol) : null;
|
|
74
|
+
let valueUsdIn = null;
|
|
75
|
+
if (src && srcPrice !== null) {
|
|
76
|
+
try {
|
|
77
|
+
valueUsdIn = units(params.amountIn, src.decimals) * srcPrice;
|
|
78
|
+
} catch {
|
|
79
|
+
valueUsdIn = null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const out = quotes.map((q) => {
|
|
83
|
+
let valueUsdOut = null;
|
|
84
|
+
if (dest && destPrice !== null) {
|
|
85
|
+
try {
|
|
86
|
+
valueUsdOut = units(q.destAmount, dest.decimals) * destPrice;
|
|
87
|
+
const nativeFee = q.txRequest?.nativeFee;
|
|
88
|
+
if (nativeFee && BigInt(nativeFee) > 0n) {
|
|
89
|
+
const native = (0, import_swap2_assets.ccAssetByToken)(params.sourceChain, "");
|
|
90
|
+
const nativePrice = native ? priceOf(prices, native.symbol) : null;
|
|
91
|
+
if (native && nativePrice !== null) valueUsdOut -= units(nativeFee, native.decimals) * nativePrice;
|
|
92
|
+
}
|
|
93
|
+
} catch {
|
|
94
|
+
valueUsdOut = null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const lossPct = valueUsdIn !== null && valueUsdIn > 0 && valueUsdOut !== null ? (valueUsdIn - valueUsdOut) / valueUsdIn * 100 : null;
|
|
98
|
+
const impact = q.providerData?.priceImpactBps;
|
|
99
|
+
const priceImpactBps = typeof impact === "string" && /^\d+$/.test(impact) ? Number(impact) : void 0;
|
|
100
|
+
const v = { provider: q.provider, valueUsdIn, valueUsdOut, lossPct };
|
|
101
|
+
if (priceImpactBps !== void 0) v.priceImpactBps = priceImpactBps;
|
|
102
|
+
return v;
|
|
103
|
+
});
|
|
104
|
+
const allPriced = out.length > 1 && out.every((v) => v.valueUsdOut !== null);
|
|
105
|
+
if (out.length > 1) {
|
|
106
|
+
const measure = (v) => allPriced ? v.valueUsdOut : (0, import_core.bigintToNumber)(BigInt(quotes.find((q) => q.provider === v.provider).destAmount));
|
|
107
|
+
const top = out.reduce((a, b) => measure(b) > measure(a) ? b : a);
|
|
108
|
+
const topQuote = quotes.find((q) => q.provider === top.provider);
|
|
109
|
+
for (const v of out) {
|
|
110
|
+
if (v === top) continue;
|
|
111
|
+
const mine = measure(v);
|
|
112
|
+
const byPct = mine > 0 ? (measure(top) - mine) / mine * 100 : Infinity;
|
|
113
|
+
if (byPct > betterBy) v.betterAlternative = { provider: top.provider, valueUsdOut: top.valueUsdOut, destAmount: topQuote.destAmount, byPct };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=value.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/value.ts"],"sourcesContent":["import {bigintToNumber, bigintToUnits} from \"@anywalletkit/core\";\nimport {ccAssetByToken, ccChain} from \"@anywalletkit/swap2-assets\";\nimport type {Quote, QuoteParams} from \"@anywalletkit/protocol-swap2-core/types\";\n\n/** Stables priced at par when the map has no entry for them. */\nconst PAR = /^(USDC|USDT|USDT0|USD₮0|DAI|USDC\\.E)$/i;\n\n/** symbol → USD, trying the exact symbol, upper case, the unwrapped native (WETH → ETH, wS → S), then par for stables. */\nfunction priceOf(prices: Readonly<Record<string, number>>, symbol: string): number | null {\n const tries = [symbol, symbol.toUpperCase()];\n if (/^w/i.test(symbol) && symbol.length > 2) tries.push(symbol.slice(1), symbol.slice(1).toUpperCase());\n for (const t of tries) {\n const p = prices[t];\n if (typeof p === \"number\" && Number.isFinite(p) && p > 0) return p;\n }\n return PAR.test(symbol) ? 1 : null;\n}\n\nconst units = (amount: string | bigint, decimals: number) => bigintToUnits(BigInt(amount), decimals);\n\n/**\n * A `valueUsd` for the router: the guaranteed output in USD, minus the native\n * fee the route charges on top of the input (bridge messaging fees), minus\n * the source gas when `gasUsd` can say it. null when a needed price is missing\n * — the auction then falls back to comparing guaranteed outputs.\n */\nexport function usdValue(\n prices: Readonly<Record<string, number>>,\n options?: {gasUsd?: (quote: Quote, params: QuoteParams) => number | null},\n): (quote: Quote, params: QuoteParams) => number | null {\n return (quote, params) => {\n const dest = ccAssetByToken(params.destChain, params.destToken);\n if (!dest) return null;\n const destPrice = priceOf(prices, dest.symbol);\n if (destPrice === null) return null;\n let value = units(quote.minDestAmount, dest.decimals) * destPrice;\n const nativeFee = quote.txRequest?.nativeFee;\n if (nativeFee && BigInt(nativeFee) > 0n) {\n const native = ccAssetByToken(params.sourceChain, \"\");\n const nativePrice = native ? priceOf(prices, native.symbol) : null;\n if (!native || nativePrice === null) return null;\n value -= units(nativeFee, native.decimals) * nativePrice;\n }\n if (options?.gasUsd) {\n const gas = options.gasUsd(quote, params);\n if (gas === null || !Number.isFinite(gas)) return null;\n value -= gas;\n }\n return value;\n };\n}\n\n// ---------------------------------------------------------------------------\n// Value information for the user (SDK-127): never a refusal, always a fact.\n// ---------------------------------------------------------------------------\n\n/**\n * A quote's value in USD: what the user pays, what arrives, the loss, and a better alternative when\n * one exists. Use it to warn before a bad-value swap.\n */\nexport interface QuoteValue {\n provider: string;\n /** USD the user pays: the source amount at the current price. null when unpriced. */\n valueUsdIn: number | null;\n /** USD the quote delivers: the expected `destAmount` at the current price, minus a native fee paid on top. null when unpriced. */\n valueUsdOut: number | null;\n /** (in − out) / in × 100. Positive = the user receives less value than they pay. null when either side is unpriced. */\n lossPct: number | null;\n /** Another provider in the same set that delivers more than `betterByPct` more. Every quote of a request pays out the\n * same destination token, so the comparison is by delivered USD when priced and by raw `destAmount` otherwise. */\n betterAlternative?: {provider: string; valueUsdOut: number | null; destAmount: string; byPct: number};\n /** The venue's own price impact in basis points (`providerData.priceImpactBps`), when it measures one. */\n priceImpactBps?: number;\n}\n\n/** Options for quote valuation. */\nexport interface QuoteValueOptions {\n /** How much more (percent of this quote's value) another provider must deliver to be named as the better alternative. Default 2. */\n betterByPct?: number;\n}\n\ntype ValueParams = Pick<QuoteParams, \"sourceChain\" | \"sourceToken\" | \"destChain\" | \"destToken\" | \"amountIn\">;\ntype ValueQuote = Pick<Quote, \"provider\" | \"destAmount\"> & Partial<Pick<Quote, \"txRequest\" | \"providerData\">>;\n\n/** The catalog asset for a token given as a contract / asset id (the wire form) or as a symbol (what apps pass for the source). */\nfunction assetFor(chain: string, token: string) {\n const byToken = ccAssetByToken(chain, token);\n if (byToken) return byToken;\n const want = token.toLowerCase();\n return ccChain(chain)?.assets.find((a) => a.symbol.toLowerCase() === want);\n}\n\n/**\n * USD in / out / loss for every quote of one request, and the better\n * alternative inside the set. Prices are the caller's current spot prices\n * (symbol → USD). The owner's rule (2026-10-16): the wallet shows this and\n * says \"be careful\"; it never blocks — the Oct 16 funded run lost ~$33 on\n * two Uniswap legs that nothing compared to the other venues' quotes.\n */\nexport function quoteValues(prices: Readonly<Record<string, number>>, params: ValueParams, quotes: readonly ValueQuote[], options: QuoteValueOptions = {}): QuoteValue[] {\n const betterBy = options.betterByPct ?? 2;\n const src = assetFor(params.sourceChain, params.sourceToken);\n const dest = assetFor(params.destChain, params.destToken);\n const srcPrice = src ? priceOf(prices, src.symbol) : null;\n const destPrice = dest ? priceOf(prices, dest.symbol) : null;\n let valueUsdIn: number | null = null;\n if (src && srcPrice !== null) {\n try {\n valueUsdIn = units(params.amountIn, src.decimals) * srcPrice;\n } catch {\n valueUsdIn = null;\n }\n }\n const out: QuoteValue[] = quotes.map((q) => {\n let valueUsdOut: number | null = null;\n if (dest && destPrice !== null) {\n try {\n valueUsdOut = units(q.destAmount, dest.decimals) * destPrice;\n const nativeFee = q.txRequest?.nativeFee;\n if (nativeFee && BigInt(nativeFee) > 0n) {\n const native = ccAssetByToken(params.sourceChain, \"\");\n const nativePrice = native ? priceOf(prices, native.symbol) : null;\n if (native && nativePrice !== null) valueUsdOut -= units(nativeFee, native.decimals) * nativePrice;\n }\n } catch {\n valueUsdOut = null;\n }\n }\n const lossPct = valueUsdIn !== null && valueUsdIn > 0 && valueUsdOut !== null ? ((valueUsdIn - valueUsdOut) / valueUsdIn) * 100 : null;\n const impact = q.providerData?.priceImpactBps;\n const priceImpactBps = typeof impact === \"string\" && /^\\d+$/.test(impact) ? Number(impact) : undefined;\n const v: QuoteValue = {provider: q.provider, valueUsdIn, valueUsdOut, lossPct};\n if (priceImpactBps !== undefined) v.priceImpactBps = priceImpactBps;\n return v;\n });\n // The best delivery in the set names itself as the alternative for every quote it beats by more than betterBy %.\n // Priced: by delivered USD (native fees netted). Unpriced: by raw destAmount — same token for every quote, so the ratio is exact.\n const allPriced = out.length > 1 && out.every((v) => v.valueUsdOut !== null);\n if (out.length > 1) {\n const measure = (v: QuoteValue): number => (allPriced ? v.valueUsdOut! : bigintToNumber(BigInt(quotes.find((q) => q.provider === v.provider)!.destAmount)));\n const top = out.reduce((a, b) => (measure(b) > measure(a) ? b : a));\n const topQuote = quotes.find((q) => q.provider === top.provider)!;\n for (const v of out) {\n if (v === top) continue;\n const mine = measure(v);\n const byPct = mine > 0 ? ((measure(top) - mine) / mine) * 100 : Infinity;\n if (byPct > betterBy) v.betterAlternative = {provider: top.provider, valueUsdOut: top.valueUsdOut, destAmount: topQuote.destAmount, byPct};\n }\n }\n return out;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA4C;AAC5C,0BAAsC;AAItC,IAAM,MAAM;AAGZ,SAAS,QAAQ,QAA0C,QAA+B;AACxF,QAAM,QAAQ,CAAC,QAAQ,OAAO,YAAY,CAAC;AAC3C,MAAI,MAAM,KAAK,MAAM,KAAK,OAAO,SAAS,EAAG,OAAM,KAAK,OAAO,MAAM,CAAC,GAAG,OAAO,MAAM,CAAC,EAAE,YAAY,CAAC;AACtG,aAAW,KAAK,OAAO;AACrB,UAAM,IAAI,OAAO,CAAC;AAClB,QAAI,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC,KAAK,IAAI,EAAG,QAAO;AAAA,EACnE;AACA,SAAO,IAAI,KAAK,MAAM,IAAI,IAAI;AAChC;AAEA,IAAM,QAAQ,CAAC,QAAyB,iBAAqB,2BAAc,OAAO,MAAM,GAAG,QAAQ;AAQ5F,SAAS,SACd,QACA,SACsD;AACtD,SAAO,CAAC,OAAO,WAAW;AACxB,UAAM,WAAO,oCAAe,OAAO,WAAW,OAAO,SAAS;AAC9D,QAAI,CAAC,KAAM,QAAO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AAa,IAAI,QAAQ;AAC/D,eAAW,KAAK,KAAK;AACnB,UAAI,MAAM,IAAK;AACf,YAAM,OAAO,QAAQ,CAAC;AACtB,YAAM,QAAQ,OAAO,KAAM,QAAQ,GAAG,IAAI,QAAQ,OAAQ,MAAM;AAChE,UAAI,QAAQ,SAAU,GAAE,oBAAoB,EAAC,UAAU,IAAI,UAAU,aAAa,IAAI,aAAa,YAAY,SAAS,YAAY,MAAK;AAAA,IAC3I;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
|
package/dist/value.d.cts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { QuoteParams, Quote } from '@anywalletkit/protocol-swap2-core/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A `valueUsd` for the router: the guaranteed output in USD, minus the native
|
|
5
|
+
* fee the route charges on top of the input (bridge messaging fees), minus
|
|
6
|
+
* the source gas when `gasUsd` can say it. null when a needed price is missing
|
|
7
|
+
* — the auction then falls back to comparing guaranteed outputs.
|
|
8
|
+
*/
|
|
9
|
+
declare function usdValue(prices: Readonly<Record<string, number>>, options?: {
|
|
10
|
+
gasUsd?: (quote: Quote, params: QuoteParams) => number | null;
|
|
11
|
+
}): (quote: Quote, params: QuoteParams) => number | null;
|
|
12
|
+
/**
|
|
13
|
+
* A quote's value in USD: what the user pays, what arrives, the loss, and a better alternative when
|
|
14
|
+
* one exists. Use it to warn before a bad-value swap.
|
|
15
|
+
*/
|
|
16
|
+
interface QuoteValue {
|
|
17
|
+
provider: string;
|
|
18
|
+
/** USD the user pays: the source amount at the current price. null when unpriced. */
|
|
19
|
+
valueUsdIn: number | null;
|
|
20
|
+
/** USD the quote delivers: the expected `destAmount` at the current price, minus a native fee paid on top. null when unpriced. */
|
|
21
|
+
valueUsdOut: number | null;
|
|
22
|
+
/** (in − out) / in × 100. Positive = the user receives less value than they pay. null when either side is unpriced. */
|
|
23
|
+
lossPct: number | null;
|
|
24
|
+
/** Another provider in the same set that delivers more than `betterByPct` more. Every quote of a request pays out the
|
|
25
|
+
* same destination token, so the comparison is by delivered USD when priced and by raw `destAmount` otherwise. */
|
|
26
|
+
betterAlternative?: {
|
|
27
|
+
provider: string;
|
|
28
|
+
valueUsdOut: number | null;
|
|
29
|
+
destAmount: string;
|
|
30
|
+
byPct: number;
|
|
31
|
+
};
|
|
32
|
+
/** The venue's own price impact in basis points (`providerData.priceImpactBps`), when it measures one. */
|
|
33
|
+
priceImpactBps?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Options for quote valuation. */
|
|
36
|
+
interface QuoteValueOptions {
|
|
37
|
+
/** How much more (percent of this quote's value) another provider must deliver to be named as the better alternative. Default 2. */
|
|
38
|
+
betterByPct?: number;
|
|
39
|
+
}
|
|
40
|
+
type ValueParams = Pick<QuoteParams, "sourceChain" | "sourceToken" | "destChain" | "destToken" | "amountIn">;
|
|
41
|
+
type ValueQuote = Pick<Quote, "provider" | "destAmount"> & Partial<Pick<Quote, "txRequest" | "providerData">>;
|
|
42
|
+
/**
|
|
43
|
+
* USD in / out / loss for every quote of one request, and the better
|
|
44
|
+
* alternative inside the set. Prices are the caller's current spot prices
|
|
45
|
+
* (symbol → USD). The owner's rule (2026-10-16): the wallet shows this and
|
|
46
|
+
* says "be careful"; it never blocks — the Oct 16 funded run lost ~$33 on
|
|
47
|
+
* two Uniswap legs that nothing compared to the other venues' quotes.
|
|
48
|
+
*/
|
|
49
|
+
declare function quoteValues(prices: Readonly<Record<string, number>>, params: ValueParams, quotes: readonly ValueQuote[], options?: QuoteValueOptions): QuoteValue[];
|
|
50
|
+
|
|
51
|
+
export { type QuoteValue, type QuoteValueOptions, quoteValues, usdValue };
|
package/dist/value.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { QuoteParams, Quote } from '@anywalletkit/protocol-swap2-core/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A `valueUsd` for the router: the guaranteed output in USD, minus the native
|
|
5
|
+
* fee the route charges on top of the input (bridge messaging fees), minus
|
|
6
|
+
* the source gas when `gasUsd` can say it. null when a needed price is missing
|
|
7
|
+
* — the auction then falls back to comparing guaranteed outputs.
|
|
8
|
+
*/
|
|
9
|
+
declare function usdValue(prices: Readonly<Record<string, number>>, options?: {
|
|
10
|
+
gasUsd?: (quote: Quote, params: QuoteParams) => number | null;
|
|
11
|
+
}): (quote: Quote, params: QuoteParams) => number | null;
|
|
12
|
+
/**
|
|
13
|
+
* A quote's value in USD: what the user pays, what arrives, the loss, and a better alternative when
|
|
14
|
+
* one exists. Use it to warn before a bad-value swap.
|
|
15
|
+
*/
|
|
16
|
+
interface QuoteValue {
|
|
17
|
+
provider: string;
|
|
18
|
+
/** USD the user pays: the source amount at the current price. null when unpriced. */
|
|
19
|
+
valueUsdIn: number | null;
|
|
20
|
+
/** USD the quote delivers: the expected `destAmount` at the current price, minus a native fee paid on top. null when unpriced. */
|
|
21
|
+
valueUsdOut: number | null;
|
|
22
|
+
/** (in − out) / in × 100. Positive = the user receives less value than they pay. null when either side is unpriced. */
|
|
23
|
+
lossPct: number | null;
|
|
24
|
+
/** Another provider in the same set that delivers more than `betterByPct` more. Every quote of a request pays out the
|
|
25
|
+
* same destination token, so the comparison is by delivered USD when priced and by raw `destAmount` otherwise. */
|
|
26
|
+
betterAlternative?: {
|
|
27
|
+
provider: string;
|
|
28
|
+
valueUsdOut: number | null;
|
|
29
|
+
destAmount: string;
|
|
30
|
+
byPct: number;
|
|
31
|
+
};
|
|
32
|
+
/** The venue's own price impact in basis points (`providerData.priceImpactBps`), when it measures one. */
|
|
33
|
+
priceImpactBps?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Options for quote valuation. */
|
|
36
|
+
interface QuoteValueOptions {
|
|
37
|
+
/** How much more (percent of this quote's value) another provider must deliver to be named as the better alternative. Default 2. */
|
|
38
|
+
betterByPct?: number;
|
|
39
|
+
}
|
|
40
|
+
type ValueParams = Pick<QuoteParams, "sourceChain" | "sourceToken" | "destChain" | "destToken" | "amountIn">;
|
|
41
|
+
type ValueQuote = Pick<Quote, "provider" | "destAmount"> & Partial<Pick<Quote, "txRequest" | "providerData">>;
|
|
42
|
+
/**
|
|
43
|
+
* USD in / out / loss for every quote of one request, and the better
|
|
44
|
+
* alternative inside the set. Prices are the caller's current spot prices
|
|
45
|
+
* (symbol → USD). The owner's rule (2026-10-16): the wallet shows this and
|
|
46
|
+
* says "be careful"; it never blocks — the Oct 16 funded run lost ~$33 on
|
|
47
|
+
* two Uniswap legs that nothing compared to the other venues' quotes.
|
|
48
|
+
*/
|
|
49
|
+
declare function quoteValues(prices: Readonly<Record<string, number>>, params: ValueParams, quotes: readonly ValueQuote[], options?: QuoteValueOptions): QuoteValue[];
|
|
50
|
+
|
|
51
|
+
export { type QuoteValue, type QuoteValueOptions, quoteValues, usdValue };
|
package/dist/value.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/module.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anywalletkit/protocol-swap2-router",
|
|
3
|
+
"type": "swap2",
|
|
4
|
+
"chains": [
|
|
5
|
+
"eip155:*",
|
|
6
|
+
"solana:*",
|
|
7
|
+
"ton:-239",
|
|
8
|
+
"tron:0x2b6653dc",
|
|
9
|
+
"bip122:000000000019d6689c085ae165831e93"
|
|
10
|
+
],
|
|
11
|
+
"tier": "core",
|
|
12
|
+
"maintainers": [
|
|
13
|
+
"@ultratestbro"
|
|
14
|
+
],
|
|
15
|
+
"conformance": "0.x",
|
|
16
|
+
"repo": "https://github.com/ultratestbro/anywalletkit",
|
|
17
|
+
"path": "packages/protocol-swap2-router",
|
|
18
|
+
"description": "Optional aggregator: auctions the swap2 providers you install and pass in, executes the winner. Chains = union of our provider modules.",
|
|
19
|
+
"addedAt": "2026-10-04"
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anywalletkit/protocol-swap2-router",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "Optional swap2 aggregator for Any Wallet Kit: auctions whichever swap2 providers you install and pass in.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://docs.anywallet.tech",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/ultratestbro/anywalletkit/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/ultratestbro/anywalletkit.git",
|
|
13
|
+
"directory": "packages/protocol-swap2-router"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./router": {
|
|
27
|
+
"types": "./dist/router.d.ts",
|
|
28
|
+
"import": "./dist/router.js",
|
|
29
|
+
"require": "./dist/router.cjs"
|
|
30
|
+
},
|
|
31
|
+
"./value": {
|
|
32
|
+
"types": "./dist/value.d.ts",
|
|
33
|
+
"import": "./dist/value.js",
|
|
34
|
+
"require": "./dist/value.cjs"
|
|
35
|
+
},
|
|
36
|
+
"./swap2/router": {
|
|
37
|
+
"types": "./dist/swap2/router.d.ts",
|
|
38
|
+
"import": "./dist/swap2/router.js",
|
|
39
|
+
"require": "./dist/swap2/router.cjs"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"README.md",
|
|
46
|
+
"module.json"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@anywalletkit/protocol-swap2-core": "0.3.1",
|
|
50
|
+
"@anywalletkit/swap2-assets": "0.1.3"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@anywalletkit/core": "0.3.1"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^22",
|
|
57
|
+
"vitest": "^5.0.3",
|
|
58
|
+
"@anywalletkit/core": "0.3.1",
|
|
59
|
+
"@anywalletkit/conformance": "0.3.1"
|
|
60
|
+
},
|
|
61
|
+
"keywords": [],
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public",
|
|
64
|
+
"provenance": true
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsup",
|
|
68
|
+
"typecheck": "tsc --noEmit",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"lint": "echo ok"
|
|
71
|
+
}
|
|
6
72
|
}
|