@clonegod/ttd-sui-common 1.0.53 → 1.0.55

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.
@@ -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 { SuiGrpcClient, SuiTxSender } from '../index';
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): 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,108 @@ class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
212
213
  }
213
214
  return { inputToken, outputToken };
214
215
  }
216
+ local_calculate_output_amt(context) {
217
+ const { price_msg, order_msg, pool_info, trade_runtime, slippage_bps } = context;
218
+ let { ask, bid } = price_msg;
219
+ const { aToB, amount: uiAmount, price: cex_order_price } = order_msg;
220
+ let { inputToken, outputToken } = (0, dist_1.get_input_out_token)(pool_info, aToB);
221
+ let amountIn = new decimal_js_1.default(uiAmount).mul(Math.pow(10, inputToken.decimals));
222
+ let slippage = slippage_bps / 10000;
223
+ let price;
224
+ let amountOut;
225
+ let amountOutMin;
226
+ if (aToB) {
227
+ price = cex_order_price !== null && cex_order_price !== void 0 ? cex_order_price : bid.price;
228
+ amountOut = new decimal_js_1.default(uiAmount).mul(price).mul(Math.pow(10, outputToken.decimals));
229
+ amountOutMin = amountOut.mul(1 - slippage);
230
+ }
231
+ else {
232
+ price = cex_order_price !== null && cex_order_price !== void 0 ? cex_order_price : ask.price;
233
+ amountOut = new decimal_js_1.default(uiAmount).div(price).mul(Math.pow(10, outputToken.decimals));
234
+ amountOutMin = amountOut.mul(1 - slippage);
235
+ }
236
+ let quote_result = {
237
+ inputToken,
238
+ outputToken,
239
+ amountIn: Number(amountIn.toFixed(0)),
240
+ amountOut: Number(amountOut.toFixed(0)),
241
+ amountOutMin: Number(amountOutMin.toFixed(0)),
242
+ slippageBps: slippage_bps,
243
+ priceImpact: 0,
244
+ price: price.toString()
245
+ };
246
+ return quote_result;
247
+ }
248
+ getWalletAssetsFromRedis(walletAddress) {
249
+ return __awaiter(this, void 0, void 0, function* () {
250
+ try {
251
+ const redisClient = this.appConfig.arb_cache.redis_cmd.redis_client;
252
+ const walletAssetsJson = yield redisClient.HGET('sui:wallet:assets', walletAddress);
253
+ if (!walletAssetsJson) {
254
+ return null;
255
+ }
256
+ return JSON.parse(walletAssetsJson);
257
+ }
258
+ catch (error) {
259
+ (0, dist_1.log_error)(`从Redis获取钱包资产信息失败`, error);
260
+ throw error;
261
+ }
262
+ });
263
+ }
264
+ getObjectInfo(objectId) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ try {
267
+ let startTime = Date.now();
268
+ const objectResponse = yield this.grpcClient.ledgerService.getObject(objectId, [
269
+ 'object_id',
270
+ 'version',
271
+ 'digest',
272
+ 'owner',
273
+ ]);
274
+ let objectInfo = null;
275
+ if (objectResponse.object) {
276
+ objectInfo = {
277
+ objectId: objectId,
278
+ version: objectResponse.object.version,
279
+ digest: objectResponse.object.digest,
280
+ initialSharedVersion: objectResponse.object.owner.version
281
+ };
282
+ }
283
+ (0, dist_1.log_info)(`get object info success, cost: ${Date.now() - startTime} ms`, objectInfo);
284
+ return objectInfo;
285
+ }
286
+ catch (error) {
287
+ (0, dist_1.log_error)(`get object info failed, object_id=${objectId}`, error);
288
+ return null;
289
+ }
290
+ });
291
+ }
292
+ compareBigIntBalance(a, b) {
293
+ const balanceA = BigInt(a.balance);
294
+ const balanceB = BigInt(b.balance);
295
+ if (balanceA > balanceB)
296
+ return -1;
297
+ if (balanceA < balanceB)
298
+ return 1;
299
+ return 0;
300
+ }
301
+ mergeTokenObjects(tx, primaryCoin, objectsToMerge, tokenSymbol) {
302
+ if (objectsToMerge.length === 0)
303
+ return;
304
+ const other_objects = objectsToMerge.map((obj) => {
305
+ if (obj.version && obj.digest) {
306
+ return tx.object(transactions_1.Inputs.ObjectRef({
307
+ objectId: obj.objectId,
308
+ version: obj.version,
309
+ digest: obj.digest
310
+ }));
311
+ }
312
+ else {
313
+ throw new Error(`${tokenSymbol} 对象 ${obj.objectId} 数据不完整`);
314
+ }
315
+ });
316
+ tx.mergeCoins(primaryCoin, other_objects);
317
+ (0, dist_1.log_info)(`✅ ${tokenSymbol} 进行对象合并,涉及 ${other_objects.length} 个对象`);
318
+ }
215
319
  }
216
320
  exports.AbstractSuiDexTradePlus = AbstractSuiDexTradePlus;
@@ -16,3 +16,9 @@ export interface SimpleTxEventsType {
16
16
  pair: string;
17
17
  events: any[];
18
18
  }
19
+ export interface PoolObjectInfo {
20
+ objectId: string;
21
+ version: string;
22
+ digest: string;
23
+ initialSharedVersion?: string;
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sui-common",
3
- "version": "1.0.53",
3
+ "version": "1.0.55",
4
4
  "description": "Sui common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",