@clonegod/ttd-sui-common 1.0.4 → 1.0.6

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.
Files changed (37) hide show
  1. package/dist/index.d.ts +5 -0
  2. package/dist/index.js +21 -1
  3. package/dist/parsed_swap_transactions_2025-08-30T12-11-48-744Z.json +19215 -0
  4. package/dist/parsed_swap_transactions_2025-08-30T12-51-39-586Z.json +19309 -0
  5. package/dist/parsed_swap_transactions_2025-08-30T13-58-34-579Z.json +19005 -0
  6. package/dist/quote/index.d.ts +1 -0
  7. package/dist/quote/index.js +17 -0
  8. package/dist/swap_transactions_2025-08-30T12-05-49-140Z.json +1384 -0
  9. package/dist/swap_transactions_2025-08-30T12-05-51-529Z.json +2907 -0
  10. package/dist/swap_transactions_2025-08-30T12-05-54-042Z.json +13392 -0
  11. package/dist/swap_transactions_2025-08-30T12-05-56-880Z.json +16210 -0
  12. package/dist/swap_transactions_2025-08-30T12-05-59-588Z.json +17993 -0
  13. package/dist/swap_transactions_2025-08-30T12-05-59-597Z.json +17993 -0
  14. package/dist/test/test_checkpoint_processor.d.ts +1 -0
  15. package/dist/test/test_checkpoint_processor.js +112 -0
  16. package/dist/test/test_checkpoint_subscription.d.ts +1 -0
  17. package/dist/test/test_checkpoint_subscription.js +132 -0
  18. package/dist/test/test_grpc.js +1 -1
  19. package/dist/test/test_momentum_events.d.ts +1 -0
  20. package/dist/test/test_momentum_events.js +113 -0
  21. package/dist/test/test_single_momentum_event.d.ts +1 -0
  22. package/dist/test/test_single_momentum_event.js +44 -0
  23. package/dist/type/index.d.ts +45 -0
  24. package/dist/type/index.js +2 -0
  25. package/dist/utils/bcs-parser.d.ts +8 -0
  26. package/dist/utils/bcs-parser.js +121 -0
  27. package/dist/utils/checkpoint-processor.d.ts +7 -0
  28. package/dist/utils/checkpoint-processor.js +123 -0
  29. package/dist/utils/checkpoint_parse.d.ts +26 -0
  30. package/dist/utils/checkpoint_parse.js +277 -0
  31. package/dist/utils/decode.d.ts +2 -0
  32. package/dist/utils/decode.js +43 -0
  33. package/dist/utils/dex-parser-manager.d.ts +7 -0
  34. package/dist/utils/dex-parser-manager.js +138 -0
  35. package/dist/utils/index.d.ts +2 -2
  36. package/dist/utils/index.js +16 -41
  37. package/package.json +2 -1
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckpointProcessor = void 0;
4
+ const dex_parser_manager_1 = require("./dex-parser-manager");
5
+ class CheckpointProcessor {
6
+ static processCheckpoint(checkpoint) {
7
+ var _a;
8
+ const result = {
9
+ checkpoint: {
10
+ sequenceNumber: checkpoint.sequence_number,
11
+ timestamp: checkpoint.timestamp
12
+ },
13
+ transactions: [],
14
+ dexEvents: []
15
+ };
16
+ if (checkpoint.transactions) {
17
+ for (const tx of checkpoint.transactions) {
18
+ const txResult = {
19
+ digest: tx.digest,
20
+ events: []
21
+ };
22
+ if ((_a = tx.events) === null || _a === void 0 ? void 0 : _a.events) {
23
+ for (const event of tx.events.events) {
24
+ const parsedEvent = dex_parser_manager_1.DexParserManager.parseEvent(event);
25
+ if (parsedEvent) {
26
+ txResult.events.push(Object.assign(Object.assign({}, parsedEvent), { rawEvent: event }));
27
+ result.dexEvents.push(Object.assign(Object.assign({ checkpoint: result.checkpoint, transaction: txResult }, parsedEvent), { rawEvent: event }));
28
+ }
29
+ }
30
+ }
31
+ if (txResult.events.length > 0) {
32
+ result.transactions.push(txResult);
33
+ }
34
+ }
35
+ }
36
+ return result;
37
+ }
38
+ static processCheckpoints(checkpoints) {
39
+ var _a;
40
+ const result = {
41
+ totalCheckpoints: checkpoints.length,
42
+ totalTransactions: 0,
43
+ totalDexEvents: 0,
44
+ dexEvents: [],
45
+ poolStats: {},
46
+ dexStats: {}
47
+ };
48
+ for (const checkpoint of checkpoints) {
49
+ const processed = this.processCheckpoint(checkpoint);
50
+ result.totalTransactions += processed.transactions.length;
51
+ result.totalDexEvents += processed.dexEvents.length;
52
+ result.dexEvents.push(...processed.dexEvents);
53
+ for (const event of processed.dexEvents) {
54
+ if (event.dex) {
55
+ result.dexStats[event.dex] = (result.dexStats[event.dex] || 0) + 1;
56
+ }
57
+ if ((_a = event.parsedData) === null || _a === void 0 ? void 0 : _a.pool_id) {
58
+ const poolId = event.parsedData.pool_id;
59
+ if (!result.poolStats[poolId]) {
60
+ result.poolStats[poolId] = {
61
+ dex: event.dex,
62
+ eventCount: 0,
63
+ lastPrice: null,
64
+ lastLiquidity: null,
65
+ lastReserveX: null,
66
+ lastReserveY: null,
67
+ lastUpdate: null
68
+ };
69
+ }
70
+ result.poolStats[poolId].eventCount++;
71
+ result.poolStats[poolId].lastUpdate = event.checkpoint.timestamp;
72
+ if (event.calculatedPrice) {
73
+ result.poolStats[poolId].lastPrice = event.calculatedPrice;
74
+ }
75
+ if (event.parsedData.liquidity) {
76
+ result.poolStats[poolId].lastLiquidity = event.parsedData.liquidity;
77
+ }
78
+ if (event.parsedData.reserve_x) {
79
+ result.poolStats[poolId].lastReserveX = event.parsedData.reserve_x;
80
+ }
81
+ if (event.parsedData.reserve_y) {
82
+ result.poolStats[poolId].lastReserveY = event.parsedData.reserve_y;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ if (result.dexEvents.length > 0) {
88
+ console.log('\n=== 最新事件示例 ===');
89
+ const latestEvent = result.dexEvents[0];
90
+ console.log(`DEX: ${latestEvent.dex}`);
91
+ console.log(`事件类型: ${latestEvent.eventType}`);
92
+ console.log(`池子ID: ${latestEvent.parsedData.pool_id}`);
93
+ console.log(`交换方向: x_for_y = ${latestEvent.parsedData.x_for_y}`);
94
+ console.log(`X 数量: ${latestEvent.parsedData.amount_x}`);
95
+ console.log(`Y 数量: ${latestEvent.parsedData.amount_y}`);
96
+ if (latestEvent.calculatedPrice) {
97
+ console.log(`价格: ${latestEvent.calculatedPrice.toFixed(6)}`);
98
+ }
99
+ if (latestEvent.parsedData.amount_x && latestEvent.parsedData.amount_y) {
100
+ console.log(`交易: ${latestEvent.parsedData.amount_x} X → ${latestEvent.parsedData.amount_y} Y`);
101
+ }
102
+ }
103
+ return result;
104
+ }
105
+ static getPoolSummary(poolStats) {
106
+ const pools = Object.entries(poolStats)
107
+ .sort(([, a], [, b]) => b.eventCount - a.eventCount);
108
+ return {
109
+ totalPools: pools.length,
110
+ topPools: pools.slice(0, 10).map(([poolId, stats]) => ({
111
+ poolId,
112
+ dex: stats.dex,
113
+ eventCount: stats.eventCount,
114
+ lastPrice: stats.lastPrice,
115
+ lastLiquidity: stats.lastLiquidity,
116
+ lastReserveX: stats.lastReserveX,
117
+ lastReserveY: stats.lastReserveY,
118
+ lastUpdate: stats.lastUpdate
119
+ }))
120
+ };
121
+ }
122
+ }
123
+ exports.CheckpointProcessor = CheckpointProcessor;
@@ -0,0 +1,26 @@
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
+ }
@@ -0,0 +1,277 @@
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;
@@ -0,0 +1,2 @@
1
+ export declare function decodeBytes(obj: any): any;
2
+ export declare function parseObjectAmount(objectResponse: any): string | null;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeBytes = decodeBytes;
4
+ exports.parseObjectAmount = parseObjectAmount;
5
+ function decodeBytes(obj) {
6
+ if (Array.isArray(obj)) {
7
+ return obj.map((item) => decodeBytes(item));
8
+ }
9
+ else if (obj && typeof obj === 'object') {
10
+ const result = {};
11
+ for (const key of Object.keys(obj)) {
12
+ const val = obj[key];
13
+ if (Buffer.isBuffer(val) || (Array.isArray(val) && typeof val[0] === 'number')) {
14
+ result[key] = Buffer.from(val).toString('hex');
15
+ }
16
+ else {
17
+ result[key] = decodeBytes(val);
18
+ }
19
+ }
20
+ return result;
21
+ }
22
+ return obj;
23
+ }
24
+ function parseObjectAmount(objectResponse) {
25
+ var _a, _b;
26
+ try {
27
+ if (!((_b = (_a = objectResponse === null || objectResponse === void 0 ? void 0 : objectResponse.object) === null || _a === void 0 ? void 0 : _a.contents) === null || _b === void 0 ? void 0 : _b.value)) {
28
+ return null;
29
+ }
30
+ const contentsValue = objectResponse.object.contents.value;
31
+ if (contentsValue.length < 80) {
32
+ return null;
33
+ }
34
+ const balanceHex = contentsValue.substring(64, 80);
35
+ const balanceBytes = Buffer.from(balanceHex, 'hex');
36
+ const balance = balanceBytes.readBigUInt64LE(0).toString();
37
+ return balance;
38
+ }
39
+ catch (error) {
40
+ console.error('提取代币余额失败:', error);
41
+ return null;
42
+ }
43
+ }
@@ -0,0 +1,7 @@
1
+ export declare class DexParserManager {
2
+ static parseEvent(eventContent: any): any;
3
+ private static parseMomentumEvent;
4
+ private static parseMomentumSwapEvent;
5
+ private static parseMomentumRepayFlashSwapEvent;
6
+ private static calculateMomentumPrice;
7
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DexParserManager = void 0;
7
+ const bcs_1 = require("@mysten/sui/bcs");
8
+ const decimal_js_1 = __importDefault(require("decimal.js"));
9
+ function decodeBytes(obj) {
10
+ if (Array.isArray(obj)) {
11
+ return obj.map((item) => decodeBytes(item));
12
+ }
13
+ else if (obj && typeof obj === 'object') {
14
+ const result = {};
15
+ for (const key of Object.keys(obj)) {
16
+ const val = obj[key];
17
+ if (Buffer.isBuffer(val) || (Array.isArray(val) && typeof val[0] === 'number')) {
18
+ result[key] = Buffer.from(val).toString('hex');
19
+ }
20
+ else {
21
+ result[key] = decodeBytes(val);
22
+ }
23
+ }
24
+ return result;
25
+ }
26
+ return obj;
27
+ }
28
+ class DexParserManager {
29
+ static parseEvent(eventContent) {
30
+ try {
31
+ const eventType = eventContent.name;
32
+ if (!eventType) {
33
+ return null;
34
+ }
35
+ if (eventType.includes('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860')) {
36
+ return this.parseMomentumEvent(eventContent);
37
+ }
38
+ return null;
39
+ }
40
+ catch (error) {
41
+ console.error('解析事件失败:', error);
42
+ return null;
43
+ }
44
+ }
45
+ static parseMomentumEvent(eventContent) {
46
+ var _a, _b, _c, _d;
47
+ try {
48
+ const decodedContent = decodeBytes(eventContent);
49
+ if (!((_a = decodedContent.value) === null || _a === void 0 ? void 0 : _a.data) || typeof decodedContent.value.data !== 'string') {
50
+ return null;
51
+ }
52
+ const bytes = new Uint8Array(Buffer.from(decodedContent.value.data, 'hex'));
53
+ let parsedData = null;
54
+ 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'))) {
55
+ parsedData = this.parseMomentumSwapEvent(bytes);
56
+ }
57
+ else if ((_d = eventContent.name) === null || _d === void 0 ? void 0 : _d.includes('RepayFlashSwapEvent')) {
58
+ parsedData = this.parseMomentumRepayFlashSwapEvent(bytes);
59
+ }
60
+ if (!parsedData) {
61
+ return null;
62
+ }
63
+ const result = {
64
+ dex: 'Momentum CLMM',
65
+ eventType: eventContent.name,
66
+ parsedData,
67
+ calculatedPrice: null
68
+ };
69
+ if (parsedData.sqrt_price_after) {
70
+ result.calculatedPrice = this.calculateMomentumPrice(BigInt(parsedData.sqrt_price_after));
71
+ }
72
+ return result;
73
+ }
74
+ catch (error) {
75
+ console.error('解析 Momentum 事件失败:', error);
76
+ return null;
77
+ }
78
+ }
79
+ static parseMomentumSwapEvent(eventData) {
80
+ try {
81
+ const SwapEventSchema = bcs_1.bcs.struct('MomentumSwapEvent', {
82
+ sender: bcs_1.bcs.Address,
83
+ pool_id: bcs_1.bcs.Address,
84
+ x_for_y: bcs_1.bcs.Bool,
85
+ amount_x: bcs_1.bcs.U64,
86
+ amount_y: bcs_1.bcs.U64,
87
+ sqrt_price_before: bcs_1.bcs.U128,
88
+ sqrt_price_after: bcs_1.bcs.U128,
89
+ liquidity: bcs_1.bcs.U128,
90
+ tick_index: bcs_1.bcs.U32,
91
+ fee_amount: bcs_1.bcs.U64,
92
+ protocol_fee: bcs_1.bcs.U64,
93
+ reserve_x: bcs_1.bcs.U64,
94
+ reserve_y: bcs_1.bcs.U64
95
+ });
96
+ return SwapEventSchema.parse(eventData);
97
+ }
98
+ catch (error) {
99
+ console.error('解析 Momentum SwapEvent 失败:', error);
100
+ return null;
101
+ }
102
+ }
103
+ static parseMomentumRepayFlashSwapEvent(eventData) {
104
+ try {
105
+ const RepayFlashSwapEventSchema = bcs_1.bcs.struct('MomentumRepayFlashSwapEvent', {
106
+ sender: bcs_1.bcs.Address,
107
+ pool_id: bcs_1.bcs.Address,
108
+ amount_x_debt: bcs_1.bcs.U64,
109
+ amount_y_debt: bcs_1.bcs.U64,
110
+ paid_x: bcs_1.bcs.U64,
111
+ paid_y: bcs_1.bcs.U64,
112
+ reserve_x: bcs_1.bcs.U64,
113
+ reserve_y: bcs_1.bcs.U64
114
+ });
115
+ return RepayFlashSwapEventSchema.parse(eventData);
116
+ }
117
+ catch (error) {
118
+ console.error('解析 Momentum RepayFlashSwapEvent 失败:', error);
119
+ return null;
120
+ }
121
+ }
122
+ static calculateMomentumPrice(sqrtPrice) {
123
+ try {
124
+ const sqrtPriceDecimal = new decimal_js_1.default(sqrtPrice.toString());
125
+ const Q64 = new decimal_js_1.default(2).pow(64);
126
+ const price = sqrtPriceDecimal.div(Q64).pow(2);
127
+ console.log('=== 价格计算调试 ===');
128
+ console.log('sqrtPrice:', sqrtPrice.toString());
129
+ console.log('计算出的原始价格:', price.toString());
130
+ return price.toNumber();
131
+ }
132
+ catch (error) {
133
+ console.error('计算 Momentum 价格失败:', error);
134
+ return 0;
135
+ }
136
+ }
137
+ }
138
+ exports.DexParserManager = DexParserManager;
@@ -1,2 +1,2 @@
1
- export declare function decodeBytes(obj: any): any;
2
- export declare function parseObjectAmount(objectResponse: any): string | null;
1
+ export * from './checkpoint_parse';
2
+ export * from './decode';