@typus/typus-perp-sdk 1.0.40 → 1.0.42
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 +4 -1
- package/dist/src/user/history.js +141 -55
- package/package.json +1 -1
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { TOKEN } from "@typus/typus-sdk/dist/src/constants";
|
|
2
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";
|
|
3
|
+
export type tokenType = {
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
3
6
|
export type sideType = "Long" | "Short";
|
|
4
7
|
export type orderType = "Market" | "Limit" | "Take Profit" | "Stop Loss";
|
|
5
8
|
export type statusType = "Open" | "Filled" | "Canceled";
|
|
@@ -11,7 +14,7 @@ export interface Event {
|
|
|
11
14
|
market: string;
|
|
12
15
|
side: sideType | undefined;
|
|
13
16
|
order_type: orderType | undefined;
|
|
14
|
-
status: statusType;
|
|
17
|
+
status: statusType | undefined;
|
|
15
18
|
size: number | undefined;
|
|
16
19
|
base_token: TOKEN;
|
|
17
20
|
collateral: number | undefined;
|
package/dist/src/user/history.js
CHANGED
|
@@ -73,6 +73,28 @@ var structs_2 = require("../typus_perp/trading/structs");
|
|
|
73
73
|
var structs_3 = require("../../src/typus_perp/lp-pool/structs");
|
|
74
74
|
var sentio_1 = require("../../src/api/sentio");
|
|
75
75
|
var src_1 = require("../../src");
|
|
76
|
+
var checkOrderType = function (filled, reduceOnly, isStopOrder) {
|
|
77
|
+
if (filled) {
|
|
78
|
+
return "Market";
|
|
79
|
+
}
|
|
80
|
+
if (reduceOnly && !isStopOrder) {
|
|
81
|
+
return "Take Profit";
|
|
82
|
+
}
|
|
83
|
+
if (reduceOnly && isStopOrder) {
|
|
84
|
+
return "Stop Loss";
|
|
85
|
+
}
|
|
86
|
+
return "Limit";
|
|
87
|
+
};
|
|
88
|
+
var parseCollateralAmount = function (collateral_amount, collateral_token, collateral_in_deposit_token, deposit_token) {
|
|
89
|
+
if (collateral_amount) {
|
|
90
|
+
var collateral_token_name = (0, constants_1.typeArgToAsset)(collateral_token);
|
|
91
|
+
return collateral_amount / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token_name));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
var deposit_token_name = (0, constants_1.typeArgToAsset)(deposit_token);
|
|
95
|
+
return collateral_in_deposit_token / Math.pow(10, (0, constants_1.assetToDecimal)(deposit_token_name));
|
|
96
|
+
}
|
|
97
|
+
};
|
|
76
98
|
function parseUserHistory(raw_events) {
|
|
77
99
|
return __awaiter(this, void 0, void 0, function () {
|
|
78
100
|
var events;
|
|
@@ -92,39 +114,40 @@ function parseUserHistory(raw_events) {
|
|
|
92
114
|
// console.log(json);
|
|
93
115
|
// console.log(timestamp);
|
|
94
116
|
var _c = __read(type.split("::"), 3), pkg = _c[0], mod = _c[1], name = _c[2];
|
|
117
|
+
var base_token;
|
|
118
|
+
var collateral_token;
|
|
119
|
+
var market;
|
|
120
|
+
var size;
|
|
121
|
+
var collateral;
|
|
122
|
+
var reverseSide;
|
|
123
|
+
var order_type;
|
|
124
|
+
var price;
|
|
125
|
+
var action;
|
|
126
|
+
var related;
|
|
127
|
+
var e;
|
|
95
128
|
switch (name) {
|
|
96
129
|
case structs_2.CreateTradingOrderEvent.$typeName.split("::")[2]:
|
|
97
130
|
case structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
var collateral;
|
|
131
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
132
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
133
|
+
market = "".concat(base_token, "/USD");
|
|
134
|
+
size = Number(json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
103
135
|
if (json.collateral_amount) {
|
|
104
|
-
collateral = Number(json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
136
|
+
collateral = -(Number(json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)));
|
|
105
137
|
}
|
|
106
138
|
else {
|
|
107
|
-
collateral = Number(json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
108
|
-
}
|
|
109
|
-
var order_type = "Limit";
|
|
110
|
-
var price = json.trigger_price;
|
|
111
|
-
if (json.filled) {
|
|
112
|
-
order_type = "Market";
|
|
113
|
-
price = json.filled_price;
|
|
114
|
-
}
|
|
115
|
-
else if (json.reduce_only && !json.is_stop_order) {
|
|
116
|
-
order_type = "Take Profit";
|
|
139
|
+
collateral = -(Number(json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)));
|
|
117
140
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
141
|
+
order_type = checkOrderType(json.filled, json.reduce_only, json.is_stop_order);
|
|
142
|
+
reverseSide = (order_type === "Stop Loss" || order_type === "Take Profit") && json.linked_position_id;
|
|
143
|
+
price = json.filled ? json.filled_price : json.trigger_price;
|
|
144
|
+
e = {
|
|
122
145
|
action: "Place Order",
|
|
123
146
|
typeName: name,
|
|
124
147
|
order_id: json.order_id,
|
|
125
148
|
position_id: json.linked_position_id,
|
|
126
149
|
market: market,
|
|
127
|
-
side: json.is_long ? "Long" : "Short",
|
|
150
|
+
side: reverseSide ? (json.is_long ? "Short" : "Long") : json.is_long ? "Long" : "Short",
|
|
128
151
|
order_type: order_type,
|
|
129
152
|
status: json.filled ? "Filled" : "Open",
|
|
130
153
|
size: size,
|
|
@@ -139,38 +162,73 @@ function parseUserHistory(raw_events) {
|
|
|
139
162
|
events.push(e);
|
|
140
163
|
break;
|
|
141
164
|
case structs_1.OrderFilledEvent.$typeName.split("::")[2]:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
var action;
|
|
148
|
-
var related;
|
|
165
|
+
base_token = (0, constants_1.typeArgToAsset)(json.symbol.base_token.name);
|
|
166
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
167
|
+
market = "".concat(base_token, "/USD");
|
|
168
|
+
size = Number(json.filled_size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
169
|
+
price = json.filled_price;
|
|
149
170
|
if (json.linked_position_id) {
|
|
150
171
|
action = "Order Filled (Close Position)";
|
|
151
|
-
|
|
172
|
+
var sourceFilledOrder_1 = raw_events.findLast(function (e) {
|
|
173
|
+
var _a, _b, _c, _d;
|
|
174
|
+
var rawEventJson = e.contents.json;
|
|
175
|
+
return (rawEventJson.new_position_id == json.linked_position_id &&
|
|
176
|
+
((_b = (_a = rawEventJson === null || rawEventJson === void 0 ? void 0 : rawEventJson.symbol) === null || _a === void 0 ? void 0 : _a.base_token) === null || _b === void 0 ? void 0 : _b.name) == ((_d = (_c = json === null || json === void 0 ? void 0 : json.symbol) === null || _c === void 0 ? void 0 : _c.base_token) === null || _d === void 0 ? void 0 : _d.name));
|
|
177
|
+
});
|
|
178
|
+
related = raw_events.findLast(function (e) {
|
|
179
|
+
var _a, _b, _c, _d;
|
|
180
|
+
var rawEventJson = e.contents.json;
|
|
181
|
+
return (rawEventJson.order_id == (sourceFilledOrder_1 === null || sourceFilledOrder_1 === void 0 ? void 0 : sourceFilledOrder_1.contents.json.order_id) &&
|
|
182
|
+
((_a = rawEventJson === null || rawEventJson === void 0 ? void 0 : rawEventJson.base_token) === null || _a === void 0 ? void 0 : _a.name) == ((_d = (_c = (_b = sourceFilledOrder_1 === null || sourceFilledOrder_1 === void 0 ? void 0 : sourceFilledOrder_1.contents.json) === null || _b === void 0 ? void 0 : _b.symbol) === null || _c === void 0 ? void 0 : _c.base_token) === null || _d === void 0 ? void 0 : _d.name));
|
|
183
|
+
});
|
|
152
184
|
// the "Place Order" is emit after Order Filled if filled immediately
|
|
153
185
|
}
|
|
154
186
|
else {
|
|
155
187
|
action = "Order Filled (Open Position)";
|
|
156
|
-
related =
|
|
188
|
+
related = raw_events.findLast(function (e) {
|
|
189
|
+
var _a, _b, _c;
|
|
190
|
+
var rawEventJson = e.contents.json;
|
|
191
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
192
|
+
var createTradingOrderEvent = structs_2.CreateTradingOrderEvent.$typeName.split("::")[2];
|
|
193
|
+
var createTradingOrderWithBidReceiptsEvent = structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2];
|
|
194
|
+
var checkEventType = eventType == createTradingOrderEvent || eventType == createTradingOrderWithBidReceiptsEvent;
|
|
195
|
+
return (checkEventType &&
|
|
196
|
+
rawEventJson.order_id == json.order_id &&
|
|
197
|
+
((_a = rawEventJson === null || rawEventJson === void 0 ? void 0 : rawEventJson.base_token) === null || _a === void 0 ? void 0 : _a.name) == ((_c = (_b = json === null || json === void 0 ? void 0 : json.symbol) === null || _b === void 0 ? void 0 : _b.base_token) === null || _c === void 0 ? void 0 : _c.name));
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (!related) {
|
|
201
|
+
console.log("Can't find related event", event);
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
order_type = checkOrderType(related.contents.json.filled, related.contents.json.reduce_only, related.contents.json.is_stop_order);
|
|
205
|
+
if (related.contents.json.collateral_amount) {
|
|
206
|
+
collateral = Number(related.contents.json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
collateral = Number(related.contents.json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
210
|
+
}
|
|
211
|
+
var typeName = name;
|
|
212
|
+
if (related.contents.type.repr.split("::")[2] == structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]) {
|
|
213
|
+
collateral = Number(related.contents.json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
214
|
+
typeName = name + " (with bid receipts)";
|
|
157
215
|
}
|
|
158
216
|
var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
|
|
159
217
|
var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
|
|
160
218
|
var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
|
|
161
219
|
var realized_pnl = ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee;
|
|
162
|
-
|
|
220
|
+
e = {
|
|
163
221
|
action: action,
|
|
164
222
|
typeName: name,
|
|
165
223
|
order_id: json.order_id,
|
|
166
224
|
position_id: (_a = json.linked_position_id) !== null && _a !== void 0 ? _a : json.new_position_id,
|
|
167
225
|
market: market,
|
|
168
226
|
side: json.position_side ? "Long" : "Short",
|
|
169
|
-
order_type:
|
|
227
|
+
order_type: order_type,
|
|
170
228
|
status: "Filled",
|
|
171
229
|
size: size,
|
|
172
230
|
base_token: base_token,
|
|
173
|
-
collateral:
|
|
231
|
+
collateral: json.linked_position_id ? collateral : -collateral,
|
|
174
232
|
collateral_token: collateral_token,
|
|
175
233
|
price: Number(price) / Math.pow(10, 8), // WARNING: fixed decimal
|
|
176
234
|
realized_pnl: realized_pnl,
|
|
@@ -190,20 +248,37 @@ function parseUserHistory(raw_events) {
|
|
|
190
248
|
}
|
|
191
249
|
break;
|
|
192
250
|
case structs_2.CancelTradingOrderEvent.$typeName.split("::")[2]:
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
251
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
252
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
253
|
+
market = "".concat(base_token, "/USD");
|
|
254
|
+
// console.log(market, json.order_id);
|
|
255
|
+
related = raw_events.findLast(function (e) {
|
|
256
|
+
var rawEventJson = e.contents.json;
|
|
257
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
258
|
+
var createTradingOrderEvent = structs_2.CreateTradingOrderEvent.$typeName.split("::")[2];
|
|
259
|
+
var createTradingOrderWithBidReceiptsEvent = structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2];
|
|
260
|
+
var checkEventType = eventType == createTradingOrderEvent || eventType == createTradingOrderWithBidReceiptsEvent;
|
|
261
|
+
return checkEventType && rawEventJson.order_id == json.order_id && rawEventJson.base_token.name == json.base_token.name;
|
|
262
|
+
});
|
|
263
|
+
order_type = checkOrderType(related.contents.json.filled, related.contents.json.reduce_only, related.contents.json.is_stop_order);
|
|
264
|
+
size = Number(related.contents.json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
265
|
+
reverseSide = (order_type === "Stop Loss" || order_type === "Take Profit") && json.linked_position_id;
|
|
266
|
+
e = {
|
|
198
267
|
action: "Cancel Order",
|
|
199
268
|
typeName: name,
|
|
200
269
|
order_id: json.order_id,
|
|
201
|
-
position_id:
|
|
270
|
+
position_id: related.contents.json.linked_position_id,
|
|
202
271
|
market: market,
|
|
203
|
-
side:
|
|
204
|
-
|
|
272
|
+
side: reverseSide
|
|
273
|
+
? related.contents.json.is_long
|
|
274
|
+
? "Short"
|
|
275
|
+
: "Long"
|
|
276
|
+
: related.contents.json.is_long
|
|
277
|
+
? "Long"
|
|
278
|
+
: "Short",
|
|
279
|
+
order_type: order_type,
|
|
205
280
|
status: "Canceled",
|
|
206
|
-
size:
|
|
281
|
+
size: size,
|
|
207
282
|
base_token: base_token,
|
|
208
283
|
collateral: Number(json.released_collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
209
284
|
collateral_token: collateral_token,
|
|
@@ -216,43 +291,54 @@ function parseUserHistory(raw_events) {
|
|
|
216
291
|
break;
|
|
217
292
|
case structs_2.IncreaseCollateralEvent.$typeName.split("::")[2]:
|
|
218
293
|
case structs_2.ReleaseCollateralEvent.$typeName.split("::")[2]:
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
294
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
295
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
296
|
+
market = "".concat(base_token, "/USD");
|
|
297
|
+
related = raw_events.findLast(function (e) {
|
|
298
|
+
var rawEventJson = e.contents.json;
|
|
299
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
300
|
+
var orderFilledEvent = structs_1.OrderFilledEvent.$typeName.split("::")[2];
|
|
301
|
+
var checkEventType = eventType == orderFilledEvent;
|
|
302
|
+
//console.log(e);
|
|
303
|
+
return (checkEventType &&
|
|
304
|
+
rawEventJson.new_position_id == json.position_id &&
|
|
305
|
+
rawEventJson.symbol.base_token.name == json.base_token.name &&
|
|
306
|
+
rawEventJson.linked_position_id == null);
|
|
307
|
+
});
|
|
308
|
+
size = Number(related.contents.json.filled_size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
224
309
|
if (json.increased_collateral_amount) {
|
|
225
310
|
collateral = Number(json.increased_collateral_amount);
|
|
226
311
|
}
|
|
227
312
|
else {
|
|
228
313
|
collateral = Number(json.released_collateral_amount) * -1;
|
|
229
314
|
}
|
|
230
|
-
|
|
315
|
+
e = {
|
|
231
316
|
action: "Modify Collateral",
|
|
232
317
|
typeName: name,
|
|
233
318
|
order_id: undefined,
|
|
234
319
|
position_id: json.position_id,
|
|
235
320
|
market: market,
|
|
236
|
-
side: related === null || related === void 0 ? void 0 : related.
|
|
237
|
-
order_type:
|
|
238
|
-
status:
|
|
239
|
-
size:
|
|
321
|
+
side: (related === null || related === void 0 ? void 0 : related.contents.json.is_long) ? "Long" : "Short",
|
|
322
|
+
order_type: undefined,
|
|
323
|
+
status: undefined,
|
|
324
|
+
size: size,
|
|
240
325
|
base_token: base_token,
|
|
241
326
|
collateral: collateral / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
242
327
|
collateral_token: collateral_token,
|
|
243
|
-
price:
|
|
328
|
+
price: undefined, // WARNING: fixed decimal
|
|
244
329
|
realized_pnl: undefined,
|
|
245
330
|
timestamp: timestamp,
|
|
246
331
|
tx_digest: tx_digest,
|
|
247
332
|
};
|
|
248
333
|
events.push(e);
|
|
334
|
+
//console.log(e, related, related.contents.json.symbol, base_token, assetToDecimal(base_token));
|
|
249
335
|
break;
|
|
250
336
|
case structs_3.SwapEvent.$typeName.split("::")[2]:
|
|
251
337
|
var from_token = (0, constants_1.typeArgToAsset)(json.from_token_type.name);
|
|
252
338
|
var to_token = (0, constants_1.typeArgToAsset)(json.to_token_type.name);
|
|
253
339
|
var from_price = Number(json.oracle_price_from_token);
|
|
254
340
|
var to_price = Number(json.oracle_price_to_token);
|
|
255
|
-
|
|
341
|
+
e = {
|
|
256
342
|
action: "Swap",
|
|
257
343
|
typeName: name,
|
|
258
344
|
order_id: undefined,
|
|
@@ -263,7 +349,7 @@ function parseUserHistory(raw_events) {
|
|
|
263
349
|
status: "Filled",
|
|
264
350
|
size: Number(json.actual_to_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(to_token)),
|
|
265
351
|
base_token: to_token,
|
|
266
|
-
collateral: Number(json.from_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(from_token)),
|
|
352
|
+
collateral: -Number(json.from_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(from_token)),
|
|
267
353
|
collateral_token: from_token,
|
|
268
354
|
price: from_price / to_price,
|
|
269
355
|
realized_pnl: -Number(json.fee_amount_usd) / Math.pow(10, 9),
|