@clonegod/ttd-core 2.0.37 → 2.0.39

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.d.ts CHANGED
@@ -24,7 +24,8 @@ export declare enum CHAIN_ID {
24
24
  TRON = "TRON",
25
25
  SOLANA = "SOLANA",
26
26
  BSC = "BSC",
27
- ETH = "ETH"
27
+ ETH = "ETH",
28
+ BASE = "BASE"
28
29
  }
29
30
  export declare enum DEX_ID {
30
31
  SUNSWAP_AMM = "SUNSWAP-AMM",
package/dist/index.js CHANGED
@@ -108,6 +108,7 @@ var CHAIN_ID;
108
108
  CHAIN_ID["SOLANA"] = "SOLANA";
109
109
  CHAIN_ID["BSC"] = "BSC";
110
110
  CHAIN_ID["ETH"] = "ETH";
111
+ CHAIN_ID["BASE"] = "BASE";
111
112
  })(CHAIN_ID || (exports.CHAIN_ID = CHAIN_ID = {}));
112
113
  var DEX_ID;
113
114
  (function (DEX_ID) {
@@ -0,0 +1,2 @@
1
+ import { FormattedTokenPrice } from "../types";
2
+ export declare function get_base_token_price_info(addresses: string[]): Promise<Map<string, FormattedTokenPrice>>;
@@ -0,0 +1,162 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.get_base_token_price_info = get_base_token_price_info;
16
+ require('dotenv').config();
17
+ const axios_1 = __importDefault(require("axios"));
18
+ const index_1 = require("../../index");
19
+ const gecko_terminal_1 = require("./gecko_terminal");
20
+ const price_cache_1 = require("./price_cache");
21
+ function get_base_token_price_info(addresses) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ addresses = addresses.map(addr => addr.toLowerCase());
24
+ const result = new Map();
25
+ const PRICE_CHANNELS = [
26
+ {
27
+ name: 'CachedPrice',
28
+ fetchFn: price_cache_1.fetchPriceFromCache,
29
+ batchSize: 100,
30
+ batchDelay: 1000,
31
+ },
32
+ {
33
+ name: 'GeckoTerminal',
34
+ fetchFn: (address_list) => (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)(index_1.CHAIN_ID.BASE, address_list),
35
+ batchSize: 10,
36
+ batchDelay: 2000,
37
+ },
38
+ ];
39
+ try {
40
+ for (const channel of PRICE_CHANNELS) {
41
+ if (addresses.length === 0)
42
+ break;
43
+ (0, index_1.log_debug)(`[get_token_price_info] Processing ${addresses.length} tokens using ${channel.name}`);
44
+ const batches = (0, index_1.chunkArray)(addresses, channel.batchSize);
45
+ (0, index_1.log_debug)(`[get_token_price_info] Split into ${batches.length} batches of size ${channel.batchSize}`);
46
+ let remainingAddresses = [...addresses];
47
+ for (let i = 0; i < batches.length; i++) {
48
+ const batch = batches[i];
49
+ if (batch.length === 0)
50
+ continue;
51
+ (0, index_1.log_debug)(`[get_token_price_info] Processing batch ${i + 1}/${batches.length} (${batch.length} tokens) with ${channel.name}`);
52
+ try {
53
+ const channelResult = yield channel.fetchFn(batch);
54
+ for (const [address, priceInfo] of channelResult.entries()) {
55
+ result.set(address, priceInfo);
56
+ remainingAddresses = remainingAddresses.filter(addr => addr !== address);
57
+ if (channel.name !== 'CachedPrice') {
58
+ (0, price_cache_1.cache_new_market_price)(address, priceInfo.price, channel.name);
59
+ }
60
+ }
61
+ }
62
+ catch (error) {
63
+ (0, index_1.log_warn)(`[get_token_price_info] Error processing batch ${i + 1} with ${channel.name}: ${error instanceof Error ? error.message : String(error)}`);
64
+ }
65
+ if (i < batches.length - 1) {
66
+ yield (0, index_1.sleep)(channel.batchDelay);
67
+ }
68
+ }
69
+ addresses = [...remainingAddresses];
70
+ if (addresses.length === 0) {
71
+ (0, index_1.log_debug)(`[get_token_price_info] All token prices retrieved using ${channel.name}`);
72
+ break;
73
+ }
74
+ }
75
+ if (result.size === 0) {
76
+ throw new Error(`Unable to get price information for any token: ${addresses.join(', ')}`);
77
+ }
78
+ if (addresses.length > 0) {
79
+ (0, index_1.log_warn)(`[get_token_price_info] Failed to get prices for ${addresses.length} tokens after trying all channels: ${addresses.join(', ')}`);
80
+ }
81
+ (0, index_1.log_debug)(`[get_token_price_info] Completed price fetching for ${result.size} tokens`);
82
+ return result;
83
+ }
84
+ catch (error) {
85
+ throw new Error(`Failed to get token price information: ${error instanceof Error ? error.message : String(error)}`);
86
+ }
87
+ });
88
+ }
89
+ function fetchPriceFromUniSwap(addresses) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ var _a, _b, _c, _d;
92
+ const result = new Map();
93
+ const currentTime = (0, index_1.getCurDateTime)();
94
+ const query = `
95
+ query Tokens($contracts: [ContractInput!]!) {
96
+ tokens(contracts: $contracts) {
97
+ id
98
+ address
99
+ symbol
100
+ name
101
+ decimals
102
+ market(currency: USD) {
103
+ price { value }
104
+ }
105
+ }
106
+ }
107
+ `;
108
+ const variables = {
109
+ contracts: addresses.map(address => ({
110
+ address: address,
111
+ chain: 'BASE'
112
+ }))
113
+ };
114
+ const headers = {
115
+ 'Content-Type': 'application/json',
116
+ 'Origin': 'https://app.uniswap.org',
117
+ 'Referer': 'https://app.uniswap.org/',
118
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
119
+ };
120
+ try {
121
+ const response = yield axios_1.default.post('https://interface.gateway.uniswap.org/v1/graphql', {
122
+ query,
123
+ variables
124
+ }, {
125
+ headers,
126
+ timeout: 30000
127
+ });
128
+ if ((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.tokens) {
129
+ for (const token of response.data.data.tokens) {
130
+ if ((_d = (_c = token.market) === null || _c === void 0 ? void 0 : _c.price) === null || _d === void 0 ? void 0 : _d.value) {
131
+ result.set(token.address.toLowerCase(), {
132
+ symbol: token.symbol || '',
133
+ address: token.address.toLowerCase(),
134
+ price: token.market.price.value.toString(),
135
+ update_time: currentTime
136
+ });
137
+ }
138
+ }
139
+ (0, index_1.log_debug)(`[fetchPriceFromUniSwap] Retrieved prices for ${result.size}/${addresses.length} tokens`);
140
+ }
141
+ else {
142
+ (0, index_1.log_debug)(`[fetchPriceFromUniSwap] Invalid response from Uniswap for ${addresses.length} tokens`);
143
+ }
144
+ }
145
+ catch (error) {
146
+ (0, index_1.log_warn)(`[fetchPriceFromUniSwap] Request failed: ${error instanceof Error ? error.message : String(error)}`);
147
+ }
148
+ return result;
149
+ });
150
+ }
151
+ if (require.main === module) {
152
+ (() => __awaiter(void 0, void 0, void 0, function* () {
153
+ (() => __awaiter(void 0, void 0, void 0, function* () {
154
+ let addresses = [
155
+ "0x4200000000000000000000000000000000000006",
156
+ "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
157
+ "0x940181a94A35A4569E4529A3CDfB74e38FD98631",
158
+ ];
159
+ console.log(yield get_base_token_price_info(addresses));
160
+ }))();
161
+ }))();
162
+ }
@@ -20,8 +20,8 @@ const gecko_terminal_1 = require("./gecko_terminal");
20
20
  const price_cache_1 = require("./price_cache");
21
21
  function get_tron_token_price_info(addresses) {
22
22
  return __awaiter(this, void 0, void 0, function* () {
23
+ (0, index_1.log_info)(`get_tron_token_price_info`, addresses);
23
24
  const result = new Map();
24
- addresses = addresses.map(e => e.toUpperCase());
25
25
  const PRICE_CHANNELS = [
26
26
  {
27
27
  name: 'CachedPrice',
@@ -192,10 +192,8 @@ function fetchPriceFromTronscanALL(addresses) {
192
192
  if (require.main === module) {
193
193
  (() => __awaiter(void 0, void 0, void 0, function* () {
194
194
  let addresses = [
195
- "TNUC9Qb1rRpS5CbWLmNMxXBjyFoydXjWFR",
196
- "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
197
- "TCFLL5dx5ZJdKnWuesXxi1VPwjLVmWZZy9",
195
+ "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
196
+ "TMacq4TDUw5q8NFBwmbY4RLXvzvG5JTkvi",
198
197
  ];
199
- console.log(yield (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)(index_1.CHAIN_ID.TRON, addresses));
200
198
  }))();
201
199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "2.0.37",
3
+ "version": "2.0.39",
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": "types/index.d.ts",