@clonegod/ttd-sui-common 1.0.90 → 1.0.92
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.
|
@@ -8,7 +8,7 @@ export declare class SimpleRedisClient {
|
|
|
8
8
|
constructor(lock_prefix: string);
|
|
9
9
|
getRedisClient(): Promise<RedisClientType>;
|
|
10
10
|
private getLockKey;
|
|
11
|
-
|
|
11
|
+
acquireLock(lock_key: string, lock_value: string, expireSeconds?: number): Promise<boolean>;
|
|
12
12
|
private releaseLock;
|
|
13
13
|
withLock<T>(lock_identifier: string, callback: () => Promise<T>, release_lock_delay_ms?: number): Promise<T>;
|
|
14
14
|
getValue(key: string): Promise<string>;
|
|
@@ -82,6 +82,24 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
|
82
82
|
if (this.getWalletMode() === 'single') {
|
|
83
83
|
return this.getSingleWallet();
|
|
84
84
|
}
|
|
85
|
+
const lockIdentifier = `choose_wallet`;
|
|
86
|
+
const lockKey = `${this.chainNameLower}:lock:${lockIdentifier}`;
|
|
87
|
+
const lockValue = `${Math.random().toString(36).substring(2, 15)}`;
|
|
88
|
+
let lockAcquired = false;
|
|
89
|
+
const start_time = Date.now();
|
|
90
|
+
const maxRetryTime = 200;
|
|
91
|
+
const retryDelay = 20;
|
|
92
|
+
do {
|
|
93
|
+
lockAcquired = yield this.redisClient.acquireLock(lockKey, lockValue, 200);
|
|
94
|
+
if (lockAcquired) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
yield (0, dist_1.sleep)(retryDelay);
|
|
98
|
+
} while (!lockAcquired && Date.now() - start_time < maxRetryTime);
|
|
99
|
+
if (!lockAcquired) {
|
|
100
|
+
throw new Error(`choose wallet, acquire lock failed: ${lockKey}, took ${Date.now() - start_time}ms`);
|
|
101
|
+
}
|
|
102
|
+
(0, dist_1.log_info)(`choose wallet, acquire lock success: ${lockKey}, took ${Date.now() - start_time}ms`);
|
|
85
103
|
const { inputToken } = this.determineInputOutputTokens(context.order_msg, context.pool_info);
|
|
86
104
|
console.log('inputToken', inputToken);
|
|
87
105
|
const requiredAmount = new decimal_js_1.default(context.order_msg.amount);
|
|
@@ -317,7 +335,7 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
|
317
335
|
(0, dist_1.log_info)(`✅ ${tokenSymbol} 进行对象合并,涉及 ${other_objects.length} 个对象`);
|
|
318
336
|
}
|
|
319
337
|
try_refresh_wallet_objects(wallet_1, txid_1, context_1) {
|
|
320
|
-
return __awaiter(this, arguments, void 0, function* (wallet, txid, context, initial_delay_ms = 0, try_times =
|
|
338
|
+
return __awaiter(this, arguments, void 0, function* (wallet, txid, context, initial_delay_ms = 0, try_times = 5, try_delay_ms = 300) {
|
|
321
339
|
for (let i = 0; i < try_times; i++) {
|
|
322
340
|
let delay_ms = initial_delay_ms + i * try_delay_ms;
|
|
323
341
|
setTimeout(() => {
|
|
@@ -325,7 +343,7 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
|
325
343
|
let coin_types = [context.pool_info.tokenA.address, context.pool_info.tokenB.address];
|
|
326
344
|
this.appConfig.arb_cache.redis_event_publisher.publish_wallet_raw_tx_event(group_id, wallet, { group_id, wallet, txid, coin_types, remark: `PRE_REFRESH_OBJECTS_${i}` });
|
|
327
345
|
}, delay_ms);
|
|
328
|
-
(0, dist_1.log_info)(`try_refresh_wallet_objects, txid=${txid}, i=${i}, delay_ms
|
|
346
|
+
(0, dist_1.log_info)(`try_refresh_wallet_objects, txid=${txid}, i=${i}, delay_ms=${delay_ms}ms`);
|
|
329
347
|
}
|
|
330
348
|
});
|
|
331
349
|
}
|
|
@@ -6,7 +6,7 @@ export declare class SuiTxSender {
|
|
|
6
6
|
sui_client: SuiClient;
|
|
7
7
|
grpcClient: SuiGrpcClient;
|
|
8
8
|
constructor(appConfig: AppConfig, sui_client: SuiClient, grpcClient: SuiGrpcClient);
|
|
9
|
-
send_tx: (signedTxBytes: string, signature: string, txDigest: string, send_type: string, read_mask_fields?: string[]
|
|
9
|
+
send_tx: (signedTxBytes: string, signature: string, txDigest: string, send_type: string, read_mask_fields?: string[]) => Promise<void>;
|
|
10
10
|
private sendTxByGrpc;
|
|
11
11
|
private sendTxByRpc;
|
|
12
12
|
}
|
|
@@ -13,50 +13,40 @@ exports.SuiTxSender = void 0;
|
|
|
13
13
|
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
14
14
|
class SuiTxSender {
|
|
15
15
|
constructor(appConfig, sui_client, grpcClient) {
|
|
16
|
-
this.send_tx = (signedTxBytes_1, signature_1, txDigest_1, send_type_1, ...args_1) => __awaiter(this, [signedTxBytes_1, signature_1, txDigest_1, send_type_1, ...args_1], void 0, function* (signedTxBytes, signature, txDigest, send_type, read_mask_fields = ['transaction']
|
|
16
|
+
this.send_tx = (signedTxBytes_1, signature_1, txDigest_1, send_type_1, ...args_1) => __awaiter(this, [signedTxBytes_1, signature_1, txDigest_1, send_type_1, ...args_1], void 0, function* (signedTxBytes, signature, txDigest, send_type, read_mask_fields = ['transaction']) {
|
|
17
17
|
const start_time = Date.now();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
try {
|
|
19
|
+
send_type = (send_type || 'ALL').toUpperCase();
|
|
20
|
+
if (send_type === 'GRPC') {
|
|
21
|
+
yield this.sendTxByGrpc(signedTxBytes, signature, txDigest, read_mask_fields);
|
|
22
|
+
}
|
|
23
|
+
else if (send_type === 'RPC') {
|
|
24
|
+
yield this.sendTxByRpc(signedTxBytes, signature, txDigest);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
yield Promise.race([
|
|
28
|
+
this.sendTxByGrpc(signedTxBytes, signature, txDigest, read_mask_fields),
|
|
29
|
+
this.sendTxByRpc(signedTxBytes, signature, txDigest)
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
(0, dist_1.log_info)(`send_tx success, txid=${txDigest}, send_type=${send_type}, cost=${Date.now() - start_time}ms`);
|
|
24
33
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.sendTxByRpc(signedTxBytes, signature, txDigest, timeoutMs)
|
|
29
|
-
]);
|
|
34
|
+
catch (error) {
|
|
35
|
+
(0, dist_1.log_warn)(`send_tx failed, txid=${txDigest}, send_type=${send_type}, cost=${Date.now() - start_time}ms, error=${error.message}`);
|
|
36
|
+
throw error;
|
|
30
37
|
}
|
|
31
|
-
(0, dist_1.log_info)(`send_tx, txid=${txDigest}, send_type=${send_type}, cost=${Date.now() - start_time}ms`);
|
|
32
38
|
});
|
|
33
39
|
this.appConfig = appConfig;
|
|
34
40
|
this.sui_client = sui_client;
|
|
35
41
|
this.grpcClient = grpcClient;
|
|
36
42
|
}
|
|
37
43
|
sendTxByGrpc(signedTxBytes_1, signature_1, txDigest_1) {
|
|
38
|
-
return __awaiter(this, arguments, void 0, function* (signedTxBytes, signature, txDigest, read_mask_fields = ['transaction']
|
|
44
|
+
return __awaiter(this, arguments, void 0, function* (signedTxBytes, signature, txDigest, read_mask_fields = ['transaction']) {
|
|
39
45
|
const bcsBytes = Buffer.from(signedTxBytes, 'base64');
|
|
40
46
|
const signatureBytes = Buffer.from(signature, 'base64');
|
|
41
47
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
45
|
-
timer = setTimeout(() => {
|
|
46
|
-
resolve(GRPC_TIMEOUT_SENTINEL);
|
|
47
|
-
}, timeoutMs);
|
|
48
|
-
});
|
|
49
|
-
const execPromise = this.grpcClient.transactionService
|
|
50
|
-
.executeTransaction(bcsBytes, signatureBytes, read_mask_fields);
|
|
51
|
-
const result = yield Promise.race([execPromise, timeoutPromise]);
|
|
52
|
-
if (timer) {
|
|
53
|
-
clearTimeout(timer);
|
|
54
|
-
timer = null;
|
|
55
|
-
}
|
|
56
|
-
if (result === GRPC_TIMEOUT_SENTINEL) {
|
|
57
|
-
throw new Error(`grpc executeTransaction timeout after ${timeoutMs}ms`);
|
|
58
|
-
}
|
|
59
|
-
this.appConfig.emit(`SUI_TX_RESULT_${txDigest}`, result);
|
|
48
|
+
let response = yield this.grpcClient.transactionService.executeTransaction(bcsBytes, signatureBytes, read_mask_fields);
|
|
49
|
+
this.appConfig.emit(`SUI_TX_RESULT_${txDigest}`, response);
|
|
60
50
|
}
|
|
61
51
|
catch (error) {
|
|
62
52
|
(0, dist_1.log_warn)(`send tx by grpc failed!!! txid=${txDigest}, error=${error.message}`);
|
|
@@ -64,17 +54,10 @@ class SuiTxSender {
|
|
|
64
54
|
}
|
|
65
55
|
});
|
|
66
56
|
}
|
|
67
|
-
sendTxByRpc(
|
|
68
|
-
return __awaiter(this,
|
|
57
|
+
sendTxByRpc(signedTxBytes, signature, txDigest) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
59
|
try {
|
|
70
|
-
|
|
71
|
-
let timer;
|
|
72
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
73
|
-
timer = setTimeout(() => {
|
|
74
|
-
resolve(RPC_TIMEOUT_SENTINEL);
|
|
75
|
-
}, timeoutMs);
|
|
76
|
-
});
|
|
77
|
-
const execPromise = this.sui_client.executeTransactionBlock({
|
|
60
|
+
let _response = yield this.sui_client.executeTransactionBlock({
|
|
78
61
|
transactionBlock: signedTxBytes,
|
|
79
62
|
signature: signature,
|
|
80
63
|
options: {
|
|
@@ -84,14 +67,6 @@ class SuiTxSender {
|
|
|
84
67
|
showEffects: true
|
|
85
68
|
}
|
|
86
69
|
});
|
|
87
|
-
const result = yield Promise.race([execPromise, timeoutPromise]);
|
|
88
|
-
if (timer) {
|
|
89
|
-
clearTimeout(timer);
|
|
90
|
-
timer = null;
|
|
91
|
-
}
|
|
92
|
-
if (result === RPC_TIMEOUT_SENTINEL) {
|
|
93
|
-
throw new Error(`rpc executeTransactionBlock timeout after ${timeoutMs}ms`);
|
|
94
|
-
}
|
|
95
70
|
this.appConfig.emit(`SUI_TX_RESULT_${txDigest}`, txDigest);
|
|
96
71
|
}
|
|
97
72
|
catch (error) {
|