@oracle-agent/oracle 0.1.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/CONTRIBUTING.md +98 -0
- package/LICENSE +202 -0
- package/README.md +384 -0
- package/SECURITY.md +89 -0
- package/SETUP.md +235 -0
- package/artifacts/inscription/oracle-was-here-preview.png +0 -0
- package/artifacts/inscription/oracle-was-here.svg +1 -0
- package/artifacts/specialist-packs/oracle-full-crypto.json +273 -0
- package/bin/desk-server.mjs +438 -0
- package/bin/oracle-data-mcp.mjs +603 -0
- package/bin/oracle-init.mjs +423 -0
- package/bin/oracle-public-server.mjs +36 -0
- package/bin/oracle-route.mjs +254 -0
- package/bin/oracle-scan.mjs +192 -0
- package/docs/adding-a-chain.md +229 -0
- package/docs/architecture.md +135 -0
- package/docs/profiles.md +132 -0
- package/examples/add-a-chain.mjs +65 -0
- package/examples/research-a-token.mjs +70 -0
- package/package.json +90 -0
- package/profiles/_template/SOUL.md +47 -0
- package/profiles/_template/profile.json +22 -0
- package/profiles/bitcoin-agent/SOUL.md +31 -0
- package/profiles/bitcoin-agent/profile.json +32 -0
- package/profiles/hyperliquid-agent/SOUL.md +34 -0
- package/profiles/hyperliquid-agent/profile.json +37 -0
- package/profiles/oracle/SOUL.md +65 -0
- package/profiles/oracle/profile.json +35 -0
- package/profiles/polymarket-agent/SOUL.md +35 -0
- package/profiles/polymarket-agent/profile.json +34 -0
- package/profiles/profile.schema.json +90 -0
- package/profiles/protocol-builder/SOUL.md +50 -0
- package/profiles/protocol-builder/profile.json +37 -0
- package/profiles/robinhood-agent/SOUL.md +45 -0
- package/profiles/robinhood-agent/profile.json +39 -0
- package/profiles/solana-agent/SOUL.md +37 -0
- package/profiles/solana-agent/profile.json +37 -0
- package/profiles/stable-agent/SOUL.md +43 -0
- package/profiles/stable-agent/profile.json +37 -0
- package/public/oracle-console/app.js +272 -0
- package/public/oracle-console/bitcoin-wallets.js +206 -0
- package/public/oracle-console/index.html +91 -0
- package/public/oracle-console/styles.css +239 -0
- package/public/oracle-splash/index.html +931 -0
- package/scripts/build-inscription.py +230 -0
- package/scripts/check-test-count.mjs +105 -0
- package/scripts/e2e-hl-markets.mjs +21 -0
- package/scripts/e2e-hl-perps.mjs +48 -0
- package/scripts/e2e-hypercore-staking.mjs +128 -0
- package/scripts/e2e-solana-bitcoin.mjs +183 -0
- package/scripts/public-api-scan.mjs +23 -0
- package/scripts/secret-scan.mjs +181 -0
- package/scripts/verify-v3-venues.mjs +192 -0
- package/skills/oracle-best-execution/SKILL.md +127 -0
- package/skills/oracle-bitcoin/SKILL.md +53 -0
- package/skills/oracle-chain-graphs-telegram-cards/SKILL.md +59 -0
- package/skills/oracle-circuit-breaker/SKILL.md +51 -0
- package/skills/oracle-contract-research/SKILL.md +55 -0
- package/skills/oracle-desk/SKILL.md +58 -0
- package/skills/oracle-dex-launch/SKILL.md +38 -0
- package/skills/oracle-grants/SKILL.md +69 -0
- package/skills/oracle-hypercore-staking/SKILL.md +57 -0
- package/skills/oracle-hyperliquid/SKILL.md +56 -0
- package/skills/oracle-meme-token-sniper/SKILL.md +73 -0
- package/skills/oracle-nft-gacha-launch/SKILL.md +48 -0
- package/skills/oracle-nft-mint-gas-war/SKILL.md +63 -0
- package/skills/oracle-polymarket/SKILL.md +60 -0
- package/skills/oracle-protocol-builder/SKILL.md +38 -0
- package/skills/oracle-protocol-security/SKILL.md +60 -0
- package/skills/oracle-public-product/SKILL.md +44 -0
- package/skills/oracle-receipts/SKILL.md +52 -0
- package/skills/oracle-rfq-tokenized-assets/SKILL.md +69 -0
- package/skills/oracle-smart-wallet-scanner/SKILL.md +49 -0
- package/skills/oracle-solana/SKILL.md +65 -0
- package/skills/oracle-solana-nft/SKILL.md +54 -0
- package/skills/oracle-token-research/SKILL.md +67 -0
- package/src/agent-auth.mjs +191 -0
- package/src/approval-guard.mjs +282 -0
- package/src/attestation-secret.mjs +88 -0
- package/src/audit-log.mjs +196 -0
- package/src/auto-slippage.mjs +378 -0
- package/src/capability-posture.mjs +125 -0
- package/src/chains.mjs +62 -0
- package/src/data/catalog.mjs +495 -0
- package/src/data/desk-data.mjs +623 -0
- package/src/data/http.mjs +200 -0
- package/src/data/provider-endpoint.mjs +94 -0
- package/src/data/providers/aerodrome.mjs +244 -0
- package/src/data/providers/balancer.mjs +208 -0
- package/src/data/providers/bitcoin-esplora.mjs +230 -0
- package/src/data/providers/bitcoin-meta.mjs +378 -0
- package/src/data/providers/blockscout.mjs +14 -0
- package/src/data/providers/bridges.mjs +241 -0
- package/src/data/providers/cowswap.mjs +501 -0
- package/src/data/providers/curve.mjs +200 -0
- package/src/data/providers/defillama.mjs +88 -0
- package/src/data/providers/dexscreener.mjs +43 -0
- package/src/data/providers/evm-rpc.mjs +203 -0
- package/src/data/providers/geckoterminal.mjs +34 -0
- package/src/data/providers/gmx.mjs +495 -0
- package/src/data/providers/hl-info.mjs +102 -0
- package/src/data/providers/hl-markets.mjs +210 -0
- package/src/data/providers/hl-perps.mjs +382 -0
- package/src/data/providers/hl-staking.mjs +352 -0
- package/src/data/providers/hl-ws.mjs +119 -0
- package/src/data/providers/hyperevm-dex.mjs +49 -0
- package/src/data/providers/jupiter.mjs +182 -0
- package/src/data/providers/lifi.mjs +150 -0
- package/src/data/providers/magiceden-sol.mjs +355 -0
- package/src/data/providers/morpho.mjs +173 -0
- package/src/data/providers/odos.mjs +155 -0
- package/src/data/providers/oneinch.mjs +173 -0
- package/src/data/providers/opensea-multichain.mjs +136 -0
- package/src/data/providers/opensea-nft.mjs +99 -0
- package/src/data/providers/paraswap.mjs +117 -0
- package/src/data/providers/pendle.mjs +187 -0
- package/src/data/providers/poly-public.mjs +96 -0
- package/src/data/providers/poly-ws.mjs +103 -0
- package/src/data/providers/rh-agent.mjs +59 -0
- package/src/data/providers/satflow.mjs +336 -0
- package/src/data/providers/solana-rpc.mjs +186 -0
- package/src/data/providers/uniswap-v3.mjs +303 -0
- package/src/data/providers/zerox.mjs +166 -0
- package/src/data/public-api-scan.mjs +61 -0
- package/src/data/quote-placeholder.mjs +31 -0
- package/src/exact-integer.mjs +72 -0
- package/src/exec-policy.mjs +444 -0
- package/src/flags.mjs +15 -0
- package/src/fresh-window.mjs +76 -0
- package/src/gmx-attestation.mjs +175 -0
- package/src/index.mjs +50 -0
- package/src/nft-gas-war-guard.mjs +139 -0
- package/src/onboarding/agent-keys.mjs +157 -0
- package/src/onboarding/index.mjs +18 -0
- package/src/onboarding/tiers.mjs +139 -0
- package/src/oracle-env.mjs +38 -0
- package/src/protocol-execution.mjs +84 -0
- package/src/public-api/buzz-integration.mjs +256 -0
- package/src/public-api/connect-agent.mjs +397 -0
- package/src/public-api/grants.mjs +142 -0
- package/src/public-api/http.mjs +374 -0
- package/src/public-control/aa-adapter.mjs +402 -0
- package/src/public-control/build-registry.mjs +227 -0
- package/src/public-control/bundler-client.mjs +372 -0
- package/src/public-control/grant-indexer.mjs +296 -0
- package/src/public-control/policy-render.mjs +69 -0
- package/src/public-control/policy-schema.mjs +318 -0
- package/src/public-control/runtime-config.mjs +265 -0
- package/src/public-control/session-key-model.mjs +374 -0
- package/src/public-control/session-orchestrator.mjs +412 -0
- package/src/route-attestation.mjs +132 -0
- package/src/router/best-execution.mjs +221 -0
- package/src/router/index.mjs +185 -0
- package/src/router/prepare-bridge.mjs +288 -0
- package/src/router/prepare-route.mjs +341 -0
- package/src/router/proposal.mjs +311 -0
- package/src/router/risk-classifier.mjs +119 -0
- package/src/router/route-sources.mjs +292 -0
- package/src/scanner/chains.config.mjs +381 -0
- package/src/scanner/contract.mjs +270 -0
- package/src/scanner/evm-scanner.mjs +394 -0
- package/src/scanner/index.mjs +9 -0
- package/src/scanner/v2-venue.mjs +335 -0
- package/src/scanner/v3-venue.mjs +290 -0
- package/src/scopes.mjs +44 -0
- package/src/sell-simulation.mjs +167 -0
- package/src/token-transfer-guard.mjs +188 -0
- package/src/vault-attestation.mjs +145 -0
- package/src/venues.mjs +206 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Oracle Router risk classifier.
|
|
2
|
+
//
|
|
3
|
+
// Pure, dependency-free, deterministic. No I/O, no network, no chain reads,
|
|
4
|
+
// no imports. The router uses this to LABEL a proposed action's risk tier for
|
|
5
|
+
// advisory purposes only -- the label is context for a human or for Oracle
|
|
6
|
+
// Control, never an authorization decision. Oracle Control independently
|
|
7
|
+
// validates and gates every real action; nothing here grants or implies
|
|
8
|
+
// permission to sign, broadcast, or spend.
|
|
9
|
+
|
|
10
|
+
export const RISK_TIERS = Object.freeze(["low", "medium", "high", "critical"]);
|
|
11
|
+
|
|
12
|
+
// Kinds the router treats as pure read/advisory work (no custody exposure).
|
|
13
|
+
export const READ_ONLY_KINDS = Object.freeze(["read", "quote", "explain", "simulate"]);
|
|
14
|
+
|
|
15
|
+
// Kinds that describe a draft custody-adjacent action. These are still just
|
|
16
|
+
// labels on a proposal -- classifying something as "swap" does not create a
|
|
17
|
+
// swap, sign anything, or move funds.
|
|
18
|
+
export const CUSTODY_ADJACENT_KINDS = Object.freeze([
|
|
19
|
+
"approval",
|
|
20
|
+
"transfer",
|
|
21
|
+
"swap",
|
|
22
|
+
"order",
|
|
23
|
+
"bridge",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
function normalizeKind(kind) {
|
|
27
|
+
const k = String(kind || "").trim().toLowerCase();
|
|
28
|
+
return k || "unknown";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Coarse USD notional bands. Unknown/invalid notional is NOT treated as "low"
|
|
32
|
+
// -- callers must not be able to escape risk scoring by omitting a number.
|
|
33
|
+
function notionalBand(notionalUsd) {
|
|
34
|
+
if (notionalUsd == null) return null;
|
|
35
|
+
const n = Number(notionalUsd);
|
|
36
|
+
if (!Number.isFinite(n) || n < 0) return null;
|
|
37
|
+
if (n > 100_000) return 3;
|
|
38
|
+
if (n > 10_000) return 2;
|
|
39
|
+
if (n > 1_000) return 1;
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function tierFromScore(score) {
|
|
44
|
+
if (score >= 7) return "critical";
|
|
45
|
+
if (score >= 4) return "high";
|
|
46
|
+
if (score >= 2) return "medium";
|
|
47
|
+
return "low";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Classify the risk tier of a proposed action. Pure function: same input
|
|
52
|
+
* always produces the same output, and calling it has zero side effects.
|
|
53
|
+
*
|
|
54
|
+
* @param {object} [input]
|
|
55
|
+
* @param {string} [input.kind] "read" | "quote" | "explain" | "simulate" |
|
|
56
|
+
* "approval" | "transfer" | "swap" | "order" |
|
|
57
|
+
* "bridge" | any other string (treated conservatively)
|
|
58
|
+
* @param {number} [input.notionalUsd] approximate USD notional, if known
|
|
59
|
+
* @param {boolean} [input.destinationKnown] true if destination is a reviewed/allowlisted venue
|
|
60
|
+
* @param {boolean} [input.unlimitedApproval] true if the action requests an unbounded/MaxUint256-style approval
|
|
61
|
+
* @param {boolean} [input.crossChain] true if the action spans chains (bridge-like)
|
|
62
|
+
* @returns {{ tier: "low"|"medium"|"high"|"critical", score: number, reasons: readonly string[] }}
|
|
63
|
+
*/
|
|
64
|
+
export function classifyRisk(input = {}) {
|
|
65
|
+
const kind = normalizeKind(input?.kind);
|
|
66
|
+
const reasons = [];
|
|
67
|
+
let score = 0;
|
|
68
|
+
|
|
69
|
+
const isReadOnly = READ_ONLY_KINDS.includes(kind);
|
|
70
|
+
const isCustodyAdjacent = CUSTODY_ADJACENT_KINDS.includes(kind);
|
|
71
|
+
|
|
72
|
+
if (isReadOnly) {
|
|
73
|
+
reasons.push(`kind "${kind}" is read/advisory-only`);
|
|
74
|
+
} else if (isCustodyAdjacent) {
|
|
75
|
+
score += 2;
|
|
76
|
+
reasons.push(`kind "${kind}" is custody-adjacent (+2)`);
|
|
77
|
+
} else {
|
|
78
|
+
score += 3;
|
|
79
|
+
reasons.push(`kind "${kind}" is unrecognized; treated conservatively (+3)`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const band = notionalBand(input?.notionalUsd);
|
|
83
|
+
if (band != null && band > 0) {
|
|
84
|
+
score += band;
|
|
85
|
+
reasons.push(`notional band ${band} (+${band})`);
|
|
86
|
+
} else if (band == null && !isReadOnly) {
|
|
87
|
+
score += 1;
|
|
88
|
+
reasons.push("notional unknown for a non-read action (+1)");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (input?.unlimitedApproval) {
|
|
92
|
+
score += 3;
|
|
93
|
+
reasons.push("unlimited/unbounded approval requested (+3)");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (input?.destinationKnown === false) {
|
|
97
|
+
score += 2;
|
|
98
|
+
reasons.push("destination not a reviewed/allowlisted venue (+2)");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (input?.crossChain) {
|
|
102
|
+
score += 1;
|
|
103
|
+
reasons.push("cross-chain action (+1)");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
tier: tierFromScore(score),
|
|
108
|
+
score,
|
|
109
|
+
reasons: Object.freeze(reasons),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function isReadOnlyKind(kind) {
|
|
114
|
+
return READ_ONLY_KINDS.includes(normalizeKind(kind));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function isCustodyAdjacentKind(kind) {
|
|
118
|
+
return CUSTODY_ADJACENT_KINDS.includes(normalizeKind(kind));
|
|
119
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// Source adapters for the best-execution router.
|
|
2
|
+
//
|
|
3
|
+
// Each aggregator returns a different shape and means slightly different things by
|
|
4
|
+
// the same words. This module is where those differences are made explicit instead
|
|
5
|
+
// of being papered over by a shared field name.
|
|
6
|
+
//
|
|
7
|
+
// Notable disagreements handled here:
|
|
8
|
+
// * LI.FI reports gas AND fee costs already denominated in USD
|
|
9
|
+
// * 0x returns totalNetworkFee in native wei -- needs a native price to compare
|
|
10
|
+
// * 1inch quote returns only dstAmount + gas units, no cost
|
|
11
|
+
// * CoW is an intent: the solver pays gas, so gasUsd is genuinely 0, not unknown
|
|
12
|
+
// * Odos/ParaSwap report USD-valued gas estimates directly
|
|
13
|
+
//
|
|
14
|
+
// Sources needing an API key are skipped when the key is absent rather than throwing.
|
|
15
|
+
// A missing optional key is a config state, not an error, and must not sink the
|
|
16
|
+
// comparison.
|
|
17
|
+
|
|
18
|
+
import { lifiQuote } from "../data/providers/lifi.mjs";
|
|
19
|
+
import { paraswapPrice } from "../data/providers/paraswap.mjs";
|
|
20
|
+
import { zeroxQuote } from "../data/providers/zerox.mjs";
|
|
21
|
+
import { oneinchQuote } from "../data/providers/oneinch.mjs";
|
|
22
|
+
import { cowQuote } from "../data/providers/cowswap.mjs";
|
|
23
|
+
import { llamaPrices } from "../data/providers/defillama.mjs";
|
|
24
|
+
import { NATIVE_PRICE_KEY } from "./best-execution.mjs";
|
|
25
|
+
|
|
26
|
+
const num = (v) => {
|
|
27
|
+
const n = Number(v);
|
|
28
|
+
return Number.isFinite(n) ? n : null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Sum a LI.FI-style cost array that carries amountUSD entries. */
|
|
32
|
+
function sumUsd(list) {
|
|
33
|
+
if (!Array.isArray(list)) return null;
|
|
34
|
+
let total = 0;
|
|
35
|
+
let sawAny = false;
|
|
36
|
+
for (const c of list) {
|
|
37
|
+
const v = num(c?.amountUSD);
|
|
38
|
+
if (v != null) {
|
|
39
|
+
total += v;
|
|
40
|
+
sawAny = true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return sawAny ? total : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Fetch the native asset's USD price, for sources that price gas in wei. */
|
|
47
|
+
export async function nativeUsd(chainId, opts = {}) {
|
|
48
|
+
const key = NATIVE_PRICE_KEY[Number(chainId)];
|
|
49
|
+
if (!key) return null;
|
|
50
|
+
try {
|
|
51
|
+
const res = await llamaPrices([key], opts);
|
|
52
|
+
return num(res?.coins?.[key]?.price);
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Build the candidate list for a same-chain swap.
|
|
60
|
+
*
|
|
61
|
+
* @returns {Array<{source: string, run: () => Promise<object>}>}
|
|
62
|
+
*/
|
|
63
|
+
export function swapCandidates({
|
|
64
|
+
chainId,
|
|
65
|
+
tokenIn,
|
|
66
|
+
tokenOut,
|
|
67
|
+
amountIn,
|
|
68
|
+
taker,
|
|
69
|
+
decimalsIn,
|
|
70
|
+
decimalsOut,
|
|
71
|
+
nativePriceUsd = null,
|
|
72
|
+
destDecimalsPromise = null,
|
|
73
|
+
opts = {},
|
|
74
|
+
}) {
|
|
75
|
+
const c = [];
|
|
76
|
+
|
|
77
|
+
c.push({
|
|
78
|
+
source: "lifi",
|
|
79
|
+
run: async () => {
|
|
80
|
+
const q = await lifiQuote(
|
|
81
|
+
{ fromChain: chainId, toChain: chainId, fromToken: tokenIn, toToken: tokenOut, fromAmount: amountIn },
|
|
82
|
+
opts,
|
|
83
|
+
);
|
|
84
|
+
const est = q?.estimate ?? q?.[0]?.estimate;
|
|
85
|
+
return {
|
|
86
|
+
amountOut: est?.toAmount,
|
|
87
|
+
minOut: est?.toAmountMin,
|
|
88
|
+
gasUsd: sumUsd(est?.gasCosts),
|
|
89
|
+
feeUsd: sumUsd(est?.feeCosts),
|
|
90
|
+
meta: { tool: q?.tool ?? q?.toolDetails?.name },
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Odos is deliberately NOT in this list: its public SOR API was sunset on
|
|
96
|
+
// 2026-07-30 and returns HTTP 410. Querying it would spend a timeout on every
|
|
97
|
+
// single swap comparison to learn a fact we already know. src/data/providers/odos.mjs
|
|
98
|
+
// is kept for callers that still reference it, and re-adding it here is a one-line
|
|
99
|
+
// change if Odos ships a replacement endpoint.
|
|
100
|
+
|
|
101
|
+
// ParaSwap needs decimals for BOTH sides to quote at all.
|
|
102
|
+
//
|
|
103
|
+
// decimalsOut may arrive from the caller or from the destination price lookup,
|
|
104
|
+
// which now resolves in parallel rather than before this list is built. So the
|
|
105
|
+
// gate is decimalsIn (caller-only), and decimalsOut is resolved inside run().
|
|
106
|
+
//
|
|
107
|
+
// Subtle regression this avoids: gating on a value that is no longer awaited here
|
|
108
|
+
// would silently DROP ParaSwap for every caller who did not pass decimalsOut --
|
|
109
|
+
// quietly shrinking the comparison from three sources to two while still reporting
|
|
110
|
+
// a "best" route.
|
|
111
|
+
if (decimalsIn != null) {
|
|
112
|
+
c.push({
|
|
113
|
+
source: "paraswap",
|
|
114
|
+
run: async () => {
|
|
115
|
+
const dOut = decimalsOut ?? (await Promise.resolve(destDecimalsPromise))?.decimals;
|
|
116
|
+
if (dOut == null) {
|
|
117
|
+
throw new Error("paraswap needs destination decimals; none supplied or resolvable");
|
|
118
|
+
}
|
|
119
|
+
const q = await paraswapPrice(
|
|
120
|
+
{ chainId, srcToken: tokenIn, destToken: tokenOut, amount: amountIn, srcDecimals: decimalsIn, destDecimals: dOut },
|
|
121
|
+
opts,
|
|
122
|
+
);
|
|
123
|
+
const pr = q?.priceRoute;
|
|
124
|
+
return {
|
|
125
|
+
amountOut: pr?.destAmount,
|
|
126
|
+
gasUsd: num(pr?.gasCostUSD),
|
|
127
|
+
meta: { bestRoute: pr?.bestRoute?.[0]?.swaps?.[0]?.swapExchanges?.[0]?.exchange },
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 0x and 1inch need keys. Skip silently when absent -- see module header.
|
|
134
|
+
if (process.env.ZEROX_API_KEY) {
|
|
135
|
+
c.push({
|
|
136
|
+
source: "0x",
|
|
137
|
+
run: async () => {
|
|
138
|
+
const q = await zeroxQuote({ chainId, sellToken: tokenIn, buyToken: tokenOut, sellAmount: amountIn, taker }, opts);
|
|
139
|
+
// totalNetworkFee is native wei; convert only if we have a price.
|
|
140
|
+
// nativePriceUsd may be a PROMISE: prices are fetched in parallel with the
|
|
141
|
+
// quotes rather than before them. Awaiting here means only the one adapter
|
|
142
|
+
// that actually needs it pays for it, and only after its own HTTP call.
|
|
143
|
+
let gasUsd = null;
|
|
144
|
+
const feeWei = q?.totalNetworkFee;
|
|
145
|
+
const nativePrice = await Promise.resolve(nativePriceUsd);
|
|
146
|
+
if (feeWei != null && nativePrice) {
|
|
147
|
+
gasUsd = (Number(feeWei) / 1e18) * nativePrice;
|
|
148
|
+
}
|
|
149
|
+
return { amountOut: q?.buyAmount, minOut: q?.minBuyAmount, gasUsd, meta: { source: q?.route?.fills?.[0]?.source } };
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (process.env.ONEINCH_API_KEY) {
|
|
155
|
+
c.push({
|
|
156
|
+
source: "1inch",
|
|
157
|
+
run: async () => {
|
|
158
|
+
const q = await oneinchQuote({ chainId, src: tokenIn, dst: tokenOut, amount: amountIn }, opts);
|
|
159
|
+
let gasUsd = null;
|
|
160
|
+
if (q?.gas != null && nativePriceUsd) {
|
|
161
|
+
// 1inch returns gas UNITS; without a gas price this stays unknown rather
|
|
162
|
+
// than being guessed. Reporting a made-up cost is worse than reporting none.
|
|
163
|
+
gasUsd = null;
|
|
164
|
+
}
|
|
165
|
+
return { amountOut: q?.dstAmount, gasUsd, meta: { gasUnits: q?.gas } };
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// CoW is an intent, not a transaction: a solver executes and eats the gas, so the
|
|
171
|
+
// user pays 0 gas. That is a MEASURED zero, not an unknown, and it is exactly the
|
|
172
|
+
// case naive "biggest number wins" ranking gets wrong -- CoW often quotes slightly
|
|
173
|
+
// lower gross while winning decisively on net.
|
|
174
|
+
c.push({
|
|
175
|
+
source: "cow",
|
|
176
|
+
run: async () => {
|
|
177
|
+
const q = await cowQuote(
|
|
178
|
+
{ chainId, sellToken: tokenIn, buyToken: tokenOut, sellAmountBeforeFee: String(amountIn), from: taker },
|
|
179
|
+
opts,
|
|
180
|
+
);
|
|
181
|
+
const quote = q?.quote ?? q;
|
|
182
|
+
const feeAmount = quote?.feeAmount;
|
|
183
|
+
return {
|
|
184
|
+
amountOut: quote?.buyAmount,
|
|
185
|
+
gasUsd: 0,
|
|
186
|
+
meta: { intent: true, feeAmountInSellToken: feeAmount, settlement: "solver-executed" },
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return c;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Build the candidate list for a cross-chain bridge.
|
|
196
|
+
*
|
|
197
|
+
* Bridging has a cost structure swaps do not: a relayer fee, a destination-gas
|
|
198
|
+
* component, and a time-to-finality that is itself a cost. A route that saves $2 and
|
|
199
|
+
* takes 30 minutes is not obviously better than one that costs $2 more and lands in
|
|
200
|
+
* 20 seconds, so duration is surfaced rather than collapsed into the score.
|
|
201
|
+
*/
|
|
202
|
+
export function bridgeCandidates({
|
|
203
|
+
fromChainId,
|
|
204
|
+
toChainId,
|
|
205
|
+
tokenIn,
|
|
206
|
+
tokenOut,
|
|
207
|
+
amountIn,
|
|
208
|
+
taker,
|
|
209
|
+
opts = {},
|
|
210
|
+
}) {
|
|
211
|
+
const c = [];
|
|
212
|
+
|
|
213
|
+
c.push({
|
|
214
|
+
source: "lifi",
|
|
215
|
+
run: async () => {
|
|
216
|
+
const q = await lifiQuote(
|
|
217
|
+
{ fromChain: fromChainId, toChain: toChainId, fromToken: tokenIn, toToken: tokenOut, fromAmount: amountIn },
|
|
218
|
+
opts,
|
|
219
|
+
);
|
|
220
|
+
const est = q?.estimate ?? q?.[0]?.estimate;
|
|
221
|
+
return {
|
|
222
|
+
amountOut: est?.toAmount,
|
|
223
|
+
minOut: est?.toAmountMin,
|
|
224
|
+
gasUsd: sumUsd(est?.gasCosts),
|
|
225
|
+
feeUsd: sumUsd(est?.feeCosts),
|
|
226
|
+
meta: {
|
|
227
|
+
tool: q?.tool ?? q?.toolDetails?.name,
|
|
228
|
+
durationSeconds: num(est?.executionDuration),
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
c.push({
|
|
235
|
+
source: "relay",
|
|
236
|
+
run: async () => {
|
|
237
|
+
const { relayQuote } = await import("../data/providers/bridges.mjs");
|
|
238
|
+
const q = await relayQuote(
|
|
239
|
+
{
|
|
240
|
+
user: taker,
|
|
241
|
+
originChainId: fromChainId,
|
|
242
|
+
destinationChainId: toChainId,
|
|
243
|
+
originCurrency: tokenIn,
|
|
244
|
+
destinationCurrency: tokenOut,
|
|
245
|
+
amount: String(amountIn),
|
|
246
|
+
},
|
|
247
|
+
opts,
|
|
248
|
+
);
|
|
249
|
+
const d = q?.details;
|
|
250
|
+
return {
|
|
251
|
+
amountOut: d?.currencyOut?.amount,
|
|
252
|
+
minOut: d?.currencyOut?.minimumAmount,
|
|
253
|
+
gasUsd: num(d?.totalImpact?.usd) != null ? Math.abs(num(d.totalImpact.usd)) : null,
|
|
254
|
+
meta: { durationSeconds: num(d?.timeEstimate), rate: d?.rate },
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
c.push({
|
|
260
|
+
source: "across",
|
|
261
|
+
run: async () => {
|
|
262
|
+
const { acrossSuggestedFees } = await import("../data/providers/bridges.mjs");
|
|
263
|
+
const q = await acrossSuggestedFees(
|
|
264
|
+
{ token: tokenIn, destinationToken: tokenOut, originChainId: fromChainId, destinationChainId: toChainId, amount: String(amountIn) },
|
|
265
|
+
opts,
|
|
266
|
+
);
|
|
267
|
+
// Across returns fees, not an output amount: derive output as input - fees so it
|
|
268
|
+
// is comparable with sources that quote an output directly.
|
|
269
|
+
const totalFee = q?.totalRelayFee?.total;
|
|
270
|
+
let amountOut = null;
|
|
271
|
+
if (totalFee != null) {
|
|
272
|
+
try {
|
|
273
|
+
const outWei = BigInt(String(amountIn)) - BigInt(String(totalFee));
|
|
274
|
+
if (outWei > 0n) amountOut = outWei.toString();
|
|
275
|
+
} catch {
|
|
276
|
+
/* leave null -- a failed derivation must not become a fabricated number */
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
amountOut,
|
|
281
|
+
gasUsd: null,
|
|
282
|
+
meta: {
|
|
283
|
+
derivedFromFees: true,
|
|
284
|
+
durationSeconds: num(q?.estimatedFillTimeSec),
|
|
285
|
+
limits: q?.limits ? { min: q.limits.minDeposit, max: q.limits.maxDeposit } : undefined,
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
return c;
|
|
292
|
+
}
|