@typus/typus-perp-sdk 1.1.10 → 1.1.12
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.
- package/dist/src/user/history.d.ts +1 -1
- package/dist/src/user/history.js +25 -24
- package/package.json +1 -1
|
@@ -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 Cancel Order" | "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 Cancel Order" | "Force Close Position" | "Swap" | "Realize 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";
|
package/dist/src/user/history.js
CHANGED
|
@@ -84,7 +84,7 @@ function parseUserHistory(raw_events) {
|
|
|
84
84
|
return __generator(this, function (_a) {
|
|
85
85
|
events = [];
|
|
86
86
|
raw_events.forEach(function (event) {
|
|
87
|
-
var _a;
|
|
87
|
+
var _a, _b;
|
|
88
88
|
var type = event.contents.type.repr;
|
|
89
89
|
if (type.endsWith("PythPrice")) {
|
|
90
90
|
return;
|
|
@@ -96,7 +96,7 @@ function parseUserHistory(raw_events) {
|
|
|
96
96
|
// console.log(tx_digest);
|
|
97
97
|
// console.log(json);
|
|
98
98
|
// console.log(timestamp);
|
|
99
|
-
var
|
|
99
|
+
var _c = __read(type.split("::"), 3), pkg = _c[0], mod = _c[1], name = _c[2];
|
|
100
100
|
switch (name) {
|
|
101
101
|
case structs_2.CreateTradingOrderEvent.$typeName.split("::")[2]:
|
|
102
102
|
case structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]:
|
|
@@ -153,20 +153,25 @@ function parseUserHistory(raw_events) {
|
|
|
153
153
|
var price = json.filled_price;
|
|
154
154
|
var action;
|
|
155
155
|
var related;
|
|
156
|
+
var collateral;
|
|
157
|
+
var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
|
|
158
|
+
var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
|
|
159
|
+
var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
|
|
160
|
+
// console.log(realized_amount);
|
|
161
|
+
var realized_pnl = realized_trading_fee > 0 ? ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee : 0;
|
|
156
162
|
if (json.linked_position_id != undefined) {
|
|
157
163
|
action = "Order Filled (Close Position)";
|
|
158
164
|
related = events.findLast(function (e) { return e.position_id === json.linked_position_id && e.market === market; });
|
|
159
165
|
// the "Place Order" is emit after Order Filled if filled immediately
|
|
166
|
+
if (realized_pnl > 0) {
|
|
167
|
+
collateral = (realized_amount - realized_trading_fee) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
168
|
+
}
|
|
160
169
|
}
|
|
161
170
|
else {
|
|
162
171
|
action = "Order Filled (Open Position)";
|
|
163
172
|
related = events.findLast(function (e) { return e.order_id === json.order_id && e.market === market; });
|
|
173
|
+
collateral = related === null || related === void 0 ? void 0 : related.collateral;
|
|
164
174
|
}
|
|
165
|
-
var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
|
|
166
|
-
var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
|
|
167
|
-
var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
|
|
168
|
-
// console.log(realized_amount);
|
|
169
|
-
var realized_pnl = realized_trading_fee > 0 ? ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee : 0;
|
|
170
175
|
var e = {
|
|
171
176
|
action: action,
|
|
172
177
|
typeName: name,
|
|
@@ -178,7 +183,7 @@ function parseUserHistory(raw_events) {
|
|
|
178
183
|
status: "Filled",
|
|
179
184
|
size: size,
|
|
180
185
|
base_token: base_token,
|
|
181
|
-
collateral:
|
|
186
|
+
collateral: collateral, // TODO: check for option collateral
|
|
182
187
|
collateral_token: collateral_token,
|
|
183
188
|
price: Number(price) / Math.pow(10, 8), // WARNING: fixed decimal
|
|
184
189
|
realized_pnl: realized_pnl,
|
|
@@ -196,21 +201,10 @@ function parseUserHistory(raw_events) {
|
|
|
196
201
|
if (index !== -1) {
|
|
197
202
|
// true => user paid to pool
|
|
198
203
|
var remaining_collateral_amount = json.remaining_collateral_amount / Math.pow(10, (0, constants_1.assetToDecimal)(events[index].collateral_token));
|
|
199
|
-
events[index] = __assign(__assign({}, events[index]), { collateral: remaining_collateral_amount +
|
|
204
|
+
events[index] = __assign(__assign({}, events[index]), { collateral: remaining_collateral_amount + events[index].collateral });
|
|
200
205
|
}
|
|
201
206
|
break;
|
|
202
207
|
case structs_1.RealizeFundingEvent.$typeName.split("::")[2]:
|
|
203
|
-
// // same tx with order filled
|
|
204
|
-
// var index = events.findLastIndex((e) => e.tx_digest == tx_digest);
|
|
205
|
-
// // console.log(index);
|
|
206
|
-
// if (index !== -1) {
|
|
207
|
-
// // true => user paid to pool
|
|
208
|
-
// let x = json.realized_funding_sign ? json.realized_funding_fee_usd / 10 ** 9 : -json.realized_funding_fee_usd / 10 ** 9;
|
|
209
|
-
// events[index] = {
|
|
210
|
-
// ...events[index],
|
|
211
|
-
// realized_pnl: (events[index].realized_pnl ?? 0) - x,
|
|
212
|
-
// };
|
|
213
|
-
// }
|
|
214
208
|
var base_token = (0, constants_1.typeArgToAsset)(json.symbol.base_token.name);
|
|
215
209
|
var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
216
210
|
var market = "".concat(base_token, "/USD");
|
|
@@ -222,8 +216,15 @@ function parseUserHistory(raw_events) {
|
|
|
222
216
|
var realized_funding_fee_usd = json.realized_funding_sign
|
|
223
217
|
? -json.realized_funding_fee_usd / Math.pow(10, 9)
|
|
224
218
|
: json.realized_funding_fee_usd / Math.pow(10, 9);
|
|
219
|
+
// same tx with order filled
|
|
220
|
+
var index = events.findLastIndex(function (e) { return e.tx_digest == tx_digest && e.action == "Order Filled (Close Position)"; });
|
|
221
|
+
// console.log(index);
|
|
222
|
+
if (index !== -1 && realized_funding_fee < 0) {
|
|
223
|
+
// true => user paid to pool
|
|
224
|
+
events[index] = __assign(__assign({}, events[index]), { collateral: (_b = events[index].collateral) !== null && _b !== void 0 ? _b : 0 - realized_funding_fee });
|
|
225
|
+
}
|
|
225
226
|
var e = {
|
|
226
|
-
action: "
|
|
227
|
+
action: "Realize Funding",
|
|
227
228
|
typeName: name,
|
|
228
229
|
order_id: undefined,
|
|
229
230
|
position_id: json.position_id,
|
|
@@ -444,7 +445,7 @@ function getRealizeFundingFromSentio(userAddress, startTimestamp, events) {
|
|
|
444
445
|
realizeFunding = datas.map(function (x) {
|
|
445
446
|
var base_token = toToken(x.base_token);
|
|
446
447
|
var txHistory = {
|
|
447
|
-
action: "
|
|
448
|
+
action: "Realize Funding",
|
|
448
449
|
typeName: "RealizeFundingEvent",
|
|
449
450
|
order_id: undefined,
|
|
450
451
|
position_id: x.position_id,
|
|
@@ -466,7 +467,7 @@ function getRealizeFundingFromSentio(userAddress, startTimestamp, events) {
|
|
|
466
467
|
return txHistory;
|
|
467
468
|
});
|
|
468
469
|
// deduplicate
|
|
469
|
-
realizeFunding = realizeFunding.filter(function (x) { return events.findIndex(function (y) { return y.tx_digest == x.tx_digest && y.action == "
|
|
470
|
+
realizeFunding = realizeFunding.filter(function (x) { return events.findIndex(function (y) { return y.tx_digest == x.tx_digest && y.action == "Realize Funding"; }) == -1; });
|
|
470
471
|
realizeFunding = realizeFunding.map(function (x) {
|
|
471
472
|
// find related position
|
|
472
473
|
var related = events.find(function (e) { return e.position_id === x.position_id && e.market === x.market; });
|
|
@@ -504,7 +505,7 @@ function getRemovePositionFromSentio(userAddress, startTimestamp, events) {
|
|
|
504
505
|
if (index !== -1) {
|
|
505
506
|
// true => user paid to pool
|
|
506
507
|
var remaining_collateral_amount = x.remaining_collateral_amount;
|
|
507
|
-
events[index] = __assign(__assign({}, events[index]), { collateral: remaining_collateral_amount + Math.max(0, events[index].
|
|
508
|
+
events[index] = __assign(__assign({}, events[index]), { collateral: remaining_collateral_amount + Math.max(0, events[index].collateral) });
|
|
508
509
|
}
|
|
509
510
|
});
|
|
510
511
|
return [2 /*return*/, events];
|