@clonegod/ttd-sui-common 1.0.8 → 1.0.10

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.
@@ -1,5 +0,0 @@
1
- export declare const sui_dex_package_id_list: {
2
- dexName: string;
3
- packageIds: string[];
4
- }[];
5
- export declare const is_dex_package_id: (package_id: string) => boolean;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.is_dex_package_id = exports.sui_dex_package_id_list = void 0;
4
- exports.sui_dex_package_id_list = [
5
- {
6
- dexName: 'Momentum CLMM',
7
- packageIds: [
8
- '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860',
9
- '0xcf60a40f45d46fc1e828871a647c1e25a0915dec860d2662eb10fdb382c3c1d1'
10
- ],
11
- },
12
- {
13
- dexName: 'Cetus',
14
- packageIds: [],
15
- }
16
- ];
17
- const is_dex_package_id = (package_id) => {
18
- return exports.sui_dex_package_id_list.some((dex) => dex.packageIds.includes(package_id));
19
- };
20
- exports.is_dex_package_id = is_dex_package_id;
package/dist/index.d.ts CHANGED
@@ -3,4 +3,4 @@ export * from './grpc';
3
3
  export * from './utils';
4
4
  export * from './type';
5
5
  export * from './grpc';
6
- export * from './constants';
6
+ export * from './utils/checkpoint';
package/dist/index.js CHANGED
@@ -19,4 +19,4 @@ __exportStar(require("./grpc"), exports);
19
19
  __exportStar(require("./utils"), exports);
20
20
  __exportStar(require("./type"), exports);
21
21
  __exportStar(require("./grpc"), exports);
22
- __exportStar(require("./constants"), exports);
22
+ __exportStar(require("./utils/checkpoint"), exports);
@@ -10,11 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const dist_1 = require("@clonegod/ttd-core/dist");
13
+ const fs_1 = require("fs");
13
14
  const grpc_connection_1 = require("../grpc/grpc-connection");
14
15
  const subscription_service_1 = require("../grpc/subscription-service");
15
- const fs_1 = require("fs");
16
- const decode_1 = require("../utils/decode");
17
- const constants_1 = require("../constants");
16
+ const checkpoint_1 = require("../utils/checkpoint");
18
17
  const grpc_endpoint = process.env.SUI_GRPC_ENDPOINT;
19
18
  const grpc_token = process.env.SUI_GRPC_TOKEN;
20
19
  const test_subscribe_checkpoints = () => __awaiter(void 0, void 0, void 0, function* () {
@@ -22,8 +21,10 @@ const test_subscribe_checkpoints = () => __awaiter(void 0, void 0, void 0, funct
22
21
  const subscriptionService = new subscription_service_1.SubscriptionService(connection);
23
22
  const readMask = ['*'];
24
23
  subscriptionService.subscribeCheckpoints((response) => {
25
- response = (0, decode_1.decodeBytes)(response);
26
- (0, dist_1.log_info)(response.cursor + ' - ' + response.checkpoint.transactions.length);
24
+ const blockHash = response.checkpoint.digest;
25
+ const blockNumber = response.checkpoint.sequence_number;
26
+ const blockTime = response.checkpoint.summary.timestamp.seconds;
27
+ (0, dist_1.log_info)(blockNumber + ' - ' + response.checkpoint.transactions.length);
27
28
  response.checkpoint.contents = {};
28
29
  response.checkpoint.summary = {};
29
30
  response.checkpoint.signature = {};
@@ -38,27 +39,34 @@ const test_subscribe_checkpoints = () => __awaiter(void 0, void 0, void 0, funct
38
39
  response.checkpoint.transactions = response.checkpoint.transactions.filter((transaction) => {
39
40
  return transaction.events && transaction.events.events && transaction.events.events.length > 0;
40
41
  });
41
- const all_tx_events = response.checkpoint.transactions.map((transaction) => {
42
- return {
43
- kind: transaction.transaction.kind.kind,
44
- checkpoint: response.cursor,
45
- digest: transaction.digest,
46
- events: transaction.events.events.map((event) => event.event_type) || []
47
- };
48
- });
49
- let liquidity_related_events = [];
50
- if (all_tx_events && all_tx_events.length > 0) {
51
- for (const one_tx_events of all_tx_events) {
52
- for (let event_type of one_tx_events.events) {
53
- if ((0, constants_1.is_dex_package_id)(event_type.split('::')[0])) {
54
- liquidity_related_events.push(event_type);
55
- }
56
- }
42
+ let blockUpdateEvent = {
43
+ blockNumber,
44
+ blockHash,
45
+ blockTime,
46
+ transactions: []
47
+ };
48
+ response.checkpoint.transactions.forEach((transaction) => {
49
+ let event_types = transaction.events.events.map((event) => event.event_type) || [];
50
+ if (event_types.some((event_type) => (0, checkpoint_1.is_dex_package_id)(event_type.split('::')[0]))) {
51
+ blockUpdateEvent.transactions.push({
52
+ txHash: transaction.digest,
53
+ txIndex: transaction.index || 0,
54
+ txType: transaction.transaction.kind.kind,
55
+ events: event_types.map((event_type) => {
56
+ return {
57
+ dex_name: (0, checkpoint_1.get_dex_name_by_package_id)(event_type.split('::')[0]),
58
+ event_type: event_type
59
+ };
60
+ }) || []
61
+ });
57
62
  }
58
- console.log('liquidity_related_events', liquidity_related_events.length);
59
- }
60
- if (liquidity_related_events.length > 0) {
63
+ });
64
+ if (blockUpdateEvent.transactions.length > 0) {
61
65
  (0, fs_1.writeFileSync)(`./dist/checkpoint_${response.cursor}_decoded.json`, JSON.stringify(response, null, 2));
66
+ console.dir(blockUpdateEvent, { depth: null });
67
+ }
68
+ else {
69
+ console.log(`no liquidity related events: ${response.cursor}, ${response.checkpoint.digest}`);
62
70
  }
63
71
  }, (error) => {
64
72
  (0, dist_1.log_info)(`error`, error);
@@ -1,45 +1,26 @@
1
1
  export interface BlockUpdateEvent {
2
+ blockHash: string;
2
3
  blockNumber: number;
3
- blockTime: string;
4
+ blockTime: number;
4
5
  transactions: TransactionData[];
5
6
  }
6
7
  export interface TransactionData {
7
8
  txHash: string;
8
9
  txIndex: number;
9
- events: DexEventData[];
10
+ txType: string;
11
+ events: SimpleTxEventType[];
10
12
  }
11
- export interface DexEventData {
12
- eventIndex: number;
13
- packageId: string;
14
- dexName: string;
15
- poolId: string;
16
- eventName: string;
17
- eventType: string;
18
- liquidity: string | null;
19
- sqrtPrice: string | null;
20
- tick: number | null;
21
- sender: string | null;
13
+ export interface SimpleTxEventType {
14
+ dex_name: string;
15
+ event_type: string;
16
+ poolId?: string | null;
17
+ liquidity?: string | null;
18
+ sqrtPrice?: string | null;
19
+ tick?: number | null;
20
+ sender?: string | null;
22
21
  amountX: string | null;
23
- amountY: string | null;
24
- xForY: boolean | null;
25
- reserveX: string | null;
26
- reserveY: string | null;
27
- }
28
- export interface SimpleTxEventDataType {
29
- txHash: string;
30
- txIndex: number;
31
- dexId: string;
32
- dexName: string;
33
- poolId: string;
34
- eventName: string;
35
- eventType: string;
36
- liquidity: string | null;
37
- sqrtPrice: string | null;
38
- tick: number | null;
39
- sender: string | null;
40
- amountX: string | null;
41
- amountY: string | null;
42
- xForY: boolean | null;
43
- reserveX: string | null;
44
- reserveY: string | null;
22
+ amountY?: string | null;
23
+ xForY?: boolean | null;
24
+ reserveX?: string | null;
25
+ reserveY?: string | null;
45
26
  }
@@ -0,0 +1,7 @@
1
+ import { DEX_ID } from "@clonegod/ttd-core/dist";
2
+ export declare const sui_dex_package_id_list: {
3
+ dexName: DEX_ID;
4
+ packageIds: string[];
5
+ }[];
6
+ export declare const is_dex_package_id: (package_id: string) => boolean;
7
+ export declare const get_dex_name_by_package_id: (package_id: string) => DEX_ID | "Unknown DEX";
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.get_dex_name_by_package_id = exports.is_dex_package_id = exports.sui_dex_package_id_list = void 0;
4
+ const dist_1 = require("@clonegod/ttd-core/dist");
5
+ exports.sui_dex_package_id_list = [
6
+ {
7
+ dexName: dist_1.DEX_ID.MOMENTUM_CLMM,
8
+ packageIds: [
9
+ '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860',
10
+ '0xcf60a40f45d46fc1e828871a647c1e25a0915dec860d2662eb10fdb382c3c1d1'
11
+ ],
12
+ },
13
+ {
14
+ dexName: dist_1.DEX_ID.CETUS_CLMM,
15
+ packageIds: [],
16
+ }
17
+ ];
18
+ const is_dex_package_id = (package_id) => {
19
+ return exports.sui_dex_package_id_list.some((dex) => dex.packageIds.includes(package_id));
20
+ };
21
+ exports.is_dex_package_id = is_dex_package_id;
22
+ const get_dex_name_by_package_id = (package_id) => {
23
+ var _a;
24
+ return ((_a = exports.sui_dex_package_id_list.find((dex) => dex.packageIds.includes(package_id))) === null || _a === void 0 ? void 0 : _a.dexName) || 'Unknown DEX';
25
+ };
26
+ exports.get_dex_name_by_package_id = get_dex_name_by_package_id;
@@ -1,26 +0,0 @@
1
- import { TransactionData } from '../type';
2
- export declare class SuiCheckpointParser {
3
- static processCheckpoint(checkpoint: any): TransactionData[];
4
- static processCheckpoints(checkpoints: any[]): {
5
- totalCheckpoints: number;
6
- totalTransactions: number;
7
- totalEvents: number;
8
- transactions: TransactionData[];
9
- dexStats: {
10
- [dex: string]: number;
11
- };
12
- poolStats: {
13
- [poolId: string]: any;
14
- };
15
- };
16
- static getPoolSummary(poolStats: {
17
- [poolId: string]: any;
18
- }): any;
19
- private static parseEvent;
20
- private static parseGenericEvent;
21
- private static extractPackageAddress;
22
- private static parseMomentumEvent;
23
- private static extractEventName;
24
- private static parseMomentumSwapEvent;
25
- private static parseMomentumRepayFlashSwapEvent;
26
- }
@@ -1,277 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SuiCheckpointParser = void 0;
4
- const bcs_1 = require("@mysten/sui/bcs");
5
- const decode_1 = require("./decode");
6
- class SuiCheckpointParser {
7
- static processCheckpoint(checkpoint) {
8
- var _a;
9
- const transactions = [];
10
- if (checkpoint.transactions) {
11
- for (let txIndex = 0; txIndex < checkpoint.transactions.length; txIndex++) {
12
- const tx = checkpoint.transactions[txIndex];
13
- const events = [];
14
- if ((_a = tx.events) === null || _a === void 0 ? void 0 : _a.events) {
15
- for (let i = 0; i < tx.events.events.length; i++) {
16
- const event = tx.events.events[i];
17
- const parsedEvent = this.parseEvent(event, i);
18
- if (parsedEvent) {
19
- events.push(parsedEvent);
20
- }
21
- }
22
- }
23
- if (events.length > 0) {
24
- const transactionData = {
25
- txHash: tx.digest || '',
26
- txIndex: txIndex,
27
- events: events
28
- };
29
- transactions.push(transactionData);
30
- }
31
- }
32
- }
33
- return transactions;
34
- }
35
- static processCheckpoints(checkpoints) {
36
- const result = {
37
- totalCheckpoints: checkpoints.length,
38
- totalTransactions: 0,
39
- totalEvents: 0,
40
- transactions: [],
41
- dexStats: {},
42
- poolStats: {}
43
- };
44
- for (const checkpoint of checkpoints) {
45
- const transactions = this.processCheckpoint(checkpoint);
46
- result.totalTransactions += transactions.length;
47
- result.transactions.push(...transactions);
48
- for (const tx of transactions) {
49
- for (const event of tx.events) {
50
- result.totalEvents++;
51
- if (event.dexName) {
52
- result.dexStats[event.dexName] = (result.dexStats[event.dexName] || 0) + 1;
53
- }
54
- if (event.poolId) {
55
- if (!result.poolStats[event.poolId]) {
56
- result.poolStats[event.poolId] = {
57
- dexName: event.dexName,
58
- eventCount: 0,
59
- lastLiquidity: null,
60
- lastSqrtPrice: null,
61
- lastTick: null,
62
- lastReserveX: null,
63
- lastReserveY: null,
64
- lastUpdate: null
65
- };
66
- }
67
- result.poolStats[event.poolId].eventCount++;
68
- result.poolStats[event.poolId].lastUpdate = new Date().toISOString();
69
- if (event.liquidity) {
70
- result.poolStats[event.poolId].lastLiquidity = event.liquidity;
71
- }
72
- if (event.sqrtPrice) {
73
- result.poolStats[event.poolId].lastSqrtPrice = event.sqrtPrice;
74
- }
75
- if (event.tick !== null) {
76
- result.poolStats[event.poolId].lastTick = event.tick;
77
- }
78
- if (event.reserveX) {
79
- result.poolStats[event.poolId].lastReserveX = event.reserveX;
80
- }
81
- if (event.reserveY) {
82
- result.poolStats[event.poolId].lastReserveY = event.reserveY;
83
- }
84
- }
85
- }
86
- }
87
- }
88
- if (result.transactions.length > 0) {
89
- console.log('\n=== 最新交易示例 ===');
90
- const latestTx = result.transactions[0];
91
- const checkpoint = checkpoints[0];
92
- console.log(`区块: ${checkpoint.sequence_number} (${checkpoint.timestamp})`);
93
- console.log(`交易: ${latestTx.txHash}`);
94
- console.log(`事件数: ${latestTx.events.length}`);
95
- for (const event of latestTx.events) {
96
- console.log(` 事件 ${event.eventIndex}: ${event.dexName} (${event.packageId}) - ${event.eventName}`);
97
- console.log(` 池子: ${event.poolId}`);
98
- if (event.liquidity) {
99
- console.log(` 流动性: ${event.liquidity}`);
100
- }
101
- if (event.sqrtPrice) {
102
- console.log(` 价格: ${event.sqrtPrice}`);
103
- }
104
- if (event.amountX && event.amountY) {
105
- console.log(` 交易: ${event.amountX} X → ${event.amountY} Y`);
106
- }
107
- }
108
- }
109
- return result;
110
- }
111
- static getPoolSummary(poolStats) {
112
- const pools = Object.entries(poolStats)
113
- .sort(([, a], [, b]) => b.eventCount - a.eventCount);
114
- return {
115
- totalPools: pools.length,
116
- topPools: pools.slice(0, 10).map(([poolId, stats]) => ({
117
- poolId,
118
- dexName: stats.dexName,
119
- eventCount: stats.eventCount,
120
- lastLiquidity: stats.lastLiquidity,
121
- lastSqrtPrice: stats.lastSqrtPrice,
122
- lastTick: stats.lastTick,
123
- lastReserveX: stats.lastReserveX,
124
- lastReserveY: stats.lastReserveY,
125
- lastUpdate: stats.lastUpdate
126
- }))
127
- };
128
- }
129
- static parseEvent(eventContent, eventIndex) {
130
- try {
131
- const eventType = eventContent.name;
132
- if (!eventType) {
133
- return null;
134
- }
135
- if (eventType.includes('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860')) {
136
- return this.parseMomentumEvent(eventContent, eventIndex);
137
- }
138
- return this.parseGenericEvent(eventContent, eventIndex);
139
- }
140
- catch (error) {
141
- console.error('解析事件失败:', error);
142
- return null;
143
- }
144
- }
145
- static parseGenericEvent(eventContent, eventIndex) {
146
- try {
147
- const eventType = eventContent.name;
148
- const packageId = this.extractPackageAddress(eventType);
149
- let dexName = 'Unknown DEX';
150
- if (packageId === '0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb') {
151
- dexName = 'Cetus';
152
- }
153
- else if (packageId === '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860') {
154
- dexName = 'Momentum CLMM';
155
- }
156
- const eventData = {
157
- eventIndex: eventIndex,
158
- packageId: packageId,
159
- dexName: dexName,
160
- poolId: '',
161
- eventName: this.extractEventName(eventType),
162
- eventType: eventType,
163
- liquidity: null,
164
- sqrtPrice: null,
165
- tick: null,
166
- sender: null,
167
- amountX: null,
168
- amountY: null,
169
- xForY: null,
170
- reserveX: null,
171
- reserveY: null
172
- };
173
- return eventData;
174
- }
175
- catch (error) {
176
- console.error('解析通用事件失败:', error);
177
- return null;
178
- }
179
- }
180
- static extractPackageAddress(eventType) {
181
- const parts = eventType.split('::');
182
- if (parts.length >= 1 && parts[0].startsWith('0x')) {
183
- return parts[0];
184
- }
185
- return 'unknown';
186
- }
187
- static parseMomentumEvent(eventContent, eventIndex) {
188
- var _a, _b, _c, _d;
189
- try {
190
- const decodedContent = (0, decode_1.decodeBytes)(eventContent);
191
- if (!((_a = decodedContent.value) === null || _a === void 0 ? void 0 : _a.data) || typeof decodedContent.value.data !== 'string') {
192
- return null;
193
- }
194
- const bytes = new Uint8Array(Buffer.from(decodedContent.value.data, 'hex'));
195
- let parsedData = null;
196
- if (((_b = eventContent.name) === null || _b === void 0 ? void 0 : _b.includes('SwapEvent')) && !((_c = eventContent.name) === null || _c === void 0 ? void 0 : _c.includes('RepayFlashSwapEvent'))) {
197
- parsedData = this.parseMomentumSwapEvent(bytes);
198
- }
199
- else if ((_d = eventContent.name) === null || _d === void 0 ? void 0 : _d.includes('RepayFlashSwapEvent')) {
200
- parsedData = this.parseMomentumRepayFlashSwapEvent(bytes);
201
- }
202
- if (!parsedData) {
203
- return null;
204
- }
205
- const eventData = {
206
- eventIndex: eventIndex,
207
- packageId: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860',
208
- dexName: 'Momentum CLMM',
209
- poolId: parsedData.pool_id || '',
210
- eventName: this.extractEventName(eventContent.name),
211
- eventType: eventContent.name,
212
- liquidity: parsedData.liquidity ? parsedData.liquidity.toString() : null,
213
- sqrtPrice: parsedData.sqrt_price_after ? parsedData.sqrt_price_after.toString() : null,
214
- tick: parsedData.tick_index !== undefined ? parsedData.tick_index : null,
215
- sender: parsedData.sender || null,
216
- amountX: parsedData.amount_x ? parsedData.amount_x.toString() : null,
217
- amountY: parsedData.amount_y ? parsedData.amount_y.toString() : null,
218
- xForY: parsedData.x_for_y !== undefined ? parsedData.x_for_y : null,
219
- reserveX: parsedData.reserve_x ? parsedData.reserve_x.toString() : null,
220
- reserveY: parsedData.reserve_y ? parsedData.reserve_y.toString() : null
221
- };
222
- return eventData;
223
- }
224
- catch (error) {
225
- console.error('解析 Momentum 事件失败:', error);
226
- return null;
227
- }
228
- }
229
- static extractEventName(fullEventType) {
230
- const parts = fullEventType.split('::');
231
- return parts[parts.length - 1] || fullEventType;
232
- }
233
- static parseMomentumSwapEvent(eventData) {
234
- try {
235
- const SwapEventSchema = bcs_1.bcs.struct('MomentumSwapEvent', {
236
- sender: bcs_1.bcs.Address,
237
- pool_id: bcs_1.bcs.Address,
238
- x_for_y: bcs_1.bcs.Bool,
239
- amount_x: bcs_1.bcs.U64,
240
- amount_y: bcs_1.bcs.U64,
241
- sqrt_price_before: bcs_1.bcs.U128,
242
- sqrt_price_after: bcs_1.bcs.U128,
243
- liquidity: bcs_1.bcs.U128,
244
- tick_index: bcs_1.bcs.U32,
245
- fee_amount: bcs_1.bcs.U64,
246
- protocol_fee: bcs_1.bcs.U64,
247
- reserve_x: bcs_1.bcs.U64,
248
- reserve_y: bcs_1.bcs.U64
249
- });
250
- return SwapEventSchema.parse(eventData);
251
- }
252
- catch (error) {
253
- console.error('解析 Momentum SwapEvent 失败:', error);
254
- return null;
255
- }
256
- }
257
- static parseMomentumRepayFlashSwapEvent(eventData) {
258
- try {
259
- const RepayFlashSwapEventSchema = bcs_1.bcs.struct('MomentumRepayFlashSwapEvent', {
260
- sender: bcs_1.bcs.Address,
261
- pool_id: bcs_1.bcs.Address,
262
- amount_x_debt: bcs_1.bcs.U64,
263
- amount_y_debt: bcs_1.bcs.U64,
264
- paid_x: bcs_1.bcs.U64,
265
- paid_y: bcs_1.bcs.U64,
266
- reserve_x: bcs_1.bcs.U64,
267
- reserve_y: bcs_1.bcs.U64
268
- });
269
- return RepayFlashSwapEventSchema.parse(eventData);
270
- }
271
- catch (error) {
272
- console.error('解析 Momentum RepayFlashSwapEvent 失败:', error);
273
- return null;
274
- }
275
- }
276
- }
277
- exports.SuiCheckpointParser = SuiCheckpointParser;
@@ -1,2 +1,2 @@
1
- export * from './checkpoint_parse';
2
1
  export * from './decode';
2
+ export * from './checkpoint';
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./checkpoint_parse"), exports);
18
17
  __exportStar(require("./decode"), exports);
18
+ __exportStar(require("./checkpoint"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sui-common",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Sui common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",