@clonegod/ttd-sui-common 1.0.52 → 1.0.54
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.
|
@@ -70,7 +70,7 @@ class SimpleRedisClient {
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
withLock(lock_identifier_1, callback_1) {
|
|
73
|
-
return __awaiter(this, arguments, void 0, function* (lock_identifier, callback, release_lock_delay_ms =
|
|
73
|
+
return __awaiter(this, arguments, void 0, function* (lock_identifier, callback, release_lock_delay_ms = 500) {
|
|
74
74
|
const lock_key = this.getLockKey(lock_identifier);
|
|
75
75
|
const lock_value = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
76
76
|
let retries = 0;
|
|
@@ -96,6 +96,7 @@ class SimpleRedisClient {
|
|
|
96
96
|
if (acquired) {
|
|
97
97
|
const release_delay = parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || String(release_lock_delay_ms));
|
|
98
98
|
if (release_delay > 0) {
|
|
99
|
+
(0, dist_1.log_warn)(`withLock release_lock_delay_ms=${release_delay}ms`);
|
|
99
100
|
yield (0, dist_1.sleep)(release_delay);
|
|
100
101
|
}
|
|
101
102
|
yield this.releaseLock(lock_key, lock_value);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { QuoteResultType } from "@clonegod/ttd-core";
|
|
1
2
|
import { AbastrcatTrade, AppConfig, TradeContext } from "@clonegod/ttd-core/dist";
|
|
2
|
-
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
3
3
|
import { SuiClient } from "@mysten/sui/client";
|
|
4
|
-
import {
|
|
4
|
+
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
5
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
6
|
+
import { PoolObjectInfo, SuiGrpcClient, SuiTxSender } from '../index';
|
|
5
7
|
import { SimpleRedisClient } from '../redis';
|
|
6
8
|
export declare abstract class AbstractSuiDexTradePlus extends AbastrcatTrade {
|
|
7
9
|
protected appConfig: AppConfig;
|
|
@@ -27,6 +29,11 @@ export declare abstract class AbstractSuiDexTradePlus extends AbastrcatTrade {
|
|
|
27
29
|
inputToken: any;
|
|
28
30
|
outputToken: any;
|
|
29
31
|
};
|
|
32
|
+
local_calculate_output_amt(context: TradeContext): Promise<QuoteResultType>;
|
|
33
|
+
protected getWalletAssetsFromRedis(walletAddress: string): Promise<any>;
|
|
34
|
+
protected getObjectInfo(objectId: string): Promise<PoolObjectInfo | null>;
|
|
35
|
+
protected compareBigIntBalance(a: any, b: any): number;
|
|
36
|
+
protected mergeTokenObjects(tx: Transaction, primaryCoin: any, objectsToMerge: any[], tokenSymbol: string): void;
|
|
30
37
|
abstract execute(context: TradeContext, retryCount?: number): Promise<string>;
|
|
31
38
|
protected abstract initConfigs(): Promise<void>;
|
|
32
39
|
}
|
|
@@ -14,11 +14,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.AbstractSuiDexTradePlus = void 0;
|
|
16
16
|
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
17
|
-
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
|
|
18
17
|
const client_1 = require("@mysten/sui/client");
|
|
18
|
+
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
|
|
19
|
+
const transactions_1 = require("@mysten/sui/transactions");
|
|
20
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
19
21
|
const index_1 = require("../index");
|
|
20
22
|
const redis_1 = require("../redis");
|
|
21
|
-
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
22
23
|
class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
23
24
|
constructor(appConfig, grpcClient) {
|
|
24
25
|
super();
|
|
@@ -212,5 +213,110 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
|
212
213
|
}
|
|
213
214
|
return { inputToken, outputToken };
|
|
214
215
|
}
|
|
216
|
+
local_calculate_output_amt(context) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
const { price_msg, order_msg, pool_info, trade_runtime, slippage_bps } = context;
|
|
219
|
+
let { ask, bid } = price_msg;
|
|
220
|
+
const { aToB, amount: uiAmount, price: cex_order_price } = order_msg;
|
|
221
|
+
let { inputToken, outputToken } = (0, dist_1.get_input_out_token)(pool_info, aToB);
|
|
222
|
+
let amountIn = new decimal_js_1.default(uiAmount).mul(Math.pow(10, inputToken.decimals));
|
|
223
|
+
let slippage = slippage_bps / 10000;
|
|
224
|
+
let price;
|
|
225
|
+
let amountOut;
|
|
226
|
+
let amountOutMin;
|
|
227
|
+
if (aToB) {
|
|
228
|
+
price = cex_order_price !== null && cex_order_price !== void 0 ? cex_order_price : bid.price;
|
|
229
|
+
amountOut = new decimal_js_1.default(uiAmount).mul(price).mul(Math.pow(10, outputToken.decimals));
|
|
230
|
+
amountOutMin = amountOut.mul(1 - slippage);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
price = cex_order_price !== null && cex_order_price !== void 0 ? cex_order_price : ask.price;
|
|
234
|
+
amountOut = new decimal_js_1.default(uiAmount).div(price).mul(Math.pow(10, outputToken.decimals));
|
|
235
|
+
amountOutMin = amountOut.mul(1 - slippage);
|
|
236
|
+
}
|
|
237
|
+
let quote_result = {
|
|
238
|
+
inputToken,
|
|
239
|
+
outputToken,
|
|
240
|
+
amountIn: Number(amountIn.toFixed(0)),
|
|
241
|
+
amountOut: Number(amountOut.toFixed(0)),
|
|
242
|
+
amountOutMin: Number(amountOutMin.toFixed(0)),
|
|
243
|
+
slippageBps: slippage_bps,
|
|
244
|
+
priceImpact: 0,
|
|
245
|
+
price: price.toString()
|
|
246
|
+
};
|
|
247
|
+
return quote_result;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
getWalletAssetsFromRedis(walletAddress) {
|
|
251
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
252
|
+
try {
|
|
253
|
+
const redisClient = this.appConfig.arb_cache.redis_cmd.redis_client;
|
|
254
|
+
const walletAssetsJson = yield redisClient.HGET('sui:wallet:assets', walletAddress);
|
|
255
|
+
if (!walletAssetsJson) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return JSON.parse(walletAssetsJson);
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
(0, dist_1.log_error)(`从Redis获取钱包资产信息失败`, error);
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
getObjectInfo(objectId) {
|
|
267
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
268
|
+
try {
|
|
269
|
+
let startTime = Date.now();
|
|
270
|
+
const objectResponse = yield this.grpcClient.ledgerService.getObject(objectId, [
|
|
271
|
+
'object_id',
|
|
272
|
+
'version',
|
|
273
|
+
'digest',
|
|
274
|
+
'owner',
|
|
275
|
+
]);
|
|
276
|
+
let objectInfo = null;
|
|
277
|
+
if (objectResponse.object) {
|
|
278
|
+
objectInfo = {
|
|
279
|
+
objectId: objectId,
|
|
280
|
+
version: objectResponse.object.version,
|
|
281
|
+
digest: objectResponse.object.digest,
|
|
282
|
+
initialSharedVersion: objectResponse.object.owner.version
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
(0, dist_1.log_info)(`get object info success, cost: ${Date.now() - startTime} ms`, objectInfo);
|
|
286
|
+
return objectInfo;
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
(0, dist_1.log_error)(`get object info failed, object_id=${objectId}`, error);
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
compareBigIntBalance(a, b) {
|
|
295
|
+
const balanceA = BigInt(a.balance);
|
|
296
|
+
const balanceB = BigInt(b.balance);
|
|
297
|
+
if (balanceA > balanceB)
|
|
298
|
+
return -1;
|
|
299
|
+
if (balanceA < balanceB)
|
|
300
|
+
return 1;
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
mergeTokenObjects(tx, primaryCoin, objectsToMerge, tokenSymbol) {
|
|
304
|
+
if (objectsToMerge.length === 0)
|
|
305
|
+
return;
|
|
306
|
+
const other_objects = objectsToMerge.map((obj) => {
|
|
307
|
+
if (obj.version && obj.digest) {
|
|
308
|
+
return tx.object(transactions_1.Inputs.ObjectRef({
|
|
309
|
+
objectId: obj.objectId,
|
|
310
|
+
version: obj.version,
|
|
311
|
+
digest: obj.digest
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
throw new Error(`${tokenSymbol} 对象 ${obj.objectId} 数据不完整`);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
tx.mergeCoins(primaryCoin, other_objects);
|
|
319
|
+
(0, dist_1.log_info)(`✅ ${tokenSymbol} 进行对象合并,涉及 ${other_objects.length} 个对象`);
|
|
320
|
+
}
|
|
215
321
|
}
|
|
216
322
|
exports.AbstractSuiDexTradePlus = AbstractSuiDexTradePlus;
|
package/dist/type/index.d.ts
CHANGED