@clonegod/ttd-bsc-common 3.0.5 → 3.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.
@@ -69,13 +69,16 @@ class AbstractDexTrade extends ttd_core_1.AbastrcatTrade {
69
69
  var _a;
70
70
  const { price_msg, order_msg, slippage_bps, order_trace_id, pool_info } = context;
71
71
  const { pair } = price_msg;
72
- const { aToB, amount, unique_order_msg_id } = order_msg;
73
- (0, ttd_core_1.log_info)(`执行 Vault 交易, orderId=${order_trace_id}`, {
74
- unique_order_msg_id,
72
+ const { isBuy, amount } = order_msg;
73
+ const { inputToken, outputToken } = this.determineInputOutputTokens(order_msg, pool_info);
74
+ const amountOutMin = this.calculateAmountOutMin(context, inputToken, outputToken);
75
+ (0, ttd_core_1.log_info)(`执行交易`, {
76
+ orderId: order_trace_id,
75
77
  pair,
76
- aToB,
77
- amount,
78
- slippage_bps
78
+ direction: isBuy ? 'BUY' : 'SELL',
79
+ input: `${amount} ${inputToken.symbol}`,
80
+ outputMin: `${ethers_1.ethers.utils.formatUnits(amountOutMin, outputToken.decimals)} ${outputToken.symbol}`,
81
+ slippage: `${slippage_bps / 100}%`,
79
82
  });
80
83
  let maxAttempts = Math.min(Math.max(parseInt(process.env.NONCE_LOCK_MAX_RETRIES || '3'), 1), 3);
81
84
  const callerHandle = yield this.callerManager.acquireCaller();
@@ -114,7 +117,7 @@ class AbstractDexTrade extends ttd_core_1.AbastrcatTrade {
114
117
  };
115
118
  const signedTx = yield caller.signTransaction(tx);
116
119
  txid = ethers_1.ethers.utils.keccak256(signedTx);
117
- (0, ttd_core_1.log_info)(`Vault 交易已签名`, { txid, nonce, gasPriceGwei: realGasPriceGwei }, order_trace_id);
120
+ (0, ttd_core_1.log_info)(`交易已签名`, { txid, nonce, caller: caller.address, gasPriceGwei: realGasPriceGwei }, order_trace_id);
118
121
  const tipNonce = nonce + 1;
119
122
  const eoa_tip_transaction = (eoa_address) => __awaiter(this, void 0, void 0, function* () {
120
123
  const transfer_amount_gwei = this.getBuilderTipAmoutGwei(context);
@@ -123,9 +126,9 @@ class AbstractDexTrade extends ttd_core_1.AbastrcatTrade {
123
126
  });
124
127
  const only_bundle = order_msg.is_dex_maker;
125
128
  yield this.transactionSender.sendTransaction(signedTx, eoa_tip_transaction, order_trace_id, pair, only_bundle);
126
- (0, ttd_core_1.log_info)(`Vault 交易发送成功`, {
127
- pair, unique_order_msg_id, txid,
128
- attempt: i, caller: caller.address
129
+ (0, ttd_core_1.log_info)(`交易发送成功`, {
130
+ pair, direction: isBuy ? 'BUY' : 'SELL',
131
+ txid, attempt: i, caller: caller.address
129
132
  }, order_trace_id);
130
133
  return txid;
131
134
  }
@@ -154,7 +157,7 @@ class AbstractDexTrade extends ttd_core_1.AbastrcatTrade {
154
157
  }
155
158
  } while (++i <= maxAttempts);
156
159
  if (!txid) {
157
- throw new Error(`Vault 交易执行失败,已重试 ${maxAttempts} 次,orderId: ${order_trace_id}`);
160
+ throw new Error(`交易执行失败,已重试 ${maxAttempts} 次,orderId: ${order_trace_id}`);
158
161
  }
159
162
  return txid;
160
163
  }
@@ -10,6 +10,5 @@ export declare abstract class BaseTxResultChecker extends AbstractTransactionRes
10
10
  };
11
11
  check_tx_result_interval(): Promise<void>;
12
12
  on_subscibe_transaction(): Promise<void>;
13
- start_ws_listener(): Promise<void>;
14
13
  private processTransactionResult;
15
14
  }
@@ -13,6 +13,60 @@ exports.BaseTxResultChecker = void 0;
13
13
  const ttd_core_1 = require("@clonegod/ttd-core");
14
14
  const trade_1 = require("@clonegod/ttd-core/dist/trade");
15
15
  const ethers_1 = require("ethers");
16
+ class TradeResultSubscriber {
17
+ constructor() {
18
+ this.ws = null;
19
+ this.connected = false;
20
+ this.listeners = new Map();
21
+ }
22
+ static getInstance() {
23
+ if (!TradeResultSubscriber.instance) {
24
+ TradeResultSubscriber.instance = new TradeResultSubscriber();
25
+ }
26
+ return TradeResultSubscriber.instance;
27
+ }
28
+ listen(txHash, callback, timeoutMs = 30000) {
29
+ const key = txHash.toLowerCase();
30
+ this.listeners.set(key, callback);
31
+ setTimeout(() => {
32
+ this.listeners.delete(key);
33
+ }, timeoutMs);
34
+ this.ensureConnected();
35
+ }
36
+ remove(txHash) {
37
+ this.listeners.delete(txHash.toLowerCase());
38
+ }
39
+ ensureConnected() {
40
+ if (this.ws && this.connected)
41
+ return;
42
+ const host = process.env.STREAM_TRADE_WS_HOST || '127.0.0.1';
43
+ const wsUrl = `ws://${host}:${ttd_core_1.SERVICE_PORT.STREAM_TRADE_WS}`;
44
+ this.ws = new ttd_core_1.WebSocketClient(wsUrl);
45
+ this.ws.onOpen(() => {
46
+ this.connected = true;
47
+ const addresses = (process.env.VAULT_ADDRESS || '').split(',').map(a => a.trim()).filter(Boolean);
48
+ for (const addr of addresses) {
49
+ this.ws.send(JSON.stringify({ address: addr }));
50
+ }
51
+ (0, ttd_core_1.log_info)(`TradeResultSubscriber connected: ${wsUrl}`);
52
+ });
53
+ this.ws.onMessage((msg) => {
54
+ if (msg.type !== 'TradeResult' || !msg.data)
55
+ return;
56
+ const { txHash, receipt } = msg.data;
57
+ if (!txHash || !receipt)
58
+ return;
59
+ const key = txHash.toLowerCase();
60
+ const callback = this.listeners.get(key);
61
+ if (callback) {
62
+ this.listeners.delete(key);
63
+ callback(receipt);
64
+ }
65
+ });
66
+ this.ws.connect();
67
+ }
68
+ }
69
+ TradeResultSubscriber.instance = null;
16
70
  class BaseTxResultChecker extends trade_1.AbstractTransactionResultCheck {
17
71
  constructor(env_args, event_emitter) {
18
72
  super(env_args, event_emitter);
@@ -51,39 +105,12 @@ class BaseTxResultChecker extends trade_1.AbstractTransactionResultCheck {
51
105
  }
52
106
  on_subscibe_transaction() {
53
107
  return __awaiter(this, void 0, void 0, function* () {
54
- this.start_ws_listener();
55
- });
56
- }
57
- start_ws_listener() {
58
- return __awaiter(this, void 0, void 0, function* () {
59
- const host = process.env.STREAM_TRADE_WS_HOST || '127.0.0.1';
60
- const wsUrl = `ws://${host}:${ttd_core_1.SERVICE_PORT.STREAM_TRADE_WS}`;
61
- (0, ttd_core_1.log_info)(`Subscribing trade result from stream-trade: ${wsUrl}, txid=${this.txid}`);
62
- const ws = new ttd_core_1.WebSocketClient(wsUrl);
63
- ws.onOpen(() => {
64
- const vaultAddress = process.env.VAULT_ADDRESS || '';
65
- if (vaultAddress) {
66
- ws.send(JSON.stringify({ address: vaultAddress }));
67
- }
68
- });
69
- ws.onMessage((msg) => {
70
- var _a;
71
- if (msg.type !== 'TradeResult' || !msg.data)
72
- return;
73
- const { txHash, receipt } = msg.data;
74
- if ((txHash === null || txHash === void 0 ? void 0 : txHash.toLowerCase()) !== ((_a = this.txid) === null || _a === void 0 ? void 0 : _a.toLowerCase()))
75
- return;
76
- if (!receipt) {
77
- (0, ttd_core_1.log_warn)(`TradeResult received but no receipt: ${txHash}`);
78
- return;
79
- }
80
- (0, ttd_core_1.log_info)(`Received transaction result via stream-trade: ${txHash}`);
108
+ (0, ttd_core_1.log_info)(`Subscribing trade result, txid=${this.txid}`);
109
+ TradeResultSubscriber.getInstance().listen(this.txid, (receipt) => {
110
+ (0, ttd_core_1.log_info)(`Received transaction result via stream-trade: ${this.txid}`);
81
111
  this.processTransactionResult(receipt, 'websocket')
82
- .catch(err => (0, ttd_core_1.log_error)(`Error processing trade result: ${txHash}`, err))
83
- .finally(() => ws.disconnect());
112
+ .catch(err => (0, ttd_core_1.log_error)(`Error processing trade result: ${this.txid}`, err));
84
113
  });
85
- ws.connect();
86
- setTimeout(() => { ws.disconnect(); }, 30000);
87
114
  });
88
115
  }
89
116
  processTransactionResult(txReceipt, source) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "3.0.5",
3
+ "version": "3.0.6",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",