@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,200 @@
|
|
|
1
|
+
// Curve public REST API + RouterNG single-hop exact-input prepare. No key.
|
|
2
|
+
import { Interface, getAddress, isAddress } from "ethers";
|
|
3
|
+
import { httpJson } from "../http.mjs";
|
|
4
|
+
import { rpcCall as defaultRpcCall } from "./evm-rpc.mjs";
|
|
5
|
+
import { attachAutoSlippage, bindAutoSlippageGuardToCall } from "../../auto-slippage.mjs";
|
|
6
|
+
|
|
7
|
+
export const CURVE_API = "https://api.curve.finance/v1";
|
|
8
|
+
const base = (o = {}) => (o.baseUrl || process.env.CURVE_API_URL || CURVE_API).replace(/\/$/, "");
|
|
9
|
+
|
|
10
|
+
// Official Curve RouterNG deployments from docs.curve.finance router/zaps +
|
|
11
|
+
// CurveRouterNG docs. Bytecode is checked before allowlisting.
|
|
12
|
+
export const CURVE_ROUTER_NG_BY_CHAIN = Object.freeze({
|
|
13
|
+
1: "0x45312ea0eFf7E09C83CBE249fa1d7598c4C8cd4e",
|
|
14
|
+
10: "0x0DCDED3545D565bA3B19E683431381007245d983",
|
|
15
|
+
56: "0xA72C85C258A81761433B4e8da60505Fe3Dd551CC",
|
|
16
|
+
137: "0x0DCDED3545D565bA3B19E683431381007245d983",
|
|
17
|
+
988: "0xFF5Cb29241F002fFeD2eAa224e3e996D24A6E8d1",
|
|
18
|
+
999: "0xd2002373543Ce3527023C75e7518C274A51ce712",
|
|
19
|
+
4663: "0xFF5Cb29241F002fFeD2eAa224e3e996D24A6E8d1",
|
|
20
|
+
8453: "0x4f37A9d177470499A2dD084621020b023fcffc1F",
|
|
21
|
+
42161: "0x2191718CD32d02B8E60BAdFFeA33E4B5DD9A0A0D",
|
|
22
|
+
43114: "0x0DCDED3545D565bA3B19E683431381007245d983",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const ZERO = "0x0000000000000000000000000000000000000000";
|
|
26
|
+
const ROUTER_IFACE = new Interface([
|
|
27
|
+
"function get_dy(address[11] route,uint256[5][5] swap_params,uint256 amount,address[5] pools) view returns (uint256)",
|
|
28
|
+
"function exchange(address[11] route,uint256[5][5] swap_params,uint256 amount,uint256 expected,address[5] pools,address receiver) payable returns (uint256)",
|
|
29
|
+
]);
|
|
30
|
+
const POOL_IFACE = new Interface(["function coins(uint256) view returns (address)"]);
|
|
31
|
+
|
|
32
|
+
function routerFor(chainId) {
|
|
33
|
+
const router = CURVE_ROUTER_NG_BY_CHAIN[Number(chainId)];
|
|
34
|
+
if (!router) throw new Error(`curve-router-ng: unsupported chainId ${chainId}`);
|
|
35
|
+
return router;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeAddress(value, label) {
|
|
39
|
+
if (!isAddress(value)) throw new Error(`${label} must be a valid EVM address`);
|
|
40
|
+
return getAddress(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function positiveAmount(value, label) {
|
|
44
|
+
let amount;
|
|
45
|
+
try {
|
|
46
|
+
amount = BigInt(String(value));
|
|
47
|
+
} catch {
|
|
48
|
+
throw new Error(`${label} must be an integer string`);
|
|
49
|
+
}
|
|
50
|
+
if (amount <= 0n) throw new Error(`${label} must be positive`);
|
|
51
|
+
return amount;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function integer(value, label, min, max) {
|
|
55
|
+
const n = Number(value);
|
|
56
|
+
if (!Number.isInteger(n) || n < min || n > max) throw new Error(`${label} must be an integer ${min}..${max}`);
|
|
57
|
+
return n;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function routeArray(tokenIn, pool, tokenOut) {
|
|
61
|
+
return [tokenIn, pool, tokenOut, ...Array(8).fill(ZERO)];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function swapParams(i, j, poolType, nCoins) {
|
|
65
|
+
return [[BigInt(i), BigInt(j), 1n, BigInt(poolType), BigInt(nCoins)], ...Array.from({ length: 4 }, () => [0n, 0n, 0n, 0n, 0n])];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function poolsArray(pool) {
|
|
69
|
+
return [pool, ...Array(4).fill(ZERO)];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function poolCoins({ chainId, pool, nCoins, rpcCall, opts }) {
|
|
73
|
+
const coins = [];
|
|
74
|
+
for (let i = 0; i < nCoins; i++) {
|
|
75
|
+
const data = POOL_IFACE.encodeFunctionData("coins", [i]);
|
|
76
|
+
const raw = await rpcCall(chainId, "eth_call", [{ to: pool, data }, opts.blockTag || "latest"], opts);
|
|
77
|
+
const decoded = POOL_IFACE.decodeFunctionResult("coins", raw);
|
|
78
|
+
coins.push(getAddress(decoded[0]));
|
|
79
|
+
}
|
|
80
|
+
return coins;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function findIndex(coins, token, label) {
|
|
84
|
+
const target = token.toLowerCase();
|
|
85
|
+
const idx = coins.findIndex((coin) => coin.toLowerCase() === target);
|
|
86
|
+
if (idx < 0) throw new Error(`Curve pool does not contain ${label}`);
|
|
87
|
+
return idx;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function curveOpenApi(opts = {}) {
|
|
91
|
+
return httpJson(`${base(opts)}/openapi.json`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function curvePools(args = {}, opts = {}) {
|
|
95
|
+
const chain = String(args.chain || args.network || "ethereum").toLowerCase();
|
|
96
|
+
const registry = String(args.registry || "main").toLowerCase();
|
|
97
|
+
if (!/^[a-z0-9-]+$/.test(chain) || !/^[a-z0-9-]+$/.test(registry)) throw new Error("invalid Curve chain or registry");
|
|
98
|
+
return httpJson(`${base(opts)}/getPools/${chain}/${registry}`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs ?? 20_000 });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function curveHealth(opts = {}) {
|
|
102
|
+
const data = await curvePools({ chain: "ethereum", registry: "main" }, opts);
|
|
103
|
+
const count = data?.data?.poolData?.length || 0;
|
|
104
|
+
return { ok: data?.success === true && count > 0, poolSample: count };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function curveQuote(q = {}, opts = {}) {
|
|
108
|
+
const chainId = Number(q.chainId ?? 8453);
|
|
109
|
+
const router = routerFor(chainId);
|
|
110
|
+
const pool = normalizeAddress(q.pool, "curve pool");
|
|
111
|
+
const tokenIn = normalizeAddress(q.tokenIn, "curve tokenIn");
|
|
112
|
+
const tokenOut = normalizeAddress(q.tokenOut, "curve tokenOut");
|
|
113
|
+
const amountIn = positiveAmount(q.amountIn, "curve amountIn");
|
|
114
|
+
const nCoins = integer(q.nCoins ?? 2, "curve nCoins", 2, 8);
|
|
115
|
+
const poolType = integer(q.poolType, "curve poolType", 1, 4);
|
|
116
|
+
const rpcCall = opts.rpcCall || defaultRpcCall;
|
|
117
|
+
const coins = await poolCoins({ chainId, pool, nCoins, rpcCall, opts });
|
|
118
|
+
const i = findIndex(coins, tokenIn, "tokenIn");
|
|
119
|
+
const j = findIndex(coins, tokenOut, "tokenOut");
|
|
120
|
+
if (i === j) throw new Error("Curve token indexes must differ");
|
|
121
|
+
const route = routeArray(tokenIn, pool, tokenOut);
|
|
122
|
+
const params = swapParams(i, j, poolType, nCoins);
|
|
123
|
+
const pools = poolsArray(pool);
|
|
124
|
+
const data = ROUTER_IFACE.encodeFunctionData("get_dy", [route, params, amountIn, pools]);
|
|
125
|
+
const raw = await rpcCall(chainId, "eth_call", [{ to: router, data }, q.blockTag || "latest"], opts);
|
|
126
|
+
const decoded = ROUTER_IFACE.decodeFunctionResult("get_dy", raw);
|
|
127
|
+
const amountOut = BigInt(decoded[0]);
|
|
128
|
+
if (amountOut <= 0n) throw new Error("curve quote returned zero output");
|
|
129
|
+
return attachAutoSlippage({
|
|
130
|
+
venue: "curve",
|
|
131
|
+
router,
|
|
132
|
+
chainId,
|
|
133
|
+
pool,
|
|
134
|
+
tokenIn,
|
|
135
|
+
tokenOut,
|
|
136
|
+
amountIn: amountIn.toString(),
|
|
137
|
+
amountOut: amountOut.toString(),
|
|
138
|
+
nCoins,
|
|
139
|
+
poolType,
|
|
140
|
+
swapType: 1,
|
|
141
|
+
i,
|
|
142
|
+
j,
|
|
143
|
+
route,
|
|
144
|
+
swapParams: params.map((row) => row.map((x) => x.toString())),
|
|
145
|
+
pools,
|
|
146
|
+
}, {
|
|
147
|
+
chainId,
|
|
148
|
+
venue: router,
|
|
149
|
+
amountOut: amountOut.toString(),
|
|
150
|
+
liquidityUsd: q.liquidityUsd,
|
|
151
|
+
priceChange5m: q.priceChange5m,
|
|
152
|
+
requestedCapBps: q.maxSlippageBps ?? q.slippageBps,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function curvePrepare(q = {}, opts = {}) {
|
|
157
|
+
const chainId = Number(q.chainId ?? 8453);
|
|
158
|
+
const router = routerFor(chainId);
|
|
159
|
+
const sender = normalizeAddress(q.sender || q.from || q.fromAddress || q.receiver, "curve sender");
|
|
160
|
+
const receiver = normalizeAddress(q.receiver || q.recipient || sender, "curve receiver");
|
|
161
|
+
const quote = await curveQuote({ ...q, sender }, opts);
|
|
162
|
+
const route = routeArray(quote.tokenIn, quote.pool, quote.tokenOut);
|
|
163
|
+
const params = swapParams(quote.i, quote.j, quote.poolType, quote.nCoins);
|
|
164
|
+
const pools = poolsArray(quote.pool);
|
|
165
|
+
const data = ROUTER_IFACE.encodeFunctionData("exchange", [
|
|
166
|
+
route,
|
|
167
|
+
params,
|
|
168
|
+
BigInt(quote.amountIn),
|
|
169
|
+
BigInt(quote.amountOutMinimum),
|
|
170
|
+
pools,
|
|
171
|
+
receiver,
|
|
172
|
+
]);
|
|
173
|
+
return {
|
|
174
|
+
provider: "curve",
|
|
175
|
+
calldataReady: true,
|
|
176
|
+
// NOT executable authority. The bytes are assembled but nothing is signed:
|
|
177
|
+
// the user's wallet is still the only thing that can authorize this. The
|
|
178
|
+
// previous `executionReady: true` read as "cleared to execute" and trained
|
|
179
|
+
// agents to treat a quote as permission.
|
|
180
|
+
requiresUserSignature: true,
|
|
181
|
+
signingReady: false,
|
|
182
|
+
broadcastReady: false,
|
|
183
|
+
chainId,
|
|
184
|
+
quote,
|
|
185
|
+
autoSlippage: quote.autoSlippage,
|
|
186
|
+
requiresApproval: {
|
|
187
|
+
token: quote.tokenIn,
|
|
188
|
+
spender: router,
|
|
189
|
+
amount: quote.amountIn,
|
|
190
|
+
},
|
|
191
|
+
transaction: {
|
|
192
|
+
chainId,
|
|
193
|
+
from: sender,
|
|
194
|
+
to: router,
|
|
195
|
+
data,
|
|
196
|
+
value: "0",
|
|
197
|
+
slippageGuard: bindAutoSlippageGuardToCall(quote.autoSlippage, { chainId, venue: router, data }),
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// DeFiLlama public prices — protocol pack D3 (no API key).
|
|
2
|
+
|
|
3
|
+
import { httpJson } from "../http.mjs";
|
|
4
|
+
|
|
5
|
+
export const DEFILLAMA_COINS = "https://coins.llama.fi";
|
|
6
|
+
export const DEFILLAMA_API = "https://api.llama.fi";
|
|
7
|
+
export const DEFILLAMA_YIELDS = "https://yields.llama.fi";
|
|
8
|
+
export const DEFILLAMA_STABLECOINS = "https://stablecoins.llama.fi";
|
|
9
|
+
|
|
10
|
+
function coinsBase(opts = {}) {
|
|
11
|
+
return (opts.baseUrl || process.env.DEFILLAMA_COINS_URL || DEFILLAMA_COINS).replace(/\/$/, "");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function apiBase(opts = {}) {
|
|
15
|
+
return (opts.baseUrl || process.env.DEFILLAMA_API_URL || DEFILLAMA_API).replace(/\/$/, "");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @param {string[]} coins e.g. ['coingecko:ethereum','coingecko:bitcoin'] */
|
|
19
|
+
export async function llamaPrices(coins, opts = {}) {
|
|
20
|
+
if (!Array.isArray(coins) || !coins.length) throw new Error("llamaPrices requires coins[]");
|
|
21
|
+
const path = coins.map(encodeURIComponent).join(",");
|
|
22
|
+
return httpJson(`${coinsBase(opts)}/prices/current/${path}`, {
|
|
23
|
+
fetchImpl: opts.fetchImpl,
|
|
24
|
+
timeoutMs: opts.timeoutMs,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function llamaHealth(opts = {}) {
|
|
29
|
+
const data = await llamaPrices(["coingecko:ethereum"], opts);
|
|
30
|
+
const eth = data?.coins?.["coingecko:ethereum"];
|
|
31
|
+
return { ok: !!eth?.price, ethUsd: eth?.price ?? null };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function llamaProtocols(opts = {}) {
|
|
35
|
+
return httpJson(`${apiBase(opts)}/protocols`, {
|
|
36
|
+
fetchImpl: opts.fetchImpl,
|
|
37
|
+
timeoutMs: opts.timeoutMs ?? 20_000,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function llamaYields(args = {}, opts = {}) {
|
|
42
|
+
const base = (opts.yieldsBaseUrl || process.env.DEFILLAMA_YIELDS_URL || DEFILLAMA_YIELDS).replace(/\/$/, "");
|
|
43
|
+
const result = await httpJson(`${base}/pools`, {
|
|
44
|
+
fetchImpl: opts.fetchImpl,
|
|
45
|
+
timeoutMs: opts.timeoutMs ?? 30_000,
|
|
46
|
+
});
|
|
47
|
+
let rows = Array.isArray(result?.data) ? result.data : [];
|
|
48
|
+
if (args.chain) rows = rows.filter((x) => String(x.chain).toLowerCase() === String(args.chain).toLowerCase());
|
|
49
|
+
if (args.project) rows = rows.filter((x) => String(x.project).toLowerCase() === String(args.project).toLowerCase());
|
|
50
|
+
if (args.stablecoin != null) rows = rows.filter((x) => Boolean(x.stablecoin) === Boolean(args.stablecoin));
|
|
51
|
+
rows.sort((a, b) => Number(b.tvlUsd || 0) - Number(a.tvlUsd || 0));
|
|
52
|
+
const limit = Math.min(5000, Math.max(1, Number(args.limit || rows.length || 1)));
|
|
53
|
+
return { ...result, data: rows.slice(0, limit) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function llamaStablecoins(args = {}, opts = {}) {
|
|
57
|
+
const base = (opts.stablecoinsBaseUrl || process.env.DEFILLAMA_STABLECOINS_URL || DEFILLAMA_STABLECOINS).replace(/\/$/, "");
|
|
58
|
+
const includePrices = args.includePrices !== false;
|
|
59
|
+
return httpJson(`${base}/stablecoins?includePrices=${includePrices}`, {
|
|
60
|
+
fetchImpl: opts.fetchImpl,
|
|
61
|
+
timeoutMs: opts.timeoutMs ?? 30_000,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function llamaDexVolumes(args = {}, opts = {}) {
|
|
66
|
+
const chain = encodeURIComponent(String(args.chain || "Ethereum"));
|
|
67
|
+
const params = new URLSearchParams({
|
|
68
|
+
excludeTotalDataChart: String(args.includeChart !== true),
|
|
69
|
+
excludeTotalDataChartBreakdown: String(args.includeBreakdown !== true),
|
|
70
|
+
dataType: "dailyVolume",
|
|
71
|
+
});
|
|
72
|
+
return httpJson(`${apiBase(opts)}/overview/dexs/${chain}?${params}`, {
|
|
73
|
+
fetchImpl: opts.fetchImpl,
|
|
74
|
+
timeoutMs: opts.timeoutMs ?? 30_000,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Convenience: map symbols via coingecko ids */
|
|
79
|
+
export async function llamaPricesBySymbol(symbols = ["ethereum", "bitcoin", "solana"], opts = {}) {
|
|
80
|
+
const coins = symbols.map((s) => `coingecko:${String(s).toLowerCase()}`);
|
|
81
|
+
const data = await llamaPrices(coins, opts);
|
|
82
|
+
const out = {};
|
|
83
|
+
for (const s of symbols) {
|
|
84
|
+
const key = `coingecko:${String(s).toLowerCase()}`;
|
|
85
|
+
out[s] = data?.coins?.[key] || null;
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// DexScreener public API — token/pair discovery, no key.
|
|
2
|
+
|
|
3
|
+
import { httpJson } from "../http.mjs";
|
|
4
|
+
|
|
5
|
+
export const DEXSCREENER_API = "https://api.dexscreener.com";
|
|
6
|
+
|
|
7
|
+
function base(opts = {}) {
|
|
8
|
+
return (opts.baseUrl || process.env.DEXSCREENER_API_URL || DEXSCREENER_API).replace(/\/$/, "");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function dexscreenerHealth(opts = {}) {
|
|
12
|
+
// tokenless root sometimes 404 — use a known stablecoin query
|
|
13
|
+
const data = await httpJson(
|
|
14
|
+
`${base(opts)}/latest/dex/search?q=USDC`,
|
|
15
|
+
{ fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs ?? 12_000 }
|
|
16
|
+
);
|
|
17
|
+
const n = data?.pairs?.length || 0;
|
|
18
|
+
return { ok: n > 0, pairSample: n };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function dexscreenerToken(tokenAddress, opts = {}) {
|
|
22
|
+
if (!tokenAddress) throw new Error("tokenAddress required");
|
|
23
|
+
return httpJson(`${base(opts)}/latest/dex/tokens/${tokenAddress}`, {
|
|
24
|
+
fetchImpl: opts.fetchImpl,
|
|
25
|
+
timeoutMs: opts.timeoutMs ?? 12_000,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function dexscreenerSearch(q, opts = {}) {
|
|
30
|
+
if (!q) throw new Error("query required");
|
|
31
|
+
return httpJson(`${base(opts)}/latest/dex/search?q=${encodeURIComponent(q)}`, {
|
|
32
|
+
fetchImpl: opts.fetchImpl,
|
|
33
|
+
timeoutMs: opts.timeoutMs ?? 12_000,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function dexscreenerPair(chainId, pairAddress, opts = {}) {
|
|
38
|
+
if (!chainId || !pairAddress) throw new Error("chainId and pairAddress required");
|
|
39
|
+
return httpJson(`${base(opts)}/latest/dex/pairs/${chainId}/${pairAddress}`, {
|
|
40
|
+
fetchImpl: opts.fetchImpl,
|
|
41
|
+
timeoutMs: opts.timeoutMs ?? 12_000,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// EVM JSON-RPC read pack — multi-chain balances/blocks via public or env RPCs.
|
|
2
|
+
// No wallet keys. Uses chain registry rpcEnv + optional public fallbacks.
|
|
3
|
+
|
|
4
|
+
import { httpJson } from "../http.mjs";
|
|
5
|
+
import { CHAINS, chainById, rpcUrlFor, isSupportedChain } from "../../chains.mjs";
|
|
6
|
+
import { Interface, getAddress } from "ethers";
|
|
7
|
+
|
|
8
|
+
const ERC20 = new Interface([
|
|
9
|
+
"function allowance(address owner,address spender) view returns (uint256)",
|
|
10
|
+
"function balanceOf(address account) view returns (uint256)",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
function normalizeAddress(value, label) {
|
|
14
|
+
try {
|
|
15
|
+
return getAddress(value).toLowerCase();
|
|
16
|
+
} catch {
|
|
17
|
+
throw new Error(`${label} must be a valid EVM address`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeTxHash(value) {
|
|
22
|
+
const hash = String(value || "").trim();
|
|
23
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(hash)) throw new Error("txHash must be a 32-byte hex transaction hash");
|
|
24
|
+
return hash.toLowerCase();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function parseHexInt(value) {
|
|
28
|
+
if (value == null) return null;
|
|
29
|
+
return Number.parseInt(String(value), 16);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Public fallbacks when env RPC unset — rate-limited, fine for health/read. */
|
|
33
|
+
export const PUBLIC_RPC_FALLBACKS = {
|
|
34
|
+
1: "https://ethereum.publicnode.com",
|
|
35
|
+
137: "https://polygon-bor-rpc.publicnode.com",
|
|
36
|
+
42161: "https://arbitrum-one-rpc.publicnode.com",
|
|
37
|
+
8453: "https://base-rpc.publicnode.com",
|
|
38
|
+
10: "https://optimism-rpc.publicnode.com",
|
|
39
|
+
988: "https://rpc.stable.xyz",
|
|
40
|
+
56: "https://bsc-rpc.publicnode.com",
|
|
41
|
+
43114: "https://avalanche-c-chain-rpc.publicnode.com",
|
|
42
|
+
999: "https://rpc.hyperliquid.xyz/evm",
|
|
43
|
+
2741: "https://api.mainnet.abs.xyz",
|
|
44
|
+
4663: "https://rpc.mainnet.chain.robinhood.com",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Resolve RPC URL for a chainId: explicit > env (registry) > public fallback.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveRpcUrl(chainId, { rpcUrl, env = process.env } = {}) {
|
|
51
|
+
if (rpcUrl) return rpcUrl;
|
|
52
|
+
const fromEnv = rpcUrlFor(chainId, env);
|
|
53
|
+
if (fromEnv) return fromEnv;
|
|
54
|
+
return PUBLIC_RPC_FALLBACKS[Number(chainId)] || null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* JSON-RPC call
|
|
59
|
+
* @param {number} chainId
|
|
60
|
+
* @param {string} method
|
|
61
|
+
* @param {any[]} [params]
|
|
62
|
+
*/
|
|
63
|
+
export async function rpcCall(chainId, method, params = [], opts = {}) {
|
|
64
|
+
const url = resolveRpcUrl(chainId, opts);
|
|
65
|
+
if (!url) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`No RPC URL for chainId ${chainId} (set env from chain registry rpcEnv or pass rpcUrl)`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const body = { jsonrpc: "2.0", id: 1, method, params };
|
|
71
|
+
const res = await httpJson(url, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
body,
|
|
74
|
+
fetchImpl: opts.fetchImpl,
|
|
75
|
+
timeoutMs: opts.timeoutMs ?? 12_000,
|
|
76
|
+
});
|
|
77
|
+
if (res?.error) {
|
|
78
|
+
const err = new Error(res.error.message || JSON.stringify(res.error));
|
|
79
|
+
err.code = res.error.code;
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
return res.result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function rpcHealth(chainId, opts = {}) {
|
|
86
|
+
const blockHex = await rpcCall(chainId, "eth_blockNumber", [], opts);
|
|
87
|
+
const block = parseInt(blockHex, 16);
|
|
88
|
+
const idHex = await rpcCall(chainId, "eth_chainId", [], opts);
|
|
89
|
+
const reported = parseInt(idHex, 16);
|
|
90
|
+
return {
|
|
91
|
+
ok: Number.isFinite(block) && reported === Number(chainId),
|
|
92
|
+
chainId: Number(chainId),
|
|
93
|
+
reportedChainId: reported,
|
|
94
|
+
blockNumber: block,
|
|
95
|
+
name: chainById(chainId)?.name || null,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function rpcBlockNumber(chainId, opts = {}) {
|
|
100
|
+
const hex = await rpcCall(chainId, "eth_blockNumber", [], opts);
|
|
101
|
+
return parseInt(hex, 16);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function rpcChainId(chainId, opts = {}) {
|
|
105
|
+
const hex = await rpcCall(chainId, "eth_chainId", [], opts);
|
|
106
|
+
return parseInt(hex, 16);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function rpcGetBalance(chainId, address, opts = {}) {
|
|
110
|
+
if (!address) throw new Error("rpcGetBalance requires address");
|
|
111
|
+
const hex = await rpcCall(chainId, "eth_getBalance", [address, "latest"], opts);
|
|
112
|
+
return { chainId: Number(chainId), address, balanceWei: BigInt(hex).toString(), balanceHex: hex };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function erc20Allowance(args = {}, opts = {}) {
|
|
116
|
+
const chainId = Number(args.chainId ?? opts.chainId ?? 1);
|
|
117
|
+
const token = normalizeAddress(args.token || args.tokenAddress, "token");
|
|
118
|
+
const owner = normalizeAddress(args.owner, "owner");
|
|
119
|
+
const spender = normalizeAddress(args.spender, "spender");
|
|
120
|
+
const data = ERC20.encodeFunctionData("allowance", [owner, spender]);
|
|
121
|
+
const result = await rpcCall(chainId, "eth_call", [{ to: token, data }, args.blockTag || "latest"], opts);
|
|
122
|
+
const decoded = ERC20.decodeFunctionResult("allowance", result);
|
|
123
|
+
const allowance = BigInt(decoded[0]);
|
|
124
|
+
return {
|
|
125
|
+
chainId,
|
|
126
|
+
token,
|
|
127
|
+
owner,
|
|
128
|
+
spender,
|
|
129
|
+
allowance: allowance.toString(),
|
|
130
|
+
allowanceHex: `0x${allowance.toString(16)}`,
|
|
131
|
+
blockTag: args.blockTag || "latest",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function erc20BalanceOf(args = {}, opts = {}) {
|
|
136
|
+
const chainId = Number(args.chainId ?? opts.chainId ?? 1);
|
|
137
|
+
const token = normalizeAddress(args.token || args.tokenAddress, "token");
|
|
138
|
+
const account = normalizeAddress(args.account || args.owner || args.address, "account");
|
|
139
|
+
const data = ERC20.encodeFunctionData("balanceOf", [account]);
|
|
140
|
+
const result = await rpcCall(chainId, "eth_call", [{ to: token, data }, args.blockTag || "latest"], opts);
|
|
141
|
+
const decoded = ERC20.decodeFunctionResult("balanceOf", result);
|
|
142
|
+
const balance = BigInt(decoded[0]);
|
|
143
|
+
return {
|
|
144
|
+
chainId,
|
|
145
|
+
token,
|
|
146
|
+
account,
|
|
147
|
+
balance: balance.toString(),
|
|
148
|
+
balanceHex: `0x${balance.toString(16)}`,
|
|
149
|
+
blockTag: args.blockTag || "latest",
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function transactionReceipt(args = {}, opts = {}) {
|
|
154
|
+
const chainId = Number(args.chainId ?? opts.chainId ?? 1);
|
|
155
|
+
const txHash = normalizeTxHash(args.txHash || args.hash || args.transactionHash);
|
|
156
|
+
const receipt = await rpcCall(chainId, "eth_getTransactionReceipt", [txHash], opts);
|
|
157
|
+
if (!receipt) {
|
|
158
|
+
return { ok: false, chainId, txHash, status: "pending", receipt: null, reason: "receipt pending" };
|
|
159
|
+
}
|
|
160
|
+
const statusNumber = parseHexInt(receipt.status);
|
|
161
|
+
let ok = statusNumber === 1;
|
|
162
|
+
const status = statusNumber === 1 ? "success" : statusNumber === 0 ? "reverted" : "unknown";
|
|
163
|
+
let reason = ok ? null : status === "reverted" ? "transaction reverted" : "unknown receipt status";
|
|
164
|
+
const expectedTo = args.expectedTo ? normalizeAddress(args.expectedTo, "expectedTo") : null;
|
|
165
|
+
const actualTo = receipt.to ? normalizeAddress(receipt.to, "receipt.to") : null;
|
|
166
|
+
if (expectedTo && actualTo !== expectedTo) {
|
|
167
|
+
ok = false;
|
|
168
|
+
reason = `to mismatch: expected ${expectedTo}, got ${actualTo || "null"}`;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
ok,
|
|
172
|
+
chainId,
|
|
173
|
+
txHash,
|
|
174
|
+
status,
|
|
175
|
+
reason,
|
|
176
|
+
statusCode: statusNumber,
|
|
177
|
+
to: actualTo,
|
|
178
|
+
from: receipt.from ? normalizeAddress(receipt.from, "receipt.from") : null,
|
|
179
|
+
blockNumber: parseHexInt(receipt.blockNumber),
|
|
180
|
+
transactionIndex: parseHexInt(receipt.transactionIndex),
|
|
181
|
+
logs: Array.isArray(receipt.logs) ? receipt.logs : [],
|
|
182
|
+
receipt,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export async function rpcHealthAll(opts = {}) {
|
|
187
|
+
const ids = opts.chainIds || Object.values(CHAINS).map((c) => c.chainId);
|
|
188
|
+
const out = {};
|
|
189
|
+
await Promise.all(
|
|
190
|
+
ids.map(async (id) => {
|
|
191
|
+
if (!isSupportedChain(id) && !PUBLIC_RPC_FALLBACKS[id] && !resolveRpcUrl(id, opts)) {
|
|
192
|
+
out[id] = { ok: false, error: "no rpc configured", chainId: id };
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
out[id] = await rpcHealth(id, opts);
|
|
197
|
+
} catch (e) {
|
|
198
|
+
out[id] = { ok: false, chainId: id, error: String(e.message || e) };
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// GeckoTerminal public market-data API — no key.
|
|
2
|
+
import { httpJson } from "../http.mjs";
|
|
3
|
+
|
|
4
|
+
export const GECKOTERMINAL_API = "https://api.geckoterminal.com/api/v2";
|
|
5
|
+
const base = (o={}) => (o.baseUrl || process.env.GECKOTERMINAL_API_URL || GECKOTERMINAL_API).replace(/\/$/, "");
|
|
6
|
+
const enc = (v) => encodeURIComponent(String(v));
|
|
7
|
+
|
|
8
|
+
export async function geckoNetworks(args = {}, opts = {}) {
|
|
9
|
+
const page = Math.max(1, Number(args.page || 1));
|
|
10
|
+
return httpJson(`${base(opts)}/networks?page=${page}`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs });
|
|
11
|
+
}
|
|
12
|
+
export async function geckoPool(args = {}, opts = {}) {
|
|
13
|
+
if (!args.network || !args.poolAddress) throw new Error("network and poolAddress required");
|
|
14
|
+
return httpJson(`${base(opts)}/networks/${enc(args.network)}/pools/${enc(args.poolAddress)}`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs });
|
|
15
|
+
}
|
|
16
|
+
export async function geckoToken(args = {}, opts = {}) {
|
|
17
|
+
if (!args.network || !(args.address || args.tokenAddress)) throw new Error("network and token address required");
|
|
18
|
+
return httpJson(`${base(opts)}/networks/${enc(args.network)}/tokens/${enc(args.address || args.tokenAddress)}`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs });
|
|
19
|
+
}
|
|
20
|
+
export async function geckoPoolOhlcv(args = {}, opts = {}) {
|
|
21
|
+
if (!args.network || !args.poolAddress) throw new Error("network and poolAddress required");
|
|
22
|
+
const timeframe = ["minute","hour","day"].includes(args.timeframe) ? args.timeframe : "hour";
|
|
23
|
+
const p = new URLSearchParams();
|
|
24
|
+
p.set("aggregate", String(Math.max(1, Number(args.aggregate || 1))));
|
|
25
|
+
p.set("limit", String(Math.min(1000, Math.max(1, Number(args.limit || 168)))));
|
|
26
|
+
if (args.beforeTimestamp != null) p.set("before_timestamp", String(args.beforeTimestamp));
|
|
27
|
+
if (args.currency) p.set("currency", String(args.currency));
|
|
28
|
+
if (args.token) p.set("token", String(args.token));
|
|
29
|
+
return httpJson(`${base(opts)}/networks/${enc(args.network)}/pools/${enc(args.poolAddress)}/ohlcv/${timeframe}?${p}`, { fetchImpl: opts.fetchImpl, timeoutMs: opts.timeoutMs });
|
|
30
|
+
}
|
|
31
|
+
export async function geckoHealth(opts = {}) {
|
|
32
|
+
const data = await geckoNetworks({ page: 1 }, opts);
|
|
33
|
+
return { ok: Array.isArray(data?.data) && data.data.length > 0, networkSample: data?.data?.length || 0 };
|
|
34
|
+
}
|