@clonegod/ttd-sui-common 1.0.92 → 1.0.93

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.
@@ -9,7 +9,7 @@ export declare class SimpleRedisClient {
9
9
  getRedisClient(): Promise<RedisClientType>;
10
10
  private getLockKey;
11
11
  acquireLock(lock_key: string, lock_value: string, expireSeconds?: number): Promise<boolean>;
12
- private releaseLock;
12
+ releaseLock(lock_key: string, lock_value: string): Promise<boolean>;
13
13
  withLock<T>(lock_identifier: string, callback: () => Promise<T>, release_lock_delay_ms?: number): Promise<T>;
14
14
  getValue(key: string): Promise<string>;
15
15
  setValue(key: string, value: string, expireSeconds: number): Promise<any>;
@@ -100,118 +100,127 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
100
100
  throw new Error(`choose wallet, acquire lock failed: ${lockKey}, took ${Date.now() - start_time}ms`);
101
101
  }
102
102
  (0, dist_1.log_info)(`choose wallet, acquire lock success: ${lockKey}, took ${Date.now() - start_time}ms`);
103
- const { inputToken } = this.determineInputOutputTokens(context.order_msg, context.pool_info);
104
- console.log('inputToken', inputToken);
105
- const requiredAmount = new decimal_js_1.default(context.order_msg.amount);
106
- const wallet_assets = yield this.redisClient.hgetall('sui:wallet:assets');
107
- if (!wallet_assets || Object.keys(wallet_assets).length === 0) {
108
- throw new Error('没有找到钱包资产信息,请确保钱包监控服务正在运行');
109
- }
110
- const wallet_assets_map = {};
111
- for (const [walletAddress, assetsJson] of Object.entries(wallet_assets)) {
112
- try {
113
- wallet_assets_map[walletAddress] = JSON.parse(assetsJson);
114
- }
115
- catch (error) {
116
- console.warn(`解析钱包资产数据失败: ${walletAddress}`, error);
103
+ try {
104
+ const { inputToken } = this.determineInputOutputTokens(context.order_msg, context.pool_info);
105
+ console.log('inputToken', inputToken);
106
+ const requiredAmount = new decimal_js_1.default(context.order_msg.amount);
107
+ const wallet_assets = yield this.redisClient.hgetall('sui:wallet:assets');
108
+ if (!wallet_assets || Object.keys(wallet_assets).length === 0) {
109
+ throw new Error('没有找到钱包资产信息,请确保钱包监控服务正在运行');
117
110
  }
118
- }
119
- const availableWallets = [];
120
- const allWallets = [];
121
- for (const [walletAddress, assets] of Object.entries(wallet_assets_map)) {
122
- try {
123
- const wallet = (_a = this.group_wallets) === null || _a === void 0 ? void 0 : _a.find(w => w.getPublicKey().toSuiAddress().toLowerCase() === walletAddress.toLowerCase());
124
- if (!wallet) {
125
- continue;
111
+ const wallet_assets_map = {};
112
+ for (const [walletAddress, assetsJson] of Object.entries(wallet_assets)) {
113
+ try {
114
+ wallet_assets_map[walletAddress] = JSON.parse(assetsJson);
126
115
  }
127
- const tokenAsset = (_b = assets.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => (0, index_1.normalizeSuiTokenAddress)(token.address) === (0, index_1.normalizeSuiTokenAddress)(inputToken.address));
128
- let tokenBalance = (tokenAsset === null || tokenAsset === void 0 ? void 0 : tokenAsset.balance) || '0';
129
- const balanceDecimal = new decimal_js_1.default(tokenBalance);
130
- if (balanceDecimal.gte(requiredAmount)) {
131
- availableWallets.push({
116
+ catch (error) {
117
+ console.warn(`解析钱包资产数据失败: ${walletAddress}`, error);
118
+ }
119
+ }
120
+ const availableWallets = [];
121
+ const allWallets = [];
122
+ for (const [walletAddress, assets] of Object.entries(wallet_assets_map)) {
123
+ try {
124
+ const wallet = (_a = this.group_wallets) === null || _a === void 0 ? void 0 : _a.find(w => w.getPublicKey().toSuiAddress().toLowerCase() === walletAddress.toLowerCase());
125
+ if (!wallet) {
126
+ continue;
127
+ }
128
+ const tokenAsset = (_b = assets.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => (0, index_1.normalizeSuiTokenAddress)(token.address) === (0, index_1.normalizeSuiTokenAddress)(inputToken.address));
129
+ let tokenBalance = (tokenAsset === null || tokenAsset === void 0 ? void 0 : tokenAsset.balance) || '0';
130
+ const balanceDecimal = new decimal_js_1.default(tokenBalance);
131
+ if (balanceDecimal.gte(requiredAmount)) {
132
+ availableWallets.push({
133
+ wallet,
134
+ balance: tokenBalance
135
+ });
136
+ }
137
+ else {
138
+ console.log(`钱包 ${walletAddress} 余额不足: ${tokenBalance} ${inputToken.symbol} < ${requiredAmount} ${inputToken.symbol}`);
139
+ }
140
+ allWallets.push({
132
141
  wallet,
133
142
  balance: tokenBalance
134
143
  });
135
144
  }
136
- else {
137
- console.log(`钱包 ${walletAddress} 余额不足: ${tokenBalance} ${inputToken.symbol} < ${requiredAmount} ${inputToken.symbol}`);
145
+ catch (error) {
146
+ console.warn(`处理钱包资产时出错: ${walletAddress}`, error);
138
147
  }
139
- allWallets.push({
140
- wallet,
141
- balance: tokenBalance
142
- });
143
148
  }
144
- catch (error) {
145
- console.warn(`处理钱包资产时出错: ${walletAddress}`, error);
149
+ if (availableWallets.length === 0) {
150
+ let error_msg = `all wallets are insufficient, required: ${requiredAmount} ${inputToken.symbol}`;
151
+ (0, dist_1.log_warn)(error_msg);
152
+ allWallets.forEach((walletInfo, index) => {
153
+ const walletAddress = walletInfo.wallet.getPublicKey().toSuiAddress();
154
+ const shortAddress = walletAddress.substring(0, 6) + '...' + walletAddress.substring(walletAddress.length - 4);
155
+ console.log(`${index + 1}. ${shortAddress}: ${walletInfo.balance} ${inputToken.symbol}`);
156
+ });
157
+ throw new Error(error_msg);
146
158
  }
147
- }
148
- if (availableWallets.length === 0) {
149
- let error_msg = `all wallets are insufficient, required: ${requiredAmount} ${inputToken.symbol}`;
150
- (0, dist_1.log_warn)(error_msg);
151
- allWallets.forEach((walletInfo, index) => {
152
- const walletAddress = walletInfo.wallet.getPublicKey().toSuiAddress();
153
- const shortAddress = walletAddress.substring(0, 6) + '...' + walletAddress.substring(walletAddress.length - 4);
154
- console.log(`${index + 1}. ${shortAddress}: ${walletInfo.balance} ${inputToken.symbol}`);
155
- });
156
- throw new Error(error_msg);
157
- }
158
- console.log('availableWallets', availableWallets.map(w => w.wallet.getPublicKey().toSuiAddress()));
159
- const wallet_last_used = yield this.redisClient.hgetall('sui:wallet:last_used');
160
- for (const availableWallet of availableWallets) {
161
- const walletAddress = availableWallet.wallet.getPublicKey().toSuiAddress();
162
- const lastUsedData = wallet_last_used === null || wallet_last_used === void 0 ? void 0 : wallet_last_used[walletAddress];
163
- if (lastUsedData) {
164
- try {
165
- const lastUsedInfo = JSON.parse(lastUsedData);
166
- availableWallet.lastUsedAt = lastUsedInfo.lastUsedAt;
159
+ console.log('availableWallets', availableWallets.map(w => w.wallet.getPublicKey().toSuiAddress()));
160
+ const wallet_last_used = yield this.redisClient.hgetall('sui:wallet:last_used');
161
+ for (const availableWallet of availableWallets) {
162
+ const walletAddress = availableWallet.wallet.getPublicKey().toSuiAddress();
163
+ const lastUsedData = wallet_last_used === null || wallet_last_used === void 0 ? void 0 : wallet_last_used[walletAddress];
164
+ if (lastUsedData) {
165
+ try {
166
+ const lastUsedInfo = JSON.parse(lastUsedData);
167
+ availableWallet.lastUsedAt = lastUsedInfo.lastUsedAt;
168
+ }
169
+ catch (error) {
170
+ console.warn(`解析钱包最后使用时间失败: ${walletAddress}`, error);
171
+ }
167
172
  }
168
- catch (error) {
169
- console.warn(`解析钱包最后使用时间失败: ${walletAddress}`, error);
173
+ else {
174
+ yield this.redisClient.hsetValue('sui:wallet:last_used', walletAddress, JSON.stringify({
175
+ lastUsedAt: 0,
176
+ lastUsedTime: new Date(0).toISOString()
177
+ }), 24 * 60 * 60);
178
+ availableWallet.lastUsedAt = 0;
170
179
  }
171
180
  }
172
- else {
173
- yield this.redisClient.hsetValue('sui:wallet:last_used', walletAddress, JSON.stringify({
174
- lastUsedAt: 0,
175
- lastUsedTime: new Date(0).toISOString()
176
- }), 24 * 60 * 60);
177
- availableWallet.lastUsedAt = 0;
178
- }
179
- }
180
- let selectedWallet;
181
- let selectionStrategy;
182
- if (availableWallets.length === 1) {
183
- selectedWallet = availableWallets[0].wallet;
184
- selectionStrategy = 'OnlyOne';
185
- }
186
- else {
187
- const hasUsageHistory = wallet_last_used && Object.keys(wallet_last_used).length > 0;
188
- if (hasUsageHistory) {
189
- availableWallets.sort((a, b) => {
190
- const aTime = a.lastUsedAt || 0;
191
- const bTime = b.lastUsedAt || 0;
192
- return aTime - bTime;
193
- });
181
+ let selectedWallet;
182
+ let selectionStrategy;
183
+ if (availableWallets.length === 1) {
194
184
  selectedWallet = availableWallets[0].wallet;
195
- selectionStrategy = 'MaxLastUsed';
185
+ selectionStrategy = 'OnlyOne';
196
186
  }
197
187
  else {
198
- availableWallets.sort((a, b) => {
199
- const aBalance = new decimal_js_1.default(a.balance);
200
- const bBalance = new decimal_js_1.default(b.balance);
201
- return bBalance.cmp(aBalance);
202
- });
203
- selectedWallet = availableWallets[0].wallet;
204
- selectionStrategy = 'MaxBalance';
188
+ const hasUsageHistory = wallet_last_used && Object.keys(wallet_last_used).length > 0;
189
+ if (hasUsageHistory) {
190
+ availableWallets.sort((a, b) => {
191
+ const aTime = a.lastUsedAt || 0;
192
+ const bTime = b.lastUsedAt || 0;
193
+ return aTime - bTime;
194
+ });
195
+ selectedWallet = availableWallets[0].wallet;
196
+ selectionStrategy = 'MaxLastUsed';
197
+ }
198
+ else {
199
+ availableWallets.sort((a, b) => {
200
+ const aBalance = new decimal_js_1.default(a.balance);
201
+ const bBalance = new decimal_js_1.default(b.balance);
202
+ return bBalance.cmp(aBalance);
203
+ });
204
+ selectedWallet = availableWallets[0].wallet;
205
+ selectionStrategy = 'MaxBalance';
206
+ }
205
207
  }
208
+ const selectedWalletAddress = selectedWallet.getPublicKey().toSuiAddress();
209
+ yield this.redisClient.hsetValue('sui:wallet:last_used', selectedWalletAddress, JSON.stringify({
210
+ lastUsedAt: Date.now(),
211
+ lastUsedTime: new Date().toISOString()
212
+ }), 24 * 60 * 60);
213
+ const selectedWalletInfo = availableWallets.find(w => w.wallet.getPublicKey().toSuiAddress() === selectedWalletAddress);
214
+ (0, dist_1.log_info)(`Choose wallet: ${selectionStrategy} -> ${selectedWalletAddress}, balance: ${selectedWalletInfo === null || selectedWalletInfo === void 0 ? void 0 : selectedWalletInfo.balance} ${inputToken.symbol}, required: ${requiredAmount} ${inputToken.symbol}`);
215
+ return selectedWallet;
216
+ }
217
+ catch (error) {
218
+ (0, dist_1.log_error)(`choose wallet failed, error: ${error.message}`, error);
219
+ throw error;
220
+ }
221
+ finally {
222
+ yield this.redisClient.releaseLock(lockKey, lockValue);
206
223
  }
207
- const selectedWalletAddress = selectedWallet.getPublicKey().toSuiAddress();
208
- yield this.redisClient.hsetValue('sui:wallet:last_used', selectedWalletAddress, JSON.stringify({
209
- lastUsedAt: Date.now(),
210
- lastUsedTime: new Date().toISOString()
211
- }), 24 * 60 * 60);
212
- const selectedWalletInfo = availableWallets.find(w => w.wallet.getPublicKey().toSuiAddress() === selectedWalletAddress);
213
- (0, dist_1.log_info)(`Choose wallet: ${selectionStrategy} -> ${selectedWalletAddress}, balance: ${selectedWalletInfo === null || selectedWalletInfo === void 0 ? void 0 : selectedWalletInfo.balance} ${inputToken.symbol}, required: ${requiredAmount} ${inputToken.symbol}`);
214
- return selectedWallet;
215
224
  });
216
225
  }
217
226
  determineInputOutputTokens(order_msg, pool_info) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sui-common",
3
- "version": "1.0.92",
3
+ "version": "1.0.93",
4
4
  "description": "Sui common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",