@clonegod/ttd-core 2.0.57 → 2.0.59

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,2 @@
1
+ import { FormattedTokenPrice } from "../../index";
2
+ export declare function fetchPriceFromDefiLlama(network: string, addresses: string[]): Promise<Map<string, FormattedTokenPrice>>;
@@ -0,0 +1,65 @@
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.fetchPriceFromDefiLlama = fetchPriceFromDefiLlama;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const index_1 = require("../../index");
18
+ function fetchPriceFromDefiLlama(network, addresses) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const result = new Map();
21
+ const currentTime = (0, index_1.getCurDateTime)();
22
+ const timeout = 10000;
23
+ const maxRetries = 3;
24
+ const retrysleepMs = 1000;
25
+ const addressString = addresses.map(address => `${network.toLowerCase()}:${address}`).join(',');
26
+ const url = `https://coins.llama.fi/prices/current/${addressString}`;
27
+ console.log(url);
28
+ (0, index_1.log_debug)(`[fetchPriceFromDefiLlama] Requesting prices for ${addresses.length} tokens`);
29
+ let retryCount = 0;
30
+ while (retryCount <= maxRetries) {
31
+ try {
32
+ const response = yield axios_1.default.get(url, { timeout });
33
+ let tokenPrices = response.data['coins'];
34
+ if (tokenPrices) {
35
+ for (const address of addresses) {
36
+ let network_token_address = `${network.toLowerCase()}:${address}`;
37
+ if (tokenPrices[network_token_address]) {
38
+ result.set(address, {
39
+ address: address,
40
+ price: tokenPrices[network_token_address].price,
41
+ symbol: tokenPrices[network_token_address].symbol,
42
+ name: '',
43
+ update_time: currentTime
44
+ });
45
+ }
46
+ }
47
+ }
48
+ else {
49
+ (0, index_1.log_debug)(`[fetchPriceFromDefiLlama] Invalid response format from DefiLlama API`);
50
+ }
51
+ break;
52
+ }
53
+ catch (error) {
54
+ retryCount++;
55
+ (0, index_1.log_warn)(`[fetchPriceFromDefiLlama] Request failed (attempt ${retryCount}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`);
56
+ if (retryCount <= maxRetries) {
57
+ const currentRetrysleep = retrysleepMs * retryCount;
58
+ (0, index_1.log_debug)(`[fetchPriceFromDefiLlama] Retrying in ${currentRetrysleep}ms...`);
59
+ yield (0, index_1.sleep)(currentRetrysleep);
60
+ }
61
+ }
62
+ }
63
+ return result;
64
+ });
65
+ }
@@ -0,0 +1,2 @@
1
+ import { FormattedTokenPrice } from "../types";
2
+ export declare function get_sui_token_price_info(addresses: string[]): 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_sui_token_price_info = get_sui_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_sui_token_price_info(addresses) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ (0, index_1.log_info)(`get_sui_token_price_info`, addresses);
21
+ const result = new Map();
22
+ const PRICE_CHANNELS = [
23
+ {
24
+ name: 'CachedPrice',
25
+ fetchFn: price_cache_1.fetchPriceFromCache,
26
+ batchSize: 100,
27
+ batchDelay: 1000,
28
+ },
29
+ {
30
+ name: 'DefiLlama',
31
+ fetchFn: (address_list) => (0, defi_llama_1.fetchPriceFromDefiLlama)(index_1.CHAIN_ID.SUI, address_list),
32
+ batchSize: 10,
33
+ batchDelay: 2000,
34
+ },
35
+ {
36
+ name: 'GeckoTerminal',
37
+ fetchFn: (address_list) => (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)('sui-network', address_list),
38
+ batchSize: 10,
39
+ batchDelay: 2000,
40
+ },
41
+ ];
42
+ try {
43
+ for (const channel of PRICE_CHANNELS) {
44
+ if (addresses.length === 0)
45
+ break;
46
+ (0, index_1.log_debug)(`[get_token_price_info] Processing ${addresses.length} tokens using ${channel.name}`);
47
+ const batches = (0, index_1.chunkArray)(addresses, channel.batchSize);
48
+ (0, index_1.log_debug)(`[get_token_price_info] Split into ${batches.length} batches of size ${channel.batchSize}`);
49
+ let remainingAddresses = [...addresses];
50
+ for (let i = 0; i < batches.length; i++) {
51
+ const batch = batches[i];
52
+ if (batch.length === 0)
53
+ continue;
54
+ (0, index_1.log_debug)(`[get_token_price_info] Processing batch ${i + 1}/${batches.length} (${batch.length} tokens) with ${channel.name}`);
55
+ try {
56
+ const channelResult = yield channel.fetchFn(batch);
57
+ for (const [address, priceInfo] of channelResult.entries()) {
58
+ result.set(address, priceInfo);
59
+ remainingAddresses = remainingAddresses.filter(addr => addr !== address);
60
+ if (channel.name !== 'CachedPrice') {
61
+ (0, price_cache_1.cache_new_market_price)(address, priceInfo.price, channel.name);
62
+ }
63
+ }
64
+ (0, index_1.log_debug)(`[get_token_price_info] ${channel.name} found prices for ${channelResult.size}/${batch.length} tokens in batch ${i + 1}`);
65
+ }
66
+ catch (error) {
67
+ (0, index_1.log_warn)(`[get_token_price_info] Error processing batch ${i + 1} with ${channel.name}: ${error instanceof Error ? error.message : String(error)}`);
68
+ }
69
+ if (i < batches.length - 1) {
70
+ yield (0, index_1.sleep)(channel.batchDelay);
71
+ }
72
+ }
73
+ addresses = [...remainingAddresses];
74
+ if (addresses.length === 0) {
75
+ (0, index_1.log_debug)(`[get_token_price_info] All token prices retrieved using ${channel.name}`);
76
+ break;
77
+ }
78
+ }
79
+ if (result.size === 0) {
80
+ throw new Error(`Unable to get price information for any token: ${addresses.join(', ')}`);
81
+ }
82
+ if (addresses.length > 0) {
83
+ (0, index_1.log_warn)(`[get_token_price_info] Failed to get prices for ${addresses.length} tokens after trying all channels: ${addresses.join(', ')}`);
84
+ }
85
+ (0, index_1.log_debug)(`[get_token_price_info] Completed price fetching for ${result.size} tokens`);
86
+ return result;
87
+ }
88
+ catch (error) {
89
+ throw new Error(`Failed to get token price information: ${error instanceof Error ? error.message : String(error)}`);
90
+ }
91
+ });
92
+ }
93
+ if (require.main === module) {
94
+ (() => __awaiter(void 0, void 0, void 0, function* () {
95
+ let addresses = [
96
+ "0x2::sui::SUI",
97
+ "0x375f70cf2ae4c00bf37117d0c85a2c71545e6ee05c4a5c7d282cd66a4504b068::usdt::USDT",
98
+ "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
99
+ "0x356a26eb9e012a68958082340d4c4116e7f55615cf27affcff209cf0ae544f59::wal::WAL",
100
+ ];
101
+ }))();
102
+ }
@@ -2,3 +2,4 @@ export * from './get_bsc_token_price';
2
2
  export * from './get_eth_token_price';
3
3
  export * from './get_solana_token_price';
4
4
  export * from './get_tron_token_price';
5
+ export * from './get_sui_token_price';
@@ -18,3 +18,4 @@ __exportStar(require("./get_bsc_token_price"), exports);
18
18
  __exportStar(require("./get_eth_token_price"), exports);
19
19
  __exportStar(require("./get_solana_token_price"), exports);
20
20
  __exportStar(require("./get_tron_token_price"), exports);
21
+ __exportStar(require("./get_sui_token_price"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "2.0.57",
3
+ "version": "2.0.59",
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",