@oracle-agent/oracle 0.9.8 → 0.11.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 +15 -10
- package/bin/oracle-data-mcp.mjs +9 -0
- package/docs/architecture.md +3 -1
- package/docs/profiles.md +3 -6
- package/docs/public-surface.md +133 -0
- package/package.json +3 -2
- package/public/oracle-splash/assets/cli-chain-hyperliquid.jpg +0 -0
- package/public/oracle-splash/brand/aave.webp +0 -0
- package/public/oracle-splash/brand/aavev2.webp +0 -0
- package/public/oracle-splash/brand/aborean.webp +0 -0
- package/public/oracle-splash/brand/across.webp +0 -0
- package/public/oracle-splash/brand/aerodrome.webp +0 -0
- package/public/oracle-splash/brand/afi.webp +0 -0
- package/public/oracle-splash/brand/arcus.webp +0 -0
- package/public/oracle-splash/brand/aster.webp +0 -0
- package/public/oracle-splash/brand/avant.webp +0 -0
- package/public/oracle-splash/brand/balancer.webp +0 -0
- package/public/oracle-splash/brand/beefy.webp +0 -0
- package/public/oracle-splash/brand/benqi.webp +0 -0
- package/public/oracle-splash/brand/bestinslot.ico +0 -0
- package/public/oracle-splash/brand/bitfi.webp +0 -0
- package/public/oracle-splash/brand/bonkswap.webp +0 -0
- package/public/oracle-splash/brand/bouncebit.webp +0 -0
- package/public/oracle-splash/brand/camelot.webp +0 -0
- package/public/oracle-splash/brand/compound.webp +0 -0
- package/public/oracle-splash/brand/concrete.webp +0 -0
- package/public/oracle-splash/brand/cow.webp +0 -0
- package/public/oracle-splash/brand/curve.webp +0 -0
- package/public/oracle-splash/brand/derive.webp +0 -0
- package/public/oracle-splash/brand/dolomite.webp +0 -0
- package/public/oracle-splash/brand/eigenlayer.webp +0 -0
- package/public/oracle-splash/brand/ekubo.webp +0 -0
- package/public/oracle-splash/brand/ethena.webp +0 -0
- package/public/oracle-splash/brand/etherfi.webp +0 -0
- package/public/oracle-splash/brand/etherfiborrow.webp +0 -0
- package/public/oracle-splash/brand/felix.webp +0 -0
- package/public/oracle-splash/brand/fluid.webp +0 -0
- package/public/oracle-splash/brand/fluxbeam.webp +0 -0
- package/public/oracle-splash/brand/folks.webp +0 -0
- package/public/oracle-splash/brand/fortyacres.webp +0 -0
- package/public/oracle-splash/brand/gainsnetwork.webp +0 -0
- package/public/oracle-splash/brand/gassss.webp +0 -0
- package/public/oracle-splash/brand/gmx.webp +0 -0
- package/public/oracle-splash/brand/harmonix.webp +0 -0
- package/public/oracle-splash/brand/hlorderbook.webp +0 -0
- package/public/oracle-splash/brand/hlp.webp +0 -0
- package/public/oracle-splash/brand/hop.webp +0 -0
- package/public/oracle-splash/brand/hyperbeat.webp +0 -0
- package/public/oracle-splash/brand/hyperlend.webp +0 -0
- package/public/oracle-splash/brand/hyperliquid.webp +0 -0
- package/public/oracle-splash/brand/hyperliquidhpl.webp +0 -0
- package/public/oracle-splash/brand/hyperliquidspot.webp +0 -0
- package/public/oracle-splash/brand/hyperswap.webp +0 -0
- package/public/oracle-splash/brand/hypha.webp +0 -0
- package/public/oracle-splash/brand/joedex.webp +0 -0
- package/public/oracle-splash/brand/jupiter.webp +0 -0
- package/public/oracle-splash/brand/kelp.webp +0 -0
- package/public/oracle-splash/brand/kinetiq.webp +0 -0
- package/public/oracle-splash/brand/kmhype.webp +0 -0
- package/public/oracle-splash/brand/kona.webp +0 -0
- package/public/oracle-splash/brand/konalend.webp +0 -0
- package/public/oracle-splash/brand/lfj.webp +0 -0
- package/public/oracle-splash/brand/lido.webp +0 -0
- package/public/oracle-splash/brand/lifi.webp +0 -0
- package/public/oracle-splash/brand/lighter.webp +0 -0
- package/public/oracle-splash/brand/lista.webp +0 -0
- package/public/oracle-splash/brand/loopedhype.webp +0 -0
- package/public/oracle-splash/brand/lorenzo.webp +0 -0
- package/public/oracle-splash/brand/magic-eden.webp +0 -0
- package/public/oracle-splash/brand/magiceden.webp +0 -0
- package/public/oracle-splash/brand/mempool.png +0 -0
- package/public/oracle-splash/brand/meteora.webp +0 -0
- package/public/oracle-splash/brand/morpho.webp +0 -0
- package/public/oracle-splash/brand/myriad.webp +0 -0
- package/public/oracle-splash/brand/noxa.webp +0 -0
- package/public/oracle-splash/brand/odos.webp +0 -0
- package/public/oracle-splash/brand/oneinch.webp +0 -0
- package/public/oracle-splash/brand/openbook.webp +0 -0
- package/public/oracle-splash/brand/opensea.webp +0 -0
- package/public/oracle-splash/brand/orca.webp +0 -0
- package/public/oracle-splash/brand/ordinals.png +0 -0
- package/public/oracle-splash/brand/ostium.webp +0 -0
- package/public/oracle-splash/brand/pancakeswap.webp +0 -0
- package/public/oracle-splash/brand/pendle.webp +0 -0
- package/public/oracle-splash/brand/perena.webp +0 -0
- package/public/oracle-splash/brand/pharaoh.webp +0 -0
- package/public/oracle-splash/brand/phoenix.webp +0 -0
- package/public/oracle-splash/brand/polymarket.webp +0 -0
- package/public/oracle-splash/brand/polynomial.webp +0 -0
- package/public/oracle-splash/brand/projectx.webp +0 -0
- package/public/oracle-splash/brand/pumpfun.webp +0 -0
- package/public/oracle-splash/brand/quickswap.webp +0 -0
- package/public/oracle-splash/brand/rain.webp +0 -0
- package/public/oracle-splash/brand/raydium.webp +0 -0
- package/public/oracle-splash/brand/relay.webp +0 -0
- package/public/oracle-splash/brand/rocketpool.webp +0 -0
- package/public/oracle-splash/brand/saber.webp +0 -0
- package/public/oracle-splash/brand/sakuraswap.webp +0 -0
- package/public/oracle-splash/brand/sanctum.webp +0 -0
- package/public/oracle-splash/brand/satflow.png +0 -0
- package/public/oracle-splash/brand/sierra.webp +0 -0
- package/public/oracle-splash/brand/sky.webp +0 -0
- package/public/oracle-splash/brand/solv.webp +0 -0
- package/public/oracle-splash/brand/spark.webp +0 -0
- package/public/oracle-splash/brand/sparksavings.webp +0 -0
- package/public/oracle-splash/brand/sphere.webp +0 -0
- package/public/oracle-splash/brand/ssv.webp +0 -0
- package/public/oracle-splash/brand/stabble.webp +0 -0
- package/public/oracle-splash/brand/stableswap.webp +0 -0
- package/public/oracle-splash/brand/stakewise.webp +0 -0
- package/public/oracle-splash/brand/stargate.webp +0 -0
- package/public/oracle-splash/brand/sthype.webp +0 -0
- package/public/oracle-splash/brand/sushiswap.webp +0 -0
- package/public/oracle-splash/brand/unisat.png +0 -0
- package/public/oracle-splash/brand/uniswap.webp +0 -0
- package/public/oracle-splash/brand/uniswapv2.webp +0 -0
- package/public/oracle-splash/brand/uniswapv4.webp +0 -0
- package/public/oracle-splash/brand/unit.webp +0 -0
- package/public/oracle-splash/brand/velodrome.webp +0 -0
- package/public/oracle-splash/brand/velora.webp +0 -0
- package/public/oracle-splash/brand/venus.webp +0 -0
- package/public/oracle-splash/brand/wbeth.webp +0 -0
- package/public/oracle-splash/brand/xsy.webp +0 -0
- package/public/oracle-splash/brand/yieldyak.webp +0 -0
- package/public/oracle-splash/brand/zerobase.webp +0 -0
- package/public/oracle-splash/brand/zerox.webp +0 -0
- package/public/oracle-splash/index.html +337 -197
- package/scripts/check-doc-drift.mjs +11 -0
- package/scripts/e2e-solana-bitcoin.mjs +1 -1
- package/scripts/secret-scan.mjs +1 -1
- package/src/action-receipts.mjs +108 -11
- package/src/auth/oauth.mjs +672 -0
- package/src/cli/commands/auth.mjs +193 -0
- package/src/cli/commands/bootstrap.mjs +2 -2
- package/src/cli/commands/chat.mjs +97 -75
- package/src/cli/commands/data.mjs +3 -1
- package/src/cli/commands/doctor.mjs +11 -1
- package/src/cli/commands/model.mjs +79 -23
- package/src/cli/kernel.mjs +4 -3
- package/src/cli/model-config.mjs +70 -0
- package/src/data/catalog.mjs +38 -7
- package/src/data/desk-data.mjs +12 -0
- package/src/data/providers/evm-rpc.mjs +19 -16
- package/src/data/providers/jupiter-venues.mjs +119 -0
- package/src/data/providers/jupiter.mjs +132 -0
- package/src/data/providers/magiceden-sol.mjs +13 -0
- package/src/data/providers/morpho.mjs +6 -2
- package/src/data/providers/pendle.mjs +3 -2
- package/src/data/providers/satflow.mjs +4 -1
- package/src/data/providers/signed-material-guard.mjs +108 -0
- package/src/data/providers/uniswap-v3.mjs +34 -5
- package/src/data/providers/uniswap-v4.mjs +414 -0
- package/src/prepare-envelope.mjs +22 -4
- package/src/router/prepare-route.mjs +17 -0
- package/src/router/route-sources.mjs +35 -0
- package/src/tui/app.mjs +18 -7
- package/src/tui/backend.mjs +145 -0
- package/src/tui/standalone-client.mjs +666 -0
- package/src/cards.mjs +0 -369
- package/src/cli/__pycache__/oracle-harness.cpython-311.pyc +0 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Shared refusal for pre-signed material returned by an upstream marketplace or
|
|
2
|
+
// aggregator API. Every prepare path calls this before selecting unsigned bytes
|
|
3
|
+
// or binding the upstream response into a stamped envelope.
|
|
4
|
+
|
|
5
|
+
const MAX_DEPTH = 16;
|
|
6
|
+
const MAX_NODES = 2048;
|
|
7
|
+
|
|
8
|
+
const UNSIGNED_PREFIXES = ["unsigned", "notsigned", "tosign", "signable"];
|
|
9
|
+
|
|
10
|
+
const STATUS_KEYS = new Set([
|
|
11
|
+
"requiresusersignature",
|
|
12
|
+
"requiressignature",
|
|
13
|
+
"needssignature",
|
|
14
|
+
"signaturerequired",
|
|
15
|
+
"signaturesrequired",
|
|
16
|
+
"signingready",
|
|
17
|
+
"signaturecount",
|
|
18
|
+
"signaturethreshold",
|
|
19
|
+
"requiredsignatures",
|
|
20
|
+
"signatureneeded",
|
|
21
|
+
"hassignature",
|
|
22
|
+
"allowsignatures",
|
|
23
|
+
"requestsignature",
|
|
24
|
+
"multisig",
|
|
25
|
+
"cosig",
|
|
26
|
+
"msig",
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
const MATERIAL_KEYS = new Set([
|
|
30
|
+
"sig",
|
|
31
|
+
"sigs",
|
|
32
|
+
"scriptsig",
|
|
33
|
+
"witness",
|
|
34
|
+
"psbt",
|
|
35
|
+
"finalizedpsbt",
|
|
36
|
+
"completepsbt",
|
|
37
|
+
"finalpsbt",
|
|
38
|
+
"psbtfinalized",
|
|
39
|
+
"serializedtransaction",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
function normalizeKey(key) {
|
|
43
|
+
return String(key).toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isUnsignedField(norm) {
|
|
47
|
+
return UNSIGNED_PREFIXES.some((prefix) => norm.startsWith(prefix)) || STATUS_KEYS.has(norm);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isSignedMaterialKey(key) {
|
|
51
|
+
const raw = String(key);
|
|
52
|
+
if (!/^[\x20-\x7e]+$/.test(raw)) return true;
|
|
53
|
+
const norm = normalizeKey(raw);
|
|
54
|
+
if (isUnsignedField(norm)) return false;
|
|
55
|
+
if (MATERIAL_KEYS.has(norm)) return true;
|
|
56
|
+
return norm.includes("signed") || norm.includes("signature") || norm.endsWith("sig") || norm.endsWith("sigs");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isSignatureTuple(value) {
|
|
60
|
+
const v = Number(value?.v);
|
|
61
|
+
const r = String(value?.r || "");
|
|
62
|
+
const s = String(value?.s || "");
|
|
63
|
+
return [0, 1, 27, 28].includes(v) && /^0x[0-9a-f]{2,64}$/i.test(r) && /^0x[0-9a-f]{2,64}$/i.test(s);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Find signed-material field names anywhere in an upstream response. */
|
|
67
|
+
export function findSignedFields(value, depth = 0, seen = new Set(), state = { nodes: 0 }) {
|
|
68
|
+
if (value == null || typeof value !== "object") return [];
|
|
69
|
+
if (depth > MAX_DEPTH) return ["<max-depth>"];
|
|
70
|
+
if (seen.has(value)) return [];
|
|
71
|
+
state.nodes += 1;
|
|
72
|
+
if (state.nodes > MAX_NODES) return ["<max-nodes>"];
|
|
73
|
+
seen.add(value);
|
|
74
|
+
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
return value.flatMap((item) => findSignedFields(item, depth + 1, seen, state));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hits = [];
|
|
80
|
+
for (const [key, child] of Object.entries(value)) {
|
|
81
|
+
if (isSignedMaterialKey(key)) {
|
|
82
|
+
hits.push(key);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
hits.push(...findSignedFields(child, depth + 1, seen, state));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const keys = Object.keys(value).map(normalizeKey);
|
|
89
|
+
const hasPsbt = keys.some((key) => key.endsWith("psbt") || key.endsWith("psbtbase64"));
|
|
90
|
+
const claimsComplete = Object.entries(value).some(
|
|
91
|
+
([key, child]) => /complete|finalized|isfinal/.test(normalizeKey(key)) && child === true,
|
|
92
|
+
);
|
|
93
|
+
if (hasPsbt && claimsComplete) hits.push("psbt(complete)");
|
|
94
|
+
if (isSignatureTuple(value)) hits.push("v/r/s");
|
|
95
|
+
|
|
96
|
+
return hits;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Refuse an upstream response that carries pre-signed material. */
|
|
100
|
+
export function assertNoSignedMaterial(provider, raw) {
|
|
101
|
+
const hits = findSignedFields(raw);
|
|
102
|
+
if (hits.length) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`${provider}: refused a pre-signed transaction payload from the API (fields: ${[...new Set(hits)].join(", ")}) — ` +
|
|
105
|
+
`@oracle-agent/oracle is prepare-only and will not bind signed material into a prepared envelope`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Uniswap V3 QuoterV2 — exact-input quotes via eth_call (no API key).
|
|
2
|
-
// Chains: Ethereum, Base, Arbitrum. Read-only; never signs or swaps.
|
|
2
|
+
// Chains: Ethereum, Optimism, BNB, Polygon, Base, Arbitrum, Avalanche, Robinhood. Read-only; never signs or swaps.
|
|
3
3
|
|
|
4
4
|
import { Interface, getAddress, isAddress } from "ethers";
|
|
5
5
|
import { rpcCall } from "./evm-rpc.mjs";
|
|
@@ -34,10 +34,8 @@ export const UNI_V3_CHAINS = {
|
|
|
34
34
|
// eth_chainId == 4663, eth_getCode non-empty for every address below, and a real
|
|
35
35
|
// quoteExactInputSingle returned 1 WETH -> 1866.58 USDG at fee 100 (2026-07-31).
|
|
36
36
|
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
// on-chain QuoterV2 is the only source that can price this chain. `usdc` is USDG
|
|
40
|
-
// (Global Dollar, 6-dec) — the stable quote RH pools actually use.
|
|
37
|
+
// The router-level price-impact guard is what blocks dead RH V3 shells from being
|
|
38
|
+
// treated as executable routes.
|
|
41
39
|
4663: {
|
|
42
40
|
name: "robinhood",
|
|
43
41
|
quoter: "0x33e885ed0ec9bf04ecfb19341582aadcb4c8a9e7",
|
|
@@ -45,6 +43,37 @@ export const UNI_V3_CHAINS = {
|
|
|
45
43
|
weth: "0x0bd7d308f8e1639fab988df18a8011f41eacad73",
|
|
46
44
|
usdc: "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168",
|
|
47
45
|
},
|
|
46
|
+
// Optimism / BNB / Polygon / Avalanche. Official Uniswap v3 deployments,
|
|
47
|
+
// each proved on-chain in the MAD PR #16 lane: eth_getCode non-empty for
|
|
48
|
+
// quoter, router, wrapped-native and stable, plus a decimals-correct quote.
|
|
49
|
+
10: {
|
|
50
|
+
name: "optimism",
|
|
51
|
+
quoter: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
|
|
52
|
+
router: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
|
|
53
|
+
weth: "0x4200000000000000000000000000000000000006",
|
|
54
|
+
usdc: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
|
|
55
|
+
},
|
|
56
|
+
56: {
|
|
57
|
+
name: "bsc",
|
|
58
|
+
quoter: "0x78D78E420Da98ad378D7799bE8f4AF69033EB077",
|
|
59
|
+
router: "0xB971eF87ede563556b2ED4b1C0b0019111Dd85d2",
|
|
60
|
+
weth: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
61
|
+
usdc: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
|
|
62
|
+
},
|
|
63
|
+
137: {
|
|
64
|
+
name: "polygon",
|
|
65
|
+
quoter: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
|
|
66
|
+
router: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
|
|
67
|
+
weth: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
|
|
68
|
+
usdc: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
|
|
69
|
+
},
|
|
70
|
+
43114: {
|
|
71
|
+
name: "avalanche",
|
|
72
|
+
quoter: "0xbe0F5544EC67e9B3b2D979aaA43f18Fd87E6257F",
|
|
73
|
+
router: "0xbb00FF08d01D300023C629E8fFfFcb65A5a578cE",
|
|
74
|
+
weth: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
|
|
75
|
+
usdc: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
|
|
76
|
+
},
|
|
48
77
|
};
|
|
49
78
|
|
|
50
79
|
export const UNI_V3_FEE_TIERS = [100, 500, 3000, 10000];
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// Uniswap V4 Quoter — multi-chain prepare-only quotes via eth_call.
|
|
2
|
+
//
|
|
3
|
+
// Cross-chain by design. Addresses are the official Uniswap V4 deployments
|
|
4
|
+
// (docs.uniswap.org/contracts/v4/deployments, scraped 2026-08-02) plus the
|
|
5
|
+
// Robinhood Chain deployment that the RH SQUEEZE book already proved live.
|
|
6
|
+
//
|
|
7
|
+
// WHY THIS EXISTS: a V3 factory quote can return a real, on-chain, DEAD shell
|
|
8
|
+
// while the live book sits on V4. Verified on Robinhood 2026-08-02 with SQUEEZE
|
|
9
|
+
// (V3 ~$7, V4 ~$471k). The impact ceiling blocks garbage V3 fills; this module
|
|
10
|
+
// is the source that can actually price the live book.
|
|
11
|
+
//
|
|
12
|
+
// Scope:
|
|
13
|
+
// * quoteExactInputSingle against V4Quoter
|
|
14
|
+
// * poolId derivation (keccak of PoolKey) for identity checks
|
|
15
|
+
// * catalog/list exposure
|
|
16
|
+
// * prepare exact-input single-hop Universal Router calldata when recipient
|
|
17
|
+
// and slippage are explicit; the fresh successful quote supplies PoolKey
|
|
18
|
+
//
|
|
19
|
+
// Never signs. Never broadcasts.
|
|
20
|
+
|
|
21
|
+
import { AbiCoder, Interface, getAddress, isAddress, keccak256 } from "ethers";
|
|
22
|
+
import { rpcCall } from "./evm-rpc.mjs";
|
|
23
|
+
import { attachAutoSlippage } from "../../auto-slippage.mjs";
|
|
24
|
+
import { stampPrepared } from "../../prepare-envelope.mjs";
|
|
25
|
+
|
|
26
|
+
const abi = AbiCoder.defaultAbiCoder();
|
|
27
|
+
const ZERO = "0x0000000000000000000000000000000000000000";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Official Uniswap V4 deployments + RH.
|
|
31
|
+
* Source: Uniswap docs V4 deployments page (2026-08-02) and RH squeeze-v4 worktree.
|
|
32
|
+
* Only chains Oracle already routes are listed here.
|
|
33
|
+
*
|
|
34
|
+
* @type {Record<number, {
|
|
35
|
+
* name: string,
|
|
36
|
+
* poolManager: string,
|
|
37
|
+
* quoter: string,
|
|
38
|
+
* stateView?: string,
|
|
39
|
+
* universalRouter?: string,
|
|
40
|
+
* weth: string,
|
|
41
|
+
* usdc: string,
|
|
42
|
+
* }>}
|
|
43
|
+
*/
|
|
44
|
+
export const UNI_V4_CHAINS = {
|
|
45
|
+
1: {
|
|
46
|
+
name: "ethereum",
|
|
47
|
+
poolManager: "0x000000000004444c5dc75cB358380D2e3dE08A90",
|
|
48
|
+
quoter: "0x52f0e24d1c21c8a0cb1e5a5dd6198556bd9e1203",
|
|
49
|
+
stateView: "0x7ffe42c4a5deea5b0fec41c94c136cf115597227",
|
|
50
|
+
universalRouter: "0x66a9893cc07d91d95644aedd05d03f95e1dba8af",
|
|
51
|
+
weth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
52
|
+
usdc: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
53
|
+
},
|
|
54
|
+
10: {
|
|
55
|
+
name: "optimism",
|
|
56
|
+
poolManager: "0x9a13f98cb987694c9f086b1f5eb990eea8264ec3",
|
|
57
|
+
quoter: "0x1f3131a13296fb91c90870043742c3cdbff1a8d7",
|
|
58
|
+
stateView: "0xc18a3169788f4f75a170290584eca6395c75ecdb",
|
|
59
|
+
universalRouter: "0x851116d9223fabed8e56c0e6b8ad0c31d98b3507",
|
|
60
|
+
weth: "0x4200000000000000000000000000000000000006",
|
|
61
|
+
usdc: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
|
|
62
|
+
},
|
|
63
|
+
56: {
|
|
64
|
+
name: "bsc",
|
|
65
|
+
poolManager: "0x28e2ea090877bf75740558f6bfb36a5ffee9e9df",
|
|
66
|
+
quoter: "0x9f75dd27d6664c475b90e105573e550ff69437b0",
|
|
67
|
+
stateView: "0xd13dd3d6e93f276fafc9db9e6bb47c1180aee0c4",
|
|
68
|
+
universalRouter: "0x1906c1d672b88cd1b9ac7593301ca990f94eae07",
|
|
69
|
+
weth: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
70
|
+
usdc: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
|
|
71
|
+
},
|
|
72
|
+
137: {
|
|
73
|
+
name: "polygon",
|
|
74
|
+
poolManager: "0x67366782805870060151383f4bbff9dab53e5cd6",
|
|
75
|
+
quoter: "0xb3d5c3dfc3a7aebff71895a7191796bffc2c81b9",
|
|
76
|
+
stateView: "0x5ea1bd7974c8a611cbab0bdcafcb1d9cc9b3ba5a",
|
|
77
|
+
universalRouter: "0x1095692a6237d83c6a72f3f5efedb9a670c49223",
|
|
78
|
+
weth: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
|
|
79
|
+
usdc: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
|
|
80
|
+
},
|
|
81
|
+
8453: {
|
|
82
|
+
name: "base",
|
|
83
|
+
poolManager: "0x498581fF718922c3f8e6A244956aF099B2652b2b",
|
|
84
|
+
quoter: "0x0d5e0f971ed27fbff6c2837bf31316121532048d",
|
|
85
|
+
stateView: "0xa3c0c9b65bad0b08107aa264b0f3db444b867a71",
|
|
86
|
+
universalRouter: "0x6ff5693b99212da76ad316178a184ab56d299b43",
|
|
87
|
+
weth: "0x4200000000000000000000000000000000000006",
|
|
88
|
+
usdc: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
89
|
+
},
|
|
90
|
+
42161: {
|
|
91
|
+
name: "arbitrum",
|
|
92
|
+
poolManager: "0x360e68faccca8ca495c1b759fd9eee466db9fb32",
|
|
93
|
+
quoter: "0x3972c00f7ed4885e145823eb7c655375d275a1c5",
|
|
94
|
+
stateView: "0x76fd297e2d437cd7f76d50f01afe6160f86e9990",
|
|
95
|
+
universalRouter: "0xa51afafe0263b40edaef0df8781ea9aa03e381a3",
|
|
96
|
+
weth: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
97
|
+
usdc: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
98
|
+
},
|
|
99
|
+
43114: {
|
|
100
|
+
name: "avalanche",
|
|
101
|
+
poolManager: "0x06380c0e0912312b5150364b9dc4542ba0dbbc85",
|
|
102
|
+
quoter: "0xbe40675bb704506a3c2ccfb762dcfd1e979845c2",
|
|
103
|
+
stateView: "0xc3c9e198c735a4b97e3e683f391ccbdd60b69286",
|
|
104
|
+
universalRouter: "0x94b75331ae8d42c1b61065089b7d48fe14aa73b7",
|
|
105
|
+
weth: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
|
|
106
|
+
usdc: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
|
|
107
|
+
},
|
|
108
|
+
// Robinhood Chain — V4 is the live book for SQUEEZE and other Doppler launches.
|
|
109
|
+
// PoolManager/Quoter/UniversalRouter proved via V4 Swap/Init log emitters +
|
|
110
|
+
// squeeze-v4 worktree (2026-08-02). usdc slot is USDG, same as V3 adapter.
|
|
111
|
+
4663: {
|
|
112
|
+
name: "robinhood",
|
|
113
|
+
poolManager: "0x8366a39cc670b4001a1121b8f6a443a643e40951",
|
|
114
|
+
quoter: "0x8dc178efb8111bb0973dd9d722ebeff267c98f94",
|
|
115
|
+
stateView: "0xf3334192d15450cdd385c8b70e03f9a6bd9e673b",
|
|
116
|
+
universalRouter: "0x8876789976dEcBfCbBbe364623C63652db8C0904",
|
|
117
|
+
weth: "0x0bd7d308f8e1639fab988df18a8011f41eacad73",
|
|
118
|
+
usdc: "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168",
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Common V4 fee/tickSpacing pairs. Callers can override with explicit fee+tickSpacing.
|
|
123
|
+
export const UNI_V4_FEE_TIERS = [
|
|
124
|
+
{ fee: 100, tickSpacing: 1 },
|
|
125
|
+
{ fee: 500, tickSpacing: 10 },
|
|
126
|
+
{ fee: 3000, tickSpacing: 60 },
|
|
127
|
+
{ fee: 10000, tickSpacing: 200 },
|
|
128
|
+
// Doppler / RH launch pools often use a 2.5% fee with tickSpacing 8.
|
|
129
|
+
{ fee: 25000, tickSpacing: 8 },
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
const QUOTER_IFACE = new Interface([
|
|
133
|
+
"function quoteExactInputSingle(((address currency0,address currency1,uint24 fee,int24 tickSpacing,address hooks) poolKey,bool zeroForOne,uint128 exactAmount,bytes hookData) params) returns (uint256 amountOut, uint256 gasEstimate)",
|
|
134
|
+
]);
|
|
135
|
+
const UNIVERSAL_ROUTER_IFACE = new Interface([
|
|
136
|
+
"function execute(bytes commands,bytes[] inputs,uint256 deadline) payable",
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
// Universal Router command/action constants from Commands.sol and Actions.sol.
|
|
140
|
+
const V4_SWAP = "0x10";
|
|
141
|
+
const V4_ACTIONS = "0x060c0f"; // SWAP_EXACT_IN_SINGLE, SETTLE_ALL, TAKE_ALL
|
|
142
|
+
const PERMIT2 = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
143
|
+
|
|
144
|
+
const NATIVE = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
145
|
+
|
|
146
|
+
function chainMeta(chainId) {
|
|
147
|
+
const id = Number(chainId);
|
|
148
|
+
const m = UNI_V4_CHAINS[id];
|
|
149
|
+
if (!m) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`uniswap-v4: unsupported chainId ${chainId} (supported: ${Object.keys(UNI_V4_CHAINS).join(", ")})`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return { chainId: id, ...m };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function normalizeToken(addr, chainId) {
|
|
158
|
+
const meta = UNI_V4_CHAINS[Number(chainId)];
|
|
159
|
+
if (!addr || addr === NATIVE || addr === ZERO || /^eth$/i.test(String(addr))) {
|
|
160
|
+
return getAddress(meta.weth);
|
|
161
|
+
}
|
|
162
|
+
const s = String(addr);
|
|
163
|
+
if (/^weth$/i.test(s)) return getAddress(meta.weth);
|
|
164
|
+
if (/^usdc$/i.test(s) || /^usdg$/i.test(s)) return getAddress(meta.usdc);
|
|
165
|
+
if (!isAddress(s)) throw new Error(`invalid token address: ${addr}`);
|
|
166
|
+
return getAddress(s);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* V4 poolId = keccak256(abi.encode(currency0, currency1, fee, tickSpacing, hooks))
|
|
171
|
+
* currency0 must be the lower address. Hooks default to address(0).
|
|
172
|
+
*/
|
|
173
|
+
export function computeV4PoolId({ currency0, currency1, fee, tickSpacing, hooks = ZERO }) {
|
|
174
|
+
const a = getAddress(currency0);
|
|
175
|
+
const b = getAddress(currency1);
|
|
176
|
+
if (a.toLowerCase() === b.toLowerCase()) throw new Error("poolKey currencies must differ");
|
|
177
|
+
const [c0, c1] = a.toLowerCase() < b.toLowerCase() ? [a, b] : [b, a];
|
|
178
|
+
const h = hooks && isAddress(hooks) ? getAddress(hooks) : ZERO;
|
|
179
|
+
return keccak256(abi.encode(
|
|
180
|
+
["address", "address", "uint24", "int24", "address"],
|
|
181
|
+
[c0, c1, Number(fee), Number(tickSpacing), h],
|
|
182
|
+
));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function buildPoolKey({ tokenIn, tokenOut, fee, tickSpacing, hooks = ZERO }) {
|
|
186
|
+
const a = getAddress(tokenIn);
|
|
187
|
+
const b = getAddress(tokenOut);
|
|
188
|
+
const [currency0, currency1] = a.toLowerCase() < b.toLowerCase() ? [a, b] : [b, a];
|
|
189
|
+
const zeroForOne = a.toLowerCase() === currency0.toLowerCase();
|
|
190
|
+
return {
|
|
191
|
+
poolKey: {
|
|
192
|
+
currency0,
|
|
193
|
+
currency1,
|
|
194
|
+
fee: Number(fee),
|
|
195
|
+
tickSpacing: Number(tickSpacing),
|
|
196
|
+
hooks: hooks && isAddress(hooks) ? getAddress(hooks) : ZERO,
|
|
197
|
+
},
|
|
198
|
+
zeroForOne,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Quote exact-input single hop via V4Quoter.
|
|
204
|
+
*
|
|
205
|
+
* @param {object} q
|
|
206
|
+
* @param {number} q.chainId
|
|
207
|
+
* @param {string} [q.tokenIn]
|
|
208
|
+
* @param {string} [q.tokenOut]
|
|
209
|
+
* @param {string|number|bigint} q.amountIn
|
|
210
|
+
* @param {number} [q.fee]
|
|
211
|
+
* @param {number} [q.tickSpacing]
|
|
212
|
+
* @param {string} [q.hooks]
|
|
213
|
+
* @param {Array<{fee:number,tickSpacing:number}>} [q.tryTiers]
|
|
214
|
+
*/
|
|
215
|
+
export async function uniV4QuoteExactIn(q = {}, opts = {}) {
|
|
216
|
+
const { chainId, quoter, poolManager, universalRouter } = chainMeta(q.chainId ?? 8453);
|
|
217
|
+
const tokenIn = normalizeToken(q.tokenIn || "WETH", chainId);
|
|
218
|
+
const tokenOut = normalizeToken(q.tokenOut || UNI_V4_CHAINS[chainId].usdc, chainId);
|
|
219
|
+
const amountIn = BigInt(q.amountIn ?? 0);
|
|
220
|
+
if (amountIn <= 0n) throw new Error("amountIn required (raw base units)");
|
|
221
|
+
if (amountIn > 0xffffffffffffffffffffffffffffffffn) {
|
|
222
|
+
throw new Error("amountIn exceeds uint128");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const tiers =
|
|
226
|
+
q.fee != null && q.tickSpacing != null
|
|
227
|
+
? [{ fee: Number(q.fee), tickSpacing: Number(q.tickSpacing) }]
|
|
228
|
+
: q.tryTiers === false
|
|
229
|
+
? [{ fee: 500, tickSpacing: 10 }]
|
|
230
|
+
: [...UNI_V4_FEE_TIERS];
|
|
231
|
+
|
|
232
|
+
const hooks = q.hooks || ZERO;
|
|
233
|
+
const results = [];
|
|
234
|
+
let best = null;
|
|
235
|
+
|
|
236
|
+
for (const tier of tiers) {
|
|
237
|
+
const { poolKey, zeroForOne } = buildPoolKey({
|
|
238
|
+
tokenIn,
|
|
239
|
+
tokenOut,
|
|
240
|
+
fee: tier.fee,
|
|
241
|
+
tickSpacing: tier.tickSpacing,
|
|
242
|
+
hooks,
|
|
243
|
+
});
|
|
244
|
+
const data = QUOTER_IFACE.encodeFunctionData("quoteExactInputSingle", [
|
|
245
|
+
{
|
|
246
|
+
poolKey,
|
|
247
|
+
zeroForOne,
|
|
248
|
+
exactAmount: amountIn,
|
|
249
|
+
hookData: "0x",
|
|
250
|
+
},
|
|
251
|
+
]);
|
|
252
|
+
try {
|
|
253
|
+
const raw = await rpcCall(chainId, "eth_call", [{ to: quoter, data }, "latest"], opts);
|
|
254
|
+
if (!raw || raw === "0x") {
|
|
255
|
+
results.push({ ...tier, ok: false, error: "empty result" });
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const decoded = QUOTER_IFACE.decodeFunctionResult("quoteExactInputSingle", raw);
|
|
259
|
+
const row = {
|
|
260
|
+
fee: tier.fee,
|
|
261
|
+
tickSpacing: tier.tickSpacing,
|
|
262
|
+
ok: true,
|
|
263
|
+
amountOut: decoded[0].toString(),
|
|
264
|
+
gasEstimate: decoded[1].toString(),
|
|
265
|
+
poolKey,
|
|
266
|
+
zeroForOne,
|
|
267
|
+
poolId: computeV4PoolId({ ...poolKey }),
|
|
268
|
+
};
|
|
269
|
+
results.push(row);
|
|
270
|
+
if (!best || BigInt(row.amountOut) > BigInt(best.amountOut)) best = row;
|
|
271
|
+
} catch (e) {
|
|
272
|
+
results.push({ ...tier, ok: false, error: String(e.message || e).slice(0, 160) });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!best) {
|
|
277
|
+
const err = results.map((r) => `${r.fee}/${r.tickSpacing}:${r.error || "fail"}`).join("; ");
|
|
278
|
+
throw new Error(`uniswap-v4 quote failed chain=${chainId}: ${err}`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return attachAutoSlippage(
|
|
282
|
+
{
|
|
283
|
+
venue: "uniswap-v4",
|
|
284
|
+
chainId,
|
|
285
|
+
chain: UNI_V4_CHAINS[chainId].name,
|
|
286
|
+
poolManager,
|
|
287
|
+
quoter,
|
|
288
|
+
universalRouter: universalRouter || null,
|
|
289
|
+
tokenIn,
|
|
290
|
+
tokenOut,
|
|
291
|
+
amountIn: amountIn.toString(),
|
|
292
|
+
fee: best.fee,
|
|
293
|
+
tickSpacing: best.tickSpacing,
|
|
294
|
+
hooks: best.poolKey.hooks,
|
|
295
|
+
poolId: best.poolId,
|
|
296
|
+
zeroForOne: best.zeroForOne,
|
|
297
|
+
amountOut: best.amountOut,
|
|
298
|
+
gasEstimate: best.gasEstimate,
|
|
299
|
+
triedTiers: results,
|
|
300
|
+
best: true,
|
|
301
|
+
executionReady: false,
|
|
302
|
+
// A successful quote discovered the complete PoolKey. Preparation is only
|
|
303
|
+
// advertised when its remaining safety inputs were explicit.
|
|
304
|
+
prepareReady:
|
|
305
|
+
typeof q.recipient === "string" && isAddress(q.recipient) && q.slippageBps != null,
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
chainId,
|
|
309
|
+
venue: universalRouter || quoter,
|
|
310
|
+
amountOut: best.amountOut,
|
|
311
|
+
liquidityUsd: q.liquidityUsd,
|
|
312
|
+
priceChange5m: q.priceChange5m,
|
|
313
|
+
requestedCapBps: q.maxSlippageBps ?? q.slippageBps,
|
|
314
|
+
},
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Prepare (never sign or broadcast) a V4 exact-input, single-hop swap.
|
|
320
|
+
* A fresh successful quote is the source of truth for the complete PoolKey.
|
|
321
|
+
* ERC-20 input only: native wrapping/unwrapping is intentionally not inferred.
|
|
322
|
+
*/
|
|
323
|
+
export async function uniV4PrepareExactIn(q = {}, opts = {}) {
|
|
324
|
+
const chainId = Number(q.chainId ?? 8453);
|
|
325
|
+
const meta = chainMeta(chainId);
|
|
326
|
+
if (!q.recipient) throw new Error("uniswap-v4 prepare requires recipient");
|
|
327
|
+
const recipient = getAddress(q.recipient);
|
|
328
|
+
if (q.slippageBps == null) {
|
|
329
|
+
throw new Error("uniswap-v4 prepare requires explicit slippageBps");
|
|
330
|
+
}
|
|
331
|
+
const slippageBps = Number(q.slippageBps);
|
|
332
|
+
if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps >= 10_000) {
|
|
333
|
+
throw new Error("slippageBps must be an integer from 0 through 9999");
|
|
334
|
+
}
|
|
335
|
+
const rawInput = String(q.tokenIn || "");
|
|
336
|
+
if (!rawInput || rawInput === NATIVE || rawInput === ZERO || /^eth$/i.test(rawInput)) {
|
|
337
|
+
throw new Error("uniswap-v4 prepare currently requires an ERC-20 tokenIn; native input is not inferred");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const quote = await uniV4QuoteExactIn(q, opts);
|
|
341
|
+
if (!meta.universalRouter) throw new Error(`uniswap-v4: no Universal Router on chain ${chainId}`);
|
|
342
|
+
const amountOutMinimum = (BigInt(quote.amountOut) * BigInt(10_000 - slippageBps)) / 10_000n;
|
|
343
|
+
const poolKey = {
|
|
344
|
+
currency0: quote.zeroForOne ? quote.tokenIn : quote.tokenOut,
|
|
345
|
+
currency1: quote.zeroForOne ? quote.tokenOut : quote.tokenIn,
|
|
346
|
+
fee: quote.fee,
|
|
347
|
+
tickSpacing: quote.tickSpacing,
|
|
348
|
+
hooks: quote.hooks,
|
|
349
|
+
};
|
|
350
|
+
const minHopPriceX36 = 0n;
|
|
351
|
+
const swap = abi.encode(
|
|
352
|
+
["tuple(tuple(address currency0,address currency1,uint24 fee,int24 tickSpacing,address hooks) poolKey,bool zeroForOne,uint128 amountIn,uint128 amountOutMinimum,uint256 minHopPriceX36,bytes hookData)"],
|
|
353
|
+
[[poolKey, quote.zeroForOne, BigInt(quote.amountIn), amountOutMinimum, minHopPriceX36, "0x"]],
|
|
354
|
+
);
|
|
355
|
+
const settle = abi.encode(["address", "uint256"], [quote.tokenIn, BigInt(quote.amountIn)]);
|
|
356
|
+
const take = abi.encode(["address", "uint256"], [quote.tokenOut, amountOutMinimum]);
|
|
357
|
+
const plan = abi.encode(["bytes", "bytes[]"], [V4_ACTIONS, [swap, settle, take]]);
|
|
358
|
+
const deadlineWindow = Math.min(600, Math.max(30, Number(q.deadlineSeconds ?? 300)));
|
|
359
|
+
const nowSeconds = Number(opts.nowSeconds ?? Math.floor(Date.now() / 1000));
|
|
360
|
+
const deadline = nowSeconds + deadlineWindow;
|
|
361
|
+
const data = UNIVERSAL_ROUTER_IFACE.encodeFunctionData("execute", [V4_SWAP, [plan], deadline]);
|
|
362
|
+
|
|
363
|
+
return stampPrepared({
|
|
364
|
+
provider: "uniswap-v4",
|
|
365
|
+
prepareReady: true,
|
|
366
|
+
calldataReady: true,
|
|
367
|
+
executionReady: false,
|
|
368
|
+
requiresUserSignature: true,
|
|
369
|
+
signingReady: false,
|
|
370
|
+
broadcastReady: false,
|
|
371
|
+
chainId,
|
|
372
|
+
deadline,
|
|
373
|
+
quote: { ...quote, amountOutMinimum: amountOutMinimum.toString(), slippageBps },
|
|
374
|
+
requiresApproval: {
|
|
375
|
+
token: quote.tokenIn,
|
|
376
|
+
spender: PERMIT2,
|
|
377
|
+
amount: quote.amountIn,
|
|
378
|
+
note: "Universal Router V4 settlement uses Permit2; Permit2 authorization for the Universal Router is also required",
|
|
379
|
+
},
|
|
380
|
+
transaction: {
|
|
381
|
+
chainId,
|
|
382
|
+
from: recipient,
|
|
383
|
+
to: meta.universalRouter,
|
|
384
|
+
data,
|
|
385
|
+
value: "0",
|
|
386
|
+
},
|
|
387
|
+
}, { provider: "uniswap-v4", kind: "univ4-swap-tx" });
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export async function uniV4Chains() {
|
|
391
|
+
return Object.entries(UNI_V4_CHAINS).map(([id, m]) => ({
|
|
392
|
+
chainId: Number(id),
|
|
393
|
+
name: m.name,
|
|
394
|
+
poolManager: m.poolManager,
|
|
395
|
+
quoter: m.quoter,
|
|
396
|
+
stateView: m.stateView || null,
|
|
397
|
+
universalRouter: m.universalRouter || null,
|
|
398
|
+
weth: m.weth,
|
|
399
|
+
usdc: m.usdc,
|
|
400
|
+
}));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export async function uniV4Health(opts = {}) {
|
|
404
|
+
const meta = chainMeta(opts.chainId ?? 8453);
|
|
405
|
+
const code = await rpcCall(meta.chainId, "eth_getCode", [meta.quoter, "latest"], opts);
|
|
406
|
+
const ok = typeof code === "string" && code !== "0x" && code.length > 4;
|
|
407
|
+
return {
|
|
408
|
+
ok,
|
|
409
|
+
chainId: meta.chainId,
|
|
410
|
+
quoter: meta.quoter,
|
|
411
|
+
poolManager: meta.poolManager,
|
|
412
|
+
codeBytes: ok ? (code.length - 2) / 2 : 0,
|
|
413
|
+
};
|
|
414
|
+
}
|
package/src/prepare-envelope.mjs
CHANGED
|
@@ -34,13 +34,20 @@ function stableStringify(value) {
|
|
|
34
34
|
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(",")}}`;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function
|
|
38
|
-
const s = String(env
|
|
37
|
+
function secretFrom(env) {
|
|
38
|
+
const s = String(env?.ORACLE_STAMP_HMAC_SECRET || "").trim();
|
|
39
39
|
return s.length >= 16 ? s : "";
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
function stampSecret(env = process.env) {
|
|
43
|
+
// The signer process owns its policy. An options env is useful for isolated
|
|
44
|
+
// tests and unkeyed clients, but cannot shadow a real process secret.
|
|
45
|
+
return secretFrom(process.env) || secretFrom(env);
|
|
46
|
+
}
|
|
47
|
+
|
|
42
48
|
function requireMac(env = process.env) {
|
|
43
|
-
return String(env.ORACLE_STAMP_REQUIRE_MAC || "").trim() === "1"
|
|
49
|
+
return String(process.env.ORACLE_STAMP_REQUIRE_MAC || "").trim() === "1" ||
|
|
50
|
+
String(env?.ORACLE_STAMP_REQUIRE_MAC || "").trim() === "1";
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
/** Fields that bind the envelope (must not include the stamp fields themselves). */
|
|
@@ -139,7 +146,18 @@ export function assertPreparedEnvelope(prepared, {
|
|
|
139
146
|
|
|
140
147
|
const secret = stampSecret(env);
|
|
141
148
|
const mac = String(prepared.prepareMac || "");
|
|
142
|
-
|
|
149
|
+
// The MAC used to be enforced only when it was PRESENT or when
|
|
150
|
+
// ORACLE_STAMP_REQUIRE_MAC=1. That is the classic optional-MAC downgrade:
|
|
151
|
+
// prepareHash is an unkeyed sha256 an attacker can recompute via the
|
|
152
|
+
// exported computePrepareHash, so stripping prepareMac and recomputing the
|
|
153
|
+
// hash let a mutated payload pass a default-posture verifier.
|
|
154
|
+
//
|
|
155
|
+
// A verifier that HOLDS the stamp secret is by definition in keyed mode, so
|
|
156
|
+
// it now requires the MAC rather than treating its absence as opt-out. An
|
|
157
|
+
// attacker can still strip the field, but they cannot forge it, and a
|
|
158
|
+
// verifier with no secret configured is unchanged (checksum-only, exactly as
|
|
159
|
+
// documented — the signer's own policy remains the authoritative wall).
|
|
160
|
+
if (requireMac(env) || mac || secret) {
|
|
143
161
|
if (!secret) {
|
|
144
162
|
throw new Error(
|
|
145
163
|
`${label}: prepareMac present or ORACLE_STAMP_REQUIRE_MAC=1 but ORACLE_STAMP_HMAC_SECRET missing in signer`,
|
|
@@ -68,6 +68,23 @@ const PREPARERS = {
|
|
|
68
68
|
};
|
|
69
69
|
},
|
|
70
70
|
|
|
71
|
+
"uniswap-v4": async ({ chainId, tokenIn, tokenOut, amountIn, taker, slippageBps, fee, tickSpacing, hooks }, opts) => {
|
|
72
|
+
const { uniV4PrepareExactIn } = await import("../data/providers/uniswap-v4.mjs");
|
|
73
|
+
const p = await uniV4PrepareExactIn(
|
|
74
|
+
{ chainId, tokenIn, tokenOut, amountIn, recipient: taker, slippageBps, fee, tickSpacing, hooks },
|
|
75
|
+
opts,
|
|
76
|
+
);
|
|
77
|
+
return {
|
|
78
|
+
artifactKind: ARTIFACT.TRANSACTION,
|
|
79
|
+
transaction: p.transaction,
|
|
80
|
+
requiresApproval: p.requiresApproval,
|
|
81
|
+
amountOut: p.quote?.amountOut ?? null,
|
|
82
|
+
minOut: p.quote?.amountOutMinimum ?? null,
|
|
83
|
+
prepareReady: true,
|
|
84
|
+
executionReady: false,
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
|
|
71
88
|
lifi: async ({ chainId, tokenIn, tokenOut, amountIn, taker }, opts) => {
|
|
72
89
|
const { lifiPrepare } = await import("../data/providers/lifi.mjs");
|
|
73
90
|
const p = await lifiPrepare(
|