@typus/typus-perp-sdk 1.0.49 → 1.0.50
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 +233 -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,52 @@ 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
|
-
order_type:
|
|
334
|
-
status:
|
|
335
|
-
size: size,
|
|
244
|
+
side: related === null || related === void 0 ? void 0 : related.side,
|
|
245
|
+
order_type: related === null || related === void 0 ? void 0 : related.order_type,
|
|
246
|
+
status: "Filled",
|
|
247
|
+
size: related === null || related === void 0 ? void 0 : related.size,
|
|
336
248
|
base_token: base_token,
|
|
337
249
|
collateral: collateral / Math.pow(10, (0, constants_1.assetToDecimal)(collateral_token)), // WARNING: fixed decimal
|
|
338
250
|
collateral_token: collateral_token,
|
|
339
|
-
price:
|
|
251
|
+
price: related === null || related === void 0 ? void 0 : related.price,
|
|
340
252
|
realized_pnl: undefined,
|
|
341
253
|
timestamp: timestamp,
|
|
342
254
|
tx_digest: tx_digest,
|
|
255
|
+
dov_index: related === null || related === void 0 ? void 0 : related.dov_index,
|
|
256
|
+
sender: "user",
|
|
343
257
|
};
|
|
344
258
|
events.push(e);
|
|
345
|
-
//console.log(e, related, related.contents.json.symbol, base_token, assetToDecimal(base_token));
|
|
346
259
|
break;
|
|
347
260
|
case structs_3.SwapEvent.$typeName.split("::")[2]:
|
|
348
261
|
var from_token = (0, constants_1.typeArgToAsset)(json.from_token_type.name);
|
|
349
262
|
var to_token = (0, constants_1.typeArgToAsset)(json.to_token_type.name);
|
|
350
263
|
var from_price = Number(json.oracle_price_from_token);
|
|
351
264
|
var to_price = Number(json.oracle_price_to_token);
|
|
352
|
-
e = {
|
|
265
|
+
var e = {
|
|
353
266
|
action: "Swap",
|
|
354
267
|
typeName: name,
|
|
355
268
|
order_id: undefined,
|
|
@@ -360,12 +273,14 @@ function parseUserHistory(raw_events) {
|
|
|
360
273
|
status: "Filled",
|
|
361
274
|
size: Number(json.actual_to_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(to_token)),
|
|
362
275
|
base_token: to_token,
|
|
363
|
-
collateral:
|
|
276
|
+
collateral: Number(json.from_amount) / Math.pow(10, (0, constants_1.assetToDecimal)(from_token)),
|
|
364
277
|
collateral_token: from_token,
|
|
365
278
|
price: from_price / to_price,
|
|
366
279
|
realized_pnl: -Number(json.fee_amount_usd) / Math.pow(10, 9),
|
|
367
280
|
timestamp: timestamp,
|
|
368
281
|
tx_digest: tx_digest,
|
|
282
|
+
dov_index: undefined,
|
|
283
|
+
sender: "user",
|
|
369
284
|
};
|
|
370
285
|
events.push(e);
|
|
371
286
|
break;
|
|
@@ -408,125 +323,186 @@ function getGraphQLEvents(module_1, sender_1) {
|
|
|
408
323
|
});
|
|
409
324
|
});
|
|
410
325
|
}
|
|
411
|
-
function getLiquidateFromSentio(userAddress, startTimestamp,
|
|
326
|
+
function getLiquidateFromSentio(userAddress, startTimestamp, events) {
|
|
412
327
|
return __awaiter(this, void 0, void 0, function () {
|
|
413
|
-
var datas;
|
|
328
|
+
var datas, liquidate;
|
|
414
329
|
return __generator(this, function (_a) {
|
|
415
330
|
switch (_a.label) {
|
|
416
331
|
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("Liquidate", userAddress, startTimestamp.toString())];
|
|
417
332
|
case 1:
|
|
418
333
|
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
|
-
|
|
334
|
+
liquidate = datas.map(function (x) {
|
|
335
|
+
var collateral = Number(x.liquidator_fee) + Number(x.value_for_lp_pool);
|
|
336
|
+
var base_token = toToken(x.trading_token);
|
|
337
|
+
var txHistory = {
|
|
338
|
+
action: "Liquidation",
|
|
339
|
+
typeName: "LiquidateEvent",
|
|
340
|
+
order_id: x.order_id,
|
|
341
|
+
position_id: x.position_id,
|
|
342
|
+
market: "".concat(base_token, "/USD"),
|
|
343
|
+
side: undefined,
|
|
344
|
+
order_type: "Market",
|
|
345
|
+
status: "Filled",
|
|
346
|
+
size: Number(x.position_size),
|
|
347
|
+
base_token: base_token,
|
|
348
|
+
collateral: collateral,
|
|
349
|
+
collateral_token: x.collateral_token,
|
|
350
|
+
price: Number(x.trading_price),
|
|
351
|
+
realized_pnl: -collateral * Number(x.collateral_price),
|
|
352
|
+
timestamp: x.timestamp,
|
|
353
|
+
tx_digest: x.transaction_hash,
|
|
354
|
+
dov_index: undefined,
|
|
355
|
+
sender: "cranker",
|
|
356
|
+
};
|
|
357
|
+
return txHistory;
|
|
358
|
+
});
|
|
359
|
+
liquidate = liquidate.map(function (x) {
|
|
360
|
+
var related = events.find(function (e) { return e.position_id == x.position_id && e.market == x.market; });
|
|
361
|
+
// console.log(x);
|
|
362
|
+
// console.log(related);
|
|
363
|
+
if (related) {
|
|
364
|
+
x.side = related.side;
|
|
365
|
+
x.dov_index = related.dov_index;
|
|
366
|
+
}
|
|
367
|
+
return x;
|
|
368
|
+
});
|
|
369
|
+
// console.log(liquidate);
|
|
370
|
+
events = events.concat(liquidate);
|
|
371
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
372
|
+
return [2 /*return*/, events];
|
|
456
373
|
}
|
|
457
374
|
});
|
|
458
375
|
});
|
|
459
376
|
}
|
|
460
|
-
function getOrderMatchFromSentio(userAddress, startTimestamp,
|
|
377
|
+
function getOrderMatchFromSentio(userAddress, startTimestamp, events) {
|
|
461
378
|
return __awaiter(this, void 0, void 0, function () {
|
|
462
|
-
var datas;
|
|
379
|
+
var datas, order_match;
|
|
463
380
|
return __generator(this, function (_a) {
|
|
464
381
|
switch (_a.label) {
|
|
465
382
|
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("OrderFilled", userAddress, startTimestamp.toString())];
|
|
466
383
|
case 1:
|
|
467
384
|
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
|
-
|
|
385
|
+
order_match = datas.map(function (x) {
|
|
386
|
+
var base_token = toToken(x.trading_token);
|
|
387
|
+
var txHistory = {
|
|
388
|
+
action: x.order_type == "Open" ? "Order Filled (Open Position)" : "Order Filled (Close Position)",
|
|
389
|
+
typeName: "OrderFilledEvent",
|
|
390
|
+
order_id: x.order_id,
|
|
391
|
+
position_id: x.position_id,
|
|
392
|
+
market: "".concat(base_token, "/USD"),
|
|
393
|
+
side: x.side,
|
|
394
|
+
order_type: undefined,
|
|
395
|
+
status: "Filled",
|
|
396
|
+
size: Number(x.filled_size),
|
|
397
|
+
base_token: base_token,
|
|
398
|
+
collateral: Number(x.realized_amount),
|
|
399
|
+
collateral_token: x.collateral_token,
|
|
400
|
+
price: Number(x.filled_price),
|
|
401
|
+
realized_pnl: Number(x.realized_pnl),
|
|
402
|
+
timestamp: x.timestamp,
|
|
403
|
+
tx_digest: x.transaction_hash,
|
|
404
|
+
dov_index: undefined,
|
|
405
|
+
sender: "cranker",
|
|
406
|
+
};
|
|
407
|
+
return txHistory;
|
|
408
|
+
});
|
|
409
|
+
// deduplicate
|
|
410
|
+
order_match = order_match.filter(function (x) { return events.findIndex(function (y) { return y.tx_digest == x.tx_digest; }) == -1; });
|
|
411
|
+
order_match = order_match.map(function (x) {
|
|
412
|
+
var related = events.findLast(function (e) { return e.order_id == x.order_id && e.market == x.market; });
|
|
413
|
+
// console.log(x, related);
|
|
414
|
+
if (related) {
|
|
415
|
+
x.order_type = related.order_type;
|
|
416
|
+
x.collateral = related.collateral;
|
|
417
|
+
x.dov_index = related.dov_index;
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
x.order_type = "Market";
|
|
421
|
+
}
|
|
422
|
+
return x;
|
|
423
|
+
});
|
|
424
|
+
// console.log(order_match);
|
|
425
|
+
events = events.concat(order_match);
|
|
426
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
427
|
+
return [2 /*return*/, events];
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
function getRealizeOptionFromSentio(userAddress, startTimestamp, events) {
|
|
433
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
434
|
+
var datas, exercise, filter_exercise;
|
|
435
|
+
return __generator(this, function (_a) {
|
|
436
|
+
switch (_a.label) {
|
|
437
|
+
case 0: return [4 /*yield*/, (0, sentio_1.getFromSentio)("RealizeOption", userAddress, startTimestamp.toString())];
|
|
438
|
+
case 1:
|
|
439
|
+
datas = _a.sent();
|
|
440
|
+
exercise = datas.map(function (x) {
|
|
441
|
+
var base_token = toToken(x.base_token);
|
|
442
|
+
var txHistory = {
|
|
443
|
+
action: "Exercise Position",
|
|
444
|
+
typeName: "RealizeOptionPositionEvent",
|
|
445
|
+
order_id: undefined,
|
|
446
|
+
position_id: x.position_id,
|
|
447
|
+
market: "".concat(base_token, "/USD"),
|
|
448
|
+
side: undefined,
|
|
449
|
+
order_type: "Market",
|
|
450
|
+
status: "Filled",
|
|
451
|
+
size: undefined,
|
|
452
|
+
base_token: base_token,
|
|
453
|
+
collateral: Number(x.exercise_balance_value),
|
|
454
|
+
collateral_token: x.collateral_token,
|
|
455
|
+
price: undefined,
|
|
456
|
+
realized_pnl: Number(x.user_remaining_in_usd),
|
|
457
|
+
timestamp: x.timestamp,
|
|
458
|
+
tx_digest: x.transaction_hash,
|
|
459
|
+
dov_index: undefined,
|
|
460
|
+
sender: "cranker",
|
|
461
|
+
};
|
|
462
|
+
// console.log(txHistory);
|
|
463
|
+
return txHistory;
|
|
464
|
+
});
|
|
465
|
+
filter_exercise = exercise.reduce(function (acc, x) {
|
|
466
|
+
var _a, _b, _c, _d;
|
|
467
|
+
var related_index = events.findLastIndex(function (e) { return e.position_id == x.position_id && e.market == x.market && e.tx_digest == x.tx_digest; });
|
|
468
|
+
// console.log(x);
|
|
469
|
+
// console.log(related_index);
|
|
470
|
+
if (related_index != -1) {
|
|
471
|
+
var related = events[related_index];
|
|
472
|
+
if (related.sender == "cranker") {
|
|
473
|
+
x.side = related.side;
|
|
474
|
+
x.size = related.size;
|
|
475
|
+
x.dov_index = related.dov_index;
|
|
476
|
+
// add to close event
|
|
477
|
+
related.collateral = Number((_a = related.collateral) !== null && _a !== void 0 ? _a : 0) + Number((_b = x.collateral) !== null && _b !== void 0 ? _b : 0);
|
|
478
|
+
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);
|
|
479
|
+
x.collateral = undefined;
|
|
480
|
+
x.realized_pnl = undefined;
|
|
481
|
+
acc.push(x);
|
|
505
482
|
}
|
|
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
|
-
})];
|
|
483
|
+
}
|
|
484
|
+
return acc;
|
|
485
|
+
}, new Array());
|
|
486
|
+
// console.log(filter_exercise);
|
|
487
|
+
events = events.concat(filter_exercise);
|
|
488
|
+
events = events.sort(function (a, b) { return Number(new Date(a.timestamp)) - Number(new Date(b.timestamp)); });
|
|
489
|
+
return [2 /*return*/, events];
|
|
528
490
|
}
|
|
529
491
|
});
|
|
530
492
|
});
|
|
531
493
|
}
|
|
532
494
|
// getOrderMatchFromSentio("0x95f26ce574fc9ace2608807648d99a4dce17f1be8964613d5b972edc82849e9e", 0);
|
|
495
|
+
// getRealizeOptionFromSentio("0x95f26ce574fc9ace2608807648d99a4dce17f1be8964613d5b972edc82849e9e", 0);
|
|
496
|
+
function toToken(name) {
|
|
497
|
+
switch (name) {
|
|
498
|
+
case "BTC":
|
|
499
|
+
case "ETH":
|
|
500
|
+
case "SOL":
|
|
501
|
+
case "APT":
|
|
502
|
+
return "w".concat(name);
|
|
503
|
+
case "WUSDC":
|
|
504
|
+
return "wUSDC";
|
|
505
|
+
default:
|
|
506
|
+
return name;
|
|
507
|
+
}
|
|
508
|
+
}
|
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.50",
|
|
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",
|