@spectratools/defillama-cli 0.2.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/LICENSE +21 -0
- package/dist/cli.js +386 -0
- package/dist/index.js +409 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 spectra-the-bot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { readFileSync, realpathSync } from "fs";
|
|
5
|
+
import { dirname, resolve } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { Cli as Cli2 } from "incur";
|
|
8
|
+
|
|
9
|
+
// src/commands/tvl.ts
|
|
10
|
+
import { Cli, z as z2 } from "incur";
|
|
11
|
+
|
|
12
|
+
// src/api.ts
|
|
13
|
+
import {
|
|
14
|
+
createHttpClient,
|
|
15
|
+
createRateLimiter,
|
|
16
|
+
withRateLimit,
|
|
17
|
+
withRetry
|
|
18
|
+
} from "@spectratools/cli-shared";
|
|
19
|
+
var DEFILLAMA_HOSTS = {
|
|
20
|
+
api: "https://api.llama.fi",
|
|
21
|
+
coins: "https://coins.llama.fi",
|
|
22
|
+
yields: "https://yields.llama.fi",
|
|
23
|
+
stablecoins: "https://stablecoins.llama.fi",
|
|
24
|
+
bridges: "https://bridges.llama.fi"
|
|
25
|
+
};
|
|
26
|
+
var RETRY_OPTIONS = { maxRetries: 3, baseMs: 500, maxMs: 1e4 };
|
|
27
|
+
function createDefiLlamaClient(options = {}) {
|
|
28
|
+
const { hosts = {}, requestsPerSecond = 5 } = options;
|
|
29
|
+
const resolvedHosts = {
|
|
30
|
+
api: hosts.api ?? DEFILLAMA_HOSTS.api,
|
|
31
|
+
coins: hosts.coins ?? DEFILLAMA_HOSTS.coins,
|
|
32
|
+
yields: hosts.yields ?? DEFILLAMA_HOSTS.yields,
|
|
33
|
+
stablecoins: hosts.stablecoins ?? DEFILLAMA_HOSTS.stablecoins,
|
|
34
|
+
bridges: hosts.bridges ?? DEFILLAMA_HOSTS.bridges
|
|
35
|
+
};
|
|
36
|
+
const clients = Object.fromEntries(
|
|
37
|
+
Object.entries(resolvedHosts).map(([key, baseUrl]) => [key, createHttpClient({ baseUrl })])
|
|
38
|
+
);
|
|
39
|
+
const acquire = createRateLimiter({ requestsPerSecond });
|
|
40
|
+
function get(host, path) {
|
|
41
|
+
const http = clients[host];
|
|
42
|
+
return withRetry(() => withRateLimit(() => http.request(path), acquire), RETRY_OPTIONS);
|
|
43
|
+
}
|
|
44
|
+
return { get };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/format.ts
|
|
48
|
+
function formatUsd(value) {
|
|
49
|
+
const abs = Math.abs(value);
|
|
50
|
+
const sign = value < 0 ? "-" : "";
|
|
51
|
+
if (abs >= 1e12) {
|
|
52
|
+
return `${sign}$${(abs / 1e12).toFixed(2)}T`;
|
|
53
|
+
}
|
|
54
|
+
if (abs >= 1e9) {
|
|
55
|
+
return `${sign}$${(abs / 1e9).toFixed(2)}B`;
|
|
56
|
+
}
|
|
57
|
+
if (abs >= 1e6) {
|
|
58
|
+
return `${sign}$${(abs / 1e6).toFixed(2)}M`;
|
|
59
|
+
}
|
|
60
|
+
if (abs >= 1e3) {
|
|
61
|
+
return `${sign}$${(abs / 1e3).toFixed(2)}K`;
|
|
62
|
+
}
|
|
63
|
+
return `${sign}$${abs.toFixed(2)}`;
|
|
64
|
+
}
|
|
65
|
+
function formatPct(value) {
|
|
66
|
+
if (value == null) {
|
|
67
|
+
return "\u2014";
|
|
68
|
+
}
|
|
69
|
+
const sign = value >= 0 ? "+" : "";
|
|
70
|
+
return `${sign}${value.toFixed(2)}%`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/types.ts
|
|
74
|
+
import { z } from "incur";
|
|
75
|
+
var protocolSummarySchema = z.object({
|
|
76
|
+
id: z.string(),
|
|
77
|
+
name: z.string(),
|
|
78
|
+
slug: z.string(),
|
|
79
|
+
symbol: z.string().optional(),
|
|
80
|
+
category: z.string().optional(),
|
|
81
|
+
chains: z.array(z.string()).optional(),
|
|
82
|
+
tvl: z.number().nullable().optional(),
|
|
83
|
+
change_1h: z.number().nullable().optional(),
|
|
84
|
+
change_1d: z.number().nullable().optional(),
|
|
85
|
+
change_7d: z.number().nullable().optional(),
|
|
86
|
+
mcap: z.number().nullable().optional()
|
|
87
|
+
});
|
|
88
|
+
var chainTvlSchema = z.object({
|
|
89
|
+
gecko_id: z.string().nullable().optional(),
|
|
90
|
+
tvl: z.number(),
|
|
91
|
+
tokenSymbol: z.string().optional(),
|
|
92
|
+
cmcId: z.string().nullable().optional(),
|
|
93
|
+
name: z.string(),
|
|
94
|
+
chainId: z.number().nullable().optional()
|
|
95
|
+
});
|
|
96
|
+
var tvlHistoryPointSchema = z.object({
|
|
97
|
+
date: z.number(),
|
|
98
|
+
totalLiquidityUSD: z.number()
|
|
99
|
+
});
|
|
100
|
+
var protocolDetailSchema = z.object({
|
|
101
|
+
id: z.string(),
|
|
102
|
+
name: z.string(),
|
|
103
|
+
url: z.string().optional(),
|
|
104
|
+
description: z.string().optional(),
|
|
105
|
+
symbol: z.string().optional(),
|
|
106
|
+
category: z.string().nullable().optional(),
|
|
107
|
+
chains: z.array(z.string()).optional(),
|
|
108
|
+
tvl: z.array(tvlHistoryPointSchema).optional(),
|
|
109
|
+
currentChainTvls: z.record(z.string(), z.number()).optional(),
|
|
110
|
+
chainTvls: z.record(
|
|
111
|
+
z.string(),
|
|
112
|
+
z.object({
|
|
113
|
+
tvl: z.array(tvlHistoryPointSchema).optional()
|
|
114
|
+
})
|
|
115
|
+
).optional(),
|
|
116
|
+
mcap: z.number().nullable().optional()
|
|
117
|
+
});
|
|
118
|
+
var volumeEntrySchema = z.object({
|
|
119
|
+
name: z.string(),
|
|
120
|
+
displayName: z.string().optional(),
|
|
121
|
+
total24h: z.number().nullable().optional(),
|
|
122
|
+
total7d: z.number().nullable().optional(),
|
|
123
|
+
total30d: z.number().nullable().optional(),
|
|
124
|
+
change_1d: z.number().nullable().optional(),
|
|
125
|
+
change_7d: z.number().nullable().optional(),
|
|
126
|
+
change_1m: z.number().nullable().optional()
|
|
127
|
+
});
|
|
128
|
+
var feeEntrySchema = z.object({
|
|
129
|
+
name: z.string(),
|
|
130
|
+
displayName: z.string().optional(),
|
|
131
|
+
total24h: z.number().nullable().optional(),
|
|
132
|
+
total7d: z.number().nullable().optional(),
|
|
133
|
+
total30d: z.number().nullable().optional(),
|
|
134
|
+
totalAllTime: z.number().nullable().optional(),
|
|
135
|
+
change_1d: z.number().nullable().optional()
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// src/commands/tvl.ts
|
|
139
|
+
var tvlCli = Cli.create("tvl", {
|
|
140
|
+
description: "Total value locked queries."
|
|
141
|
+
});
|
|
142
|
+
var protocolSortFields = ["tvl", "change_1d", "change_7d"];
|
|
143
|
+
tvlCli.command("protocols", {
|
|
144
|
+
description: "List protocols ranked by TVL.",
|
|
145
|
+
options: z2.object({
|
|
146
|
+
chain: z2.string().optional().describe("Filter protocols by chain name"),
|
|
147
|
+
limit: z2.coerce.number().default(20).describe("Max protocols to display"),
|
|
148
|
+
sort: z2.enum(protocolSortFields).default("tvl").describe("Sort field: tvl, change_1d, or change_7d")
|
|
149
|
+
}),
|
|
150
|
+
output: z2.object({
|
|
151
|
+
protocols: z2.array(
|
|
152
|
+
z2.object({
|
|
153
|
+
name: z2.string(),
|
|
154
|
+
tvl: z2.string(),
|
|
155
|
+
change_1d: z2.string(),
|
|
156
|
+
change_7d: z2.string(),
|
|
157
|
+
category: z2.string()
|
|
158
|
+
})
|
|
159
|
+
),
|
|
160
|
+
chain: z2.string().optional(),
|
|
161
|
+
total: z2.number()
|
|
162
|
+
}),
|
|
163
|
+
examples: [
|
|
164
|
+
{ options: { limit: 10 }, description: "Top 10 protocols by TVL" },
|
|
165
|
+
{ options: { chain: "abstract", limit: 10 }, description: "Top protocols on Abstract" },
|
|
166
|
+
{ options: { sort: "change_1d", limit: 5 }, description: "Top 5 by 1-day change" }
|
|
167
|
+
],
|
|
168
|
+
async run(c) {
|
|
169
|
+
const client = createDefiLlamaClient();
|
|
170
|
+
const raw = await client.get("api", "/protocols");
|
|
171
|
+
const protocols = raw.map((p) => protocolSummarySchema.parse(p));
|
|
172
|
+
let filtered = protocols;
|
|
173
|
+
if (c.options.chain) {
|
|
174
|
+
const chainLower = c.options.chain.toLowerCase();
|
|
175
|
+
filtered = filtered.filter(
|
|
176
|
+
(p) => p.chains?.some((ch) => ch.toLowerCase() === chainLower) ?? false
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
filtered = filtered.filter((p) => p.tvl != null && p.tvl > 0);
|
|
180
|
+
const sortKey = c.options.sort;
|
|
181
|
+
filtered.sort((a, b) => {
|
|
182
|
+
const aVal = a[sortKey] ?? 0;
|
|
183
|
+
const bVal = b[sortKey] ?? 0;
|
|
184
|
+
return bVal - aVal;
|
|
185
|
+
});
|
|
186
|
+
const limited = filtered.slice(0, c.options.limit);
|
|
187
|
+
const rows = limited.map((p) => ({
|
|
188
|
+
name: p.name,
|
|
189
|
+
tvl: formatUsd(p.tvl ?? 0),
|
|
190
|
+
change_1d: formatPct(p.change_1d),
|
|
191
|
+
change_7d: formatPct(p.change_7d),
|
|
192
|
+
category: p.category ?? "\u2014"
|
|
193
|
+
}));
|
|
194
|
+
return c.ok({
|
|
195
|
+
protocols: rows,
|
|
196
|
+
chain: c.options.chain,
|
|
197
|
+
total: filtered.length
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
tvlCli.command("chains", {
|
|
202
|
+
description: "List chains ranked by TVL.",
|
|
203
|
+
options: z2.object({
|
|
204
|
+
limit: z2.coerce.number().default(20).describe("Max chains to display")
|
|
205
|
+
}),
|
|
206
|
+
output: z2.object({
|
|
207
|
+
chains: z2.array(
|
|
208
|
+
z2.object({
|
|
209
|
+
name: z2.string(),
|
|
210
|
+
tvl: z2.string()
|
|
211
|
+
})
|
|
212
|
+
),
|
|
213
|
+
total: z2.number()
|
|
214
|
+
}),
|
|
215
|
+
examples: [{ options: { limit: 10 }, description: "Top 10 chains by TVL" }],
|
|
216
|
+
async run(c) {
|
|
217
|
+
const client = createDefiLlamaClient();
|
|
218
|
+
const raw = await client.get("api", "/v2/chains");
|
|
219
|
+
const chains = raw.map((ch) => chainTvlSchema.parse(ch));
|
|
220
|
+
chains.sort((a, b) => b.tvl - a.tvl);
|
|
221
|
+
const limited = chains.slice(0, c.options.limit);
|
|
222
|
+
const rows = limited.map((ch) => ({
|
|
223
|
+
name: ch.name,
|
|
224
|
+
tvl: formatUsd(ch.tvl)
|
|
225
|
+
}));
|
|
226
|
+
return c.ok({
|
|
227
|
+
chains: rows,
|
|
228
|
+
total: chains.length
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
tvlCli.command("protocol", {
|
|
233
|
+
description: "Get detailed protocol info with TVL breakdown by chain.",
|
|
234
|
+
args: z2.object({
|
|
235
|
+
slug: z2.string().describe("Protocol slug (e.g. aave, uniswap)")
|
|
236
|
+
}),
|
|
237
|
+
output: z2.object({
|
|
238
|
+
name: z2.string(),
|
|
239
|
+
description: z2.string(),
|
|
240
|
+
category: z2.string(),
|
|
241
|
+
url: z2.string(),
|
|
242
|
+
symbol: z2.string(),
|
|
243
|
+
tvl: z2.string(),
|
|
244
|
+
chains: z2.array(
|
|
245
|
+
z2.object({
|
|
246
|
+
chain: z2.string(),
|
|
247
|
+
tvl: z2.string()
|
|
248
|
+
})
|
|
249
|
+
)
|
|
250
|
+
}),
|
|
251
|
+
examples: [{ args: { slug: "aave" }, description: "Aave protocol details" }],
|
|
252
|
+
async run(c) {
|
|
253
|
+
const client = createDefiLlamaClient();
|
|
254
|
+
const raw = await client.get("api", `/protocol/${c.args.slug}`);
|
|
255
|
+
const detail = protocolDetailSchema.parse(raw);
|
|
256
|
+
const chainBreakdown = [];
|
|
257
|
+
if (detail.currentChainTvls) {
|
|
258
|
+
for (const [chain, tvl] of Object.entries(detail.currentChainTvls)) {
|
|
259
|
+
if (chain.includes("-")) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (["borrowed", "staking", "pool2"].includes(chain)) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
chainBreakdown.push({ chain, tvl: formatUsd(tvl) });
|
|
266
|
+
}
|
|
267
|
+
chainBreakdown.sort((a, b) => {
|
|
268
|
+
const aRaw = detail.currentChainTvls?.[a.chain] ?? 0;
|
|
269
|
+
const bRaw = detail.currentChainTvls?.[b.chain] ?? 0;
|
|
270
|
+
return bRaw - aRaw;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
let currentTvl = 0;
|
|
274
|
+
if (detail.tvl && detail.tvl.length > 0) {
|
|
275
|
+
currentTvl = detail.tvl[detail.tvl.length - 1].totalLiquidityUSD;
|
|
276
|
+
}
|
|
277
|
+
return c.ok({
|
|
278
|
+
name: detail.name,
|
|
279
|
+
description: detail.description ?? "\u2014",
|
|
280
|
+
category: detail.category ?? "\u2014",
|
|
281
|
+
url: detail.url ?? "\u2014",
|
|
282
|
+
symbol: detail.symbol ?? "\u2014",
|
|
283
|
+
tvl: formatUsd(currentTvl),
|
|
284
|
+
chains: chainBreakdown
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
tvlCli.command("history", {
|
|
289
|
+
description: "Show historical TVL for a protocol.",
|
|
290
|
+
args: z2.object({
|
|
291
|
+
slug: z2.string().describe("Protocol slug (e.g. aave, uniswap)")
|
|
292
|
+
}),
|
|
293
|
+
options: z2.object({
|
|
294
|
+
days: z2.coerce.number().default(30).describe("Number of days of history to display"),
|
|
295
|
+
chain: z2.string().optional().describe("Filter to a specific chain")
|
|
296
|
+
}),
|
|
297
|
+
output: z2.object({
|
|
298
|
+
protocol: z2.string(),
|
|
299
|
+
chain: z2.string().optional(),
|
|
300
|
+
days: z2.number(),
|
|
301
|
+
history: z2.array(
|
|
302
|
+
z2.object({
|
|
303
|
+
date: z2.string(),
|
|
304
|
+
tvl: z2.string()
|
|
305
|
+
})
|
|
306
|
+
)
|
|
307
|
+
}),
|
|
308
|
+
examples: [
|
|
309
|
+
{ args: { slug: "aave" }, options: { days: 7 }, description: "Aave 7-day TVL history" },
|
|
310
|
+
{
|
|
311
|
+
args: { slug: "uniswap" },
|
|
312
|
+
options: { days: 14, chain: "Ethereum" },
|
|
313
|
+
description: "Uniswap Ethereum chain TVL over 14 days"
|
|
314
|
+
}
|
|
315
|
+
],
|
|
316
|
+
async run(c) {
|
|
317
|
+
const client = createDefiLlamaClient();
|
|
318
|
+
const raw = await client.get("api", `/protocol/${c.args.slug}`);
|
|
319
|
+
const detail = protocolDetailSchema.parse(raw);
|
|
320
|
+
let tvlSeries = [];
|
|
321
|
+
if (c.options.chain) {
|
|
322
|
+
const chainLower = c.options.chain.toLowerCase();
|
|
323
|
+
if (detail.chainTvls) {
|
|
324
|
+
const matchKey = Object.keys(detail.chainTvls).find(
|
|
325
|
+
(k) => k.toLowerCase() === chainLower && !k.includes("-")
|
|
326
|
+
);
|
|
327
|
+
if (matchKey && detail.chainTvls[matchKey]?.tvl) {
|
|
328
|
+
tvlSeries = detail.chainTvls[matchKey].tvl ?? [];
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
} else if (detail.tvl) {
|
|
332
|
+
tvlSeries = detail.tvl;
|
|
333
|
+
}
|
|
334
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
335
|
+
const cutoff = now - c.options.days * 86400;
|
|
336
|
+
const filtered = tvlSeries.filter((p) => p.date >= cutoff);
|
|
337
|
+
const history = filtered.map((p) => ({
|
|
338
|
+
date: new Date(p.date * 1e3).toISOString().split("T")[0],
|
|
339
|
+
tvl: formatUsd(p.totalLiquidityUSD)
|
|
340
|
+
}));
|
|
341
|
+
return c.ok({
|
|
342
|
+
protocol: detail.name,
|
|
343
|
+
chain: c.options.chain,
|
|
344
|
+
days: c.options.days,
|
|
345
|
+
history
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// src/cli.ts
|
|
351
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
352
|
+
var pkg = JSON.parse(readFileSync(resolve(__dirname, "../package.json"), "utf8"));
|
|
353
|
+
var cli = Cli2.create("defillama", {
|
|
354
|
+
version: pkg.version,
|
|
355
|
+
description: "Query DefiLlama API data from the command line."
|
|
356
|
+
});
|
|
357
|
+
cli.command(tvlCli);
|
|
358
|
+
var volumeCli = Cli2.create("volume", {
|
|
359
|
+
description: "DEX volume queries."
|
|
360
|
+
});
|
|
361
|
+
cli.command(volumeCli);
|
|
362
|
+
var feesCli = Cli2.create("fees", {
|
|
363
|
+
description: "Protocol fees and revenue queries."
|
|
364
|
+
});
|
|
365
|
+
cli.command(feesCli);
|
|
366
|
+
var pricesCli = Cli2.create("prices", {
|
|
367
|
+
description: "Token price queries."
|
|
368
|
+
});
|
|
369
|
+
cli.command(pricesCli);
|
|
370
|
+
var isMain = (() => {
|
|
371
|
+
const entrypoint = process.argv[1];
|
|
372
|
+
if (!entrypoint) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
return realpathSync(entrypoint) === realpathSync(fileURLToPath(import.meta.url));
|
|
377
|
+
} catch {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
})();
|
|
381
|
+
if (isMain) {
|
|
382
|
+
cli.serve();
|
|
383
|
+
}
|
|
384
|
+
export {
|
|
385
|
+
cli
|
|
386
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { readFileSync, realpathSync } from "fs";
|
|
5
|
+
import { dirname, resolve } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { Cli as Cli2 } from "incur";
|
|
8
|
+
|
|
9
|
+
// src/commands/tvl.ts
|
|
10
|
+
import { Cli, z as z2 } from "incur";
|
|
11
|
+
|
|
12
|
+
// src/api.ts
|
|
13
|
+
import {
|
|
14
|
+
createHttpClient,
|
|
15
|
+
createRateLimiter,
|
|
16
|
+
withRateLimit,
|
|
17
|
+
withRetry
|
|
18
|
+
} from "@spectratools/cli-shared";
|
|
19
|
+
var DEFILLAMA_HOSTS = {
|
|
20
|
+
api: "https://api.llama.fi",
|
|
21
|
+
coins: "https://coins.llama.fi",
|
|
22
|
+
yields: "https://yields.llama.fi",
|
|
23
|
+
stablecoins: "https://stablecoins.llama.fi",
|
|
24
|
+
bridges: "https://bridges.llama.fi"
|
|
25
|
+
};
|
|
26
|
+
var RETRY_OPTIONS = { maxRetries: 3, baseMs: 500, maxMs: 1e4 };
|
|
27
|
+
function createDefiLlamaClient(options = {}) {
|
|
28
|
+
const { hosts = {}, requestsPerSecond = 5 } = options;
|
|
29
|
+
const resolvedHosts = {
|
|
30
|
+
api: hosts.api ?? DEFILLAMA_HOSTS.api,
|
|
31
|
+
coins: hosts.coins ?? DEFILLAMA_HOSTS.coins,
|
|
32
|
+
yields: hosts.yields ?? DEFILLAMA_HOSTS.yields,
|
|
33
|
+
stablecoins: hosts.stablecoins ?? DEFILLAMA_HOSTS.stablecoins,
|
|
34
|
+
bridges: hosts.bridges ?? DEFILLAMA_HOSTS.bridges
|
|
35
|
+
};
|
|
36
|
+
const clients = Object.fromEntries(
|
|
37
|
+
Object.entries(resolvedHosts).map(([key, baseUrl]) => [key, createHttpClient({ baseUrl })])
|
|
38
|
+
);
|
|
39
|
+
const acquire = createRateLimiter({ requestsPerSecond });
|
|
40
|
+
function get(host, path) {
|
|
41
|
+
const http = clients[host];
|
|
42
|
+
return withRetry(() => withRateLimit(() => http.request(path), acquire), RETRY_OPTIONS);
|
|
43
|
+
}
|
|
44
|
+
return { get };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/format.ts
|
|
48
|
+
function formatDelta(current, previous) {
|
|
49
|
+
if (previous === 0) {
|
|
50
|
+
return current === 0 ? "0.0%" : current > 0 ? "+\u221E%" : "-\u221E%";
|
|
51
|
+
}
|
|
52
|
+
const pct = (current - previous) / Math.abs(previous) * 100;
|
|
53
|
+
const sign = pct >= 0 ? "+" : "";
|
|
54
|
+
return `${sign}${pct.toFixed(1)}%`;
|
|
55
|
+
}
|
|
56
|
+
function formatUsd(value) {
|
|
57
|
+
const abs = Math.abs(value);
|
|
58
|
+
const sign = value < 0 ? "-" : "";
|
|
59
|
+
if (abs >= 1e12) {
|
|
60
|
+
return `${sign}$${(abs / 1e12).toFixed(2)}T`;
|
|
61
|
+
}
|
|
62
|
+
if (abs >= 1e9) {
|
|
63
|
+
return `${sign}$${(abs / 1e9).toFixed(2)}B`;
|
|
64
|
+
}
|
|
65
|
+
if (abs >= 1e6) {
|
|
66
|
+
return `${sign}$${(abs / 1e6).toFixed(2)}M`;
|
|
67
|
+
}
|
|
68
|
+
if (abs >= 1e3) {
|
|
69
|
+
return `${sign}$${(abs / 1e3).toFixed(2)}K`;
|
|
70
|
+
}
|
|
71
|
+
return `${sign}$${abs.toFixed(2)}`;
|
|
72
|
+
}
|
|
73
|
+
function formatPct(value) {
|
|
74
|
+
if (value == null) {
|
|
75
|
+
return "\u2014";
|
|
76
|
+
}
|
|
77
|
+
const sign = value >= 0 ? "+" : "";
|
|
78
|
+
return `${sign}${value.toFixed(2)}%`;
|
|
79
|
+
}
|
|
80
|
+
function formatNumber(value) {
|
|
81
|
+
return value.toLocaleString("en-US");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/types.ts
|
|
85
|
+
import { z } from "incur";
|
|
86
|
+
var protocolSummarySchema = z.object({
|
|
87
|
+
id: z.string(),
|
|
88
|
+
name: z.string(),
|
|
89
|
+
slug: z.string(),
|
|
90
|
+
symbol: z.string().optional(),
|
|
91
|
+
category: z.string().optional(),
|
|
92
|
+
chains: z.array(z.string()).optional(),
|
|
93
|
+
tvl: z.number().nullable().optional(),
|
|
94
|
+
change_1h: z.number().nullable().optional(),
|
|
95
|
+
change_1d: z.number().nullable().optional(),
|
|
96
|
+
change_7d: z.number().nullable().optional(),
|
|
97
|
+
mcap: z.number().nullable().optional()
|
|
98
|
+
});
|
|
99
|
+
var chainTvlSchema = z.object({
|
|
100
|
+
gecko_id: z.string().nullable().optional(),
|
|
101
|
+
tvl: z.number(),
|
|
102
|
+
tokenSymbol: z.string().optional(),
|
|
103
|
+
cmcId: z.string().nullable().optional(),
|
|
104
|
+
name: z.string(),
|
|
105
|
+
chainId: z.number().nullable().optional()
|
|
106
|
+
});
|
|
107
|
+
var tvlHistoryPointSchema = z.object({
|
|
108
|
+
date: z.number(),
|
|
109
|
+
totalLiquidityUSD: z.number()
|
|
110
|
+
});
|
|
111
|
+
var protocolDetailSchema = z.object({
|
|
112
|
+
id: z.string(),
|
|
113
|
+
name: z.string(),
|
|
114
|
+
url: z.string().optional(),
|
|
115
|
+
description: z.string().optional(),
|
|
116
|
+
symbol: z.string().optional(),
|
|
117
|
+
category: z.string().nullable().optional(),
|
|
118
|
+
chains: z.array(z.string()).optional(),
|
|
119
|
+
tvl: z.array(tvlHistoryPointSchema).optional(),
|
|
120
|
+
currentChainTvls: z.record(z.string(), z.number()).optional(),
|
|
121
|
+
chainTvls: z.record(
|
|
122
|
+
z.string(),
|
|
123
|
+
z.object({
|
|
124
|
+
tvl: z.array(tvlHistoryPointSchema).optional()
|
|
125
|
+
})
|
|
126
|
+
).optional(),
|
|
127
|
+
mcap: z.number().nullable().optional()
|
|
128
|
+
});
|
|
129
|
+
var volumeEntrySchema = z.object({
|
|
130
|
+
name: z.string(),
|
|
131
|
+
displayName: z.string().optional(),
|
|
132
|
+
total24h: z.number().nullable().optional(),
|
|
133
|
+
total7d: z.number().nullable().optional(),
|
|
134
|
+
total30d: z.number().nullable().optional(),
|
|
135
|
+
change_1d: z.number().nullable().optional(),
|
|
136
|
+
change_7d: z.number().nullable().optional(),
|
|
137
|
+
change_1m: z.number().nullable().optional()
|
|
138
|
+
});
|
|
139
|
+
var feeEntrySchema = z.object({
|
|
140
|
+
name: z.string(),
|
|
141
|
+
displayName: z.string().optional(),
|
|
142
|
+
total24h: z.number().nullable().optional(),
|
|
143
|
+
total7d: z.number().nullable().optional(),
|
|
144
|
+
total30d: z.number().nullable().optional(),
|
|
145
|
+
totalAllTime: z.number().nullable().optional(),
|
|
146
|
+
change_1d: z.number().nullable().optional()
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// src/commands/tvl.ts
|
|
150
|
+
var tvlCli = Cli.create("tvl", {
|
|
151
|
+
description: "Total value locked queries."
|
|
152
|
+
});
|
|
153
|
+
var protocolSortFields = ["tvl", "change_1d", "change_7d"];
|
|
154
|
+
tvlCli.command("protocols", {
|
|
155
|
+
description: "List protocols ranked by TVL.",
|
|
156
|
+
options: z2.object({
|
|
157
|
+
chain: z2.string().optional().describe("Filter protocols by chain name"),
|
|
158
|
+
limit: z2.coerce.number().default(20).describe("Max protocols to display"),
|
|
159
|
+
sort: z2.enum(protocolSortFields).default("tvl").describe("Sort field: tvl, change_1d, or change_7d")
|
|
160
|
+
}),
|
|
161
|
+
output: z2.object({
|
|
162
|
+
protocols: z2.array(
|
|
163
|
+
z2.object({
|
|
164
|
+
name: z2.string(),
|
|
165
|
+
tvl: z2.string(),
|
|
166
|
+
change_1d: z2.string(),
|
|
167
|
+
change_7d: z2.string(),
|
|
168
|
+
category: z2.string()
|
|
169
|
+
})
|
|
170
|
+
),
|
|
171
|
+
chain: z2.string().optional(),
|
|
172
|
+
total: z2.number()
|
|
173
|
+
}),
|
|
174
|
+
examples: [
|
|
175
|
+
{ options: { limit: 10 }, description: "Top 10 protocols by TVL" },
|
|
176
|
+
{ options: { chain: "abstract", limit: 10 }, description: "Top protocols on Abstract" },
|
|
177
|
+
{ options: { sort: "change_1d", limit: 5 }, description: "Top 5 by 1-day change" }
|
|
178
|
+
],
|
|
179
|
+
async run(c) {
|
|
180
|
+
const client = createDefiLlamaClient();
|
|
181
|
+
const raw = await client.get("api", "/protocols");
|
|
182
|
+
const protocols = raw.map((p) => protocolSummarySchema.parse(p));
|
|
183
|
+
let filtered = protocols;
|
|
184
|
+
if (c.options.chain) {
|
|
185
|
+
const chainLower = c.options.chain.toLowerCase();
|
|
186
|
+
filtered = filtered.filter(
|
|
187
|
+
(p) => p.chains?.some((ch) => ch.toLowerCase() === chainLower) ?? false
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
filtered = filtered.filter((p) => p.tvl != null && p.tvl > 0);
|
|
191
|
+
const sortKey = c.options.sort;
|
|
192
|
+
filtered.sort((a, b) => {
|
|
193
|
+
const aVal = a[sortKey] ?? 0;
|
|
194
|
+
const bVal = b[sortKey] ?? 0;
|
|
195
|
+
return bVal - aVal;
|
|
196
|
+
});
|
|
197
|
+
const limited = filtered.slice(0, c.options.limit);
|
|
198
|
+
const rows = limited.map((p) => ({
|
|
199
|
+
name: p.name,
|
|
200
|
+
tvl: formatUsd(p.tvl ?? 0),
|
|
201
|
+
change_1d: formatPct(p.change_1d),
|
|
202
|
+
change_7d: formatPct(p.change_7d),
|
|
203
|
+
category: p.category ?? "\u2014"
|
|
204
|
+
}));
|
|
205
|
+
return c.ok({
|
|
206
|
+
protocols: rows,
|
|
207
|
+
chain: c.options.chain,
|
|
208
|
+
total: filtered.length
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
tvlCli.command("chains", {
|
|
213
|
+
description: "List chains ranked by TVL.",
|
|
214
|
+
options: z2.object({
|
|
215
|
+
limit: z2.coerce.number().default(20).describe("Max chains to display")
|
|
216
|
+
}),
|
|
217
|
+
output: z2.object({
|
|
218
|
+
chains: z2.array(
|
|
219
|
+
z2.object({
|
|
220
|
+
name: z2.string(),
|
|
221
|
+
tvl: z2.string()
|
|
222
|
+
})
|
|
223
|
+
),
|
|
224
|
+
total: z2.number()
|
|
225
|
+
}),
|
|
226
|
+
examples: [{ options: { limit: 10 }, description: "Top 10 chains by TVL" }],
|
|
227
|
+
async run(c) {
|
|
228
|
+
const client = createDefiLlamaClient();
|
|
229
|
+
const raw = await client.get("api", "/v2/chains");
|
|
230
|
+
const chains = raw.map((ch) => chainTvlSchema.parse(ch));
|
|
231
|
+
chains.sort((a, b) => b.tvl - a.tvl);
|
|
232
|
+
const limited = chains.slice(0, c.options.limit);
|
|
233
|
+
const rows = limited.map((ch) => ({
|
|
234
|
+
name: ch.name,
|
|
235
|
+
tvl: formatUsd(ch.tvl)
|
|
236
|
+
}));
|
|
237
|
+
return c.ok({
|
|
238
|
+
chains: rows,
|
|
239
|
+
total: chains.length
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
tvlCli.command("protocol", {
|
|
244
|
+
description: "Get detailed protocol info with TVL breakdown by chain.",
|
|
245
|
+
args: z2.object({
|
|
246
|
+
slug: z2.string().describe("Protocol slug (e.g. aave, uniswap)")
|
|
247
|
+
}),
|
|
248
|
+
output: z2.object({
|
|
249
|
+
name: z2.string(),
|
|
250
|
+
description: z2.string(),
|
|
251
|
+
category: z2.string(),
|
|
252
|
+
url: z2.string(),
|
|
253
|
+
symbol: z2.string(),
|
|
254
|
+
tvl: z2.string(),
|
|
255
|
+
chains: z2.array(
|
|
256
|
+
z2.object({
|
|
257
|
+
chain: z2.string(),
|
|
258
|
+
tvl: z2.string()
|
|
259
|
+
})
|
|
260
|
+
)
|
|
261
|
+
}),
|
|
262
|
+
examples: [{ args: { slug: "aave" }, description: "Aave protocol details" }],
|
|
263
|
+
async run(c) {
|
|
264
|
+
const client = createDefiLlamaClient();
|
|
265
|
+
const raw = await client.get("api", `/protocol/${c.args.slug}`);
|
|
266
|
+
const detail = protocolDetailSchema.parse(raw);
|
|
267
|
+
const chainBreakdown = [];
|
|
268
|
+
if (detail.currentChainTvls) {
|
|
269
|
+
for (const [chain, tvl] of Object.entries(detail.currentChainTvls)) {
|
|
270
|
+
if (chain.includes("-")) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (["borrowed", "staking", "pool2"].includes(chain)) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
chainBreakdown.push({ chain, tvl: formatUsd(tvl) });
|
|
277
|
+
}
|
|
278
|
+
chainBreakdown.sort((a, b) => {
|
|
279
|
+
const aRaw = detail.currentChainTvls?.[a.chain] ?? 0;
|
|
280
|
+
const bRaw = detail.currentChainTvls?.[b.chain] ?? 0;
|
|
281
|
+
return bRaw - aRaw;
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
let currentTvl = 0;
|
|
285
|
+
if (detail.tvl && detail.tvl.length > 0) {
|
|
286
|
+
currentTvl = detail.tvl[detail.tvl.length - 1].totalLiquidityUSD;
|
|
287
|
+
}
|
|
288
|
+
return c.ok({
|
|
289
|
+
name: detail.name,
|
|
290
|
+
description: detail.description ?? "\u2014",
|
|
291
|
+
category: detail.category ?? "\u2014",
|
|
292
|
+
url: detail.url ?? "\u2014",
|
|
293
|
+
symbol: detail.symbol ?? "\u2014",
|
|
294
|
+
tvl: formatUsd(currentTvl),
|
|
295
|
+
chains: chainBreakdown
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
tvlCli.command("history", {
|
|
300
|
+
description: "Show historical TVL for a protocol.",
|
|
301
|
+
args: z2.object({
|
|
302
|
+
slug: z2.string().describe("Protocol slug (e.g. aave, uniswap)")
|
|
303
|
+
}),
|
|
304
|
+
options: z2.object({
|
|
305
|
+
days: z2.coerce.number().default(30).describe("Number of days of history to display"),
|
|
306
|
+
chain: z2.string().optional().describe("Filter to a specific chain")
|
|
307
|
+
}),
|
|
308
|
+
output: z2.object({
|
|
309
|
+
protocol: z2.string(),
|
|
310
|
+
chain: z2.string().optional(),
|
|
311
|
+
days: z2.number(),
|
|
312
|
+
history: z2.array(
|
|
313
|
+
z2.object({
|
|
314
|
+
date: z2.string(),
|
|
315
|
+
tvl: z2.string()
|
|
316
|
+
})
|
|
317
|
+
)
|
|
318
|
+
}),
|
|
319
|
+
examples: [
|
|
320
|
+
{ args: { slug: "aave" }, options: { days: 7 }, description: "Aave 7-day TVL history" },
|
|
321
|
+
{
|
|
322
|
+
args: { slug: "uniswap" },
|
|
323
|
+
options: { days: 14, chain: "Ethereum" },
|
|
324
|
+
description: "Uniswap Ethereum chain TVL over 14 days"
|
|
325
|
+
}
|
|
326
|
+
],
|
|
327
|
+
async run(c) {
|
|
328
|
+
const client = createDefiLlamaClient();
|
|
329
|
+
const raw = await client.get("api", `/protocol/${c.args.slug}`);
|
|
330
|
+
const detail = protocolDetailSchema.parse(raw);
|
|
331
|
+
let tvlSeries = [];
|
|
332
|
+
if (c.options.chain) {
|
|
333
|
+
const chainLower = c.options.chain.toLowerCase();
|
|
334
|
+
if (detail.chainTvls) {
|
|
335
|
+
const matchKey = Object.keys(detail.chainTvls).find(
|
|
336
|
+
(k) => k.toLowerCase() === chainLower && !k.includes("-")
|
|
337
|
+
);
|
|
338
|
+
if (matchKey && detail.chainTvls[matchKey]?.tvl) {
|
|
339
|
+
tvlSeries = detail.chainTvls[matchKey].tvl ?? [];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} else if (detail.tvl) {
|
|
343
|
+
tvlSeries = detail.tvl;
|
|
344
|
+
}
|
|
345
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
346
|
+
const cutoff = now - c.options.days * 86400;
|
|
347
|
+
const filtered = tvlSeries.filter((p) => p.date >= cutoff);
|
|
348
|
+
const history = filtered.map((p) => ({
|
|
349
|
+
date: new Date(p.date * 1e3).toISOString().split("T")[0],
|
|
350
|
+
tvl: formatUsd(p.totalLiquidityUSD)
|
|
351
|
+
}));
|
|
352
|
+
return c.ok({
|
|
353
|
+
protocol: detail.name,
|
|
354
|
+
chain: c.options.chain,
|
|
355
|
+
days: c.options.days,
|
|
356
|
+
history
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// src/cli.ts
|
|
362
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
363
|
+
var pkg = JSON.parse(readFileSync(resolve(__dirname, "../package.json"), "utf8"));
|
|
364
|
+
var cli = Cli2.create("defillama", {
|
|
365
|
+
version: pkg.version,
|
|
366
|
+
description: "Query DefiLlama API data from the command line."
|
|
367
|
+
});
|
|
368
|
+
cli.command(tvlCli);
|
|
369
|
+
var volumeCli = Cli2.create("volume", {
|
|
370
|
+
description: "DEX volume queries."
|
|
371
|
+
});
|
|
372
|
+
cli.command(volumeCli);
|
|
373
|
+
var feesCli = Cli2.create("fees", {
|
|
374
|
+
description: "Protocol fees and revenue queries."
|
|
375
|
+
});
|
|
376
|
+
cli.command(feesCli);
|
|
377
|
+
var pricesCli = Cli2.create("prices", {
|
|
378
|
+
description: "Token price queries."
|
|
379
|
+
});
|
|
380
|
+
cli.command(pricesCli);
|
|
381
|
+
var isMain = (() => {
|
|
382
|
+
const entrypoint = process.argv[1];
|
|
383
|
+
if (!entrypoint) {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
return realpathSync(entrypoint) === realpathSync(fileURLToPath(import.meta.url));
|
|
388
|
+
} catch {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
})();
|
|
392
|
+
if (isMain) {
|
|
393
|
+
cli.serve();
|
|
394
|
+
}
|
|
395
|
+
export {
|
|
396
|
+
DEFILLAMA_HOSTS,
|
|
397
|
+
chainTvlSchema,
|
|
398
|
+
cli,
|
|
399
|
+
createDefiLlamaClient,
|
|
400
|
+
feeEntrySchema,
|
|
401
|
+
formatDelta,
|
|
402
|
+
formatNumber,
|
|
403
|
+
formatPct,
|
|
404
|
+
formatUsd,
|
|
405
|
+
protocolDetailSchema,
|
|
406
|
+
protocolSummarySchema,
|
|
407
|
+
tvlHistoryPointSchema,
|
|
408
|
+
volumeEntrySchema
|
|
409
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spectratools/defillama-cli",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "DefiLlama API CLI for spectra-the-bot",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "spectra-the-bot",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"defillama-cli": "./dist/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"incur": "^0.3.0",
|
|
22
|
+
"@spectratools/cli-shared": "0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typescript": "5.7.3",
|
|
26
|
+
"vitest": "2.1.8"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"main": "./dist/cli.js",
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
}
|
|
38
|
+
}
|