@clonegod/ttd-core 3.1.43 → 3.1.45

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.
@@ -71,15 +71,35 @@ function getWsClient() {
71
71
  return null;
72
72
  if (!_wsClient) {
73
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);
74
+ _wsClient = new ws_client_1.AnalyzeWsClient(url, () => buildHello());
75
75
  }
76
76
  return _wsClient;
77
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
+ }
78
97
  const WS_TYPES = new Set([
79
98
  'PreTradeEvent',
80
99
  'PriceMessageType',
81
100
  'DepthLevels',
82
101
  'QuoteVerify',
102
+ 'QuoteCandidate',
83
103
  ]);
84
104
  const HTTP_PATH = {
85
105
  OrderMessageType: '/ingest/order',
@@ -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', () => {
@@ -30,4 +30,13 @@ export interface QuoteResponseOptions {
30
30
  depth?: PoolDepthData;
31
31
  }
32
32
  export declare function on_quote_response(opts: QuoteResponseOptions): void;
33
+ export declare function report_quote_candidate(opts: {
34
+ pool_address: string;
35
+ pair: string;
36
+ dex_id: string;
37
+ source: string;
38
+ block_number: number;
39
+ ask_price: number | string;
40
+ bid_price: number | string;
41
+ }): void;
33
42
  export declare function on_quote_respose(appConfig: AppConfig, pool_info: StandardPoolInfoType, quote_amount_usd: number, _execution_price: number, stream_time: number, quote_start_time: number, slot_info: string, result: [QuoteResultType, QuoteResultType, number], txid: string, push_price_message?: boolean, source?: string, depth?: PoolDepthData): void;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.on_quote_response = on_quote_response;
4
+ exports.report_quote_candidate = report_quote_candidate;
4
5
  exports.on_quote_respose = on_quote_respose;
5
6
  const analyze_1 = require("../analyze");
6
7
  const log_quote_price_1 = require("./log_quote_price");
@@ -32,7 +33,7 @@ function on_quote_response(opts) {
32
33
  price_id: price_msg.price_id,
33
34
  unique_orderbook_id: price_msg.unique_orderbook_id,
34
35
  pair: price_msg.pair,
35
- depth
36
+ depth,
36
37
  });
37
38
  }
38
39
  })
@@ -40,6 +41,20 @@ function on_quote_response(opts) {
40
41
  console.error(`[on_quote_response] error: ${(err === null || err === void 0 ? void 0 : err.message) || err}`);
41
42
  });
42
43
  }
44
+ function report_quote_candidate(opts) {
45
+ if (!opts.pool_address || !opts.block_number || !opts.source)
46
+ return;
47
+ (0, analyze_1.report_data_to_analyze)('QuoteCandidate', {
48
+ pool_address: opts.pool_address.toLowerCase(),
49
+ pair: opts.pair || '',
50
+ dex_id: opts.dex_id || '',
51
+ source: opts.source,
52
+ block_number: opts.block_number,
53
+ ask_price: Number(opts.ask_price) || 0,
54
+ bid_price: Number(opts.bid_price) || 0,
55
+ computed_at: Date.now(),
56
+ });
57
+ }
43
58
  function on_quote_respose(appConfig, pool_info, quote_amount_usd, _execution_price, stream_time, quote_start_time, slot_info, result, txid, push_price_message = true, source = "", depth) {
44
59
  on_quote_response({
45
60
  appConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "3.1.43",
3
+ "version": "3.1.45",
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",
package/types/index.d.ts CHANGED
@@ -940,6 +940,9 @@ export interface PostTradeType {
940
940
 
941
941
  /** 同区块分析 (MEV) */
942
942
  same_block: AnalyzeSameBlockType
943
+
944
+ /** 完整 OnTrade 记录:时间线 / 成交结果 / 盘口快照 / 偏差 — 给 UI 渲染时间线和滑点归因用 */
945
+ ontrade?: OnTradeType
943
946
  }
944
947
 
945
948
  export interface AnalyzeQuoteSnapshotType {