@clonegod/ttd-core 3.1.103 → 3.1.105

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.
@@ -0,0 +1,5 @@
1
+ import { FormattedTokenPrice } from "../types";
2
+ import type { PriceSource } from './get_bsc_token_price';
3
+ export declare function get_ton_token_price_info(addresses: string[], opts?: {
4
+ source?: PriceSource;
5
+ }): Promise<Map<string, FormattedTokenPrice>>;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.get_ton_token_price_info = get_ton_token_price_info;
13
+ require('dotenv').config();
14
+ const index_1 = require("../../index");
15
+ const defi_llama_1 = require("./defi_llama");
16
+ const gecko_terminal_1 = require("./gecko_terminal");
17
+ const price_cache_1 = require("./price_cache");
18
+ function get_ton_token_price_info(addresses, opts) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ var _a;
21
+ (0, index_1.log_info)(`get_ton_token_price_info`, addresses);
22
+ const source = (_a = opts === null || opts === void 0 ? void 0 : opts.source) !== null && _a !== void 0 ? _a : 'cache_only';
23
+ const result = new Map();
24
+ const cachedChannel = {
25
+ name: 'CachedPrice',
26
+ fetchFn: price_cache_1.fetchPriceFromCache,
27
+ batchSize: 100,
28
+ batchDelay: 1000,
29
+ };
30
+ const externalChannels = [
31
+ {
32
+ name: 'DefiLlama',
33
+ fetchFn: (address_list) => (0, defi_llama_1.fetchPriceFromDefiLlama)(index_1.CHAIN_ID.TON, address_list),
34
+ batchSize: 10,
35
+ batchDelay: 2000,
36
+ },
37
+ {
38
+ name: 'GeckoTerminal',
39
+ fetchFn: (address_list) => (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)('ton', address_list),
40
+ batchSize: 30,
41
+ batchDelay: 1500,
42
+ },
43
+ ];
44
+ const PRICE_CHANNELS = source === 'cache_only' ? [cachedChannel] :
45
+ source === 'cache_first' ? [cachedChannel, ...externalChannels] :
46
+ [...externalChannels];
47
+ const externalHits = [];
48
+ let worklist = [...addresses];
49
+ try {
50
+ for (const channel of PRICE_CHANNELS) {
51
+ if (worklist.length === 0)
52
+ break;
53
+ (0, index_1.log_debug)(`[get_ton_token_price_info] Processing ${worklist.length} tokens using ${channel.name}`);
54
+ const batches = (0, index_1.chunkArray)(worklist, channel.batchSize);
55
+ let remainingAddresses = [...worklist];
56
+ for (let i = 0; i < batches.length; i++) {
57
+ const batch = batches[i];
58
+ if (batch.length === 0)
59
+ continue;
60
+ try {
61
+ const channelResult = yield channel.fetchFn(batch);
62
+ for (const [address, priceInfo] of channelResult.entries()) {
63
+ result.set(address, priceInfo);
64
+ remainingAddresses = remainingAddresses.filter(addr => addr !== address);
65
+ if (channel.name !== 'CachedPrice') {
66
+ externalHits.push({ address, price: priceInfo.price, source: channel.name });
67
+ }
68
+ }
69
+ }
70
+ catch (error) {
71
+ (0, index_1.log_warn)(`[get_ton_token_price_info] Error processing batch ${i + 1} with ${channel.name}: ${error instanceof Error ? error.message : String(error)}`);
72
+ }
73
+ if (i < batches.length - 1) {
74
+ yield (0, index_1.sleep)(channel.batchDelay);
75
+ }
76
+ }
77
+ worklist = [...remainingAddresses];
78
+ if (worklist.length === 0)
79
+ break;
80
+ }
81
+ if (externalHits.length > 0) {
82
+ yield (0, price_cache_1.cache_new_market_price_batch)(externalHits);
83
+ }
84
+ if (source === 'cache_only' && worklist.length > 0) {
85
+ (0, price_cache_1.warnPriceCacheMiss)(worklist);
86
+ }
87
+ if (source === 'force_fetch' && result.size === 0) {
88
+ throw new Error(`Unable to get price information for any token: ${addresses.join(', ')}`);
89
+ }
90
+ if (source !== 'cache_only' && worklist.length > 0) {
91
+ (0, index_1.log_warn)(`[get_ton_token_price_info] Failed to get prices for ${worklist.length} tokens after trying all channels: ${worklist.join(', ')}`);
92
+ }
93
+ return result;
94
+ }
95
+ catch (error) {
96
+ if (source === 'force_fetch') {
97
+ throw new Error(`Failed to get token price information: ${error instanceof Error ? error.message : String(error)}`);
98
+ }
99
+ return result;
100
+ }
101
+ });
102
+ }
@@ -4,6 +4,7 @@ export * from './get_eth_token_price';
4
4
  export * from './get_solana_token_price';
5
5
  export * from './get_tron_token_price';
6
6
  export * from './get_sui_token_price';
7
+ export * from './get_ton_token_price';
7
8
  export * from './get_xlayer_token_price';
8
9
  export * from './price_cache';
9
10
  export * from './token_price_syncer';
@@ -20,6 +20,7 @@ __exportStar(require("./get_eth_token_price"), exports);
20
20
  __exportStar(require("./get_solana_token_price"), exports);
21
21
  __exportStar(require("./get_tron_token_price"), exports);
22
22
  __exportStar(require("./get_sui_token_price"), exports);
23
+ __exportStar(require("./get_ton_token_price"), exports);
23
24
  __exportStar(require("./get_xlayer_token_price"), exports);
24
25
  __exportStar(require("./price_cache"), exports);
25
26
  __exportStar(require("./token_price_syncer"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "3.1.103",
3
+ "version": "3.1.105",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",