@clonegod/ttd-core 3.1.103 → 3.1.106

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/dist/index.js CHANGED
@@ -249,11 +249,21 @@ const format_pair_symbol = (symbol) => {
249
249
  };
250
250
  exports.format_pair_symbol = format_pair_symbol;
251
251
  const format_unique_orderbook_id = (chain_id, dex_id, pool_id, fee_rate) => {
252
+ if (String(chain_id).includes('_') || String(dex_id).includes('_') || String(fee_rate).includes('_')) {
253
+ throw new Error(`format_unique_orderbook_id: chain_id/dex_id/fee_rate can't contain '_'! ${chain_id}, ${dex_id}, ${fee_rate}`);
254
+ }
255
+ if (!Number.isFinite(fee_rate)) {
256
+ log_warn(`format_unique_orderbook_id: fee_rate is not a finite number! ${chain_id}, ${dex_id}, ${pool_id}, ${fee_rate}`);
257
+ }
252
258
  return `${chain_id}_${dex_id}_${pool_id}_${fee_rate}`;
253
259
  };
254
260
  exports.format_unique_orderbook_id = format_unique_orderbook_id;
255
261
  const parse_unique_orderbook_id = (unique_orderbook_id) => {
256
- let [chain_id, dex_id, pool_id, fee_rate] = unique_orderbook_id.split('_');
262
+ let parts = unique_orderbook_id.split('_');
263
+ let chain_id = parts[0];
264
+ let dex_id = parts[1];
265
+ let fee_rate = parts[parts.length - 1];
266
+ let pool_id = parts.slice(2, parts.length - 1).join('_');
257
267
  return {
258
268
  chain_id,
259
269
  dex_id,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const index_1 = require("../index");
4
+ const main = () => {
5
+ const cases = [
6
+ [index_1.CHAIN_ID.TON, index_1.DEX_ID.DEDUST, 'EQA-X_yo3fzzbDbJ_0bzFWKqtRuZFIRa1sJsveZJ1YpViO3r', 25],
7
+ [index_1.CHAIN_ID.TON, index_1.DEX_ID.STONFI, 'EQCqFPpSLtcstURtrXLRCv9wyjfrw7_44_nwvD8JiSmSjbUI', 30],
8
+ [index_1.CHAIN_ID.TON, index_1.DEX_ID.STONFI_V2, 'EQD9BmgQQ2_nzk-9LfxthcoLYC3yBHWK5WqEv_FyMU2riRvE', 20],
9
+ [index_1.CHAIN_ID.TON, index_1.DEX_ID.DEDUST, 'EQCO9NDT4Il25_4ZpHIOgMAUbRJvpsI9pLzqhD8X7eTVB7X_', 30],
10
+ [index_1.CHAIN_ID.TON, index_1.DEX_ID.STONFI, 'EQD8TJ8xEWB1SpnRE4d89YO3jl0W0EiBnNS4IBaHaUmdfizE', 20],
11
+ [index_1.CHAIN_ID.BSC, index_1.DEX_ID.PANCAKE_CLMM, '0x36696169C63e42cd08ce11f5deeBbCeBae652050', 500],
12
+ [index_1.CHAIN_ID.SOLANA, index_1.DEX_ID.RAYDIUM_CLMM, '8sLbNZoA1cfnvMJLPfp98ZLAnFSYCFApfJKMbiXNLwxj', 25],
13
+ ];
14
+ const parse_positional = (id) => {
15
+ let [chain_id, dex_id, pool_id, fee_rate] = id.split('_');
16
+ return { chain_id, dex_id, pool_id, fee_rate };
17
+ };
18
+ let failed = 0;
19
+ for (const [chain_id, dex_id, pool_id, fee_rate] of cases) {
20
+ const id = (0, index_1.format_unique_orderbook_id)(chain_id, dex_id, pool_id, fee_rate);
21
+ const parsed = (0, index_1.parse_unique_orderbook_id)(id);
22
+ let ok = parsed.chain_id === chain_id
23
+ && parsed.dex_id === dex_id
24
+ && parsed.pool_id === pool_id
25
+ && String(parsed.fee_rate) === String(fee_rate);
26
+ if (ok && id.split('_').length === 4) {
27
+ const old = parse_positional(id);
28
+ ok = old.chain_id === parsed.chain_id && old.dex_id === parsed.dex_id
29
+ && old.pool_id === parsed.pool_id && old.fee_rate === parsed.fee_rate;
30
+ }
31
+ console.log(ok ? 'OK ' : 'FAIL', id, ok ? '' : JSON.stringify(parsed));
32
+ if (!ok)
33
+ failed++;
34
+ }
35
+ try {
36
+ (0, index_1.format_unique_orderbook_id)(index_1.CHAIN_ID.TON, 'FOO_BAR', 'EQxx', 30);
37
+ console.log('FAIL guard: dex_id 含 _ 未被拦截');
38
+ failed++;
39
+ }
40
+ catch (err) {
41
+ console.log('OK guard: dex_id 含 _ 生成时即 throw');
42
+ }
43
+ if (failed > 0) {
44
+ console.error(`parse_unique_orderbook_id: ${failed} case(s) FAILED`);
45
+ process.exit(1);
46
+ }
47
+ console.log(`parse_unique_orderbook_id: all passed`);
48
+ };
49
+ main();
@@ -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.106",
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",