@clonegod/ttd-sol-common 2.0.39 → 2.0.41
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.
|
@@ -5,7 +5,11 @@ const dist_1 = require("@clonegod/ttd-core/dist");
|
|
|
5
5
|
class SolanaTradeAppConfig extends dist_1.AbstractTradeAppConfig {
|
|
6
6
|
constructor() {
|
|
7
7
|
super();
|
|
8
|
+
}
|
|
9
|
+
async init() {
|
|
10
|
+
await super.init();
|
|
8
11
|
this.subscribe_wallet_raw_txn_event();
|
|
12
|
+
(0, dist_1.log_info)('SolanaTradeAppConfig init ...');
|
|
9
13
|
}
|
|
10
14
|
subscribe_wallet_raw_txn_event() {
|
|
11
15
|
const transactionHandler = async (messageStr) => {
|
|
@@ -18,7 +22,7 @@ class SolanaTradeAppConfig extends dist_1.AbstractTradeAppConfig {
|
|
|
18
22
|
this.emit(dist_1.LOCAL_EVENT_NAME.EVENT_WALLET_TRANSACTION + '#' + tx_id, messageObj);
|
|
19
23
|
};
|
|
20
24
|
const ws_port = process.env.STREAM_WS_TRADE_PORT || 10002;
|
|
21
|
-
const ws = new dist_1.WebSocketClient(`ws://
|
|
25
|
+
const ws = new dist_1.WebSocketClient(`ws://127.0.0.1:${ws_port}`);
|
|
22
26
|
ws.onMessage(transactionHandler);
|
|
23
27
|
ws.connect();
|
|
24
28
|
}
|
|
@@ -36,23 +36,49 @@ class TransactionResultChecker extends dist_1.AbstractTransactionResultCheck {
|
|
|
36
36
|
}
|
|
37
37
|
on_subscibe_transaction() {
|
|
38
38
|
this.event_emitter.once(dist_1.LOCAL_EVENT_NAME.EVENT_WALLET_TRANSACTION + '#' + this.txid, (message) => {
|
|
39
|
+
var _a, _b, _c;
|
|
39
40
|
try {
|
|
40
41
|
this.trade_result_already_processed = true;
|
|
41
42
|
let messageObj = message;
|
|
42
43
|
if (typeof message === 'string') {
|
|
43
44
|
messageObj = JSON.parse(message);
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
let result;
|
|
47
|
+
let _txid;
|
|
48
|
+
let slot;
|
|
49
|
+
let transactionData;
|
|
50
|
+
let meta;
|
|
51
|
+
let version;
|
|
52
|
+
if (messageObj.transaction && messageObj.transaction.signature) {
|
|
53
|
+
_txid = messageObj.transaction.signature;
|
|
54
|
+
slot = messageObj.slot;
|
|
55
|
+
transactionData = messageObj.transaction.transaction;
|
|
56
|
+
meta = messageObj.transaction.meta;
|
|
57
|
+
version = messageObj.transaction.version !== undefined ? messageObj.transaction.version : 0;
|
|
58
|
+
}
|
|
59
|
+
else if (messageObj.params && messageObj.params.result) {
|
|
60
|
+
result = messageObj.params.result;
|
|
61
|
+
_txid = result.signature;
|
|
62
|
+
slot = result.slot;
|
|
63
|
+
transactionData = (_a = result.transaction) === null || _a === void 0 ? void 0 : _a.transaction;
|
|
64
|
+
meta = (_b = result.transaction) === null || _b === void 0 ? void 0 : _b.meta;
|
|
65
|
+
version = ((_c = result.transaction) === null || _c === void 0 ? void 0 : _c.version) !== undefined ? result.transaction.version : 0;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
(0, dist_1.log_error)('Invalid message format: unrecognized structure', new Error(JSON.stringify(messageObj)));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (_txid !== this.txid) {
|
|
72
|
+
(0, dist_1.log_error)('[tx_result_check] txid mismatch!', new Error(JSON.stringify({ _txid, txid: this.txid, slot })));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
transactionData.signatures = [_txid];
|
|
50
76
|
let tx_result = {
|
|
51
|
-
slot,
|
|
52
|
-
transaction:
|
|
53
|
-
meta:
|
|
54
|
-
blockTime: Date.now() / 1000,
|
|
55
|
-
version:
|
|
77
|
+
slot: slot,
|
|
78
|
+
transaction: transactionData,
|
|
79
|
+
meta: meta || null,
|
|
80
|
+
blockTime: messageObj.blockTime || (Date.now() / 1000),
|
|
81
|
+
version: (typeof version === 'number' && version === 0) ? 0 : version
|
|
56
82
|
};
|
|
57
83
|
let swap_result = this.transactionParser.parse_transaction_data(tx_result, this.pool_info);
|
|
58
84
|
let trade_result = this.map_swap_result_to_tx_result(swap_result);
|
|
@@ -67,7 +93,7 @@ class TransactionResultChecker extends dist_1.AbstractTransactionResultCheck {
|
|
|
67
93
|
console.log('---------------------------------------------------->');
|
|
68
94
|
}
|
|
69
95
|
catch (err) {
|
|
70
|
-
(0, dist_1.log_error)('parse
|
|
96
|
+
(0, dist_1.log_error)('parse stream transaction message error!', err);
|
|
71
97
|
return;
|
|
72
98
|
}
|
|
73
99
|
});
|
package/package.json
CHANGED
|
@@ -6,12 +6,16 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
6
6
|
// 子类需要设置的属性
|
|
7
7
|
public connection: Connection;
|
|
8
8
|
public keypair: Keypair;
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
constructor() {
|
|
11
11
|
super();
|
|
12
|
-
this.subscribe_wallet_raw_txn_event();
|
|
13
12
|
}
|
|
14
13
|
|
|
14
|
+
async init() {
|
|
15
|
+
await super.init();
|
|
16
|
+
this.subscribe_wallet_raw_txn_event();
|
|
17
|
+
log_info('SolanaTradeAppConfig init ...');
|
|
18
|
+
}
|
|
15
19
|
|
|
16
20
|
// 订阅 Wallet 在链上发生的tx data
|
|
17
21
|
subscribe_wallet_raw_txn_event(): void {
|
|
@@ -30,7 +34,7 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
30
34
|
};
|
|
31
35
|
|
|
32
36
|
const ws_port = process.env.STREAM_WS_TRADE_PORT || 10002;
|
|
33
|
-
const ws = new WebSocketClient(`ws://
|
|
37
|
+
const ws = new WebSocketClient(`ws://127.0.0.1:${ws_port}`)
|
|
34
38
|
ws.onMessage(transactionHandler);
|
|
35
39
|
ws.connect();
|
|
36
40
|
|
|
@@ -39,7 +43,6 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
|
|
42
|
-
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
|
|
@@ -88,20 +88,57 @@ export class TransactionResultChecker extends AbstractTransactionResultCheck {
|
|
|
88
88
|
messageObj = JSON.parse(message)
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
let
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
91
|
+
// 支持两种消息格式:
|
|
92
|
+
// 1. 原始格式 (decodedTransaction): { slot, transaction: { signature, index, transaction, meta, version } }
|
|
93
|
+
// 2. 转换后格式: { params: { result: { signature, slot, transaction: { transaction, meta, version } } } }
|
|
94
|
+
|
|
95
|
+
let result: any
|
|
96
|
+
let _txid: string
|
|
97
|
+
let slot: number
|
|
98
|
+
let transactionData: any
|
|
99
|
+
let meta: any
|
|
100
|
+
let version: number
|
|
101
|
+
|
|
102
|
+
// 检查是否为转换后的格式 (params.result)
|
|
103
|
+
|
|
104
|
+
// 基于 laser_stream 推送的 transaction 格式
|
|
105
|
+
if (messageObj.transaction && messageObj.transaction.signature) {
|
|
106
|
+
_txid = messageObj.transaction.signature
|
|
107
|
+
slot = messageObj.slot
|
|
108
|
+
transactionData = messageObj.transaction.transaction
|
|
109
|
+
meta = messageObj.transaction.meta
|
|
110
|
+
version = messageObj.transaction.version !== undefined ? messageObj.transaction.version : 0
|
|
111
|
+
}
|
|
112
|
+
// 基于 geyser ws 推送的 transaction 格式 ?
|
|
113
|
+
else if (messageObj.params && messageObj.params.result) {
|
|
114
|
+
result = messageObj.params.result
|
|
115
|
+
_txid = result.signature
|
|
116
|
+
slot = result.slot
|
|
117
|
+
transactionData = result.transaction?.transaction
|
|
118
|
+
meta = result.transaction?.meta
|
|
119
|
+
version = result.transaction?.version !== undefined ? result.transaction.version : 0
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
log_error('Invalid message format: unrecognized structure', new Error(JSON.stringify(messageObj)))
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if(_txid !== this.txid) {
|
|
127
|
+
log_error('[tx_result_check] txid mismatch!', new Error(JSON.stringify({ _txid, txid: this.txid, slot })))
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// set txid for parse - 确保 signatures 数组存在
|
|
132
|
+
transactionData.signatures = [_txid]
|
|
133
|
+
|
|
134
|
+
// 构建 ParsedTransactionWithMeta 对象,确保所有属性都被正确复制
|
|
135
|
+
// version 字段类型为 TransactionVersion,通常是 0 或 'legacy'
|
|
136
|
+
let tx_result: ParsedTransactionWithMeta = {
|
|
137
|
+
slot: slot,
|
|
138
|
+
transaction: transactionData,
|
|
139
|
+
meta: meta || null,
|
|
140
|
+
blockTime: messageObj.blockTime || (Date.now() / 1000),
|
|
141
|
+
version: (typeof version === 'number' && version === 0) ? 0 : (version as any)
|
|
105
142
|
}
|
|
106
143
|
|
|
107
144
|
let swap_result = this.transactionParser.parse_transaction_data(tx_result, this.pool_info)
|
|
@@ -117,7 +154,7 @@ export class TransactionResultChecker extends AbstractTransactionResultCheck {
|
|
|
117
154
|
console.dir(trade_result, { depth: 8 })
|
|
118
155
|
console.log('---------------------------------------------------->')
|
|
119
156
|
} catch (err) {
|
|
120
|
-
log_error('parse
|
|
157
|
+
log_error('parse stream transaction message error!', err)
|
|
121
158
|
return
|
|
122
159
|
}
|
|
123
160
|
})
|