@clonegod/ttd-core 3.1.42 → 3.1.44

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.
@@ -53,27 +53,47 @@ const index_1 = require("../index");
53
53
  const service_ports_1 = require("../constants/service_ports");
54
54
  const ws_client_1 = require("./ws_client");
55
55
  Object.defineProperty(exports, "AnalyzeWsClient", { enumerable: true, get: function () { return ws_client_1.AnalyzeWsClient; } });
56
- const _env = (() => {
56
+ const httpHeaders = { 'Content-Type': 'application/json' };
57
+ const httpAgent = new http.Agent({ keepAlive: true, maxSockets: 10 });
58
+ function getAnalyzeHost() {
59
+ var _a;
57
60
  try {
58
- return (0, core_env_1.getCoreEnv)();
61
+ return ((_a = (0, core_env_1.getCoreEnv)()) === null || _a === void 0 ? void 0 : _a.trade_analyze_host) || '';
59
62
  }
60
- catch (_a) {
61
- return null;
63
+ catch (_b) {
64
+ return '';
62
65
  }
63
- })();
64
- const ANALYZE_HOST = (_env === null || _env === void 0 ? void 0 : _env.trade_analyze_host) || '';
65
- const ANALYZE_HTTP_BASE = ANALYZE_HOST ? `http://${ANALYZE_HOST}:${service_ports_1.SERVICE_PORT.TRADE_ANALYZE_HTTP}/trade/analyze` : '';
66
- const ANALYZE_WS_URL = ANALYZE_HOST ? `ws://${ANALYZE_HOST}:${service_ports_1.SERVICE_PORT.TRADE_ANALYZE_HTTP}/trade/analyze/ws/ingest` : '';
67
- const httpHeaders = { 'Content-Type': 'application/json' };
68
- const httpAgent = new http.Agent({ keepAlive: true, maxSockets: 10 });
66
+ }
69
67
  let _wsClient = null;
70
68
  function getWsClient() {
71
- if (!ANALYZE_WS_URL)
69
+ const host = getAnalyzeHost();
70
+ if (!host)
72
71
  return null;
73
- if (!_wsClient)
74
- _wsClient = new ws_client_1.AnalyzeWsClient(ANALYZE_WS_URL);
72
+ if (!_wsClient) {
73
+ const url = `ws://${host}:${service_ports_1.SERVICE_PORT.TRADE_ANALYZE_HTTP}/trade/analyze/ws/ingest`;
74
+ _wsClient = new ws_client_1.AnalyzeWsClient(url, () => buildHello());
75
+ }
75
76
  return _wsClient;
76
77
  }
78
+ function buildHello() {
79
+ let env = {};
80
+ try {
81
+ env = (0, core_env_1.getCoreEnv)() || {};
82
+ }
83
+ catch (_a) { }
84
+ const hasQuotePool = !!(env.quote_pool_address || env.quote_pool_name);
85
+ const hasGroup = !!env.group_id;
86
+ const kind = hasQuotePool ? 'QUOTE' : hasGroup ? 'TRADE' : 'OTHER';
87
+ return {
88
+ kind,
89
+ app_id: process.env.name || env.app_name || '',
90
+ pair: env.pair || '',
91
+ dex_id: env.dex_id || '',
92
+ group_id: env.group_id || '',
93
+ pool_name: env.quote_pool_name || '',
94
+ fee_rate: env.quote_pool_fee_rate ? Number(env.quote_pool_fee_rate) : undefined,
95
+ };
96
+ }
77
97
  const WS_TYPES = new Set([
78
98
  'PreTradeEvent',
79
99
  'PriceMessageType',
@@ -85,7 +105,8 @@ const HTTP_PATH = {
85
105
  TradeResultType: '/ingest/trade-result',
86
106
  };
87
107
  const report_data_to_analyze = (type, data) => {
88
- if (!ANALYZE_HOST)
108
+ const host = getAnalyzeHost();
109
+ if (!host)
89
110
  return;
90
111
  if (WS_TYPES.has(type)) {
91
112
  const client = getWsClient();
@@ -99,7 +120,8 @@ const report_data_to_analyze = (type, data) => {
99
120
  return;
100
121
  }
101
122
  try {
102
- axios_1.default.post(ANALYZE_HTTP_BASE + path, data, { headers: httpHeaders, timeout: 3000, httpAgent })
123
+ const url = `http://${host}:${service_ports_1.SERVICE_PORT.TRADE_ANALYZE_HTTP}/trade/analyze${path}`;
124
+ axios_1.default.post(url, data, { headers: httpHeaders, timeout: 3000, httpAgent })
103
125
  .then(() => (0, index_1.log_trace)(`[analyze] report ${type} ok`))
104
126
  .catch(err => (0, index_1.log_warn)(`[analyze] report ${type} fail: ${err.message}`));
105
127
  }
@@ -109,10 +131,11 @@ const report_data_to_analyze = (type, data) => {
109
131
  };
110
132
  exports.report_data_to_analyze = report_data_to_analyze;
111
133
  const get_pool_latest_quote_price = (unique_orderbook_id) => __awaiter(void 0, void 0, void 0, function* () {
112
- if (!ANALYZE_HTTP_BASE || (0, index_1.isEmpty)(unique_orderbook_id))
134
+ const host = getAnalyzeHost();
135
+ if (!host || (0, index_1.isEmpty)(unique_orderbook_id))
113
136
  return null;
114
137
  try {
115
- const url = `${ANALYZE_HTTP_BASE}/quote/price-msg?unique_orderbook_id=${unique_orderbook_id}`;
138
+ const url = `http://${host}:${service_ports_1.SERVICE_PORT.TRADE_ANALYZE_HTTP}/trade/analyze/quote/price-msg?unique_orderbook_id=${unique_orderbook_id}`;
116
139
  const res = (yield axios_1.default.get(url, { headers: httpHeaders, timeout: 3000, httpAgent })).data;
117
140
  return res.data;
118
141
  }
@@ -1,5 +1,15 @@
1
+ export interface AnalyzeWsHello {
2
+ kind: 'QUOTE' | 'TRADE' | 'OTHER';
3
+ app_id?: string;
4
+ pair?: string;
5
+ dex_id?: string;
6
+ group_id?: string;
7
+ pool_name?: string;
8
+ fee_rate?: number;
9
+ }
1
10
  export declare class AnalyzeWsClient {
2
11
  private readonly url;
12
+ private readonly helloFn?;
3
13
  private ws;
4
14
  private reconnectTimer;
5
15
  private connected;
@@ -7,7 +17,7 @@ export declare class AnalyzeWsClient {
7
17
  private readonly MAX_QUEUE;
8
18
  private droppedCount;
9
19
  private lastDroppedLogAt;
10
- constructor(url: string);
20
+ constructor(url: string, helloFn?: () => AnalyzeWsHello);
11
21
  private connect;
12
22
  private scheduleReconnect;
13
23
  private flushQueue;
@@ -7,8 +7,9 @@ exports.AnalyzeWsClient = void 0;
7
7
  const ws_1 = __importDefault(require("ws"));
8
8
  const index_1 = require("../index");
9
9
  class AnalyzeWsClient {
10
- constructor(url) {
10
+ constructor(url, helloFn) {
11
11
  this.url = url;
12
+ this.helloFn = helloFn;
12
13
  this.ws = null;
13
14
  this.reconnectTimer = null;
14
15
  this.connected = false;
@@ -24,6 +25,15 @@ class AnalyzeWsClient {
24
25
  this.ws.on('open', () => {
25
26
  this.connected = true;
26
27
  (0, index_1.log_info)(`[AnalyzeWs] connected: ${this.url}`);
28
+ if (this.helloFn) {
29
+ try {
30
+ const hello = this.helloFn();
31
+ this.ws.send(JSON.stringify({ type: 'HELLO', data: hello }));
32
+ }
33
+ catch (e) {
34
+ (0, index_1.log_debug)(`[AnalyzeWs] hello send failed: ${e.message}`);
35
+ }
36
+ }
27
37
  this.flushQueue();
28
38
  });
29
39
  this.ws.on('close', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "3.1.42",
3
+ "version": "3.1.44",
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",