@clonegod/ttd-sui-common 1.0.7 → 1.0.9

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 (60) hide show
  1. package/dist/checkpoint_185049558.json +13761 -0
  2. package/dist/checkpoint_185049559.json +7487 -0
  3. package/dist/checkpoint_185093245_decoded.json +4887 -0
  4. package/dist/checkpoint_185093247_decoded.json +2680 -0
  5. package/dist/checkpoint_185093253_decoded.json +3115 -0
  6. package/dist/checkpoint_185094135_decoded.json +4755 -0
  7. package/dist/checkpoint_185094138_decoded.json +3603 -0
  8. package/dist/checkpoint_185094140_decoded.json +6755 -0
  9. package/dist/checkpoint_185096247_decoded.json +7230 -0
  10. package/dist/constants/index.d.ts +0 -0
  11. package/dist/constants/index.js +0 -0
  12. package/dist/grpc/grpc-connection.js +7 -1
  13. package/dist/grpc/subscription-service.js +11 -2
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.js +1 -0
  16. package/dist/test/test_checkpoint.d.ts +1 -0
  17. package/dist/test/test_checkpoint.js +78 -0
  18. package/dist/test/test_checkpoint_with_schema.d.ts +1 -0
  19. package/dist/test/test_checkpoint_with_schema.js +250 -0
  20. package/dist/test/test_dynamic_bcs.d.ts +1 -0
  21. package/dist/test/test_dynamic_bcs.js +82 -0
  22. package/dist/test/test_github_extractor.d.ts +1 -0
  23. package/dist/test/test_github_extractor.js +69 -0
  24. package/dist/test/test_grpc.js +0 -11
  25. package/dist/test/test_hybrid_bcs.d.ts +1 -0
  26. package/dist/test/test_hybrid_bcs.js +98 -0
  27. package/dist/test/test_schema_database.d.ts +1 -0
  28. package/dist/test/test_schema_database.js +91 -0
  29. package/dist/test/test_simple_schema.d.ts +1 -0
  30. package/dist/test/test_simple_schema.js +78 -0
  31. package/dist/type/index.d.ts +15 -34
  32. package/dist/utils/checkpoint.d.ts +7 -0
  33. package/dist/utils/checkpoint.js +26 -0
  34. package/dist/utils/checkpoint_parse.d.ts +0 -26
  35. package/dist/utils/checkpoint_parse.js +0 -277
  36. package/dist/utils/dex_event_parser.d.ts +34 -0
  37. package/dist/utils/dex_event_parser.js +132 -0
  38. package/dist/utils/dex_schema_database.d.ts +206 -0
  39. package/dist/utils/dex_schema_database.js +169 -0
  40. package/dist/utils/dynamic_bcs_parser.d.ts +21 -0
  41. package/dist/utils/dynamic_bcs_parser.js +166 -0
  42. package/dist/utils/github_schema_extractor.d.ts +20 -0
  43. package/dist/utils/github_schema_extractor.js +153 -0
  44. package/dist/utils/hybrid_bcs_parser.d.ts +22 -0
  45. package/dist/utils/hybrid_bcs_parser.js +194 -0
  46. package/dist/utils/index.d.ts +1 -1
  47. package/dist/utils/index.js +1 -1
  48. package/dist/utils/simple_schema_parser.d.ts +76 -0
  49. package/dist/utils/simple_schema_parser.js +193 -0
  50. package/package.json +2 -2
  51. package/dist/parsed_swap_transactions_2025-08-30T12-11-48-744Z.json +0 -19215
  52. package/dist/parsed_swap_transactions_2025-08-30T12-51-39-586Z.json +0 -19309
  53. package/dist/parsed_swap_transactions_2025-08-30T13-58-34-579Z.json +0 -19005
  54. package/dist/swap_transactions_2025-08-30T12-05-49-140Z.json +0 -1384
  55. package/dist/swap_transactions_2025-08-30T12-05-51-529Z.json +0 -2907
  56. package/dist/swap_transactions_2025-08-30T12-05-54-042Z.json +0 -13392
  57. package/dist/swap_transactions_2025-08-30T12-05-56-880Z.json +0 -16210
  58. package/dist/swap_transactions_2025-08-30T12-05-59-588Z.json +0 -17993
  59. package/dist/swap_transactions_2025-08-30T12-05-59-597Z.json +0 -17993
  60. package/dist/transaction_28FaeTYoctpP1VdWCuyPxNEBeaMm3ebj27kdF5umi9YL.json +0 -4754
@@ -0,0 +1,98 @@
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
+ require('dotenv').config();
13
+ const client_1 = require("@mysten/sui/client");
14
+ const hybrid_bcs_parser_1 = require("../utils/hybrid_bcs_parser");
15
+ const suiClient = new client_1.SuiClient({
16
+ url: 'https://fullnode.mainnet.sui.io'
17
+ });
18
+ const parser = new hybrid_bcs_parser_1.HybridBCSParser(suiClient);
19
+ const testHybridBCSParsing = () => __awaiter(void 0, void 0, void 0, function* () {
20
+ console.log('=== 测试混合BCS解析 ===');
21
+ const testEvents = [
22
+ {
23
+ name: 'Momentum SwapEvent',
24
+ event: {
25
+ type: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860::pool::SwapEvent',
26
+ bcs: {
27
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
28
+ }
29
+ }
30
+ },
31
+ {
32
+ name: 'Cetus SwapEvent',
33
+ event: {
34
+ type: '0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb::pool::SwapEvent',
35
+ bcs: {
36
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
37
+ }
38
+ }
39
+ },
40
+ {
41
+ name: 'Momentum RepayFlashSwapEvent',
42
+ event: {
43
+ type: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860::pool::RepayFlashSwapEvent',
44
+ bcs: {
45
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
46
+ }
47
+ }
48
+ },
49
+ {
50
+ name: 'Unknown Event',
51
+ event: {
52
+ type: '0xunknown::module::UnknownEvent',
53
+ bcs: {
54
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
55
+ }
56
+ }
57
+ }
58
+ ];
59
+ for (const testCase of testEvents) {
60
+ console.log(`\n测试: ${testCase.name}`);
61
+ try {
62
+ const result = yield parser.parseEvent(testCase.event);
63
+ console.log('解析结果:', JSON.stringify(result, null, 2));
64
+ }
65
+ catch (error) {
66
+ console.error('解析失败:', error);
67
+ }
68
+ }
69
+ });
70
+ const testAddSchema = () => {
71
+ console.log('\n=== 测试添加新schema ===');
72
+ parser.addSchema('0xnewpackage', 'NewSwapEvent', {
73
+ pool_id: 'address',
74
+ amount: 'u64'
75
+ });
76
+ const stats = parser.getCacheStats();
77
+ console.log('解析器统计:', stats);
78
+ };
79
+ const testCacheFunctionality = () => {
80
+ console.log('\n=== 测试缓存功能 ===');
81
+ const stats = parser.getCacheStats();
82
+ console.log('缓存统计:', stats);
83
+ parser.clearCache();
84
+ console.log('缓存已清除');
85
+ const newStats = parser.getCacheStats();
86
+ console.log('清除后统计:', newStats);
87
+ };
88
+ const main = () => __awaiter(void 0, void 0, void 0, function* () {
89
+ try {
90
+ yield testHybridBCSParsing();
91
+ testAddSchema();
92
+ testCacheFunctionality();
93
+ }
94
+ catch (error) {
95
+ console.error('测试失败:', error);
96
+ }
97
+ });
98
+ main();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require('dotenv').config();
4
+ const dex_schema_database_1 = require("../utils/dex_schema_database");
5
+ const db = new dex_schema_database_1.DexSchemaDatabase();
6
+ const testSchemaDatabase = () => {
7
+ console.log('=== 测试 DEX Schema 数据库 ===');
8
+ console.log('\n1. 测试获取 DEX 信息...');
9
+ const momentumInfo = db.getDexInfo('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860');
10
+ console.log('Momentum 信息:', momentumInfo);
11
+ console.log('\n2. 测试获取事件 schema...');
12
+ const swapSchema = db.getEventSchema('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860', 'SwapEvent');
13
+ console.log('SwapEvent schema:', swapSchema);
14
+ console.log('\n3. 测试检查支持...');
15
+ const isSupported = db.isSupported('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860');
16
+ console.log('Momentum 是否支持:', isSupported);
17
+ console.log('\n4. 测试获取支持的包...');
18
+ const supportedPackages = db.getSupportedPackages();
19
+ console.log('支持的包:', supportedPackages);
20
+ console.log('\n5. 测试添加新 schema...');
21
+ db.addDexSchema('0xnewdex', 'New DEX', 'https://github.com/newdex', {
22
+ 'NewEvent': {
23
+ field1: 'u64',
24
+ field2: 'address'
25
+ }
26
+ });
27
+ console.log('添加新 DEX 后支持的包:', db.getSupportedPackages());
28
+ console.log('\n6. 测试添加新事件...');
29
+ db.addEventSchema('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860', 'NewMomentumEvent', {
30
+ new_field: 'u128'
31
+ });
32
+ const newEventSchema = db.getEventSchema('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860', 'NewMomentumEvent');
33
+ console.log('新事件 schema:', newEventSchema);
34
+ };
35
+ const testCodeGeneration = () => {
36
+ console.log('\n=== 测试代码生成 ===');
37
+ const tsCode = db.generateTypeScriptCode();
38
+ console.log('生成的 TypeScript 代码:');
39
+ console.log(tsCode);
40
+ const json = db.exportToJSON();
41
+ console.log('\n导出的 JSON:');
42
+ console.log(json);
43
+ };
44
+ const testActualParsing = () => {
45
+ console.log('\n=== 测试实际解析 ===');
46
+ const testEvents = [
47
+ {
48
+ name: 'Momentum SwapEvent',
49
+ event: {
50
+ type: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860::pool::SwapEvent',
51
+ bcs: {
52
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
53
+ }
54
+ }
55
+ },
56
+ {
57
+ name: 'Cetus SwapEvent',
58
+ event: {
59
+ type: '0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb::pool::SwapEvent',
60
+ bcs: {
61
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
62
+ }
63
+ }
64
+ }
65
+ ];
66
+ for (const testCase of testEvents) {
67
+ console.log(`\n测试: ${testCase.name}`);
68
+ const parts = testCase.event.type.split('::');
69
+ const packageId = parts[0];
70
+ const eventName = parts[parts.length - 1];
71
+ const schema = db.getEventSchema(packageId, eventName);
72
+ console.log('Schema:', schema);
73
+ if (schema) {
74
+ console.log('✅ 找到 schema,可以解析 BCS 数据');
75
+ }
76
+ else {
77
+ console.log('❌ 未找到 schema');
78
+ }
79
+ }
80
+ };
81
+ const main = () => {
82
+ try {
83
+ testSchemaDatabase();
84
+ testCodeGeneration();
85
+ testActualParsing();
86
+ }
87
+ catch (error) {
88
+ console.error('测试失败:', error);
89
+ }
90
+ };
91
+ main();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require('dotenv').config();
4
+ const simple_schema_parser_1 = require("../utils/simple_schema_parser");
5
+ const parser = new simple_schema_parser_1.SimpleSchemaParser();
6
+ const testSimpleSchemaParsing = () => {
7
+ console.log('=== 测试简单Schema解析 ===');
8
+ const testEvents = [
9
+ {
10
+ name: 'Momentum SwapEvent',
11
+ event: {
12
+ type: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860::pool::SwapEvent',
13
+ bcs: {
14
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
15
+ }
16
+ }
17
+ },
18
+ {
19
+ name: 'Cetus SwapEvent',
20
+ event: {
21
+ type: '0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb::pool::SwapEvent',
22
+ bcs: {
23
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
24
+ }
25
+ }
26
+ },
27
+ {
28
+ name: 'Momentum RepayFlashSwapEvent',
29
+ event: {
30
+ type: '0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860::pool::RepayFlashSwapEvent',
31
+ bcs: {
32
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
33
+ }
34
+ }
35
+ },
36
+ {
37
+ name: 'Unknown Event',
38
+ event: {
39
+ type: '0xunknown::module::UnknownEvent',
40
+ bcs: {
41
+ value: '00096703000000000000d9690d000000000000af9374ff98010000202a10443fdf509da5b8a6f8d7b4a58f46a8b3827e11c18cc9ec98a8524bab5daa010020430948c6b6336d3c3fef5a9b710799b0afae62c780592c5a38a99b6e19ce17b30000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000'
42
+ }
43
+ }
44
+ }
45
+ ];
46
+ for (const testCase of testEvents) {
47
+ console.log(`\n测试: ${testCase.name}`);
48
+ try {
49
+ const result = parser.parseEvent(testCase.event);
50
+ console.log('解析结果:', JSON.stringify(result, null, 2));
51
+ }
52
+ catch (error) {
53
+ console.error('解析失败:', error);
54
+ }
55
+ }
56
+ };
57
+ const testFeatures = () => {
58
+ console.log('\n=== 测试功能 ===');
59
+ const supportedPackages = parser.getSupportedPackages();
60
+ console.log('支持的包:', supportedPackages);
61
+ const isMomentumSupported = parser.isSupported('0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860');
62
+ console.log('Momentum 是否支持:', isMomentumSupported);
63
+ parser.addSchema('0xnewpackage', 'NewEvent', {
64
+ field1: 'u64',
65
+ field2: 'address'
66
+ });
67
+ console.log('添加新 schema 后支持的包:', parser.getSupportedPackages());
68
+ };
69
+ const main = () => {
70
+ try {
71
+ testSimpleSchemaParsing();
72
+ testFeatures();
73
+ }
74
+ catch (error) {
75
+ console.error('测试失败:', error);
76
+ }
77
+ };
78
+ main();
@@ -1,4 +1,5 @@
1
1
  export interface BlockUpdateEvent {
2
+ blockHash: string;
2
3
  blockNumber: number;
3
4
  blockTime: string;
4
5
  transactions: TransactionData[];
@@ -6,40 +7,20 @@ export interface BlockUpdateEvent {
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;