@typus/typus-perp-sdk 1.0.39 → 1.0.41
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 +127 -53
- package/package.json +2 -2
|
@@ -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,33 +114,32 @@ 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 order_type;
|
|
123
|
+
var price;
|
|
124
|
+
var action;
|
|
125
|
+
var related;
|
|
126
|
+
var e;
|
|
95
127
|
switch (name) {
|
|
96
128
|
case structs_2.CreateTradingOrderEvent.$typeName.split("::")[2]:
|
|
97
129
|
case structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
var collateral;
|
|
130
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
131
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
132
|
+
market = "".concat(base_token, "/USD");
|
|
133
|
+
size = Number(json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
103
134
|
if (json.collateral_amount) {
|
|
104
|
-
collateral = Number(json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
135
|
+
collateral = -(Number(json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)));
|
|
105
136
|
}
|
|
106
137
|
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";
|
|
138
|
+
collateral = -(Number(json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)));
|
|
117
139
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
var e = {
|
|
140
|
+
order_type = checkOrderType(json.filled, json.reduce_only, json.is_stop_order);
|
|
141
|
+
price = json.filled ? json.filled_price : json.trigger_price;
|
|
142
|
+
e = {
|
|
122
143
|
action: "Place Order",
|
|
123
144
|
typeName: name,
|
|
124
145
|
order_id: json.order_id,
|
|
@@ -139,38 +160,73 @@ function parseUserHistory(raw_events) {
|
|
|
139
160
|
events.push(e);
|
|
140
161
|
break;
|
|
141
162
|
case structs_1.OrderFilledEvent.$typeName.split("::")[2]:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
var action;
|
|
148
|
-
var related;
|
|
163
|
+
base_token = (0, constants_1.typeArgToAsset)(json.symbol.base_token.name);
|
|
164
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
165
|
+
market = "".concat(base_token, "/USD");
|
|
166
|
+
size = Number(json.filled_size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
167
|
+
price = json.filled_price;
|
|
149
168
|
if (json.linked_position_id) {
|
|
150
169
|
action = "Order Filled (Close Position)";
|
|
151
|
-
|
|
170
|
+
var sourceFilledOrder_1 = raw_events.findLast(function (e) {
|
|
171
|
+
var _a, _b, _c, _d;
|
|
172
|
+
var rawEventJson = e.contents.json;
|
|
173
|
+
return (rawEventJson.new_position_id == json.linked_position_id &&
|
|
174
|
+
((_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));
|
|
175
|
+
});
|
|
176
|
+
related = raw_events.findLast(function (e) {
|
|
177
|
+
var _a, _b, _c, _d;
|
|
178
|
+
var rawEventJson = e.contents.json;
|
|
179
|
+
return (rawEventJson.order_id == (sourceFilledOrder_1 === null || sourceFilledOrder_1 === void 0 ? void 0 : sourceFilledOrder_1.contents.json.order_id) &&
|
|
180
|
+
((_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));
|
|
181
|
+
});
|
|
152
182
|
// the "Place Order" is emit after Order Filled if filled immediately
|
|
153
183
|
}
|
|
154
184
|
else {
|
|
155
185
|
action = "Order Filled (Open Position)";
|
|
156
|
-
related =
|
|
186
|
+
related = raw_events.findLast(function (e) {
|
|
187
|
+
var _a, _b, _c;
|
|
188
|
+
var rawEventJson = e.contents.json;
|
|
189
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
190
|
+
var createTradingOrderEvent = structs_2.CreateTradingOrderEvent.$typeName.split("::")[2];
|
|
191
|
+
var createTradingOrderWithBidReceiptsEvent = structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2];
|
|
192
|
+
var checkEventType = eventType == createTradingOrderEvent || eventType == createTradingOrderWithBidReceiptsEvent;
|
|
193
|
+
return (checkEventType &&
|
|
194
|
+
rawEventJson.order_id == json.order_id &&
|
|
195
|
+
((_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));
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
if (!related) {
|
|
199
|
+
console.log("Can't find related event", event);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
order_type = checkOrderType(related.contents.json.filled, related.contents.json.reduce_only, related.contents.json.is_stop_order);
|
|
203
|
+
if (related.contents.json.collateral_amount) {
|
|
204
|
+
collateral = Number(related.contents.json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
collateral = Number(related.contents.json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
208
|
+
}
|
|
209
|
+
var typeName = name;
|
|
210
|
+
if (related.contents.type.repr.split("::")[2] == structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]) {
|
|
211
|
+
collateral = Number(related.contents.json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
212
|
+
typeName = name + " (with bid receipts)";
|
|
157
213
|
}
|
|
158
214
|
var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
|
|
159
215
|
var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
|
|
160
216
|
var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
|
|
161
217
|
var realized_pnl = ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee;
|
|
162
|
-
|
|
218
|
+
e = {
|
|
163
219
|
action: action,
|
|
164
220
|
typeName: name,
|
|
165
221
|
order_id: json.order_id,
|
|
166
222
|
position_id: (_a = json.linked_position_id) !== null && _a !== void 0 ? _a : json.new_position_id,
|
|
167
223
|
market: market,
|
|
168
224
|
side: json.position_side ? "Long" : "Short",
|
|
169
|
-
order_type:
|
|
225
|
+
order_type: order_type,
|
|
170
226
|
status: "Filled",
|
|
171
227
|
size: size,
|
|
172
228
|
base_token: base_token,
|
|
173
|
-
collateral:
|
|
229
|
+
collateral: json.linked_position_id ? collateral : -collateral,
|
|
174
230
|
collateral_token: collateral_token,
|
|
175
231
|
price: Number(price) / Math.pow(10, 8), // WARNING: fixed decimal
|
|
176
232
|
realized_pnl: realized_pnl,
|
|
@@ -190,20 +246,30 @@ function parseUserHistory(raw_events) {
|
|
|
190
246
|
}
|
|
191
247
|
break;
|
|
192
248
|
case structs_2.CancelTradingOrderEvent.$typeName.split("::")[2]:
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
249
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
250
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
251
|
+
market = "".concat(base_token, "/USD");
|
|
252
|
+
// console.log(market, json.order_id);
|
|
253
|
+
related = raw_events.findLast(function (e) {
|
|
254
|
+
var rawEventJson = e.contents.json;
|
|
255
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
256
|
+
var createTradingOrderEvent = structs_2.CreateTradingOrderEvent.$typeName.split("::")[2];
|
|
257
|
+
var createTradingOrderWithBidReceiptsEvent = structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2];
|
|
258
|
+
var checkEventType = eventType == createTradingOrderEvent || eventType == createTradingOrderWithBidReceiptsEvent;
|
|
259
|
+
return checkEventType && rawEventJson.order_id == json.order_id && rawEventJson.base_token.name == json.base_token.name;
|
|
260
|
+
});
|
|
261
|
+
order_type = checkOrderType(related.contents.json.filled, related.contents.json.reduce_only, related.contents.json.is_stop_order);
|
|
262
|
+
size = Number(related.contents.json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
263
|
+
e = {
|
|
198
264
|
action: "Cancel Order",
|
|
199
265
|
typeName: name,
|
|
200
266
|
order_id: json.order_id,
|
|
201
|
-
position_id:
|
|
267
|
+
position_id: related.contents.json.linked_position_id,
|
|
202
268
|
market: market,
|
|
203
|
-
side: related
|
|
204
|
-
order_type:
|
|
269
|
+
side: related.contents.json.is_long ? "Long" : "Short",
|
|
270
|
+
order_type: order_type,
|
|
205
271
|
status: "Canceled",
|
|
206
|
-
size:
|
|
272
|
+
size: size,
|
|
207
273
|
base_token: base_token,
|
|
208
274
|
collateral: Number(json.released_collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
209
275
|
collateral_token: collateral_token,
|
|
@@ -216,31 +282,39 @@ function parseUserHistory(raw_events) {
|
|
|
216
282
|
break;
|
|
217
283
|
case structs_2.IncreaseCollateralEvent.$typeName.split("::")[2]:
|
|
218
284
|
case structs_2.ReleaseCollateralEvent.$typeName.split("::")[2]:
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
285
|
+
base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
286
|
+
collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
287
|
+
market = "".concat(base_token, "/USD");
|
|
288
|
+
related = raw_events.findLast(function (e) {
|
|
289
|
+
var rawEventJson = e.contents.json;
|
|
290
|
+
var eventType = e.contents.type.repr.split("::")[2];
|
|
291
|
+
var orderFilledEvent = structs_1.OrderFilledEvent.$typeName.split("::")[2];
|
|
292
|
+
var checkEventType = eventType == orderFilledEvent;
|
|
293
|
+
return (checkEventType &&
|
|
294
|
+
rawEventJson.new_position_id == json.position_id &&
|
|
295
|
+
rawEventJson.symbol.base_token.name == json.base_token.name);
|
|
296
|
+
});
|
|
297
|
+
size = Number(related.contents.json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
224
298
|
if (json.increased_collateral_amount) {
|
|
225
299
|
collateral = Number(json.increased_collateral_amount);
|
|
226
300
|
}
|
|
227
301
|
else {
|
|
228
302
|
collateral = Number(json.released_collateral_amount) * -1;
|
|
229
303
|
}
|
|
230
|
-
|
|
304
|
+
e = {
|
|
231
305
|
action: "Modify Collateral",
|
|
232
306
|
typeName: name,
|
|
233
307
|
order_id: undefined,
|
|
234
308
|
position_id: json.position_id,
|
|
235
309
|
market: market,
|
|
236
|
-
side: related === null || related === void 0 ? void 0 : related.
|
|
237
|
-
order_type:
|
|
238
|
-
status:
|
|
239
|
-
size:
|
|
310
|
+
side: (related === null || related === void 0 ? void 0 : related.contents.json.is_long) ? "Long" : "Short",
|
|
311
|
+
order_type: undefined,
|
|
312
|
+
status: undefined,
|
|
313
|
+
size: size,
|
|
240
314
|
base_token: base_token,
|
|
241
315
|
collateral: collateral / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
242
316
|
collateral_token: collateral_token,
|
|
243
|
-
price:
|
|
317
|
+
price: undefined, // WARNING: fixed decimal
|
|
244
318
|
realized_pnl: undefined,
|
|
245
319
|
timestamp: timestamp,
|
|
246
320
|
tx_digest: tx_digest,
|
|
@@ -252,7 +326,7 @@ function parseUserHistory(raw_events) {
|
|
|
252
326
|
var to_token = (0, constants_1.typeArgToAsset)(json.to_token_type.name);
|
|
253
327
|
var from_price = Number(json.oracle_price_from_token);
|
|
254
328
|
var to_price = Number(json.oracle_price_to_token);
|
|
255
|
-
|
|
329
|
+
e = {
|
|
256
330
|
action: "Swap",
|
|
257
331
|
typeName: name,
|
|
258
332
|
order_id: undefined,
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typus/typus-perp-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.41",
|
|
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.6.
|
|
9
|
+
"@typus/typus-sdk": "1.6.16"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@types/bs58": "^4.0.1",
|