@typus/typus-perp-sdk 1.0.49 → 1.0.51
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/api/sentio.d.ts +1 -0
- package/dist/src/api/sentio.js +64 -1
- package/dist/src/user/history.d.ts +7 -7
- package/dist/src/user/history.js +239 -257
- package/package.json +2 -2
package/dist/src/api/sentio.d.ts
CHANGED
package/dist/src/api/sentio.js
CHANGED
|
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.getFromSentio = getFromSentio;
|
|
40
40
|
exports.getTlpAPRFromSentio = getTlpAPRFromSentio;
|
|
41
41
|
exports.getVolumeFromSentio = getVolumeFromSentio;
|
|
42
|
+
exports.getTlpFeeFromSentio = getTlpFeeFromSentio;
|
|
42
43
|
var src_1 = require("../../src");
|
|
43
44
|
var headers = {
|
|
44
45
|
"api-key": "ffJa6FwxeJNrQP8NZ5doEMXqdSA7XM6mT",
|
|
@@ -71,7 +72,13 @@ function getFromSentio(event, userAddress, startTimestamp) {
|
|
|
71
72
|
case 2:
|
|
72
73
|
data = _a.sent();
|
|
73
74
|
// console.log(data);
|
|
74
|
-
|
|
75
|
+
if (data.result) {
|
|
76
|
+
return [2 /*return*/, data.result.rows];
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return [2 /*return*/, []];
|
|
80
|
+
}
|
|
81
|
+
return [2 /*return*/];
|
|
75
82
|
}
|
|
76
83
|
});
|
|
77
84
|
});
|
|
@@ -190,3 +197,59 @@ function getVolumeFromSentio() {
|
|
|
190
197
|
});
|
|
191
198
|
});
|
|
192
199
|
}
|
|
200
|
+
function getTlpFeeFromSentio() {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
202
|
+
var apiUrl, requestData, jsonData, response, data, first, last, fee;
|
|
203
|
+
return __generator(this, function (_a) {
|
|
204
|
+
switch (_a.label) {
|
|
205
|
+
case 0:
|
|
206
|
+
apiUrl = src_1.NETWORK == "MAINNET"
|
|
207
|
+
? "https://app.sentio.xyz/api/v1/insights/typus/typus_perp_mainnet/query"
|
|
208
|
+
: "https://app.sentio.xyz/api/v1/insights/typus/typus_perp/query";
|
|
209
|
+
requestData = {
|
|
210
|
+
timeRange: {
|
|
211
|
+
start: "now-7d",
|
|
212
|
+
end: "now",
|
|
213
|
+
step: 3600,
|
|
214
|
+
},
|
|
215
|
+
limit: 20,
|
|
216
|
+
queries: [
|
|
217
|
+
{
|
|
218
|
+
metricsQuery: {
|
|
219
|
+
query: "tlp_fee_usd",
|
|
220
|
+
alias: "",
|
|
221
|
+
id: "a",
|
|
222
|
+
labelSelector: {},
|
|
223
|
+
aggregate: null,
|
|
224
|
+
functions: [],
|
|
225
|
+
disabled: false,
|
|
226
|
+
},
|
|
227
|
+
dataSource: "METRICS",
|
|
228
|
+
sourceName: "",
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
formulas: [],
|
|
232
|
+
};
|
|
233
|
+
jsonData = JSON.stringify(requestData);
|
|
234
|
+
return [4 /*yield*/, fetch(apiUrl, {
|
|
235
|
+
method: "POST",
|
|
236
|
+
headers: headers,
|
|
237
|
+
body: jsonData,
|
|
238
|
+
})];
|
|
239
|
+
case 1:
|
|
240
|
+
response = _a.sent();
|
|
241
|
+
return [4 /*yield*/, response.json()];
|
|
242
|
+
case 2:
|
|
243
|
+
data = _a.sent();
|
|
244
|
+
first = data.results[0].matrix.samples[0].values[0];
|
|
245
|
+
last = data.results[0].matrix.samples[0].values.at(-1);
|
|
246
|
+
fee = last.value - first.value;
|
|
247
|
+
// console.log(fee);
|
|
248
|
+
return [2 /*return*/, fee];
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
// getTlpAPRFromSentio();
|
|
254
|
+
// getVolumeFromSentio();
|
|
255
|
+
// getTlpFeeFromSentio();
|
|
@@ -1,8 +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
|
|
3
|
-
export type tokenType = {
|
|
4
|
-
name: string;
|
|
5
|
-
};
|
|
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";
|
|
6
3
|
export type sideType = "Long" | "Short";
|
|
7
4
|
export type orderType = "Market" | "Limit" | "Take Profit" | "Stop Loss";
|
|
8
5
|
export type statusType = "Open" | "Filled" | "Canceled";
|
|
@@ -14,7 +11,7 @@ export interface Event {
|
|
|
14
11
|
market: string;
|
|
15
12
|
side: sideType | undefined;
|
|
16
13
|
order_type: orderType | undefined;
|
|
17
|
-
status: statusType
|
|
14
|
+
status: statusType;
|
|
18
15
|
size: number | undefined;
|
|
19
16
|
base_token: TOKEN;
|
|
20
17
|
collateral: number | undefined;
|
|
@@ -23,8 +20,11 @@ export interface Event {
|
|
|
23
20
|
realized_pnl: number | undefined;
|
|
24
21
|
timestamp: string;
|
|
25
22
|
tx_digest: string;
|
|
23
|
+
dov_index: string | undefined;
|
|
24
|
+
sender: "user" | "cranker";
|
|
26
25
|
}
|
|
27
26
|
export declare function parseUserHistory(raw_events: any): Promise<Event[]>;
|
|
28
27
|
export declare function getGraphQLEvents(module: string, sender: string, beforeCursor?: string | null): Promise<any>;
|
|
29
|
-
export declare function getLiquidateFromSentio(userAddress: string, startTimestamp: number,
|
|
30
|
-
export declare function getOrderMatchFromSentio(userAddress: string, startTimestamp: number,
|
|
28
|
+
export declare function getLiquidateFromSentio(userAddress: string, startTimestamp: number, events: Event[]): Promise<Event[]>;
|
|
29
|
+
export declare function getOrderMatchFromSentio(userAddress: string, startTimestamp: number, events: Event[]): Promise<Event[]>;
|
|
30
|
+
export declare function getRealizeOptionFromSentio(userAddress: string, startTimestamp: number, events: Event[]): Promise<Event[]>;
|
package/dist/src/user/history.js
CHANGED
|
@@ -67,34 +67,13 @@ exports.parseUserHistory = parseUserHistory;
|
|
|
67
67
|
exports.getGraphQLEvents = getGraphQLEvents;
|
|
68
68
|
exports.getLiquidateFromSentio = getLiquidateFromSentio;
|
|
69
69
|
exports.getOrderMatchFromSentio = getOrderMatchFromSentio;
|
|
70
|
+
exports.getRealizeOptionFromSentio = getRealizeOptionFromSentio;
|
|
70
71
|
var constants_1 = require("@typus/typus-sdk/dist/src/constants");
|
|
71
72
|
var structs_1 = require("../typus_perp/position/structs");
|
|
72
73
|
var structs_2 = require("../typus_perp/trading/structs");
|
|
73
74
|
var structs_3 = require("../../src/typus_perp/lp-pool/structs");
|
|
74
75
|
var sentio_1 = require("../../src/api/sentio");
|
|
75
76
|
var src_1 = require("../../src");
|
|
76
|
-
var checkOrderType = function (filled, reduceOnly, isStopOrder) {
|
|
77
|
-
if (reduceOnly && !isStopOrder) {
|
|
78
|
-
return "Take Profit";
|
|
79
|
-
}
|
|
80
|
-
if (reduceOnly && isStopOrder) {
|
|
81
|
-
return "Stop Loss";
|
|
82
|
-
}
|
|
83
|
-
if (filled) {
|
|
84
|
-
return "Market";
|
|
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
|
-
};
|
|
98
77
|
function parseUserHistory(raw_events) {
|
|
99
78
|
return __awaiter(this, void 0, void 0, function () {
|
|
100
79
|
var events;
|
|
@@ -114,40 +93,39 @@ function parseUserHistory(raw_events) {
|
|
|
114
93
|
// console.log(json);
|
|
115
94
|
// console.log(timestamp);
|
|
116
95
|
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;
|
|
128
96
|
switch (name) {
|
|
129
97
|
case structs_2.CreateTradingOrderEvent.$typeName.split("::")[2]:
|
|
130
98
|
case structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2]:
|
|
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));
|
|
99
|
+
var base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
100
|
+
var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
101
|
+
var market = "".concat(base_token, "/USD");
|
|
102
|
+
var size = Number(json.size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
103
|
+
var collateral;
|
|
135
104
|
if (json.collateral_amount) {
|
|
136
|
-
collateral =
|
|
105
|
+
collateral = Number(json.collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
137
106
|
}
|
|
138
107
|
else {
|
|
139
|
-
collateral =
|
|
108
|
+
collateral = Number(json.collateral_in_deposit_token) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token));
|
|
109
|
+
}
|
|
110
|
+
var order_type = "Limit";
|
|
111
|
+
var price = json.trigger_price;
|
|
112
|
+
if (json.filled) {
|
|
113
|
+
order_type = "Market";
|
|
114
|
+
price = json.filled_price;
|
|
140
115
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
116
|
+
else if (json.reduce_only && !json.is_stop_order) {
|
|
117
|
+
order_type = "Take Profit";
|
|
118
|
+
}
|
|
119
|
+
else if (json.reduce_only && json.is_stop_order) {
|
|
120
|
+
order_type = "Stop Loss";
|
|
121
|
+
}
|
|
122
|
+
var e = {
|
|
145
123
|
action: "Place Order",
|
|
146
124
|
typeName: name,
|
|
147
125
|
order_id: json.order_id,
|
|
148
126
|
position_id: json.linked_position_id,
|
|
149
127
|
market: market,
|
|
150
|
-
side:
|
|
128
|
+
side: json.is_long ? "Long" : "Short",
|
|
151
129
|
order_type: order_type,
|
|
152
130
|
status: json.filled ? "Filled" : "Open",
|
|
153
131
|
size: size,
|
|
@@ -158,82 +136,52 @@ function parseUserHistory(raw_events) {
|
|
|
158
136
|
realized_pnl: undefined,
|
|
159
137
|
timestamp: timestamp,
|
|
160
138
|
tx_digest: tx_digest,
|
|
139
|
+
dov_index: json.dov_index,
|
|
140
|
+
sender: "user",
|
|
161
141
|
};
|
|
162
142
|
events.push(e);
|
|
163
143
|
break;
|
|
164
144
|
case structs_1.OrderFilledEvent.$typeName.split("::")[2]:
|
|
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;
|
|
170
|
-
|
|
145
|
+
var base_token = (0, constants_1.typeArgToAsset)(json.symbol.base_token.name);
|
|
146
|
+
var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
147
|
+
var market = "".concat(base_token, "/USD");
|
|
148
|
+
var size = Number(json.filled_size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
149
|
+
var price = json.filled_price;
|
|
150
|
+
var action;
|
|
151
|
+
var related;
|
|
152
|
+
if (json.linked_position_id != undefined) {
|
|
171
153
|
action = "Order Filled (Close Position)";
|
|
172
|
-
|
|
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
|
-
});
|
|
154
|
+
related = events.findLast(function (e) { return e.position_id === json.linked_position_id && e.market === market; });
|
|
184
155
|
// the "Place Order" is emit after Order Filled if filled immediately
|
|
185
156
|
}
|
|
186
157
|
else {
|
|
187
158
|
action = "Order Filled (Open Position)";
|
|
188
|
-
related =
|
|
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)";
|
|
159
|
+
related = events.findLast(function (e) { return e.order_id === json.order_id && e.market === market; });
|
|
215
160
|
}
|
|
216
161
|
var realized_trading_fee = Number(json.realized_trading_fee) + Number(json.realized_borrow_fee);
|
|
217
162
|
var realized_fee_in_usd = Number(json.realized_fee_in_usd) / Math.pow(10, 9);
|
|
218
163
|
var realized_amount = json.realized_amount_sign ? Number(json.realized_amount) : -Number(json.realized_amount);
|
|
219
|
-
|
|
220
|
-
|
|
164
|
+
// console.log(realized_amount);
|
|
165
|
+
var realized_pnl = realized_trading_fee > 0 ? ((realized_amount - realized_trading_fee) * realized_fee_in_usd) / realized_trading_fee : 0;
|
|
166
|
+
var e = {
|
|
221
167
|
action: action,
|
|
222
|
-
typeName:
|
|
168
|
+
typeName: name,
|
|
223
169
|
order_id: json.order_id,
|
|
224
170
|
position_id: (_a = json.linked_position_id) !== null && _a !== void 0 ? _a : json.new_position_id,
|
|
225
171
|
market: market,
|
|
226
172
|
side: json.position_side ? "Long" : "Short",
|
|
227
|
-
order_type: order_type,
|
|
173
|
+
order_type: related === null || related === void 0 ? void 0 : related.order_type,
|
|
228
174
|
status: "Filled",
|
|
229
175
|
size: size,
|
|
230
176
|
base_token: base_token,
|
|
231
|
-
collateral:
|
|
177
|
+
collateral: related === null || related === void 0 ? void 0 : related.collateral, // TODO: check for option collateral
|
|
232
178
|
collateral_token: collateral_token,
|
|
233
179
|
price: Number(price) / Math.pow(10, 8), // WARNING: fixed decimal
|
|
234
180
|
realized_pnl: realized_pnl,
|
|
235
181
|
timestamp: timestamp,
|
|
236
182
|
tx_digest: tx_digest,
|
|
183
|
+
dov_index: related === null || related === void 0 ? void 0 : related.dov_index,
|
|
184
|
+
sender: "user",
|
|
237
185
|
};
|
|
238
186
|
events.push(e);
|
|
239
187
|
break;
|
|
@@ -248,37 +196,20 @@ function parseUserHistory(raw_events) {
|
|
|
248
196
|
}
|
|
249
197
|
break;
|
|
250
198
|
case structs_2.CancelTradingOrderEvent.$typeName.split("::")[2]:
|
|
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
|
-
|
|
255
|
-
|
|
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 = {
|
|
199
|
+
var base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
200
|
+
var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
201
|
+
var market = "".concat(base_token, "/USD");
|
|
202
|
+
var related = events.findLast(function (e) { return e.order_id === json.order_id && e.market === market; });
|
|
203
|
+
var e = {
|
|
267
204
|
action: "Cancel Order",
|
|
268
205
|
typeName: name,
|
|
269
206
|
order_id: json.order_id,
|
|
270
|
-
position_id: related.
|
|
207
|
+
position_id: related === null || related === void 0 ? void 0 : related.position_id,
|
|
271
208
|
market: market,
|
|
272
|
-
side:
|
|
273
|
-
|
|
274
|
-
? "Short"
|
|
275
|
-
: "Long"
|
|
276
|
-
: related.contents.json.is_long
|
|
277
|
-
? "Long"
|
|
278
|
-
: "Short",
|
|
279
|
-
order_type: order_type,
|
|
209
|
+
side: related === null || related === void 0 ? void 0 : related.side,
|
|
210
|
+
order_type: related === null || related === void 0 ? void 0 : related.order_type,
|
|
280
211
|
status: "Canceled",
|
|
281
|
-
size: size,
|
|
212
|
+
size: related === null || related === void 0 ? void 0 : related.size,
|
|
282
213
|
base_token: base_token,
|
|
283
214
|
collateral: Number(json.released_collateral_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
284
215
|
collateral_token: collateral_token,
|
|
@@ -286,70 +217,58 @@ function parseUserHistory(raw_events) {
|
|
|
286
217
|
realized_pnl: undefined,
|
|
287
218
|
timestamp: timestamp,
|
|
288
219
|
tx_digest: tx_digest,
|
|
220
|
+
dov_index: related === null || related === void 0 ? void 0 : related.dov_index,
|
|
221
|
+
sender: "user",
|
|
289
222
|
};
|
|
290
223
|
events.push(e);
|
|
291
224
|
break;
|
|
292
225
|
case structs_2.IncreaseCollateralEvent.$typeName.split("::")[2]:
|
|
293
226
|
case structs_2.ReleaseCollateralEvent.$typeName.split("::")[2]:
|
|
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 =
|
|
298
|
-
|
|
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
|
-
var relatedOrder = raw_events.findLast(function (e) {
|
|
309
|
-
var _a, _b;
|
|
310
|
-
var rawEventJson = e.contents.json;
|
|
311
|
-
var eventType = e.contents.type.repr.split("::")[2];
|
|
312
|
-
var createTradingOrderEvent = structs_2.CreateTradingOrderEvent.$typeName.split("::")[2];
|
|
313
|
-
var createTradingOrderWithBidReceiptsEvent = structs_2.CreateTradingOrderWithBidReceiptsEvent.$typeName.split("::")[2];
|
|
314
|
-
var checkEventType = eventType == createTradingOrderEvent || eventType == createTradingOrderWithBidReceiptsEvent;
|
|
315
|
-
return (checkEventType &&
|
|
316
|
-
rawEventJson.order_id == (related === null || related === void 0 ? void 0 : related.contents.json.order_id) &&
|
|
317
|
-
((_a = rawEventJson === null || rawEventJson === void 0 ? void 0 : rawEventJson.base_token) === null || _a === void 0 ? void 0 : _a.name) == ((_b = json === null || json === void 0 ? void 0 : json.base_token) === null || _b === void 0 ? void 0 : _b.name));
|
|
318
|
-
});
|
|
319
|
-
size = Number(related === null || related === void 0 ? void 0 : related.contents.json.filled_size) / Math.pow(10, (0, constants_1.assetToDecimal)(base_token));
|
|
227
|
+
var base_token = (0, constants_1.typeArgToAsset)(json.base_token.name);
|
|
228
|
+
var collateral_token = (0, constants_1.typeArgToAsset)(json.collateral_token.name);
|
|
229
|
+
var market = "".concat(base_token, "/USD");
|
|
230
|
+
var related = events.find(function (e) { return e.position_id === json.position_id && e.market === market; });
|
|
231
|
+
var collateral;
|
|
320
232
|
if (json.increased_collateral_amount) {
|
|
321
|
-
collateral = Number(json.increased_collateral_amount);
|
|
233
|
+
collateral = Number(json.increased_collateral_amount) * -1;
|
|
322
234
|
}
|
|
323
235
|
else {
|
|
324
|
-
collateral = Number(json.released_collateral_amount)
|
|
236
|
+
collateral = Number(json.released_collateral_amount);
|
|
325
237
|
}
|
|
326
|
-
e = {
|
|
238
|
+
var e = {
|
|
327
239
|
action: "Modify Collateral",
|
|
328
240
|
typeName: name,
|
|
329
241
|
order_id: undefined,
|
|
330
242
|
position_id: json.position_id,
|
|
331
243
|
market: market,
|
|
332
|
-
side:
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
244
|
+
side: !related
|
|
245
|
+
? undefined
|
|
246
|
+
: related.action === "Order Filled (Open Position)"
|
|
247
|
+
? related.side
|
|
248
|
+
: related.side === "Long"
|
|
249
|
+
? "Short"
|
|
250
|
+
: "Long",
|
|
251
|
+
order_type: related === null || related === void 0 ? void 0 : related.order_type,
|
|
252
|
+
status: "Filled",
|
|
253
|
+
size: related === null || related === void 0 ? void 0 : related.size,
|
|
336
254
|
base_token: base_token,
|
|
337
255
|
collateral: collateral / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
338
256
|
collateral_token: collateral_token,
|
|
339
|
-
price:
|
|
257
|
+
price: related === null || related === void 0 ? void 0 : related.price,
|
|
340
258
|
realized_pnl: undefined,
|
|
341
259
|
timestamp: timestamp,
|
|
342
260
|
tx_digest: tx_digest,
|
|
261
|
+
dov_index: related === null || related === void 0 ? void 0 : related.dov_index,
|
|
262
|
+
sender: "user",
|
|
343
263
|
};
|
|
344
264
|
events.push(e);
|
|
345
|
-
//console.log(e, related, related.contents.json.symbol, base_token, assetToDecimal(base_token));
|
|
346
265
|
break;
|
|
347
266
|
case structs_3.SwapEvent.$typeName.split("::")[2]:
|
|
348
267
|
var from_token = (0, constants_1.typeArgToAsset)(json.from_token_type.name);
|
|
349
268
|
var to_token = (0, constants_1.typeArgToAsset)(json.to_token_type.name);
|
|
350
269
|
var from_price = Number(json.oracle_price_from_token);
|
|
351
270
|
var to_price = Number(json.oracle_price_to_token);
|
|
352
|
-
e = {
|
|
271
|
+
var e = {
|
|
353
272
|
action: "Swap",
|
|
354
273
|
typeName: name,
|
|
355
274
|
order_id: undefined,
|
|
@@ -360,12 +279,14 @@ function parseUserHistory(raw_events) {
|
|
|
360
279
|
status: "Filled",
|
|
361
280
|
size: Number(json.actual_to_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(to_token)),
|
|
362
281
|
base_token: to_token,
|
|
363
|
-
collateral:
|
|
282
|
+
collateral: Number(json.from_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(from_token)),
|
|
364
283
|
collateral_token: from_token,
|
|
365
284
|
price: from_price / to_price,
|
|
366
285
|
realized_pnl: -Number(json.fee_amount_usd) / Math.pow(10, 9),
|
|
367
286
|
timestamp: timestamp,
|
|
368
287
|
tx_digest: tx_digest,
|
|
288
|
+
dov_index: undefined,
|
|
289
|
+
sender: "user",
|
|
369
290
|
};
|
|
370
291
|
events.push(e);
|
|
371
292
|
break;
|
|
@@ -408,125 +329,186 @@ function getGraphQLEvents(module_1, sender_1) {
|
|
|
408
329
|
});
|
|
409
330
|
});
|
|
410
331
|
}
|
|
411
|
-
function getLiquidateFromSentio(userAddress, startTimestamp,
|
|
332
|
+
function getLiquidateFromSentio(userAddress, startTimestamp, events) {
|
|
412
333
|
return __awaiter(this, void 0, void 0, function () {
|
|
413
|
-
var datas;
|
|
334
|
+
var datas, liquidate;
|
|
414
335
|
return __generator(this, function (_a) {
|
|
415
336
|
switch (_a.label) {
|
|
416
337
|
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("Liquidate", userAddress, startTimestamp.toString())];
|
|
417
338
|
case 1:
|
|
418
339
|
datas = _a.sent();
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
340
|
+
liquidate = datas.map(function (x) {
|
|
341
|
+
var collateral = Number(x.liquidator_fee) + Number(x.value_for_lp_pool);
|
|
342
|
+
var base_token = toToken(x.trading_token);
|
|
343
|
+
var txHistory = {
|
|
344
|
+
action: "Liquidation",
|
|
345
|
+
typeName: "LiquidateEvent",
|
|
346
|
+
order_id: x.order_id,
|
|
347
|
+
position_id: x.position_id,
|
|
348
|
+
market: "".concat(base_token, "/USD"),
|
|
349
|
+
side: undefined,
|
|
350
|
+
order_type: "Market",
|
|
351
|
+
status: "Filled",
|
|
352
|
+
size: Number(x.position_size),
|
|
353
|
+
base_token: base_token,
|
|
354
|
+
collateral: collateral,
|
|
355
|
+
collateral_token: x.collateral_token,
|
|
356
|
+
price: Number(x.trading_price),
|
|
357
|
+
realized_pnl: -collateral * Number(x.collateral_price),
|
|
358
|
+
timestamp: x.timestamp,
|
|
359
|
+
tx_digest: x.transaction_hash,
|
|
360
|
+
dov_index: undefined,
|
|
361
|
+
sender: "cranker",
|
|
362
|
+
};
|
|
363
|
+
return txHistory;
|
|
364
|
+
});
|
|
365
|
+
liquidate = liquidate.map(function (x) {
|
|
366
|
+
var related = events.find(function (e) { return e.position_id == x.position_id && e.market == x.market; });
|
|
367
|
+
// console.log(x);
|
|
368
|
+
// console.log(related);
|
|
369
|
+
if (related) {
|
|
370
|
+
x.side = related.side;
|
|
371
|
+
x.dov_index = related.dov_index;
|
|
372
|
+
}
|
|
373
|
+
return x;
|
|
374
|
+
});
|
|
375
|
+
// console.log(liquidate);
|
|
376
|
+
events = events.concat(liquidate);
|
|
377
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
378
|
+
return [2 /*return*/, events];
|
|
456
379
|
}
|
|
457
380
|
});
|
|
458
381
|
});
|
|
459
382
|
}
|
|
460
|
-
function getOrderMatchFromSentio(userAddress, startTimestamp,
|
|
383
|
+
function getOrderMatchFromSentio(userAddress, startTimestamp, events) {
|
|
461
384
|
return __awaiter(this, void 0, void 0, function () {
|
|
462
|
-
var datas;
|
|
385
|
+
var datas, order_match;
|
|
463
386
|
return __generator(this, function (_a) {
|
|
464
387
|
switch (_a.label) {
|
|
465
388
|
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("OrderFilled", userAddress, startTimestamp.toString())];
|
|
466
389
|
case 1:
|
|
467
390
|
datas = _a.sent();
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
391
|
+
order_match = datas.map(function (x) {
|
|
392
|
+
var base_token = toToken(x.trading_token);
|
|
393
|
+
var txHistory = {
|
|
394
|
+
action: x.order_type == "Open" ? "Order Filled (Open Position)" : "Order Filled (Close Position)",
|
|
395
|
+
typeName: "OrderFilledEvent",
|
|
396
|
+
order_id: x.order_id,
|
|
397
|
+
position_id: x.position_id,
|
|
398
|
+
market: "".concat(base_token, "/USD"),
|
|
399
|
+
side: x.side,
|
|
400
|
+
order_type: undefined,
|
|
401
|
+
status: "Filled",
|
|
402
|
+
size: Number(x.filled_size),
|
|
403
|
+
base_token: base_token,
|
|
404
|
+
collateral: Number(x.realized_amount),
|
|
405
|
+
collateral_token: x.collateral_token,
|
|
406
|
+
price: Number(x.filled_price),
|
|
407
|
+
realized_pnl: Number(x.realized_pnl),
|
|
408
|
+
timestamp: x.timestamp,
|
|
409
|
+
tx_digest: x.transaction_hash,
|
|
410
|
+
dov_index: undefined,
|
|
411
|
+
sender: "cranker",
|
|
412
|
+
};
|
|
413
|
+
return txHistory;
|
|
414
|
+
});
|
|
415
|
+
// deduplicate
|
|
416
|
+
order_match = order_match.filter(function (x) { return events.findIndex(function (y) { return y.tx_digest == x.tx_digest; }) == -1; });
|
|
417
|
+
order_match = order_match.map(function (x) {
|
|
418
|
+
var related = events.findLast(function (e) { return e.order_id == x.order_id && e.market == x.market; });
|
|
419
|
+
// console.log(x, related);
|
|
420
|
+
if (related) {
|
|
421
|
+
x.order_type = related.order_type;
|
|
422
|
+
x.collateral = related.collateral;
|
|
423
|
+
x.dov_index = related.dov_index;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
x.order_type = "Market";
|
|
427
|
+
}
|
|
428
|
+
return x;
|
|
429
|
+
});
|
|
430
|
+
// console.log(order_match);
|
|
431
|
+
events = events.concat(order_match);
|
|
432
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
433
|
+
return [2 /*return*/, events];
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
function getRealizeOptionFromSentio(userAddress, startTimestamp, events) {
|
|
439
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
440
|
+
var datas, exercise, filter_exercise;
|
|
441
|
+
return __generator(this, function (_a) {
|
|
442
|
+
switch (_a.label) {
|
|
443
|
+
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("RealizeOption", userAddress, startTimestamp.toString())];
|
|
444
|
+
case 1:
|
|
445
|
+
datas = _a.sent();
|
|
446
|
+
exercise = datas.map(function (x) {
|
|
447
|
+
var base_token = toToken(x.base_token);
|
|
448
|
+
var txHistory = {
|
|
449
|
+
action: "Exercise Position",
|
|
450
|
+
typeName: "RealizeOptionPositionEvent",
|
|
451
|
+
order_id: undefined,
|
|
452
|
+
position_id: x.position_id,
|
|
453
|
+
market: "".concat(base_token, "/USD"),
|
|
454
|
+
side: undefined,
|
|
455
|
+
order_type: "Market",
|
|
456
|
+
status: "Filled",
|
|
457
|
+
size: undefined,
|
|
458
|
+
base_token: base_token,
|
|
459
|
+
collateral: Number(x.exercise_balance_value),
|
|
460
|
+
collateral_token: x.collateral_token,
|
|
461
|
+
price: undefined,
|
|
462
|
+
realized_pnl: Number(x.user_remaining_in_usd),
|
|
463
|
+
timestamp: x.timestamp,
|
|
464
|
+
tx_digest: x.transaction_hash,
|
|
465
|
+
dov_index: undefined,
|
|
466
|
+
sender: "cranker",
|
|
467
|
+
};
|
|
468
|
+
// console.log(txHistory);
|
|
469
|
+
return txHistory;
|
|
470
|
+
});
|
|
471
|
+
filter_exercise = exercise.reduce(function (acc, x) {
|
|
472
|
+
var _a, _b, _c, _d;
|
|
473
|
+
var related_index = events.findLastIndex(function (e) { return e.position_id == x.position_id && e.market == x.market && e.tx_digest == x.tx_digest; });
|
|
474
|
+
// console.log(x);
|
|
475
|
+
// console.log(related_index);
|
|
476
|
+
if (related_index != -1) {
|
|
477
|
+
var related = events[related_index];
|
|
478
|
+
if (related.sender == "cranker") {
|
|
479
|
+
x.side = related.side;
|
|
480
|
+
x.size = related.size;
|
|
481
|
+
x.dov_index = related.dov_index;
|
|
482
|
+
// add to close event
|
|
483
|
+
related.collateral = Number((_a = related.collateral) !== null && _a !== void 0 ? _a : 0) + Number((_b = x.collateral) !== null && _b !== void 0 ? _b : 0);
|
|
484
|
+
related.realized_pnl = Number((_c = related.realized_pnl) !== null && _c !== void 0 ? _c : 0) + Number((_d = x.realized_pnl) !== null && _d !== void 0 ? _d : 0);
|
|
485
|
+
x.collateral = undefined;
|
|
486
|
+
x.realized_pnl = undefined;
|
|
487
|
+
acc.push(x);
|
|
505
488
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
order_type: related
|
|
514
|
-
? checkOrderType(related.contents.json.filled, related.contents.json.reduce_only, related.contents.json.is_stop_order)
|
|
515
|
-
: undefined,
|
|
516
|
-
status: "Filled",
|
|
517
|
-
size: x.filled_size,
|
|
518
|
-
base_token: x.trading_token,
|
|
519
|
-
collateral: undefined,
|
|
520
|
-
collateral_token: x.collateral_token,
|
|
521
|
-
price: x.filled_price,
|
|
522
|
-
realized_pnl: realized_pnl,
|
|
523
|
-
timestamp: x.timestamp,
|
|
524
|
-
tx_digest: x.transaction_hash,
|
|
525
|
-
};
|
|
526
|
-
return txHistory;
|
|
527
|
-
})];
|
|
489
|
+
}
|
|
490
|
+
return acc;
|
|
491
|
+
}, new Array());
|
|
492
|
+
// console.log(filter_exercise);
|
|
493
|
+
events = events.concat(filter_exercise);
|
|
494
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
495
|
+
return [2 /*return*/, events];
|
|
528
496
|
}
|
|
529
497
|
});
|
|
530
498
|
});
|
|
531
499
|
}
|
|
532
500
|
// getOrderMatchFromSentio("0x95f26ce574fc9ace2608807648d99a4dce17f1be8964613d5b972edc82849e9e", 0);
|
|
501
|
+
// getRealizeOptionFromSentio("0x95f26ce574fc9ace2608807648d99a4dce17f1be8964613d5b972edc82849e9e", 0);
|
|
502
|
+
function toToken(name) {
|
|
503
|
+
switch (name) {
|
|
504
|
+
case "BTC":
|
|
505
|
+
case "ETH":
|
|
506
|
+
case "SOL":
|
|
507
|
+
case "APT":
|
|
508
|
+
return "w".concat(name);
|
|
509
|
+
case "WUSDC":
|
|
510
|
+
return "wUSDC";
|
|
511
|
+
default:
|
|
512
|
+
return name;
|
|
513
|
+
}
|
|
514
|
+
}
|
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.51",
|
|
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",
|