@typus/typus-perp-sdk 1.0.61-ut-upgrade → 1.0.61-ut-upgrade-c

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.
@@ -30,8 +30,14 @@ export declare function getLiquidationPriceAndPnl(config: TypusConfig, pythClien
30
30
  positions: Position[];
31
31
  user: string;
32
32
  }): Promise<(string | number)[][] | undefined>;
33
+ export declare function getPositionCount(config: TypusConfig, input: {
34
+ baseToken: TOKEN;
35
+ }): Promise<number>;
33
36
  export declare function getAllPositions(config: TypusConfig, input: {
34
37
  baseToken: TOKEN;
35
38
  slice: string;
36
39
  page: string;
37
40
  }): Promise<Position[]>;
41
+ export declare function getAllPositionsWithTradingSymbol(config: TypusConfig, input: {
42
+ baseToken: TOKEN;
43
+ }): Promise<Position[]>;
package/dist/src/fetch.js CHANGED
@@ -57,7 +57,9 @@ exports.getUserPositions = getUserPositions;
57
57
  exports.parseOptionBidReceipts = parseOptionBidReceipts;
58
58
  exports.getUserStake = getUserStake;
59
59
  exports.getLiquidationPriceAndPnl = getLiquidationPriceAndPnl;
60
+ exports.getPositionCount = getPositionCount;
60
61
  exports.getAllPositions = getAllPositions;
62
+ exports.getAllPositionsWithTradingSymbol = getAllPositionsWithTradingSymbol;
61
63
  var client_1 = require("@mysten/sui/client");
62
64
  var transactions_1 = require("@mysten/sui/transactions");
63
65
  var bcs_1 = require("@mysten/bcs");
@@ -494,6 +496,46 @@ function getLiquidationPriceAndPnl(config, pythClient, input) {
494
496
  });
495
497
  });
496
498
  }
499
+ function getPositionCount(config, input) {
500
+ return __awaiter(this, void 0, void 0, function () {
501
+ var provider, tx, res, raw, maxPageBytes, view, maxPage;
502
+ var _a, _b, _c, _d;
503
+ return __generator(this, function (_e) {
504
+ switch (_e.label) {
505
+ case 0:
506
+ provider = new client_1.SuiClient({ url: config.rpcEndpoint });
507
+ tx = new transactions_1.Transaction();
508
+ // Rust 實作是呼叫 get_all_positions(slice = 1, page = 1) 然後取最後 8 bytes。
509
+ // 這裡直接複用相同邏輯,只需要 max_page。
510
+ (0, functions_1.getAllPositions)(tx, constants_1.tokenType[_1.NETWORK][input.baseToken], {
511
+ version: _1.PERP_VERSION,
512
+ registry: _1.MARKET,
513
+ marketIndex: BigInt(0),
514
+ slice: BigInt(1),
515
+ page: BigInt(1),
516
+ });
517
+ return [4 /*yield*/, provider.devInspectTransactionBlock({
518
+ sender: "0x0000000000000000000000000000000000000000000000000000000000000000",
519
+ transactionBlock: tx,
520
+ })];
521
+ case 1:
522
+ res = _e.sent();
523
+ // 沒有資料時,回傳 0
524
+ 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])) {
525
+ return [2 /*return*/, 0];
526
+ }
527
+ raw = new Uint8Array(res.results[0].returnValues[0][0]);
528
+ // 至少要含 8 bytes 的 max_page;不足代表無 Position
529
+ if (raw.length < 8)
530
+ return [2 /*return*/, 0];
531
+ maxPageBytes = raw.slice(raw.length - 8);
532
+ view = new DataView(maxPageBytes.buffer, maxPageBytes.byteOffset, 8);
533
+ maxPage = Number(view.getBigUint64(0, true));
534
+ return [2 /*return*/, maxPage];
535
+ }
536
+ });
537
+ });
538
+ }
497
539
  function getAllPositions(config, input) {
498
540
  return __awaiter(this, void 0, void 0, function () {
499
541
  var provider, tx, res, raw, withoutMaxPage, reader, userPositionsLen, positions, i, fields, pos;
@@ -539,3 +581,34 @@ function getAllPositions(config, input) {
539
581
  });
540
582
  });
541
583
  }
584
+ function getAllPositionsWithTradingSymbol(config, input) {
585
+ return __awaiter(this, void 0, void 0, function () {
586
+ var total, slice, pages, pagePromises, page, results;
587
+ return __generator(this, function (_a) {
588
+ switch (_a.label) {
589
+ case 0: return [4 /*yield*/, getPositionCount(config, { baseToken: input.baseToken })];
590
+ case 1:
591
+ total = _a.sent();
592
+ // 2) 若無倉位直接回傳 []
593
+ if (total === 0) {
594
+ return [2 /*return*/, []];
595
+ }
596
+ slice = Math.min(total, 100);
597
+ pages = Math.ceil(total / slice);
598
+ pagePromises = [];
599
+ for (page = 1; page <= pages; page++) {
600
+ pagePromises.push(getAllPositions(config, {
601
+ baseToken: input.baseToken,
602
+ slice: slice.toString(),
603
+ page: page.toString(),
604
+ }));
605
+ }
606
+ return [4 /*yield*/, Promise.all(pagePromises)];
607
+ case 2:
608
+ results = _a.sent();
609
+ // 5) 扁平化後回傳
610
+ return [2 /*return*/, results.flat()];
611
+ }
612
+ });
613
+ });
614
+ }
@@ -16,6 +16,13 @@ export declare function mintStakeLp(config: TypusConfig, tx: Transaction, pythCl
16
16
  user: string;
17
17
  stake: boolean;
18
18
  }): Promise<Transaction>;
19
+ export declare function stakeLp(config: TypusConfig, tx: Transaction, input: {
20
+ stakePool: StakePool;
21
+ lpCoins: string[];
22
+ amount: string;
23
+ userShareId: string | null;
24
+ user: string;
25
+ }): Promise<Transaction>;
19
26
  export declare function unstake(config: TypusConfig, tx: Transaction, input: {
20
27
  lpPool: LiquidityPool;
21
28
  stakePool: StakePool;
@@ -65,6 +65,7 @@ var __read = (this && this.__read) || function (o, n) {
65
65
  Object.defineProperty(exports, "__esModule", { value: true });
66
66
  exports.snapshot = snapshot;
67
67
  exports.mintStakeLp = mintStakeLp;
68
+ exports.stakeLp = stakeLp;
68
69
  exports.unstake = unstake;
69
70
  exports.unstakeRedeem = unstakeRedeem;
70
71
  exports.redeemTlp = redeemTlp;
@@ -167,6 +168,32 @@ function mintStakeLp(config, tx, pythClient, input) {
167
168
  });
168
169
  });
169
170
  }
171
+ function stakeLp(config, tx, input) {
172
+ return __awaiter(this, void 0, void 0, function () {
173
+ var coin, destination;
174
+ var _a;
175
+ return __generator(this, function (_b) {
176
+ destination = input.lpCoins.pop();
177
+ if (input.lpCoins.length > 0) {
178
+ tx.mergeCoins(destination, input.lpCoins);
179
+ }
180
+ _a = __read(tx.splitCoins(destination, [input.amount]), 1), coin = _a[0];
181
+ // console.log(iToken);
182
+ if (input.userShareId) {
183
+ harvestStakeReward(config, tx, { stakePool: input.stakePool, userShareId: input.userShareId, user: input.user });
184
+ }
185
+ (0, functions_2.stake)(tx, __1.TLP_TOKEN, {
186
+ version: __1.STAKE_POOL_VERSION,
187
+ registry: __1.STAKE_POOL,
188
+ index: BigInt(0),
189
+ lpToken: coin,
190
+ clock: constants_1.CLOCK,
191
+ userShareId: input.userShareId ? BigInt(input.userShareId) : null,
192
+ });
193
+ return [2 /*return*/, tx];
194
+ });
195
+ });
196
+ }
170
197
  function unstake(config, tx, input) {
171
198
  return __awaiter(this, void 0, void 0, function () {
172
199
  var lpCoin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typus/typus-perp-sdk",
3
- "version": "1.0.61-ut-upgrade",
3
+ "version": "1.0.61-ut-upgrade-c",
4
4
  "repository": "https://github.com/Typus-Lab/typus-perp-sdk.git",
5
5
  "author": "Typus",
6
6
  "description": "typus perp sdk",