forked from TONresistor/teleton-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
669 lines (606 loc) · 20.6 KB
/
index.js
File metadata and controls
669 lines (606 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**
* DYOR plugin -- TON jetton analytics from DYOR.io
*
* Provides token search, detailed info, trust scores, pricing, charts,
* metrics, statistics, holder data, DEX transactions, market pools, and
* trending token discovery. All data comes from the public DYOR.io API
* (no auth required).
*/
const API_BASE = "https://api.dyor.io";
const RATE_LIMIT_MS = 1000; // free plan: 1 req/s
// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------
let lastRequestTime = 0;
async function dyorFetch(path, params = {}) {
const now = Date.now();
const elapsed = now - lastRequestTime;
if (elapsed < RATE_LIMIT_MS) {
await new Promise((r) => setTimeout(r, RATE_LIMIT_MS - elapsed));
}
lastRequestTime = Date.now();
const url = new URL(path, API_BASE);
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== null) {
url.searchParams.set(key, String(value));
}
}
const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
if (!res.ok) {
throw new Error(`DYOR API error: ${res.status} ${res.statusText}`);
}
return res.json();
}
function parseDecimal(obj) {
if (!obj || obj.value === undefined || obj.decimals === undefined) return null;
return Number(obj.value) * Math.pow(10, -obj.decimals);
}
// ---------------------------------------------------------------------------
// Formatting helpers
// ---------------------------------------------------------------------------
function formatJettonSummary(j) {
return {
address: j.metadata?.address ?? j.address,
name: j.metadata?.name ?? j.name,
symbol: j.metadata?.symbol ?? j.symbol,
price_usd: parseDecimal(j.priceUsd) ?? parseDecimal(j.price),
trustScore: j.trustScore ?? null,
holdersCount: j.holdersCount ?? null,
fdmc: parseDecimal(j.fdmc),
liquidityUsd: parseDecimal(j.liquidityUsd),
verification: j.verification ?? null,
};
}
function formatJettonDetails(d) {
const m = d.metadata ?? {};
return {
address: m.address,
name: m.name,
symbol: m.symbol,
decimals: m.decimals,
image: m.image ?? null,
description: m.description ?? null,
links: m.links ?? [],
createdAt: m.createdAt ?? null,
admin: d.admin?.address ?? null,
totalSupply: parseDecimal(d.totalSupply),
mintable: d.mintable ?? null,
modifiedContract: d.modifiedContract ?? null,
verification: d.verification ?? null,
price_ton: parseDecimal(d.price),
price_usd: parseDecimal(d.priceUsd),
holdersCount: d.holdersCount ?? null,
liquidityUsd: parseDecimal(d.liquidityUsd),
fdmc: parseDecimal(d.fdmc),
mcap: parseDecimal(d.mcap),
trustScore: d.trustScore ?? null,
circulatingSupply: parseDecimal(d.circulatingSupply),
};
}
function formatMetrics(m) {
return {
address: m.address ?? null,
price_ton: parseDecimal(m.price),
price_usd: parseDecimal(m.priceUsd),
price_currency: parseDecimal(m.priceCurrency),
holdersCount: m.holdersCount ?? null,
liquidityUsd: parseDecimal(m.liquidityUsd),
liquidityCurrency: parseDecimal(m.liquidityCurrency),
fdmc: parseDecimal(m.fdmc),
fdmcCurrency: parseDecimal(m.fdmcCurrency),
mcap: parseDecimal(m.mcap),
mcapCurrency: parseDecimal(m.mcapCurrency),
circulatingSupply: parseDecimal(m.circulatingSupply),
trustScore: m.trustScore ?? null,
};
}
// ---------------------------------------------------------------------------
// Sort field enums
// ---------------------------------------------------------------------------
const SORT_FIELDS = [
"createdAt", "fdmc", "tvl", "liquidityUsd", "trustScore",
"volume24h", "holders", "traders24h", "transactions24h",
"tonPriceChangeHour", "tonPriceChangeHour6", "tonPriceChangeDay",
"tonPriceChangeWeek", "tonPriceChangeMonth",
];
const TRENDING_SORT_FIELDS = [
"volume24h", "tonPriceChangeHour", "tonPriceChangeDay",
"tonPriceChangeWeek", "holders", "trustScore", "fdmc",
];
const TX_TYPE_MAP = {
buy: "TT_BUY",
sell: "TT_SELL",
liquidity_deposit: "TT_LIQUIDITY_DEPOSIT",
liquidity_withdraw: "TT_LIQUIDITY_WITHDRAW",
};
// ---------------------------------------------------------------------------
// Export -- SDK wrapper
// ---------------------------------------------------------------------------
export const tools = (sdk) => {
// ---------------------------------------------------------------------------
// Tool 1: dyor_search
// ---------------------------------------------------------------------------
const dyorSearch = {
name: "dyor_search",
description:
"Search TON jettons by name or symbol on DYOR.io. Use when the user wants to find a token by keyword. Requires at least 3 characters. Returns address, name, symbol, price, trust score, holders, FDMC, and verification status.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
search: {
type: "string",
description: "Search query (token name or symbol, min 3 characters)",
},
sort: {
type: "string",
enum: SORT_FIELDS,
description: "Sort field (default: fdmc)",
},
order: {
type: "string",
enum: ["asc", "desc"],
description: "Sort order (default: desc)",
},
limit: {
type: "integer",
description: "Number of results, 1-100 (default: 20)",
minimum: 1,
maximum: 100,
},
excludeScam: {
type: "boolean",
description: "Exclude tokens flagged as scam (default: true)",
},
},
required: ["search"],
},
execute: async (params) => {
try {
const data = await dyorFetch("/v1/jettons", {
search: params.search,
sort: params.sort ?? "fdmc",
order: params.order ?? "desc",
limit: params.limit ?? 20,
excludeScam: params.excludeScam ?? true,
});
const jettons = (data.jettons ?? []).map(formatJettonSummary);
return { success: true, data: { jettons, next: data.next ?? null } };
} catch (err) {
sdk.log.error("dyor_search:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 2: dyor_details
// ---------------------------------------------------------------------------
const dyorDetails = {
name: "dyor_details",
description:
"Get full details for a TON jetton by its contract address on DYOR.io. Returns metadata (name, symbol, decimals, image, description, links, creation date), admin address, total supply, mintability, verification status, prices in TON and USD, holders, liquidity, FDMC, market cap, trust score, and circulating supply.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}`);
return { success: true, data: formatJettonDetails(data.details ?? data) };
} catch (err) {
sdk.log.error("dyor_details:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 3: dyor_trust_score
// ---------------------------------------------------------------------------
const dyorTrustScore = {
name: "dyor_trust_score",
description:
"Get the DYOR.io trust score for a TON jetton. Use to quickly assess token legitimacy. Returns a score from 0-100 and the last update time.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/trust-score`);
return {
success: true,
data: {
address: data.address ?? params.address,
score: data.score ?? null,
updatedAt: data.updatedAt ?? null,
},
};
} catch (err) {
sdk.log.error("dyor_trust_score:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 4: dyor_price
// ---------------------------------------------------------------------------
const dyorPrice = {
name: "dyor_price",
description:
"Get the current price of a TON jetton in TON, USD, and an optional currency. Use when the user needs the latest price for a specific token address.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
currency: {
type: "string",
description: "Additional currency code (default: usd)",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/price`, {
currency: params.currency ?? "usd",
});
return {
success: true,
data: {
ton: data.ton ? { value: parseDecimal(data.ton), changedAt: data.ton.changedAt } : null,
usd: data.usd ? { value: parseDecimal(data.usd), changedAt: data.usd.changedAt } : null,
currency: data.currency ? { value: parseDecimal(data.currency), changedAt: data.currency.changedAt } : null,
},
};
} catch (err) {
sdk.log.error("dyor_price:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 5: dyor_price_chart
// ---------------------------------------------------------------------------
const dyorPriceChart = {
name: "dyor_price_chart",
description:
"Get price chart data points for a TON jetton over time. Supports different resolutions: min1 (max 24h), min15 (max 7d), hour1 (max 30d), day1 (max 365d). Use for price trend analysis and charting.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
resolution: {
type: "string",
enum: ["min1", "min15", "hour1", "day1"],
description: "Chart resolution (default: hour1). Max ranges: min1=24h, min15=7d, hour1=30d, day1=365d",
},
from: {
type: "string",
description: "Start time as ISO 8601 datetime (optional)",
},
to: {
type: "string",
description: "End time as ISO 8601 datetime (optional)",
},
currency: {
type: "string",
description: "Price currency (default: usd)",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/price/chart`, {
resolution: params.resolution ?? "hour1",
from: params.from,
to: params.to,
currency: params.currency ?? "usd",
});
const points = (data.points ?? []).map((p) => ({
value: parseDecimal(p.value) ?? p.value,
time: p.time,
}));
return { success: true, data: { points } };
} catch (err) {
sdk.log.error("dyor_price_chart:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 6: dyor_metrics
// ---------------------------------------------------------------------------
const dyorMetrics = {
name: "dyor_metrics",
description:
"Get consolidated metrics for a TON jetton: price (TON/USD), holders, liquidity, FDMC, market cap, circulating supply, and trust score. Use for a quick overview of key token metrics.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
currency: {
type: "string",
description: "Currency for values (default: usd)",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/metrics`, {
currency: params.currency ?? "usd",
});
return { success: true, data: formatMetrics(data.metrics ?? data) };
} catch (err) {
sdk.log.error("dyor_metrics:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 7: dyor_stats
// ---------------------------------------------------------------------------
const dyorStats = {
name: "dyor_stats",
description:
"Get percentage change statistics for a TON jetton: price changes, volume, traders, and transactions broken down by hour, 6h, day, week, and month. Use to analyze token momentum and trends.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/stats`);
return { success: true, data: data.stats ?? data };
} catch (err) {
sdk.log.error("dyor_stats:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 8: dyor_holders
// ---------------------------------------------------------------------------
const dyorHolders = {
name: "dyor_holders",
description:
"Get holder data for a TON jetton. By default returns the current holder count. Set history=true to get holder count over time (ticks). Use to track holder growth or check current holder numbers.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
history: {
type: "boolean",
description: "If true, return holder count history ticks instead of current count (default: false)",
},
limit: {
type: "integer",
description: "Number of history ticks to return (only used when history=true)",
minimum: 1,
},
},
required: ["address"],
},
execute: async (params) => {
try {
const history = params.history ?? false;
if (history) {
const data = await dyorFetch(`/v1/jettons/${params.address}/holders/ticks`, {
limit: params.limit,
});
return { success: true, data: data };
}
const data = await dyorFetch(`/v1/jettons/${params.address}/holders`);
return {
success: true,
data: {
value: data.value ?? null,
changedAt: data.changedAt ?? null,
},
};
} catch (err) {
sdk.log.error("dyor_holders:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 9: dyor_transactions
// ---------------------------------------------------------------------------
const dyorTransactions = {
name: "dyor_transactions",
description:
"Get recent DEX transactions for a TON jetton. Supports filtering by transaction type (buy/sell/liquidity_deposit/liquidity_withdraw), exchange (dedust/stonfi/tonco), and wallet address. Use to analyze trading activity.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
limit: {
type: "integer",
description: "Number of transactions, 1-100 (default: 20)",
minimum: 1,
maximum: 100,
},
type: {
type: "string",
enum: ["buy", "sell", "liquidity_deposit", "liquidity_withdraw"],
description: "Filter by transaction type",
},
exchangeId: {
type: "string",
enum: ["dedust", "stonfi", "tonco"],
description: "Filter by DEX exchange",
},
who: {
type: "string",
description: "Filter by wallet address",
},
},
required: ["address"],
},
execute: async (params) => {
try {
const apiType = params.type ? TX_TYPE_MAP[params.type] : undefined;
const data = await dyorFetch(`/v1/jettons/${params.address}/transactions`, {
limit: params.limit ?? 20,
type: apiType,
exchangeId: params.exchangeId,
who: params.who,
});
return { success: true, data: data.transactions ?? data };
} catch (err) {
sdk.log.error("dyor_transactions:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 10: dyor_markets
// ---------------------------------------------------------------------------
const dyorMarkets = {
name: "dyor_markets",
description:
"Get DEX pool/market data for a TON jetton. Returns available trading pools with liquidity, prices, and counterpart tokens. Optionally filter by exchange (dedust/stonfi/tonco).",
category: "data-bearing",
parameters: {
type: "object",
properties: {
address: {
type: "string",
description: "Jetton contract address",
},
exchangeId: {
type: "string",
enum: ["dedust", "stonfi", "tonco"],
description: "Filter by DEX exchange",
},
limit: {
type: "integer",
description: "Number of markets, 1-100 (default: 20)",
minimum: 1,
maximum: 100,
},
},
required: ["address"],
},
execute: async (params) => {
try {
const data = await dyorFetch(`/v1/jettons/${params.address}/markets`, {
exchangeId: params.exchangeId,
limit: params.limit ?? 20,
});
return { success: true, data: data.markets ?? data };
} catch (err) {
sdk.log.error("dyor_markets:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Tool 11: dyor_trending
// ---------------------------------------------------------------------------
const dyorTrending = {
name: "dyor_trending",
description:
"Get trending TON jettons sorted by a chosen metric. Use to discover top tokens by volume, price change, holders, trust score, or market cap. Does NOT search by name -- use dyor_search for that.",
category: "data-bearing",
parameters: {
type: "object",
properties: {
sort: {
type: "string",
enum: TRENDING_SORT_FIELDS,
description: "Metric to sort by (default: volume24h)",
},
order: {
type: "string",
enum: ["asc", "desc"],
description: "Sort order (default: desc)",
},
limit: {
type: "integer",
description: "Number of results, 1-100 (default: 20)",
minimum: 1,
maximum: 100,
},
excludeScam: {
type: "boolean",
description: "Exclude tokens flagged as scam (default: true)",
},
},
},
execute: async (params) => {
try {
const data = await dyorFetch("/v1/jettons", {
sort: params.sort ?? "volume24h",
order: params.order ?? "desc",
limit: params.limit ?? 20,
excludeScam: params.excludeScam ?? true,
});
const jettons = (data.jettons ?? []).map(formatJettonSummary);
return { success: true, data: { jettons, next: data.next ?? null } };
} catch (err) {
sdk.log.error("dyor_trending:", err.message);
return { success: false, error: String(err.message || err).slice(0, 500) };
}
},
};
// ---------------------------------------------------------------------------
// Return tools array
// ---------------------------------------------------------------------------
return [
dyorSearch,
dyorDetails,
dyorTrustScore,
dyorPrice,
dyorPriceChart,
dyorMetrics,
dyorStats,
dyorHolders,
dyorTransactions,
dyorMarkets,
dyorTrending,
];
}; // end tools(sdk)