@typus/typus-perp-sdk 1.1.3 → 1.1.4

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.
@@ -100,7 +100,7 @@ function getRecentTradesFromSentio(base_token) {
100
100
  }
101
101
  requestData = {
102
102
  sqlQuery: {
103
- sql: "\n SELECT * FROM (\n SELECT\n PlaceOrder.timestamp as timestamp,\n PlaceOrder.base_token as base_token,\n PlaceOrder.collateral_token as collateral_token,\n PlaceOrder.order_type as order_type,\n OrderFilled.order_type as status,\n PlaceOrder.side as side,\n PlaceOrder.distinct_id as distinct_id,\n price,\n size,\n realized_amount-realized_fee-RealizeFunding.realized_funding_fee as realized_pnl,\n OrderFilled.realized_pnl-RealizeFunding.realized_funding_fee_usd as realized_pnl_usd,\n GREATEST(PlaceOrder.collateral, RemovePosition.remaining_collateral_amount) as collateral_amount,\n PlaceOrder.transaction_hash as transaction_hash\n FROM PlaceOrder\n LEFT JOIN OrderFilled ON OrderFilled.order_id == PlaceOrder.order_id AND OrderFilled.base_token == PlaceOrder.base_token\n LEFT JOIN RealizeFunding ON RealizeFunding.position_id == OrderFilled.position_id AND RealizeFunding.base_token == OrderFilled.base_token AND RealizeFunding.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RealizeOption ON RealizeOption.position_id == OrderFilled.position_id AND RealizeOption.base_token == OrderFilled.base_token AND RealizeOption.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RemovePosition ON RemovePosition.transaction_hash == OrderFilled.transaction_hash\n ".concat(tokenFilter, "\n UNION ALL\n SELECT\n PlaceOrderWithBidReceipt.timestamp as timestamp,\n PlaceOrderWithBidReceipt.base_token as base_token,\n PlaceOrderWithBidReceipt.collateral_token as collateral_token,\n PlaceOrderWithBidReceipt.order_type as order_type,\n OrderFilled.order_type as status,\n PlaceOrderWithBidReceipt.side as side,\n PlaceOrderWithBidReceipt.distinct_id as distinct_id,\n price,\n size,\n realized_amount-realized_fee-RealizeFunding.realized_funding_fee as realized_pnl,\n OrderFilled.realized_pnl-RealizeFunding.realized_funding_fee_usd as realized_pnl_usd,\n RealizeOption.exercise_balance_value as collateral_amount,\n PlaceOrderWithBidReceipt.transaction_hash as transaction_hash\n FROM PlaceOrderWithBidReceipt\n JOIN OrderFilled ON OrderFilled.order_id == PlaceOrderWithBidReceipt.order_id AND OrderFilled.base_token == PlaceOrderWithBidReceipt.base_token\n LEFT JOIN RealizeFunding ON RealizeFunding.position_id == OrderFilled.position_id AND RealizeFunding.base_token == OrderFilled.base_token AND RealizeFunding.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RealizeOption ON RealizeOption.position_id == OrderFilled.position_id AND RealizeOption.base_token == OrderFilled.base_token AND RealizeOption.transaction_hash == OrderFilled.transaction_hash\n ").concat(tokenFilter, "\n ) AS combined\n ORDER BY timestamp DESC\n "),
103
+ sql: "\n SELECT * FROM (\n SELECT\n PlaceOrder.timestamp as timestamp,\n PlaceOrder.base_token as base_token,\n PlaceOrder.collateral_token as collateral_token,\n PlaceOrder.order_type as order_type,\n OrderFilled.order_type as status,\n PlaceOrder.side as side,\n PlaceOrder.distinct_id as distinct_id,\n price,\n size,\n realized_amount-realized_fee-RealizeFunding.realized_funding_fee as realized_pnl,\n OrderFilled.realized_pnl-RealizeFunding.realized_funding_fee_usd as realized_pnl_usd,\n CASE\n WHEN PlaceOrder.collateral > 0 THEN PlaceOrder.collateral\n WHEN RemovePosition.remaining_collateral_amount > 0 AND realized_pnl_usd >= 0 THEN RemovePosition.remaining_collateral_amount\n WHEN RemovePosition.remaining_collateral_amount > 0 AND realized_pnl_usd < 0 THEN RemovePosition.remaining_collateral_amount - realized_pnl_usd\n ELSE 0\n END AS collateral_amount,\n PlaceOrder.transaction_hash as transaction_hash\n FROM PlaceOrder\n LEFT JOIN OrderFilled ON OrderFilled.order_id == PlaceOrder.order_id AND OrderFilled.base_token == PlaceOrder.base_token\n LEFT JOIN RealizeFunding ON RealizeFunding.position_id == OrderFilled.position_id AND RealizeFunding.base_token == OrderFilled.base_token AND RealizeFunding.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RealizeOption ON RealizeOption.position_id == OrderFilled.position_id AND RealizeOption.base_token == OrderFilled.base_token AND RealizeOption.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RemovePosition ON RemovePosition.transaction_hash == OrderFilled.transaction_hash\n ".concat(tokenFilter, "\n UNION ALL\n SELECT\n PlaceOrderWithBidReceipt.timestamp as timestamp,\n PlaceOrderWithBidReceipt.base_token as base_token,\n PlaceOrderWithBidReceipt.collateral_token as collateral_token,\n PlaceOrderWithBidReceipt.order_type as order_type,\n OrderFilled.order_type as status,\n PlaceOrderWithBidReceipt.side as side,\n PlaceOrderWithBidReceipt.distinct_id as distinct_id,\n price,\n size,\n realized_amount-realized_fee-RealizeFunding.realized_funding_fee as realized_pnl,\n OrderFilled.realized_pnl-RealizeFunding.realized_funding_fee_usd as realized_pnl_usd,\n RealizeOption.exercise_balance_value as collateral_amount,\n PlaceOrderWithBidReceipt.transaction_hash as transaction_hash\n FROM PlaceOrderWithBidReceipt\n JOIN OrderFilled ON OrderFilled.order_id == PlaceOrderWithBidReceipt.order_id AND OrderFilled.base_token == PlaceOrderWithBidReceipt.base_token\n LEFT JOIN RealizeFunding ON RealizeFunding.position_id == OrderFilled.position_id AND RealizeFunding.base_token == OrderFilled.base_token AND RealizeFunding.transaction_hash == OrderFilled.transaction_hash\n LEFT JOIN RealizeOption ON RealizeOption.position_id == OrderFilled.position_id AND RealizeOption.base_token == OrderFilled.base_token AND RealizeOption.transaction_hash == OrderFilled.transaction_hash\n ").concat(tokenFilter, "\n ) AS combined\n ORDER BY timestamp DESC\n "),
104
104
  size: 100,
105
105
  },
106
106
  };
@@ -1,5 +1,5 @@
1
1
  import { TOKEN } from "@typus/typus-sdk/dist/src/constants";
2
- export type actionType = "Place Order" | "Cancel Order" | "Order Filled (Open Position)" | "Order Filled (Close Position)" | "Realized PnL" | "Modify Collateral" | "Exercise Position" | "Liquidation" | "Force Close Position" | "Swap";
2
+ export type actionType = "Place Order" | "Cancel Order" | "Order Filled (Open Position)" | "Order Filled (Close Position)" | "Realized PnL" | "Modify Collateral" | "Exercise Position" | "Liquidation" | "Force Close Position" | "Swap" | "Realized Funding";
3
3
  export type sideType = "Long" | "Short";
4
4
  export type orderType = "Market" | "Limit" | "Take Profit" | "Stop Loss";
5
5
  export type statusType = "Open" | "Filled" | "Canceled";
@@ -81,7 +81,7 @@ function parseUserHistory(raw_events) {
81
81
  return __generator(this, function (_a) {
82
82
  events = [];
83
83
  raw_events.forEach(function (event) {
84
- var _a, _b;
84
+ var _a;
85
85
  var type = event.contents.type.repr;
86
86
  if (type.endsWith("PythPrice")) {
87
87
  return;
@@ -93,7 +93,7 @@ function parseUserHistory(raw_events) {
93
93
  // console.log(tx_digest);
94
94
  // console.log(json);
95
95
  // console.log(timestamp);
96
- var _c = __read(type.split("::"), 3), pkg = _c[0], mod = _c[1], name = _c[2];
96
+ var _b = __read(type.split("::"), 3), pkg = _b[0], mod = _b[1], name = _b[2];
97
97
  switch (name) {
98
98
  case structs_2.CreateTradingOrderEvent.$typeName.split("::")[2]:
99
99
  case structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]:
@@ -186,16 +186,60 @@ function parseUserHistory(raw_events) {
186
186
  };
187
187
  events.push(e);
188
188
  break;
189
- case structs_1.RealizeFundingEvent.$typeName.split("::")[2]:
189
+ case structs_1.RemovePositionEvent.$typeName.split("::")[2]:
190
190
  // same tx with order filled
191
- var index = events.findLastIndex(function (e) { return e.tx_digest == tx_digest; });
191
+ var index = events.findLastIndex(function (e) { return e.tx_digest == tx_digest && e.action == "Order Filled (Close Position)"; });
192
192
  // console.log(index);
193
193
  if (index !== -1) {
194
194
  // true => user paid to pool
195
- var x = json.realized_funding_sign ? json.realized_funding_fee_usd / Math.pow(10, 9) : -json.realized_funding_fee_usd / Math.pow(10, 9);
196
- events[index] = __assign(__assign({}, events[index]), { realized_pnl: ((_b = events[index].realized_pnl) !== null && _b !== void 0 ? _b : 0) - x });
195
+ var remaining_collateral_amount = json.remaining_collateral_amount / Math.pow(10, (0, constants_1.assetToDecimal)(events[index].collateral_token));
196
+ events[index] = __assign(__assign({}, events[index]), { collateral: remaining_collateral_amount + Math.max(0, events[index].realized_pnl) });
197
197
  }
198
198
  break;
199
+ case structs_1.RealizeFundingEvent.$typeName.split("::")[2]:
200
+ // // same tx with order filled
201
+ // var index = events.findLastIndex((e) => e.tx_digest == tx_digest);
202
+ // // console.log(index);
203
+ // if (index !== -1) {
204
+ // // true => user paid to pool
205
+ // let x = json.realized_funding_sign ? json.realized_funding_fee_usd / 10 ** 9 : -json.realized_funding_fee_usd / 10 ** 9;
206
+ // events[index] = {
207
+ // ...events[index],
208
+ // realized_pnl: (events[index].realized_pnl ?? 0) - x,
209
+ // };
210
+ // }
211
+ var base_token = (0, constants_1.typeArgToAsset)(json.symbol.base_token.name);
212
+ var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
213
+ var market = "".concat(base_token, "/USD");
214
+ var related = events.find(function (e) { return e.position_id === json.position_id && e.market === market; });
215
+ var realized_funding_fee = json.realized_funding_sign
216
+ ? json.realized_funding_fee / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token))
217
+ : -json.realized_funding_fee / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
218
+ var realized_funding_fee_usd = json.realized_funding_sign
219
+ ? json.realized_funding_fee_usd / Math.pow(10, 9)
220
+ : -json.realized_funding_fee_usd / Math.pow(10, 9);
221
+ var e = {
222
+ action: "Realized Funding",
223
+ typeName: name,
224
+ order_id: undefined,
225
+ position_id: json.position_id,
226
+ market: market,
227
+ side: related === null || related === void 0 ? void 0 : related.side,
228
+ order_type: related === null || related === void 0 ? void 0 : related.order_type,
229
+ status: "Filled",
230
+ size: related === null || related === void 0 ? void 0 : related.size,
231
+ base_token: base_token,
232
+ collateral: realized_funding_fee,
233
+ collateral_token: collateral_token,
234
+ price: undefined,
235
+ realized_pnl: realized_funding_fee_usd,
236
+ timestamp: timestamp,
237
+ tx_digest: tx_digest,
238
+ dov_index: related === null || related === void 0 ? void 0 : related.dov_index,
239
+ sender: "user",
240
+ };
241
+ events.push(e);
242
+ break;
199
243
  case structs_2.CancelTradingOrderEvent.$typeName.split("::")[2]:
200
244
  var base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
201
245
  var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
@@ -1,6 +1,17 @@
1
1
  import { Transaction } from "@mysten/sui/transactions";
2
2
  import { PythClient, TypusConfig } from "@typus/typus-sdk/dist/src/utils";
3
3
  import { TOKEN } from "@typus/typus-sdk/dist/src/constants";
4
+ export declare function createTradingOrderWithBidReceiptByAutoBid(config: TypusConfig, tx: Transaction, pythClient: PythClient, input: {
5
+ cToken: TOKEN;
6
+ tradingToken: TOKEN;
7
+ isLong: boolean;
8
+ user: string;
9
+ index: string;
10
+ bToken: TOKEN;
11
+ signalIndex: string;
12
+ strategyIndex: string;
13
+ suiCoins?: string[];
14
+ }): Promise<Transaction>;
4
15
  export declare function createTradingOrderWithBidReceipt(config: TypusConfig, tx: Transaction, pythClient: PythClient, input: {
5
16
  cToken: TOKEN;
6
17
  tradingToken: TOKEN;
@@ -47,14 +47,16 @@ var __values = (this && this.__values) || function(o) {
47
47
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
48
  };
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.createTradingOrderWithBidReceiptByAutoBid = createTradingOrderWithBidReceiptByAutoBid;
50
51
  exports.createTradingOrderWithBidReceipt = createTradingOrderWithBidReceipt;
51
52
  exports.reduceOptionCollateralPositionSize = reduceOptionCollateralPositionSize;
52
53
  var functions_1 = require("../typus_perp/trading/functions");
53
54
  var utils_1 = require("@typus/typus-sdk/dist/src/utils");
54
55
  var constants_1 = require("@typus/typus-sdk/dist/src/constants");
55
56
  var typus_dov_single_v2_1 = require("@typus/typus-sdk/dist/src/typus-dov-single-v2");
57
+ var user_entry_1 = require("@typus/typus-sdk/dist/src/auto-bid/user-entry");
56
58
  var __1 = require("..");
57
- function createTradingOrderWithBidReceipt(config, tx, pythClient, input) {
59
+ function createTradingOrderWithBidReceiptByAutoBid(config, tx, pythClient, input) {
58
60
  return __awaiter(this, void 0, void 0, function () {
59
61
  var TOKEN, BASE_TOKEN, tokens, suiCoin, tokens_1, tokens_1_1, token, collateralBidReceipt, cToken, bToken, baseToken;
60
62
  var e_1, _a;
@@ -83,11 +85,11 @@ function createTradingOrderWithBidReceipt(config, tx, pythClient, input) {
83
85
  }
84
86
  finally { if (e_1) throw e_1.error; }
85
87
  }
86
- collateralBidReceipt = (0, typus_dov_single_v2_1.getSplitBidReceiptTx)(config, tx, {
87
- index: input.index,
88
- receipts: input.bidReceipts,
89
- share: input.share, // if undefined, merge all receipts
90
- recipient: input.user,
88
+ collateralBidReceipt = (0, user_entry_1.getWithdrawBidReceiptTx)(config, tx, {
89
+ vaultIndex: input.index,
90
+ signalIndex: input.signalIndex,
91
+ strategyIndex: input.strategyIndex,
92
+ user: input.user,
91
93
  });
92
94
  cToken = constants_1.tokenType[__1.NETWORK][TOKEN];
93
95
  bToken = constants_1.tokenType[__1.NETWORK][input.bToken];
@@ -116,9 +118,9 @@ function createTradingOrderWithBidReceipt(config, tx, pythClient, input) {
116
118
  });
117
119
  });
118
120
  }
119
- function reduceOptionCollateralPositionSize(config, tx, pythClient, input) {
121
+ function createTradingOrderWithBidReceipt(config, tx, pythClient, input) {
120
122
  return __awaiter(this, void 0, void 0, function () {
121
- var TOKEN, BASE_TOKEN, tokens, suiCoin, tokens_2, tokens_2_1, token, cToken, bToken, baseToken;
123
+ var TOKEN, BASE_TOKEN, tokens, suiCoin, tokens_2, tokens_2_1, token, collateralBidReceipt, cToken, bToken, baseToken;
122
124
  var e_2, _a;
123
125
  return __generator(this, function (_b) {
124
126
  switch (_b.label) {
@@ -145,6 +147,68 @@ function reduceOptionCollateralPositionSize(config, tx, pythClient, input) {
145
147
  }
146
148
  finally { if (e_2) throw e_2.error; }
147
149
  }
150
+ collateralBidReceipt = (0, typus_dov_single_v2_1.getSplitBidReceiptTx)(config, tx, {
151
+ index: input.index,
152
+ receipts: input.bidReceipts,
153
+ share: input.share, // if undefined, merge all receipts
154
+ recipient: input.user,
155
+ });
156
+ cToken = constants_1.tokenType[__1.NETWORK][TOKEN];
157
+ bToken = constants_1.tokenType[__1.NETWORK][input.bToken];
158
+ baseToken = constants_1.tokenType[__1.NETWORK][BASE_TOKEN];
159
+ (0, functions_1.createTradingOrderWithBidReceiptV2)(tx, [cToken, bToken, baseToken], {
160
+ version: __1.PERP_VERSION,
161
+ registry: __1.MARKET,
162
+ poolRegistry: __1.LP_POOL,
163
+ marketIndex: BigInt(0),
164
+ poolIndex: BigInt(0),
165
+ typusOracleCToken: constants_1.oracle[__1.NETWORK][TOKEN],
166
+ typusOracleTradingSymbol: constants_1.oracle[__1.NETWORK][BASE_TOKEN],
167
+ clock: constants_1.CLOCK,
168
+ typusEcosystemVersion: config.version.typus,
169
+ typusUserRegistry: config.registry.typus.user,
170
+ typusLeaderboardRegistry: config.registry.typus.leaderboard,
171
+ isLong: input.isLong,
172
+ dovRegistry: config.registry.dov.dovSingle,
173
+ collateralBidReceipt: collateralBidReceipt,
174
+ user: input.user,
175
+ tailsStakingRegistry: config.registry.typus.tailsStaking,
176
+ competitionConfig: __1.COMPETITION_CONFIG,
177
+ });
178
+ return [2 /*return*/, tx];
179
+ }
180
+ });
181
+ });
182
+ }
183
+ function reduceOptionCollateralPositionSize(config, tx, pythClient, input) {
184
+ return __awaiter(this, void 0, void 0, function () {
185
+ var TOKEN, BASE_TOKEN, tokens, suiCoin, tokens_3, tokens_3_1, token, cToken, bToken, baseToken;
186
+ var e_3, _a;
187
+ return __generator(this, function (_b) {
188
+ switch (_b.label) {
189
+ case 0:
190
+ TOKEN = input.cToken;
191
+ BASE_TOKEN = input.tradingToken;
192
+ tokens = Array.from(new Set([TOKEN, BASE_TOKEN]));
193
+ if (config.sponsored) {
194
+ suiCoin = (0, utils_1.splitCoin)(tx, constants_1.tokenType.MAINNET.SUI, input.suiCoins, tokens.length.toString(), config.sponsored);
195
+ }
196
+ return [4 /*yield*/, (0, utils_1.updatePyth)(pythClient, tx, tokens, suiCoin)];
197
+ case 1:
198
+ _b.sent();
199
+ try {
200
+ for (tokens_3 = __values(tokens), tokens_3_1 = tokens_3.next(); !tokens_3_1.done; tokens_3_1 = tokens_3.next()) {
201
+ token = tokens_3_1.value;
202
+ (0, utils_1.updateOracleWithPythUsd)(pythClient, tx, config.package.oracle, token);
203
+ }
204
+ }
205
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
206
+ finally {
207
+ try {
208
+ if (tokens_3_1 && !tokens_3_1.done && (_a = tokens_3.return)) _a.call(tokens_3);
209
+ }
210
+ finally { if (e_3) throw e_3.error; }
211
+ }
148
212
  cToken = constants_1.tokenType[__1.NETWORK][TOKEN];
149
213
  bToken = constants_1.tokenType[__1.NETWORK][input.bToken];
150
214
  baseToken = constants_1.tokenType[__1.NETWORK][BASE_TOKEN];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@typus/typus-perp-sdk",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
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.8.1"
9
+ "@typus/typus-sdk": "1.8.5"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@types/bs58": "^4.0.1",