@typus/typus-perp-sdk 1.0.4 → 1.0.6

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.
@@ -17,7 +17,7 @@ export declare function getUserOrders(config: TypusConfig, user: string): Promis
17
17
  export declare function getUserPositions(config: TypusConfig, user: string): Promise<Position[]>;
18
18
  export declare function parseOptionBidReceipts(positions: Position[]): (TypusBidReceipt | null)[];
19
19
  export declare function getUserStake(config: TypusConfig, user: string): Promise<LpUserShare[]>;
20
- export declare function getLiquidationPrice(config: TypusConfig, pythClient: PythClient, input: {
20
+ export declare function getLiquidationPriceAndPnl(config: TypusConfig, pythClient: PythClient, input: {
21
21
  positions: Position[];
22
22
  user: string;
23
- }): Promise<string[] | undefined>;
23
+ }): Promise<(string | number)[][] | undefined>;
package/dist/src/fetch.js CHANGED
@@ -54,7 +54,7 @@ exports.getUserOrders = getUserOrders;
54
54
  exports.getUserPositions = getUserPositions;
55
55
  exports.parseOptionBidReceipts = parseOptionBidReceipts;
56
56
  exports.getUserStake = getUserStake;
57
- exports.getLiquidationPrice = getLiquidationPrice;
57
+ exports.getLiquidationPriceAndPnl = getLiquidationPriceAndPnl;
58
58
  var client_1 = require("@mysten/sui/client");
59
59
  var transactions_1 = require("@mysten/sui/transactions");
60
60
  var bcs_1 = require("@mysten/bcs");
@@ -352,9 +352,10 @@ function getUserStake(config, user) {
352
352
  });
353
353
  });
354
354
  }
355
- function getLiquidationPrice(config, pythClient, input) {
355
+ /// returns [liquidationPrice, pnl(in USD)]
356
+ function getLiquidationPriceAndPnl(config, pythClient, input) {
356
357
  return __awaiter(this, void 0, void 0, function () {
357
- var provider, tx, pythTokens, _a, _b, position, TOKEN, BASE_TOKEN, _c, _d, position, TOKEN, BASE_TOKEN, res, prices;
358
+ var provider, tx, pythTokens, _a, _b, position, TOKEN, BASE_TOKEN, _c, _d, position, TOKEN, BASE_TOKEN, res, results;
358
359
  var e_3, _e, e_4, _f;
359
360
  var _g;
360
361
  return __generator(this, function (_h) {
@@ -387,7 +388,7 @@ function getLiquidationPrice(config, pythClient, input) {
387
388
  position = _d.value;
388
389
  TOKEN = (0, constants_1.typeArgToToken)(position.collateralToken.name);
389
390
  BASE_TOKEN = (0, constants_1.typeArgToToken)(position.symbol.baseToken.name);
390
- (0, functions_1.getEstimatedLiquidationPrice)(tx, [position.collateralToken.name, position.symbol.baseToken.name], {
391
+ (0, functions_1.getEstimatedLiquidationPriceAndPnl)(tx, [position.collateralToken.name, position.symbol.baseToken.name], {
391
392
  version: _1.PERP_VERSION,
392
393
  registry: _1.MARKET,
393
394
  poolRegistry: _1.LP_POOL,
@@ -411,9 +412,19 @@ function getLiquidationPrice(config, pythClient, input) {
411
412
  return [4 /*yield*/, provider.devInspectTransactionBlock({ sender: input.user, transactionBlock: tx })];
412
413
  case 2:
413
414
  res = _h.sent();
414
- prices = (_g = res.results) === null || _g === void 0 ? void 0 : _g.slice(-input.positions.length).map(function (x) { return bcs_1.bcs.u64().parse(Uint8Array.from(x.returnValues[0][0])); });
415
- // console.log(prices);
416
- return [2 /*return*/, prices];
415
+ results = (_g = res.results) === null || _g === void 0 ? void 0 : _g.slice(-input.positions.length).map(function (x) {
416
+ // console.log(x);
417
+ var liquidationPrice = bcs_1.bcs.u64().parse(Uint8Array.from(x.returnValues[0][0]));
418
+ var isProfit = bcs_1.bcs.bool().parse(Uint8Array.from(x.returnValues[1][0]));
419
+ var pnl = Number(bcs_1.bcs.u64().parse(Uint8Array.from(x.returnValues[2][0])));
420
+ pnl = isProfit ? pnl : -pnl;
421
+ var isCost = bcs_1.bcs.bool().parse(Uint8Array.from(x.returnValues[3][0]));
422
+ var cost = Number(bcs_1.bcs.u64().parse(Uint8Array.from(x.returnValues[4][0])));
423
+ cost = isCost ? cost : -cost;
424
+ return [liquidationPrice, pnl - cost];
425
+ });
426
+ // console.log(results);
427
+ return [2 /*return*/, results];
417
428
  }
418
429
  });
419
430
  });
@@ -5,6 +5,7 @@ export declare const LP_POOL: string;
5
5
  export declare const LIQUIDITY_POOL: string;
6
6
  export declare const MARKET: string;
7
7
  export declare const TLP: string;
8
+ export declare const TLP_TOKEN: string;
8
9
  export declare const TLP_TREASURY_CAP: string;
9
10
  export declare const STAKE_POOL: string;
10
11
  export declare const PERP_VERSION: string;
package/dist/src/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.STAKE_POOL_VERSION = exports.PERP_VERSION = exports.STAKE_POOL = exports.TLP_TREASURY_CAP = exports.TLP = exports.MARKET = exports.LIQUIDITY_POOL = exports.LP_POOL = exports.NETWORK = void 0;
20
+ exports.STAKE_POOL_VERSION = exports.PERP_VERSION = exports.STAKE_POOL = exports.TLP_TREASURY_CAP = exports.TLP_TOKEN = exports.TLP = exports.MARKET = exports.LIQUIDITY_POOL = exports.LP_POOL = exports.NETWORK = void 0;
21
21
  __exportStar(require("./fetch"), exports);
22
22
  __exportStar(require("./user"), exports);
23
23
  var dotenv_1 = __importDefault(require("dotenv"));
@@ -30,8 +30,8 @@ exports.LP_POOL = exports.NETWORK == "MAINNET" ? "" : "0xf85bb9b6bbe877e43e9a79f
30
30
  exports.LIQUIDITY_POOL = exports.NETWORK == "MAINNET" ? "" : "0x93bc498e59e97c93820c14687a9a340bc794eada7f7ce0f458bb25190bdd395e";
31
31
  exports.MARKET = exports.NETWORK == "MAINNET" ? "" : "0xb73983f279effad13b455846c192a2eb402ed453f84a1e627c9e34cfd329bf92";
32
32
  exports.TLP = exports.NETWORK == "MAINNET" ? "" : "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509";
33
+ exports.TLP_TOKEN = exports.NETWORK == "MAINNET" ? "" : "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509::tlp::TLP";
33
34
  exports.TLP_TREASURY_CAP = exports.NETWORK == "MAINNET" ? "" : "0x7d9c6f9dfceb4788c21388f77e4ccf83e1b3f10282ae272b379402e92c97bbf6";
34
35
  exports.STAKE_POOL = exports.NETWORK == "MAINNET" ? "" : "0x27efed0157eea40aa1a8483622edeb48ca8c3fa3073f9e662503280a22a4e51b";
35
36
  exports.PERP_VERSION = exports.NETWORK == "MAINNET" ? "" : "0xa3066f440d0e2fa74b1c98e55c316ce63b208be87b018b089bf39d5dd11e8dfd";
36
37
  exports.STAKE_POOL_VERSION = exports.NETWORK == "MAINNET" ? "" : "0x25868624d2254a14eb513ae2b276fb56ca355ffd6cdd8ee05c5a51620f3f1bf3";
37
- // "TLP_TOKEN": "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509::tlp::TLP",
@@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PKG_V1 = exports.PUBLISHED_AT = exports.PACKAGE_ID = void 0;
4
4
  var index_1 = require("../index");
5
5
  exports.PACKAGE_ID = index_1.NETWORK == "MAINNET" ? "" : "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509";
6
- exports.PUBLISHED_AT = index_1.NETWORK == "MAINNET" ? "" : "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509";
6
+ exports.PUBLISHED_AT = index_1.NETWORK == "MAINNET" ? "" : "0x002be1fdabc2d7e0de0244d55cc967a2a9bbd19e8fb7e9e97c6118576d2ce767";
7
7
  exports.PKG_V1 = index_1.NETWORK == "MAINNET" ? "" : "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509";
@@ -73,6 +73,7 @@ export interface CalculateTradingFeeArgs {
73
73
  collateralTokenDecimal: bigint | TransactionArgument;
74
74
  }
75
75
  export declare function calculateTradingFee(tx: Transaction, args: CalculateTradingFeeArgs): import("@mysten/sui/transactions").TransactionResult;
76
+ export declare function calculateUnrealizedCost(tx: Transaction, position: TransactionObjectInput): import("@mysten/sui/transactions").TransactionResult;
76
77
  export interface CalculateUnrealizedPnlArgs {
77
78
  position: TransactionObjectInput;
78
79
  tradingPairOraclePrice: bigint | TransactionArgument;
@@ -178,7 +179,6 @@ export interface GetEstimatedLiquidationPriceArgs {
178
179
  tradingPairOraclePrice: bigint | TransactionArgument;
179
180
  tradingPairOraclePriceDecimal: bigint | TransactionArgument;
180
181
  tradingFeeMbp: bigint | TransactionArgument;
181
- cumulativeBorrowRate: bigint | TransactionArgument;
182
182
  }
183
183
  export declare function getEstimatedLiquidationPrice(tx: Transaction, args: GetEstimatedLiquidationPriceArgs): import("@mysten/sui/transactions").TransactionResult;
184
184
  export declare function getMaxOrderTypeTag(tx: Transaction): import("@mysten/sui/transactions").TransactionResult;
@@ -8,6 +8,7 @@ exports.calculateIntrinsicValue = calculateIntrinsicValue;
8
8
  exports.calculatePositionFundingRate = calculatePositionFundingRate;
9
9
  exports.calculateRealizedPnlUsd = calculateRealizedPnlUsd;
10
10
  exports.calculateTradingFee = calculateTradingFee;
11
+ exports.calculateUnrealizedCost = calculateUnrealizedCost;
11
12
  exports.calculateUnrealizedPnl = calculateUnrealizedPnl;
12
13
  exports.checkOptionCollateralPositionLiquidated = checkOptionCollateralPositionLiquidated;
13
14
  exports.checkOrderFilled = checkOrderFilled;
@@ -169,6 +170,9 @@ function calculateTradingFee(tx, args) {
169
170
  ],
170
171
  });
171
172
  }
173
+ function calculateUnrealizedCost(tx, position) {
174
+ return tx.moveCall({ target: "".concat(__1.PUBLISHED_AT, "::position::calculate_unrealized_cost"), arguments: [(0, util_1.obj)(tx, position)] });
175
+ }
172
176
  function calculateUnrealizedPnl(tx, args) {
173
177
  return tx.moveCall({
174
178
  target: "".concat(__1.PUBLISHED_AT, "::position::calculate_unrealized_pnl"),
@@ -308,7 +312,6 @@ function getEstimatedLiquidationPrice(tx, args) {
308
312
  (0, util_1.pure)(tx, args.tradingPairOraclePrice, "u64"),
309
313
  (0, util_1.pure)(tx, args.tradingPairOraclePriceDecimal, "u64"),
310
314
  (0, util_1.pure)(tx, args.tradingFeeMbp, "u64"),
311
- (0, util_1.pure)(tx, args.cumulativeBorrowRate, "u64"),
312
315
  ],
313
316
  });
314
317
  }
@@ -72,7 +72,7 @@ export declare class TLP implements StructClass {
72
72
  static reified(): TLPReified;
73
73
  static get r(): import("../../_framework/reified").StructClassReified<TLP, TLPFields>;
74
74
  static phantom(): PhantomReified<ToTypeStr<TLP>>;
75
- static get p(): PhantomReified<"::tlp::TLP" | "0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509::tlp::TLP">;
75
+ static get p(): PhantomReified<"0x7c19f81d9d411e78305d7af8dad25317c56bb449fede8a78b6021232478f0509::tlp::TLP" | "::tlp::TLP">;
76
76
  static get bcs(): import("@mysten/sui/bcs").BcsType<{
77
77
  dummy_field: boolean;
78
78
  }, {
@@ -1,18 +1,5 @@
1
1
  import { Transaction, TransactionArgument, TransactionObjectInput } from "@mysten/sui/transactions";
2
2
  export declare function init(tx: Transaction): import("@mysten/sui/transactions").TransactionResult;
3
- export interface GetEstimatedLiquidationPriceArgs {
4
- version: TransactionObjectInput;
5
- registry: TransactionObjectInput;
6
- poolRegistry: TransactionObjectInput;
7
- marketIndex: bigint | TransactionArgument;
8
- poolIndex: bigint | TransactionArgument;
9
- pythState: TransactionObjectInput;
10
- oracleCToken: TransactionObjectInput;
11
- oracleTradingSymbol: TransactionObjectInput;
12
- clock: TransactionObjectInput;
13
- positionId: bigint | TransactionArgument;
14
- }
15
- export declare function getEstimatedLiquidationPrice(tx: Transaction, typeArgs: [string, string], args: GetEstimatedLiquidationPriceArgs): import("@mysten/sui/transactions").TransactionResult;
16
3
  export interface IncreaseCollateralArgs {
17
4
  version: TransactionObjectInput;
18
5
  registry: TransactionObjectInput;
@@ -232,6 +219,19 @@ export interface GetActiveOrdersByOrderTagAndCtokenArgs {
232
219
  orderTypeTag: number | TransactionArgument;
233
220
  }
234
221
  export declare function getActiveOrdersByOrderTagAndCtoken(tx: Transaction, typeArgs: [string, string], args: GetActiveOrdersByOrderTagAndCtokenArgs): import("@mysten/sui/transactions").TransactionResult;
222
+ export interface GetEstimatedLiquidationPriceAndPnlArgs {
223
+ version: TransactionObjectInput;
224
+ registry: TransactionObjectInput;
225
+ poolRegistry: TransactionObjectInput;
226
+ marketIndex: bigint | TransactionArgument;
227
+ poolIndex: bigint | TransactionArgument;
228
+ pythState: TransactionObjectInput;
229
+ oracleCToken: TransactionObjectInput;
230
+ oracleTradingSymbol: TransactionObjectInput;
231
+ clock: TransactionObjectInput;
232
+ positionId: bigint | TransactionArgument;
233
+ }
234
+ export declare function getEstimatedLiquidationPriceAndPnl(tx: Transaction, typeArgs: [string, string], args: GetEstimatedLiquidationPriceAndPnlArgs): import("@mysten/sui/transactions").TransactionResult;
235
235
  export interface GetExpiredPositionInfoArgs {
236
236
  version: TransactionObjectInput;
237
237
  registry: TransactionObjectInput;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.init = init;
4
- exports.getEstimatedLiquidationPrice = getEstimatedLiquidationPrice;
5
4
  exports.increaseCollateral = increaseCollateral;
6
5
  exports.releaseCollateral = releaseCollateral;
7
6
  exports.addTradingSymbol = addTradingSymbol;
@@ -20,6 +19,7 @@ exports.executeOrder_ = executeOrder_;
20
19
  exports.exerciseBidReceipts = exerciseBidReceipts;
21
20
  exports.getActiveOrdersByOrderTag = getActiveOrdersByOrderTag;
22
21
  exports.getActiveOrdersByOrderTagAndCtoken = getActiveOrdersByOrderTagAndCtoken;
22
+ exports.getEstimatedLiquidationPriceAndPnl = getEstimatedLiquidationPriceAndPnl;
23
23
  exports.getExpiredPositionInfo = getExpiredPositionInfo;
24
24
  exports.getLinkedPosition = getLinkedPosition;
25
25
  exports.getLiquidationInfo = getLiquidationInfo;
@@ -58,24 +58,6 @@ var util_1 = require("../../_framework/util");
58
58
  function init(tx) {
59
59
  return tx.moveCall({ target: "".concat(__1.PUBLISHED_AT, "::trading::init"), arguments: [] });
60
60
  }
61
- function getEstimatedLiquidationPrice(tx, typeArgs, args) {
62
- return tx.moveCall({
63
- target: "".concat(__1.PUBLISHED_AT, "::trading::get_estimated_liquidation_price"),
64
- typeArguments: typeArgs,
65
- arguments: [
66
- (0, util_1.obj)(tx, args.version),
67
- (0, util_1.obj)(tx, args.registry),
68
- (0, util_1.obj)(tx, args.poolRegistry),
69
- (0, util_1.pure)(tx, args.marketIndex, "u64"),
70
- (0, util_1.pure)(tx, args.poolIndex, "u64"),
71
- (0, util_1.obj)(tx, args.pythState),
72
- (0, util_1.obj)(tx, args.oracleCToken),
73
- (0, util_1.obj)(tx, args.oracleTradingSymbol),
74
- (0, util_1.obj)(tx, args.clock),
75
- (0, util_1.pure)(tx, args.positionId, "u64"),
76
- ],
77
- });
78
- }
79
61
  function increaseCollateral(tx, typeArgs, args) {
80
62
  return tx.moveCall({
81
63
  target: "".concat(__1.PUBLISHED_AT, "::trading::increase_collateral"),
@@ -369,6 +351,24 @@ function getActiveOrdersByOrderTagAndCtoken(tx, typeArgs, args) {
369
351
  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")],
370
352
  });
371
353
  }
354
+ function getEstimatedLiquidationPriceAndPnl(tx, typeArgs, args) {
355
+ return tx.moveCall({
356
+ target: "".concat(__1.PUBLISHED_AT, "::trading::get_estimated_liquidation_price_and_pnl"),
357
+ typeArguments: typeArgs,
358
+ arguments: [
359
+ (0, util_1.obj)(tx, args.version),
360
+ (0, util_1.obj)(tx, args.registry),
361
+ (0, util_1.obj)(tx, args.poolRegistry),
362
+ (0, util_1.pure)(tx, args.marketIndex, "u64"),
363
+ (0, util_1.pure)(tx, args.poolIndex, "u64"),
364
+ (0, util_1.obj)(tx, args.pythState),
365
+ (0, util_1.obj)(tx, args.oracleCToken),
366
+ (0, util_1.obj)(tx, args.oracleTradingSymbol),
367
+ (0, util_1.obj)(tx, args.clock),
368
+ (0, util_1.pure)(tx, args.positionId, "u64"),
369
+ ],
370
+ });
371
+ }
372
372
  function getExpiredPositionInfo(tx, args) {
373
373
  return tx.moveCall({
374
374
  target: "".concat(__1.PUBLISHED_AT, "::trading::get_expired_position_info"),
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -55,7 +66,7 @@ function getUserHistory(sender) {
55
66
  console.log(pageInfo);
56
67
  events = [];
57
68
  raw_events.nodes.forEach(function (event) {
58
- var _a;
69
+ var _a, _b;
59
70
  var type = event.contents.type.repr;
60
71
  if (type.endsWith("PythPrice")) {
61
72
  return;
@@ -64,6 +75,7 @@ function getUserHistory(sender) {
64
75
  var timestamp = event.timestamp;
65
76
  var tx_digest = event.transactionBlock.digest;
66
77
  // console.log(type);
78
+ // console.log(tx_digest);
67
79
  // console.log(json);
68
80
  // console.log(timestamp);
69
81
  switch (type) {
@@ -128,8 +140,10 @@ function getUserHistory(sender) {
128
140
  action = "Order Filled (Open Position)";
129
141
  related = events.findLast(function (e) { return e.order_id === json.order_id && e.market === market; });
130
142
  }
131
- // var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
143
+ var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
132
144
  var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
145
+ var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
146
+ var realized_pnl = ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee;
133
147
  var e = {
134
148
  action: action,
135
149
  order_id: json.order_id,
@@ -143,7 +157,7 @@ function getUserHistory(sender) {
143
157
  collateral: related === null || related === void 0 ? void 0 : related.collateral,
144
158
  collateral_token: collateral_token,
145
159
  price: Number(price) / Math.pow(10, 8), // WARNING: fixed decimal
146
- realized_pnl: 0 - realized_fee_in_usd,
160
+ realized_pnl: realized_pnl,
147
161
  timestamp: timestamp,
148
162
  tx_digest: tx_digest,
149
163
  };
@@ -151,11 +165,14 @@ function getUserHistory(sender) {
151
165
  break;
152
166
  case structs_1.RealizeFundingEvent.$typeName:
153
167
  // same tx with order filled
154
- related = events.findLast(function (e) { return e.tx_digest === json.tx_digest; });
155
- if (related) {
156
- var x = json.realized_funding_sign ? json.realized_funding_fee : -json.realized_funding_fee;
157
- related.realized_pnl = related.realized_pnl + x;
168
+ var index = events.findLastIndex(function (e) { return e.tx_digest == tx_digest; });
169
+ // console.log(index);
170
+ if (index !== -1) {
171
+ // true => user paid to pool
172
+ var x = json.realized_funding_sign ? json.realized_funding_fee_usd / Math.pow(10, 9) : -json.realized_funding_fee_usd / Math.pow(10, 9);
173
+ events[index] = __assign(__assign({}, events[index]), { realized_pnl: ((_b = events[index].realized_pnl) !== null && _b !== void 0 ? _b : 0) - x });
158
174
  }
175
+ break;
159
176
  case structs_2.CancelTradingOrderEvent.$typeName:
160
177
  var base_token = (0, constants_1.typeArgToToken)(json.base_token.name);
161
178
  var collateral_token = (0, constants_1.typeArgToToken)(json.collateral_token.name);
@@ -14,6 +14,7 @@ export declare function createTradingOrder(config: TypusConfig, tx: Transaction,
14
14
  reduceOnly: boolean;
15
15
  linkedPositionId: string | null;
16
16
  }): Promise<Transaction>;
17
+ export declare function zeroCoin(tx: Transaction, typeArgs: [string]): import("@mysten/sui/transactions").TransactionResult;
17
18
  export declare function cancelTradingOrder(config: TypusConfig, tx: Transaction, input: {
18
19
  order: TradingOrder;
19
20
  user: string;
@@ -53,6 +53,7 @@ var __read = (this && this.__read) || function (o, n) {
53
53
  };
54
54
  Object.defineProperty(exports, "__esModule", { value: true });
55
55
  exports.createTradingOrder = createTradingOrder;
56
+ exports.zeroCoin = zeroCoin;
56
57
  exports.cancelTradingOrder = cancelTradingOrder;
57
58
  exports.increaseCollateral = increaseCollateral;
58
59
  exports.releaseCollateral = releaseCollateral;
@@ -63,26 +64,32 @@ var constants_1 = require("@typus/typus-sdk/dist/src/constants");
63
64
  function createTradingOrder(config, tx, pythClient, input) {
64
65
  return __awaiter(this, void 0, void 0, function () {
65
66
  var TOKEN, BASE_TOKEN, cToken, baseToken, coin, destination;
66
- var _a, _b;
67
- return __generator(this, function (_c) {
68
- switch (_c.label) {
67
+ var _a, _b, _c;
68
+ return __generator(this, function (_d) {
69
+ switch (_d.label) {
69
70
  case 0:
70
71
  TOKEN = input.cToken;
71
72
  BASE_TOKEN = input.tradingToken;
72
73
  return [4 /*yield*/, (0, utils_1.updatePyth)(pythClient, tx, [TOKEN, BASE_TOKEN])];
73
74
  case 1:
74
- _c.sent();
75
+ _d.sent();
75
76
  cToken = constants_1.tokenType[__1.NETWORK][TOKEN];
76
77
  baseToken = constants_1.tokenType[__1.NETWORK][BASE_TOKEN];
77
78
  if (TOKEN == "SUI") {
78
79
  _a = __read(tx.splitCoins(tx.gas, [input.amount]), 1), coin = _a[0];
79
80
  }
80
81
  else {
81
- destination = input.coins.pop();
82
- if (input.coins.length > 0) {
83
- tx.mergeCoins(destination, input.coins);
82
+ if (input.coins.length == 0) {
83
+ // support zero coin input for closing position
84
+ _b = __read(zeroCoin(tx, [cToken]), 1), coin = _b[0];
85
+ }
86
+ else {
87
+ destination = input.coins.pop();
88
+ if (input.coins.length > 0) {
89
+ tx.mergeCoins(destination, input.coins);
90
+ }
91
+ _c = __read(tx.splitCoins(destination, [input.amount]), 1), coin = _c[0];
84
92
  }
85
- _b = __read(tx.splitCoins(destination, [input.amount]), 1), coin = _b[0];
86
93
  }
87
94
  (0, functions_1.createTradingOrder)(tx, [cToken, baseToken], {
88
95
  version: __1.PERP_VERSION,
@@ -110,6 +117,13 @@ function createTradingOrder(config, tx, pythClient, input) {
110
117
  });
111
118
  });
112
119
  }
120
+ function zeroCoin(tx, typeArgs) {
121
+ return tx.moveCall({
122
+ target: "0x2::coin::zero",
123
+ typeArguments: typeArgs,
124
+ arguments: [],
125
+ });
126
+ }
113
127
  function cancelTradingOrder(config, tx, input) {
114
128
  return __awaiter(this, void 0, void 0, function () {
115
129
  var cToken, BASE_TOKEN, coin;
@@ -117,17 +117,17 @@ function mintStakeLp(config, tx, pythClient, input) {
117
117
  }
118
118
  _c = __read(tx.splitCoins(destination, [input.amount]), 1), coin = _c[0];
119
119
  }
120
- lpCoin = (0, functions_1.mintLp)(tx, [cToken, config.token.tlp], {
120
+ lpCoin = (0, functions_1.mintLp)(tx, [cToken, __1.TLP_TOKEN], {
121
121
  version: __1.PERP_VERSION,
122
122
  registry: __1.LP_POOL,
123
- treasuryCaps: config.object.tlpTreasuryCap,
123
+ treasuryCaps: __1.TLP_TREASURY_CAP,
124
124
  index: BigInt(0),
125
125
  pythState: utils_1.pythStateId[__1.NETWORK],
126
126
  oracle: utils_1.priceInfoObjectIds[__1.NETWORK][input.cTOKEN],
127
127
  coin: coin,
128
128
  clock: constants_1.CLOCK,
129
129
  });
130
- (0, functions_2.stake)(tx, config.token.tlp, {
130
+ (0, functions_2.stake)(tx, __1.TLP_TOKEN, {
131
131
  version: __1.STAKE_POOL_VERSION,
132
132
  registry: __1.STAKE_POOL,
133
133
  index: BigInt(0),
@@ -173,7 +173,7 @@ function unstakeBurn(config, tx, pythClient, input) {
173
173
  }
174
174
  finally { if (e_2) throw e_2.error; }
175
175
  }
176
- lpCoin = (0, functions_2.unstake)(tx, config.token.tlp, {
176
+ lpCoin = (0, functions_2.unstake)(tx, __1.TLP_TOKEN, {
177
177
  version: __1.STAKE_POOL_VERSION,
178
178
  registry: __1.STAKE_POOL,
179
179
  index: BigInt(0),
@@ -181,10 +181,10 @@ function unstakeBurn(config, tx, pythClient, input) {
181
181
  clock: constants_1.CLOCK,
182
182
  unstakedShares: input.share ? BigInt(input.share) : null,
183
183
  });
184
- coin = (0, functions_1.burnLp)(tx, [cToken, config.token.tlp], {
184
+ coin = (0, functions_1.burnLp)(tx, [cToken, __1.TLP_TOKEN], {
185
185
  version: __1.PERP_VERSION,
186
186
  registry: __1.LP_POOL,
187
- treasuryCaps: config.object.tlpTreasuryCap,
187
+ treasuryCaps: __1.TLP_TREASURY_CAP,
188
188
  index: BigInt(0),
189
189
  pythState: utils_1.pythStateId[__1.NETWORK],
190
190
  oracle: oracle,
@@ -238,7 +238,7 @@ function swap(config, tx, pythClient, input) {
238
238
  function unsubscribe(config, tx, input) {
239
239
  return __awaiter(this, void 0, void 0, function () {
240
240
  return __generator(this, function (_a) {
241
- (0, functions_2.unsubscribe)(tx, config.token.tlp, {
241
+ (0, functions_2.unsubscribe)(tx, __1.TLP_TOKEN, {
242
242
  version: __1.STAKE_POOL_VERSION,
243
243
  registry: __1.STAKE_POOL,
244
244
  index: BigInt(0),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typus/typus-perp-sdk",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "repository": "https://github.com/Typus-Lab/typus-perp-sdk.git",
5
5
  "author": "Typus",
6
6
  "description": "typus perp sdk",