@typus/typus-perp-sdk 1.0.62 → 1.0.64

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.
@@ -24,7 +24,7 @@ export declare function obj(tx: Transaction, arg: TransactionObjectInput): {
24
24
  } | ((tx: Transaction) => Exclude<import("valibot").InferInput<typeof import("@mysten/sui/transactions").Argument>, {
25
25
  Input: unknown;
26
26
  type?: "pure";
27
- }>);
27
+ }>) | import("@mysten/sui/dist/cjs/transactions/Transaction").AsyncTransactionThunk<import("@mysten/sui/dist/cjs/transactions/Transaction").TransactionResultArgument>;
28
28
  export declare function pure(tx: Transaction, arg: PureArg, type: string): TransactionArgument;
29
29
  export declare function option(tx: Transaction, type: string, arg: GenericArg | null): TransactionArgument;
30
30
  export declare function generic(tx: Transaction, type: string, arg: GenericArg): TransactionArgument;
@@ -2,6 +2,7 @@ import { Markets, SymbolMarket } from "./typus_perp/trading/structs";
2
2
  import { LiquidityPool } from "./typus_perp/lp-pool/structs";
3
3
  import { TradingOrder, Position } from "./typus_perp/position/structs";
4
4
  import { LpUserShare, StakePool } from "./typus_stake_pool/stake-pool/structs";
5
+ import { TOKEN } from "@typus/typus-sdk/dist/src/constants";
5
6
  import { PythClient, TypusConfig } from "@typus/typus-sdk/dist/src/utils";
6
7
  import { TypusBidReceipt } from "./_dependencies/source/0xb4f25230ba74837d8299e92951306100c4a532e8c48cc3d8828abe9b91c8b274/vault/structs";
7
8
  export declare function getLpPools(config: TypusConfig): Promise<LiquidityPool[]>;
@@ -29,3 +30,14 @@ export declare function getLiquidationPriceAndPnl(config: TypusConfig, pythClien
29
30
  positions: Position[];
30
31
  user: string;
31
32
  }): Promise<(string | number)[][] | undefined>;
33
+ export declare function getPositionCount(config: TypusConfig, input: {
34
+ baseToken: TOKEN;
35
+ }): Promise<number>;
36
+ export declare function getAllPositions(config: TypusConfig, input: {
37
+ baseToken: TOKEN;
38
+ slice: string;
39
+ page: string;
40
+ }): Promise<Position[]>;
41
+ export declare function getAllPositionsWithTradingSymbol(config: TypusConfig, input: {
42
+ baseToken: TOKEN;
43
+ }): Promise<Position[]>;
package/dist/src/fetch.js CHANGED
@@ -57,6 +57,9 @@ exports.getUserPositions = getUserPositions;
57
57
  exports.parseOptionBidReceipts = parseOptionBidReceipts;
58
58
  exports.getUserStake = getUserStake;
59
59
  exports.getLiquidationPriceAndPnl = getLiquidationPriceAndPnl;
60
+ exports.getPositionCount = getPositionCount;
61
+ exports.getAllPositions = getAllPositions;
62
+ exports.getAllPositionsWithTradingSymbol = getAllPositionsWithTradingSymbol;
60
63
  var client_1 = require("@mysten/sui/client");
61
64
  var transactions_1 = require("@mysten/sui/transactions");
62
65
  var bcs_1 = require("@mysten/bcs");
@@ -377,9 +380,8 @@ function getUserStake(config, user) {
377
380
  lpShares_1 = [];
378
381
  reader.readVec(function (reader) {
379
382
  var length = reader.readULEB();
380
- // let bytes = reader.readBytes(length);
381
- // let lpShare = LpUserShare.fromBcs(Uint8Array.from(Array.from(bytes)));
382
- var lpShare = structs_4.LpUserShare.fromFields(structs_4.LpUserShare.bcs.read(reader));
383
+ var bytes = reader.readBytes(length);
384
+ var lpShare = structs_4.LpUserShare.fromBcs(Uint8Array.from(Array.from(bytes)));
383
385
  var incentives = [];
384
386
  reader.readVec(function (reader) {
385
387
  var incentive = reader.read64();
@@ -493,3 +495,136 @@ function getLiquidationPriceAndPnl(config, pythClient, input) {
493
495
  });
494
496
  });
495
497
  }
498
+ function getPositionCount(config, input) {
499
+ return __awaiter(this, void 0, void 0, function () {
500
+ var provider, tx, res, raw, maxPageBytes, view, maxPage;
501
+ var _a, _b, _c, _d;
502
+ return __generator(this, function (_e) {
503
+ switch (_e.label) {
504
+ case 0:
505
+ provider = new client_1.SuiClient({ url: config.rpcEndpoint });
506
+ tx = new transactions_1.Transaction();
507
+ // Rust 實作是呼叫 get_all_positions(slice = 1, page = 1) 然後取最後 8 bytes。
508
+ // 這裡直接複用相同邏輯,只需要 max_page。
509
+ (0, functions_1.getAllPositions)(tx, constants_1.tokenType[_1.NETWORK][input.baseToken], {
510
+ version: _1.PERP_VERSION,
511
+ registry: _1.MARKET,
512
+ marketIndex: BigInt(0),
513
+ slice: BigInt(1),
514
+ page: BigInt(1),
515
+ });
516
+ return [4 /*yield*/, provider.devInspectTransactionBlock({
517
+ sender: "0x0000000000000000000000000000000000000000000000000000000000000000",
518
+ transactionBlock: tx,
519
+ })];
520
+ case 1:
521
+ res = _e.sent();
522
+ // 沒有資料時,回傳 0
523
+ if (!((_d = (_c = (_b = (_a = res.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.returnValues) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d[0])) {
524
+ return [2 /*return*/, 0];
525
+ }
526
+ raw = new Uint8Array(res.results[0].returnValues[0][0]);
527
+ // 至少要含 8 bytes 的 max_page;不足代表無 Position
528
+ if (raw.length < 8)
529
+ return [2 /*return*/, 0];
530
+ maxPageBytes = raw.slice(raw.length - 8);
531
+ view = new DataView(maxPageBytes.buffer, maxPageBytes.byteOffset, 8);
532
+ maxPage = Number(view.getBigUint64(0, true));
533
+ return [2 /*return*/, maxPage];
534
+ }
535
+ });
536
+ });
537
+ }
538
+ function getAllPositions(config, input) {
539
+ return __awaiter(this, void 0, void 0, function () {
540
+ var provider, tx, res, raw, withoutMaxPage, buffer, view, reader, userPositionsLen, positions, i, bytes, pos;
541
+ var _a, _b, _c, _d;
542
+ return __generator(this, function (_e) {
543
+ switch (_e.label) {
544
+ case 0:
545
+ provider = new client_1.SuiClient({ url: config.rpcEndpoint });
546
+ tx = new transactions_1.Transaction();
547
+ (0, functions_1.getAllPositions)(tx, constants_1.tokenType[_1.NETWORK][input.baseToken], {
548
+ version: _1.PERP_VERSION,
549
+ registry: _1.MARKET,
550
+ marketIndex: BigInt(0),
551
+ slice: BigInt(input.slice),
552
+ page: BigInt(input.page),
553
+ });
554
+ return [4 /*yield*/, provider.devInspectTransactionBlock({
555
+ sender: "0x0000000000000000000000000000000000000000000000000000000000000000",
556
+ transactionBlock: tx,
557
+ })];
558
+ case 1:
559
+ res = _e.sent();
560
+ if (!((_d = (_c = (_b = (_a = res.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.returnValues) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d[0])) {
561
+ return [2 /*return*/, []];
562
+ }
563
+ raw = new Uint8Array(res.results[0].returnValues[0][0]);
564
+ console.debug("Raw data length:", raw.length);
565
+ // 1) 至少要有 8 bytes 的 max_page
566
+ if (raw.length < 8)
567
+ return [2 /*return*/, []];
568
+ withoutMaxPage = raw.slice(0, raw.length - 8);
569
+ console.log("Data without max page length:", withoutMaxPage.length);
570
+ buffer = new ArrayBuffer(withoutMaxPage.length);
571
+ view = new Uint8Array(buffer);
572
+ view.set(withoutMaxPage);
573
+ reader = new bcs_1.BcsReader(view);
574
+ try {
575
+ userPositionsLen = reader.read8() - 1;
576
+ console.debug("User positions length:", userPositionsLen);
577
+ positions = [];
578
+ for (i = 0; i < userPositionsLen; i++) {
579
+ try {
580
+ bytes = reader.readBytes(reader.readULEB());
581
+ pos = structs_3.Position.fromBcs(Uint8Array.from(Array.from(bytes)));
582
+ positions.push(pos);
583
+ }
584
+ catch (e) {
585
+ console.error("Error parsing position ".concat(i, ":"), e);
586
+ break;
587
+ }
588
+ }
589
+ return [2 /*return*/, positions];
590
+ }
591
+ catch (e) {
592
+ console.error("Error parsing positions:", e);
593
+ return [2 /*return*/, []];
594
+ }
595
+ return [2 /*return*/];
596
+ }
597
+ });
598
+ });
599
+ }
600
+ function getAllPositionsWithTradingSymbol(config, input) {
601
+ return __awaiter(this, void 0, void 0, function () {
602
+ var total, slice, pages, pagePromises, page, results;
603
+ return __generator(this, function (_a) {
604
+ switch (_a.label) {
605
+ case 0: return [4 /*yield*/, getPositionCount(config, { baseToken: input.baseToken })];
606
+ case 1:
607
+ total = _a.sent();
608
+ // 2) 若無倉位直接回傳 []
609
+ if (total === 0) {
610
+ return [2 /*return*/, []];
611
+ }
612
+ slice = Math.min(total, 100);
613
+ pages = Math.ceil(total / slice);
614
+ pagePromises = [];
615
+ for (page = 1; page <= pages; page++) {
616
+ pagePromises.push(getAllPositions(config, {
617
+ baseToken: input.baseToken,
618
+ slice: slice.toString(),
619
+ page: page.toString(),
620
+ }));
621
+ }
622
+ return [4 /*yield*/, Promise.all(pagePromises)];
623
+ case 2:
624
+ results = _a.sent();
625
+ // 5) 扁平化後回傳
626
+ return [2 /*return*/, results.flat()];
627
+ }
628
+ });
629
+ });
630
+ }
package/dist/src/index.js CHANGED
@@ -30,7 +30,7 @@ exports.PERP_PACKAGE_ID = exports.NETWORK == "MAINNET"
30
30
  ? "0xe27969a70f93034de9ce16e6ad661b480324574e68d15a64b513fd90eb2423e5"
31
31
  : "0x585924f160f83ef16f8927ec117e4d740abb6f4e571ecfa89ff3e973042cb1b9";
32
32
  exports.PERP_PUBLISHED_AT = exports.NETWORK == "MAINNET"
33
- ? "0x347b833859a487218737de4a5e6b0fb861d37e653a10f44d420279c984badf9c"
33
+ ? "0xb7e4416296bb3629547b5a0ab24dec17851760eca393c58fb62edbab71c76fa2"
34
34
  : "0x15844f80fb085bb8fd7cc688ade6282cd6991209eae78934ca001dced8b1b7de";
35
35
  exports.PERP_PKG_V1 = exports.NETWORK == "MAINNET"
36
36
  ? "0xe27969a70f93034de9ce16e6ad661b480324574e68d15a64b513fd90eb2423e5"
@@ -39,7 +39,7 @@ exports.STAKE_PACKAGE_ID = exports.NETWORK == "MAINNET"
39
39
  ? "0xc427209145715a00a93d7e674a95c556a7147d79fda1bbaeb1a1cac5f9923966"
40
40
  : "0x220e7ba8923c0c30b57c0baab3bc15d781a39bb294cf7af318c0fc816b5cf7e6";
41
41
  exports.STAKE_PUBLISHED_AT = exports.NETWORK == "MAINNET"
42
- ? "0xd9965c09d5ad7d69306b7c550b3b135d94b34f98c3adc379a7333fff2f05808b"
42
+ ? "0x758c5a79d4f788364303cc547f44a46a1622318073006937451e817c0e682c00"
43
43
  : "0x34ba6bf1893409b1a7494350189e060ee79cc2aba6f0c5e4045af9372d96fe85";
44
44
  exports.STAKE_PKG_V1 = exports.NETWORK == "MAINNET"
45
45
  ? "0xc427209145715a00a93d7e674a95c556a7147d79fda1bbaeb1a1cac5f9923966"
@@ -13,6 +13,11 @@ export interface AddTradingSymbolArgs {
13
13
  basicFundingRate: bigint | TransactionArgument;
14
14
  fundingIntervalTsMs: bigint | TransactionArgument;
15
15
  expMultiplier: bigint | TransactionArgument;
16
+ maxBuyOpenInterest: bigint | TransactionArgument;
17
+ maxSellOpenInterest: bigint | TransactionArgument;
18
+ maintenanceMarginRateBp: bigint | TransactionArgument;
19
+ optionMaintenanceMarginRateBp: bigint | TransactionArgument;
20
+ optionTradingFeeConfig: Array<bigint | TransactionArgument> | TransactionArgument;
16
21
  clock: TransactionObjectInput;
17
22
  }
18
23
  export declare function addTradingSymbol(tx: Transaction, typeArg: string, args: AddTradingSymbolArgs, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
@@ -238,6 +243,14 @@ export interface GetActiveOrdersByOrderTagAndCtokenArgs {
238
243
  orderTypeTag: number | TransactionArgument;
239
244
  }
240
245
  export declare function getActiveOrdersByOrderTagAndCtoken(tx: Transaction, typeArgs: [string, string], args: GetActiveOrdersByOrderTagAndCtokenArgs, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
246
+ export interface GetAllPositionsArgs {
247
+ version: TransactionObjectInput;
248
+ registry: TransactionObjectInput;
249
+ marketIndex: bigint | TransactionArgument;
250
+ slice: bigint | TransactionArgument;
251
+ page: bigint | TransactionArgument;
252
+ }
253
+ export declare function getAllPositions(tx: Transaction, typeArg: string, args: GetAllPositionsArgs, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
241
254
  export interface GetEstimatedLiquidationPriceAndPnlArgs {
242
255
  version: TransactionObjectInput;
243
256
  registry: TransactionObjectInput;
@@ -592,6 +605,14 @@ export interface ReleaseCollateralArgs {
592
605
  releaseAmount: bigint | TransactionArgument;
593
606
  }
594
607
  export declare function releaseCollateral(tx: Transaction, typeArgs: [string, string], args: ReleaseCollateralArgs, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
608
+ export interface RemoveLinkedOrder_Args {
609
+ version: TransactionObjectInput;
610
+ marketIndex: bigint | TransactionArgument;
611
+ symbolMarket: TransactionObjectInput;
612
+ order: TransactionObjectInput;
613
+ user: string | TransactionArgument;
614
+ }
615
+ export declare function removeLinkedOrder_(tx: Transaction, typeArg: string, args: RemoveLinkedOrder_Args, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
595
616
  export interface RemoveLinkedOrdersArgs {
596
617
  version: TransactionObjectInput;
597
618
  marketIndex: bigint | TransactionArgument;
@@ -676,6 +697,9 @@ export interface UpdateMarketConfigArgs {
676
697
  expMultiplier: bigint | TransactionArgument | TransactionArgument | null;
677
698
  maxBuyOpenInterest: bigint | TransactionArgument | TransactionArgument | null;
678
699
  maxSellOpenInterest: bigint | TransactionArgument | TransactionArgument | null;
700
+ maintenanceMarginRateBp: bigint | TransactionArgument | TransactionArgument | null;
701
+ optionCollateralMaintenanceMarginRateBp: bigint | TransactionArgument | TransactionArgument | null;
702
+ optionCollateralTradingFeeConfig: Array<bigint | TransactionArgument> | TransactionArgument | TransactionArgument | null;
679
703
  }
680
704
  export declare function updateMarketConfig(tx: Transaction, typeArg: string, args: UpdateMarketConfigArgs, published_at?: string): import("@mysten/sui/transactions").TransactionResult;
681
705
  export interface UpdateProtocolFeeShareBpArgs {
@@ -19,6 +19,7 @@ exports.executeOrder_ = executeOrder_;
19
19
  exports.exerciseBidReceipts = exerciseBidReceipts;
20
20
  exports.getActiveOrdersByOrderTag = getActiveOrdersByOrderTag;
21
21
  exports.getActiveOrdersByOrderTagAndCtoken = getActiveOrdersByOrderTagAndCtoken;
22
+ exports.getAllPositions = getAllPositions;
22
23
  exports.getEstimatedLiquidationPriceAndPnl = getEstimatedLiquidationPriceAndPnl;
23
24
  exports.getExpiredPositionInfo = getExpiredPositionInfo;
24
25
  exports.getLinkedPosition = getLinkedPosition;
@@ -48,6 +49,7 @@ exports.newMarkets = newMarkets;
48
49
  exports.reduceOptionCollateralPositionSize = reduceOptionCollateralPositionSize;
49
50
  exports.reduceOptionCollateralPositionSizeV2 = reduceOptionCollateralPositionSizeV2;
50
51
  exports.releaseCollateral = releaseCollateral;
52
+ exports.removeLinkedOrder_ = removeLinkedOrder_;
51
53
  exports.removeLinkedOrders = removeLinkedOrders;
52
54
  exports.removeTradingSymbol = removeTradingSymbol;
53
55
  exports.resumeMarket = resumeMarket;
@@ -83,6 +85,11 @@ function addTradingSymbol(tx, typeArg, args, published_at) {
83
85
  (0, util_1.pure)(tx, args.basicFundingRate, "u64"),
84
86
  (0, util_1.pure)(tx, args.fundingIntervalTsMs, "u64"),
85
87
  (0, util_1.pure)(tx, args.expMultiplier, "u64"),
88
+ (0, util_1.pure)(tx, args.maxBuyOpenInterest, "u64"),
89
+ (0, util_1.pure)(tx, args.maxSellOpenInterest, "u64"),
90
+ (0, util_1.pure)(tx, args.maintenanceMarginRateBp, "u64"),
91
+ (0, util_1.pure)(tx, args.optionMaintenanceMarginRateBp, "u64"),
92
+ (0, util_1.pure)(tx, args.optionTradingFeeConfig, "vector<u64>"),
86
93
  (0, util_1.obj)(tx, args.clock),
87
94
  ],
88
95
  });
@@ -398,6 +405,20 @@ function getActiveOrdersByOrderTagAndCtoken(tx, typeArgs, args, published_at) {
398
405
  arguments: [(0, util_1.obj)(tx, args.version), (0, util_1.obj)(tx, args.registry), (0, util_1.pure)(tx, args.marketIndex, "u64"), (0, util_1.pure)(tx, args.orderTypeTag, "u8")],
399
406
  });
400
407
  }
408
+ function getAllPositions(tx, typeArg, args, published_at) {
409
+ if (published_at === void 0) { published_at = __1.PUBLISHED_AT; }
410
+ return tx.moveCall({
411
+ target: "".concat(published_at, "::trading::get_all_positions"),
412
+ typeArguments: [typeArg],
413
+ arguments: [
414
+ (0, util_1.obj)(tx, args.version),
415
+ (0, util_1.obj)(tx, args.registry),
416
+ (0, util_1.pure)(tx, args.marketIndex, "u64"),
417
+ (0, util_1.pure)(tx, args.slice, "u64"),
418
+ (0, util_1.pure)(tx, args.page, "u64"),
419
+ ],
420
+ });
421
+ }
401
422
  function getEstimatedLiquidationPriceAndPnl(tx, typeArgs, args, published_at) {
402
423
  if (published_at === void 0) { published_at = __1.PUBLISHED_AT; }
403
424
  return tx.moveCall({
@@ -891,6 +912,20 @@ function releaseCollateral(tx, typeArgs, args, published_at) {
891
912
  ],
892
913
  });
893
914
  }
915
+ function removeLinkedOrder_(tx, typeArg, args, published_at) {
916
+ if (published_at === void 0) { published_at = __1.PUBLISHED_AT; }
917
+ return tx.moveCall({
918
+ target: "".concat(published_at, "::trading::remove_linked_order_"),
919
+ typeArguments: [typeArg],
920
+ arguments: [
921
+ (0, util_1.obj)(tx, args.version),
922
+ (0, util_1.pure)(tx, args.marketIndex, "u64"),
923
+ (0, util_1.obj)(tx, args.symbolMarket),
924
+ (0, util_1.obj)(tx, args.order),
925
+ (0, util_1.pure)(tx, args.user, "address"),
926
+ ],
927
+ });
928
+ }
894
929
  function removeLinkedOrders(tx, typeArg, args, published_at) {
895
930
  if (published_at === void 0) { published_at = __1.PUBLISHED_AT; }
896
931
  return tx.moveCall({
@@ -1018,6 +1053,9 @@ function updateMarketConfig(tx, typeArg, args, published_at) {
1018
1053
  (0, util_1.pure)(tx, args.expMultiplier, "".concat(structs_1.Option.$typeName, "<u64>")),
1019
1054
  (0, util_1.pure)(tx, args.maxBuyOpenInterest, "".concat(structs_1.Option.$typeName, "<u64>")),
1020
1055
  (0, util_1.pure)(tx, args.maxSellOpenInterest, "".concat(structs_1.Option.$typeName, "<u64>")),
1056
+ (0, util_1.pure)(tx, args.maintenanceMarginRateBp, "".concat(structs_1.Option.$typeName, "<u64>")),
1057
+ (0, util_1.pure)(tx, args.optionCollateralMaintenanceMarginRateBp, "".concat(structs_1.Option.$typeName, "<u64>")),
1058
+ (0, util_1.pure)(tx, args.optionCollateralTradingFeeConfig, "".concat(structs_1.Option.$typeName, "<vector<u64>>")),
1021
1059
  ],
1022
1060
  });
1023
1061
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@typus/typus-perp-sdk",
3
- "version": "1.0.62",
3
+ "version": "1.0.64",
4
4
  "repository": "https://github.com/Typus-Lab/typus-perp-sdk.git",
5
5
  "author": "Typus",
6
6
  "description": "typus perp sdk",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@typus/typus-sdk": "1.6.30"
9
+ "@typus/typus-sdk": "1.7.0"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@types/bs58": "^4.0.1",