@typus/typus-sdk 1.9.1 → 1.9.2

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.
@@ -0,0 +1,652 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
56
+ if (ar || !(i in from)) {
57
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
58
+ ar[i] = from[i];
59
+ }
60
+ }
61
+ return to.concat(ar || Array.prototype.slice.call(from));
62
+ };
63
+ var __values = (this && this.__values) || function(o) {
64
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
65
+ if (m) return m.call(o);
66
+ if (o && typeof o.length === "number") return {
67
+ next: function () {
68
+ if (o && i >= o.length) o = void 0;
69
+ return { value: o && o[i++], done: !o };
70
+ }
71
+ };
72
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
73
+ };
74
+ var __importDefault = (this && this.__importDefault) || function (mod) {
75
+ return (mod && mod.__esModule) ? mod : { "default": mod };
76
+ };
77
+ Object.defineProperty(exports, "__esModule", { value: true });
78
+ exports.getUserBidReceipts = exports.parseBid = exports.parseBidReceipt = exports.parseStrikes = exports.calcEstPnl = exports.calcBreakEvenPrice = exports.calcDeliveryPrice = exports.calcIncentiveRate = exports.IncentiveRateBp = exports.periodOrder = exports.optionTypeOrder = exports.tokenOrder = void 0;
79
+ exports.getUserOwnedObjects = getUserOwnedObjects;
80
+ exports.fetchUserBids = fetchUserBids;
81
+ var vault_1 = require("./vault");
82
+ var typus_dov_single_v2_1 = require("../../../src/typus-dov-single-v2");
83
+ var utils_1 = require("../../../src/utils");
84
+ var auto_bid_1 = require("../../../src/auto-bid");
85
+ var lodash_1 = require("lodash");
86
+ var token_1 = require("./token");
87
+ var constants_1 = require("../../../src/constants");
88
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
89
+ var moment_1 = __importDefault(require("moment"));
90
+ var PriceDecimal = (0, bignumber_js_1.default)(10).pow(8);
91
+ exports.tokenOrder = {
92
+ // Basically it's a to z but put SUI at first
93
+ SUI: 0,
94
+ AFSUI: 1,
95
+ APT: 2,
96
+ WBTC: 3,
97
+ BTC: 3,
98
+ BUCK: 4,
99
+ CETUS: 5,
100
+ WETH: 6,
101
+ ETH: 6,
102
+ FUD: 7,
103
+ INJ: 8,
104
+ JUP: 9,
105
+ NAVX: 10,
106
+ SCA: 11,
107
+ SEI: 12,
108
+ WSOL: 13,
109
+ SOL: 14,
110
+ TURBOS: 15,
111
+ USDC: 16,
112
+ wUSDC: 16,
113
+ USDT: 17,
114
+ USDY: 18,
115
+ VSUI: 20,
116
+ };
117
+ exports.optionTypeOrder = {
118
+ 0: 0,
119
+ 1: 2,
120
+ 2: 3,
121
+ 4: 4,
122
+ 6: 5,
123
+ 5: 6,
124
+ };
125
+ exports.periodOrder = {
126
+ 0: 1,
127
+ 1: 2,
128
+ 2: 3,
129
+ 3: 0,
130
+ };
131
+ exports.IncentiveRateBp = 4;
132
+ var calcIncentiveRate = function (incentiveBp) {
133
+ var incentiveRateBp = (0, bignumber_js_1.default)(incentiveBp).div((0, bignumber_js_1.default)(10).pow(exports.IncentiveRateBp));
134
+ var incentiveRate = 1;
135
+ if (incentiveRateBp.gt(0)) {
136
+ incentiveRate = 1 - Number(incentiveRateBp);
137
+ }
138
+ return incentiveRate;
139
+ };
140
+ exports.calcIncentiveRate = calcIncentiveRate;
141
+ var calcDeliveryPrice = function (bidShare, vaultInfo) {
142
+ var bTokenDecimal = vaultInfo.info.bTokenDecimal;
143
+ var deliveryPrice = (0, bignumber_js_1.default)("0");
144
+ // check if bid has settled already
145
+ if (bidShare.bidVault.u64Padding[1]) {
146
+ deliveryPrice = (0, bignumber_js_1.default)(bidShare.bidVault.u64Padding[1]);
147
+ }
148
+ else {
149
+ var deliveryInfos = vaultInfo.info.deliveryInfos.deliveryInfo;
150
+ var deliveryInfo = deliveryInfos[deliveryInfos.length - 1];
151
+ deliveryPrice = deliveryInfo ? (0, bignumber_js_1.default)(deliveryInfo.deliveryPrice) : (0, bignumber_js_1.default)("0");
152
+ }
153
+ deliveryPrice = (0, bignumber_js_1.default)(deliveryPrice).div((0, bignumber_js_1.default)(10).pow((0, bignumber_js_1.default)(bTokenDecimal)));
154
+ return deliveryPrice;
155
+ };
156
+ exports.calcDeliveryPrice = calcDeliveryPrice;
157
+ var calcBreakEvenPrice = function (optionType, period, strikes, bToken, price, incentive) {
158
+ var breakEvenPrice = 0;
159
+ switch (optionType) {
160
+ case "0":
161
+ if (token_1.StableCoin.includes(bToken)) {
162
+ breakEvenPrice = Number(strikes[0]) + Number(price) * 1.1 * incentive;
163
+ }
164
+ else {
165
+ breakEvenPrice = Number(strikes[0]) / (1 - Number(price) * 1.1 * incentive);
166
+ }
167
+ break;
168
+ case "1":
169
+ if (token_1.StableCoin.includes(bToken)) {
170
+ breakEvenPrice = Number(strikes[0]) - Number(price) * 1.1 * incentive;
171
+ }
172
+ else {
173
+ breakEvenPrice = Number(strikes[0]) / (1 + Number(price) * 1.1 * incentive);
174
+ }
175
+ break;
176
+ case "2":
177
+ // Calculate with the lower one
178
+ if (token_1.StableCoin.includes(bToken)) {
179
+ breakEvenPrice = Number(strikes[0]) + Number(price) * 1.1 * incentive;
180
+ }
181
+ else {
182
+ breakEvenPrice = Number(strikes[0]) / (1 - Number(price) * 1.1 * incentive);
183
+ }
184
+ break;
185
+ case "4":
186
+ // Calculate with the lower one
187
+ if (token_1.StableCoin.includes(bToken)) {
188
+ breakEvenPrice = Number(strikes[0]) + Number(price) * 1.1 * incentive;
189
+ }
190
+ else {
191
+ breakEvenPrice = Number(strikes[0]) / (1 - Number(price) * 1.1 * incentive);
192
+ }
193
+ break;
194
+ case "5":
195
+ if (token_1.StableCoin.includes(bToken)) {
196
+ breakEvenPrice = Number(strikes[1]) - Number(price) * 1.1 * incentive;
197
+ }
198
+ else {
199
+ breakEvenPrice = Number(strikes[1]) / (1 + Number(price) * 1.1 * incentive);
200
+ }
201
+ break;
202
+ case "6":
203
+ // Calculate with the lower one
204
+ if (token_1.StableCoin.includes(bToken)) {
205
+ breakEvenPrice = Number(strikes[0]) + Number(price) * 1.1 * incentive;
206
+ }
207
+ else {
208
+ breakEvenPrice = Number(strikes[0]) / (1 + Number(price) * 1.1 * incentive);
209
+ }
210
+ break;
211
+ }
212
+ return breakEvenPrice;
213
+ };
214
+ exports.calcBreakEvenPrice = calcBreakEvenPrice;
215
+ var calcEstPnl = function (live, incentive, bidSize, optionType, assets, strikes, bidShare, deliveryPrice, oTokenPrice) {
216
+ var profit = "0";
217
+ var cost = "0";
218
+ var _a = __read((0, constants_1.typeArgsToAssets)(assets), 3), dToken = _a[0], bToken = _a[1], oToken = _a[2];
219
+ var dTokenWrappedName = (0, token_1.getTokenName)({ token: dToken, wrapped: true });
220
+ var bTokenWrappedName = (0, token_1.getTokenName)({ token: bToken, wrapped: true });
221
+ var estPnls = [];
222
+ var referencePrice = bidShare.bidVault.u64Padding.length > 0 ? (0, bignumber_js_1.default)(bidShare.bidVault.u64Padding[0]).div(PriceDecimal) : (0, bignumber_js_1.default)(oTokenPrice);
223
+ if (optionType === "0") {
224
+ profit = bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(referencePrice.minus((0, bignumber_js_1.default)(strikes[0])))
225
+ .div(referencePrice)
226
+ .multipliedBy(bidSize)).toString();
227
+ }
228
+ else if (optionType === "1") {
229
+ profit = bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(strikes[0]).minus(referencePrice).multipliedBy(bidSize)).toString();
230
+ }
231
+ else if (optionType === "2" || optionType === "4") {
232
+ profit = bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(referencePrice.minus((0, bignumber_js_1.default)(strikes[0])))
233
+ .div(referencePrice)
234
+ .multipliedBy(bidSize))
235
+ .minus(bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(referencePrice.minus((0, bignumber_js_1.default)(strikes[1])))
236
+ .div(referencePrice)
237
+ .multipliedBy(bidSize)))
238
+ .toString();
239
+ }
240
+ else if (optionType === "5") {
241
+ profit = bignumber_js_1.default.max(0, (0, bignumber_js_1.default)((0, bignumber_js_1.default)(strikes[1]).minus(referencePrice)).multipliedBy(bidSize))
242
+ .minus(bignumber_js_1.default.max(0, (0, bignumber_js_1.default)((0, bignumber_js_1.default)(strikes[0]).minus((0, bignumber_js_1.default)(referencePrice))).multipliedBy(bidSize)))
243
+ .toString();
244
+ }
245
+ else if (optionType === "6") {
246
+ profit = bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(referencePrice.minus(strikes[0])).multipliedBy(bidSize))
247
+ .minus(bignumber_js_1.default.max(0, (0, bignumber_js_1.default)(referencePrice.minus(strikes[1])).multipliedBy(bidSize)))
248
+ .toString();
249
+ }
250
+ cost = (0, bignumber_js_1.default)(deliveryPrice).multipliedBy(bidSize).multipliedBy(1.1).multipliedBy(incentive).toString();
251
+ if (dToken === bToken) {
252
+ profit = (0, bignumber_js_1.default)(profit).minus(cost).toString();
253
+ }
254
+ if (!live) {
255
+ estPnls.push({
256
+ value: profit,
257
+ token: assets[0],
258
+ });
259
+ if (dToken !== bToken) {
260
+ estPnls.push({ value: "-" + cost, token: assets[1] });
261
+ }
262
+ }
263
+ return estPnls;
264
+ };
265
+ exports.calcEstPnl = calcEstPnl;
266
+ var parseStrikes = function (period, optionType, metadata) {
267
+ var strikes = [period === "3" ? metadata.split("-")[3] : metadata.split("-")[2]];
268
+ switch (optionType) {
269
+ case "0":
270
+ break;
271
+ case "1":
272
+ break;
273
+ case "2":
274
+ strikes = [metadata.split("-")[2], metadata.split("-")[3]];
275
+ break;
276
+ case "4":
277
+ strikes = [metadata.split("-")[2], metadata.split("-")[3]];
278
+ if (period == "3") {
279
+ strikes = [metadata.split("-")[3], metadata.split("-")[4]];
280
+ }
281
+ strikes = strikes.sort(function (a, b) { return Number(a) - Number(b); });
282
+ break;
283
+ case "5":
284
+ strikes = [metadata.split("-")[2], metadata.split("-")[3]];
285
+ if (period == "3") {
286
+ strikes = [metadata.split("-")[3], metadata.split("-")[4]];
287
+ }
288
+ strikes = strikes.sort(function (a, b) { return Number(a) - Number(b); });
289
+ break;
290
+ case "6":
291
+ strikes = [metadata.split("-")[2], metadata.split("-")[3]];
292
+ if (period == "3") {
293
+ strikes = [metadata.split("-")[3], metadata.split("-")[4]];
294
+ }
295
+ strikes = strikes.sort(function (a, b) { return Number(a) - Number(b); });
296
+ break;
297
+ }
298
+ strikes = (0, utils_1.checkNumber)(strikes[0]) ? strikes : [];
299
+ strikes = strikes.sort(function (a, b) { return Number(a) - Number(b); });
300
+ return strikes;
301
+ };
302
+ exports.parseStrikes = parseStrikes;
303
+ var parseBidReceipt = function (vaults, bidReceipts) {
304
+ var sortedBidReceipts = [];
305
+ var bidVaultsInfo = [];
306
+ var vidMap = new Map();
307
+ vaults.forEach(function (v) {
308
+ var curReceipts = bidReceipts[v.info.index];
309
+ if (curReceipts) {
310
+ curReceipts.map(function (receipt) {
311
+ // ###### NOTE: #####
312
+ // Bid receipts need to be sorted and put together
313
+ // BY vid for SDK getMyBids to fetch the correct data.
314
+ // ###########
315
+ // Filer out autoBidReceiptsData receipts cuz it's wrapped in strategy and
316
+ // SDK getMyBids can't retrieve it!
317
+ var data = vidMap.get(receipt.vid);
318
+ if (!data) {
319
+ vidMap.set(receipt.vid, [receipt.id]);
320
+ }
321
+ else {
322
+ data.push(receipt.id);
323
+ vidMap.set(receipt.vid, data);
324
+ }
325
+ bidVaultsInfo.push({ vaultInfo: v, receipt: receipt });
326
+ });
327
+ var values = Array.from(vidMap.values());
328
+ sortedBidReceipts = values.reduce(function (previousValue, currentValue, currentIndex, array) {
329
+ return previousValue.concat(currentValue);
330
+ }, []);
331
+ }
332
+ });
333
+ return { sortedBidReceipts: sortedBidReceipts, bidVaultsInfo: bidVaultsInfo };
334
+ };
335
+ exports.parseBidReceipt = parseBidReceipt;
336
+ var parseBid = function (bidVaultInfo, bidShare, auction, oTokenPrice, isAutoBid) {
337
+ var _a, _b;
338
+ var vaultInfo = bidVaultInfo.vaultInfo, _c = bidVaultInfo.vaultInfo, info = _c.info, _d = _c.info, index = _d.index, bTokenDecimal = _d.bTokenDecimal, oTokenDecimal = _d.oTokenDecimal, optionType = _d.optionType, period = _d.period, depositToken = _d.depositToken, bidToken = _d.bidToken, settlementBase = _d.settlementBase, _e = _c.config, lotSize = _e.bidLotSize, bidIncentiveBp = _e.bidIncentiveBp, u64Padding = _e.u64Padding, receipt = bidVaultInfo.receipt;
339
+ var incentiveRate = (0, exports.calcIncentiveRate)(bidIncentiveBp);
340
+ var _f = __read((0, vault_1.parseAssets)(info), 3), dToken = _f[0], bToken = _f[1], oToken = _f[2];
341
+ var oTokenName = (0, token_1.getTokenName)({ token: oToken });
342
+ var dTokenWrappedName = (0, token_1.getTokenName)({ token: dToken, wrapped: true });
343
+ var bTokenWrappedName = (0, token_1.getTokenName)({ token: bToken, wrapped: true });
344
+ var metadata = bidShare.bidVault.metadata;
345
+ var tokenLabel = metadata.split("-")[0];
346
+ var periodLabel = vault_1.Period[Number(period)].charAt(0).toUpperCase() + vault_1.Period[Number(period)].slice(1);
347
+ var optionTypeLabel = vault_1.AbbrStrategyName[optionType];
348
+ var bidsSize = Number(bidShare.share) / Math.pow(10, Number(oTokenDecimal));
349
+ var expirationDate = (0, moment_1.default)(metadata.split("-")[1], "DDMMMYY").format("yyyy-MM-DD");
350
+ expirationDate = moment_1.default
351
+ .utc(period === "3" ? "".concat(expirationDate, " ").concat((0, utils_1.insertAt)(metadata.split("-")[2], ":", 2)) : "".concat(expirationDate, " 08:00"))
352
+ .local()
353
+ .format("DD MMM YY, HH:mm");
354
+ var live = !auction
355
+ ? false
356
+ : moment_1.default.unix(Number(auction.endTsMs) / 1000).isAfter((0, moment_1.default)()) &&
357
+ moment_1.default.unix(Number(auction.startTsMs) / 1000).isBefore((0, moment_1.default)()) &&
358
+ (0, moment_1.default)(expirationDate, "DD MMM YY, HH:mm").isAfter(moment_1.default.unix(Number(auction.endTsMs) / 1000));
359
+ var deliveryPrice = (0, exports.calcDeliveryPrice)(bidShare, vaultInfo);
360
+ var initialPrice = (_a = auction === null || auction === void 0 ? void 0 : auction.initialPrice) !== null && _a !== void 0 ? _a : 0;
361
+ initialPrice = (0, bignumber_js_1.default)(initialPrice).div((0, bignumber_js_1.default)(10).pow((0, bignumber_js_1.default)(bTokenDecimal)));
362
+ var strikes = (0, exports.parseStrikes)(period, optionType, metadata);
363
+ var breakEvenPriceReference = live ? initialPrice : deliveryPrice;
364
+ var breakEvenPrice = (0, exports.calcBreakEvenPrice)(optionType, period, strikes, bToken, breakEvenPriceReference, incentiveRate);
365
+ var defaultMinBidSize = (0, bignumber_js_1.default)(lotSize).div((0, bignumber_js_1.default)(10).pow(oTokenDecimal));
366
+ var bidSize = bidsSize.toFixed((0, utils_1.countFloating)(defaultMinBidSize.toNumber()));
367
+ var settlePrice = (0, bignumber_js_1.default)((_b = bidShare.bidVault.u64Padding[0]) !== null && _b !== void 0 ? _b : "0").div(PriceDecimal);
368
+ var estPnls = (0, exports.calcEstPnl)(live, incentiveRate, bidSize, optionType, [depositToken, bidToken, settlementBase], strikes, bidShare, deliveryPrice.toString(), oTokenPrice);
369
+ return {
370
+ vaultIndex: index,
371
+ auctionName: "".concat(tokenLabel, " ").concat(periodLabel, " ").concat(optionTypeLabel),
372
+ expiry: expirationDate,
373
+ strikes: strikes.map(function (s) {
374
+ if (tokenLabel === "MFUD") {
375
+ return (0, bignumber_js_1.default)(s).div(1000000).toString();
376
+ }
377
+ return s;
378
+ }),
379
+ bidSize: {
380
+ value: (0, bignumber_js_1.default)(bidSize)
381
+ .div((0, bignumber_js_1.default)(10).pow(oToken === "MFUD" ? 6 : 0))
382
+ .toString(),
383
+ token: settlementBase,
384
+ },
385
+ breakEvenPrice: (0, bignumber_js_1.default)(breakEvenPrice)
386
+ .div((0, bignumber_js_1.default)(10).pow(oToken === "MFUD" ? 6 : 0))
387
+ .toString(),
388
+ settlePrice: (0, bignumber_js_1.default)(settlePrice)
389
+ .div((0, bignumber_js_1.default)(10).pow(oToken === "MFUD" ? 6 : 0))
390
+ .toString(),
391
+ estPnls: estPnls,
392
+ isAutoBid: isAutoBid,
393
+ receiptsId: [receipt.id],
394
+ receiptsVid: [receipt.vid],
395
+ optionTypeOrder: exports.optionTypeOrder[optionType],
396
+ periodOrder: exports.periodOrder[period],
397
+ tokenOrder: exports.tokenOrder[oToken.toUpperCase()],
398
+ };
399
+ };
400
+ exports.parseBid = parseBid;
401
+ function getUserOwnedObjects(config, user) {
402
+ return __awaiter(this, void 0, void 0, function () {
403
+ var provider, result, hasNextPage, data, cursor;
404
+ return __generator(this, function (_a) {
405
+ switch (_a.label) {
406
+ case 0:
407
+ provider = config.gRpcClient();
408
+ return [4 /*yield*/, provider.listOwnedObjects({ owner: user, include: { content: true } })];
409
+ case 1:
410
+ result = _a.sent();
411
+ hasNextPage = result.hasNextPage;
412
+ data = result.objects;
413
+ cursor = result.cursor;
414
+ _a.label = 2;
415
+ case 2:
416
+ if (!hasNextPage) return [3 /*break*/, 4];
417
+ return [4 /*yield*/, provider.listOwnedObjects({
418
+ owner: user,
419
+ cursor: cursor,
420
+ include: { content: true },
421
+ })];
422
+ case 3:
423
+ result = _a.sent();
424
+ data = __spreadArray(__spreadArray([], __read(data), false), __read(result.objects), false);
425
+ hasNextPage = result.hasNextPage;
426
+ cursor = result.cursor;
427
+ return [3 /*break*/, 2];
428
+ case 4: return [2 /*return*/, data];
429
+ }
430
+ });
431
+ });
432
+ }
433
+ var getUserBidReceipts = function (config, data) { return __awaiter(void 0, void 0, void 0, function () {
434
+ var bidReceipts, data_1, data_1_1, object, content, typeStringComponents, subtype, typeComponents, type, typePackage, vaultIndex, receipt;
435
+ var e_1, _a;
436
+ return __generator(this, function (_b) {
437
+ bidReceipts = {};
438
+ if (data.length === 0) {
439
+ return [2 /*return*/, bidReceipts];
440
+ }
441
+ try {
442
+ for (data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
443
+ object = data_1_1.value;
444
+ try {
445
+ content = (object.data || {}).content;
446
+ if (!content)
447
+ continue;
448
+ typeStringComponents = (content.type || "").split("<");
449
+ subtype = (typeStringComponents[1] || "").replace(/>/, "");
450
+ typeComponents = typeStringComponents[0].split("::");
451
+ type = typeComponents[typeComponents.length - 1];
452
+ typePackage = typeComponents[0];
453
+ if (type === "TypusBidReceipt" && config.packageOrigin.framework == typePackage) {
454
+ vaultIndex = content.fields.index;
455
+ receipt = {
456
+ // @ts-ignore
457
+ id: content.fields.id.id,
458
+ // @ts-ignore
459
+ index: vaultIndex,
460
+ // @ts-ignore
461
+ name: content.fields.name,
462
+ // @ts-ignore
463
+ description: content.fields.description,
464
+ // @ts-ignore
465
+ vid: content.fields.vid,
466
+ // @ts-ignore
467
+ metadata: content.fields.metadata,
468
+ };
469
+ bidReceipts[vaultIndex] || (bidReceipts[vaultIndex] = []);
470
+ bidReceipts[vaultIndex].push(receipt);
471
+ }
472
+ }
473
+ catch (e) {
474
+ console.log("Error retrieving object", object, e);
475
+ }
476
+ }
477
+ }
478
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
479
+ finally {
480
+ try {
481
+ if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
482
+ }
483
+ finally { if (e_1) throw e_1.error; }
484
+ }
485
+ return [2 /*return*/, bidReceipts];
486
+ });
487
+ }); };
488
+ exports.getUserBidReceipts = getUserBidReceipts;
489
+ /**
490
+ * Fetch user's bids info
491
+ *
492
+ * @param provider - Sui Client instance.
493
+ * @param network - network type in lowercase.
494
+ * @param packageAddress - Typus main package address.
495
+ * @param framworkAddress - Typus framwork package address.
496
+ * @param originFramworkAddress - Typus intial framwork package address.
497
+ * @param registryAddress - Typus registry package address.
498
+ * @param strategyPoolAddress - strategy pool package address.
499
+ * @param user - user's wallet address.
500
+ * @param prices - tokens prices (usd pair on Pyth)
501
+ * @return User Bids.
502
+ */
503
+ function fetchUserBids(config, user, vaultsInfo, userReceipts) {
504
+ return __awaiter(this, void 0, void 0, function () {
505
+ var userStrategies, auctions, prices, _a, sortedBidReceipts, bidVaultsInfo, bidShares, bidsFromBidShares, _loop_1, bidVaultsInfo_1, bidVaultsInfo_1_1, bidVaultInfo, autoBidsShares, vaultAutoBidReceipts, userStrategies_1, userStrategies_1_1, strategy, receipts, receipts_1, receipts_1_1, receiptItem, vaultIndex, receipt, autoBidVaultInfos, bidsFromStrategies, _loop_2, autoBidVaultInfos_1, autoBidVaultInfos_1_1, autoBidVaultInfo, byOrdered;
506
+ var e_2, _b, e_3, _c, e_4, _d, e_5, _e;
507
+ return __generator(this, function (_f) {
508
+ switch (_f.label) {
509
+ case 0: return [4 /*yield*/, (0, auto_bid_1.getUserStrategies)(config, { user: user })];
510
+ case 1:
511
+ userStrategies = _f.sent();
512
+ return [4 /*yield*/, (0, typus_dov_single_v2_1.getAuctions)(config, { indexes: [] })];
513
+ case 2:
514
+ auctions = _f.sent();
515
+ return [4 /*yield*/, (0, utils_1.getLatestPriceUSD)()];
516
+ case 3:
517
+ prices = _f.sent();
518
+ _a = (0, exports.parseBidReceipt)(Object.values(vaultsInfo), userReceipts), sortedBidReceipts = _a.sortedBidReceipts, bidVaultsInfo = _a.bidVaultsInfo;
519
+ return [4 /*yield*/, (0, typus_dov_single_v2_1.getMyBids)(config, { receipts: sortedBidReceipts })];
520
+ case 4:
521
+ bidShares = _f.sent();
522
+ bidsFromBidShares = [];
523
+ _loop_1 = function (bidVaultInfo) {
524
+ var bidShare = bidShares["".concat(bidVaultInfo.receipt.index, "-").concat(bidVaultInfo.receipt.vid)];
525
+ var auction = auctions ? auctions[bidVaultInfo.vaultInfo.info.index] : null;
526
+ var _g = __read((0, vault_1.parseAssets)(bidVaultInfo.vaultInfo.info), 3), dToken = _g[0], bToken = _g[1], oToken = _g[2];
527
+ if (bidShare) {
528
+ var price = prices[oToken.toUpperCase()].toString();
529
+ var data = (0, exports.parseBid)(bidVaultInfo, bidShare, auction, price, false);
530
+ var checkExistVault = bidsFromBidShares.find(function (p) { return p.vaultIndex === bidVaultInfo.vaultInfo.info.index && p.receiptsVid.includes(bidVaultInfo.receipt.vid); });
531
+ if (checkExistVault) {
532
+ // Merge the bid receipts into the same vault
533
+ checkExistVault.receiptsId = __spreadArray(__spreadArray([], __read(checkExistVault.receiptsId), false), __read(data.receiptsId), false);
534
+ }
535
+ else {
536
+ bidsFromBidShares.push(data);
537
+ }
538
+ }
539
+ };
540
+ try {
541
+ for (bidVaultsInfo_1 = __values(bidVaultsInfo), bidVaultsInfo_1_1 = bidVaultsInfo_1.next(); !bidVaultsInfo_1_1.done; bidVaultsInfo_1_1 = bidVaultsInfo_1.next()) {
542
+ bidVaultInfo = bidVaultsInfo_1_1.value;
543
+ _loop_1(bidVaultInfo);
544
+ }
545
+ }
546
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
547
+ finally {
548
+ try {
549
+ if (bidVaultsInfo_1_1 && !bidVaultsInfo_1_1.done && (_b = bidVaultsInfo_1.return)) _b.call(bidVaultsInfo_1);
550
+ }
551
+ finally { if (e_2) throw e_2.error; }
552
+ }
553
+ autoBidsShares = {};
554
+ vaultAutoBidReceipts = {};
555
+ try {
556
+ for (userStrategies_1 = __values(userStrategies), userStrategies_1_1 = userStrategies_1.next(); !userStrategies_1_1.done; userStrategies_1_1 = userStrategies_1.next()) {
557
+ strategy = userStrategies_1_1.value;
558
+ receipts = strategy.receipts;
559
+ try {
560
+ for (receipts_1 = (e_4 = void 0, __values(receipts)), receipts_1_1 = receipts_1.next(); !receipts_1_1.done; receipts_1_1 = receipts_1.next()) {
561
+ receiptItem = receipts_1_1.value;
562
+ vaultIndex = receiptItem.index;
563
+ receipt = {
564
+ id: receiptItem.id,
565
+ index: vaultIndex,
566
+ name: "",
567
+ description: "",
568
+ vid: receiptItem.vid,
569
+ metadata: receiptItem.metadata,
570
+ };
571
+ if (vaultAutoBidReceipts[vaultIndex]) {
572
+ vaultAutoBidReceipts[vaultIndex] = __spreadArray(__spreadArray([], __read(vaultAutoBidReceipts[vaultIndex]), false), [receipt], false);
573
+ }
574
+ else {
575
+ vaultAutoBidReceipts[vaultIndex] = [receipt];
576
+ }
577
+ if (autoBidsShares["".concat(vaultIndex, "-").concat(receiptItem.vid)]) {
578
+ autoBidsShares["".concat(vaultIndex, "-").concat(receiptItem.vid)].share = (Number(autoBidsShares["".concat(vaultIndex, "-").concat(receiptItem.vid)].share) +
579
+ Number(strategy.my_bids["".concat(vaultIndex, "-").concat(receiptItem.vid)].share)).toString();
580
+ }
581
+ else {
582
+ autoBidsShares["".concat(vaultIndex, "-").concat(receiptItem.vid)] = {
583
+ bidVault: strategy.my_bids["".concat(vaultIndex, "-").concat(receiptItem.vid)].bidVault,
584
+ share: strategy.my_bids["".concat(vaultIndex, "-").concat(receiptItem.vid)].share,
585
+ };
586
+ }
587
+ }
588
+ }
589
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
590
+ finally {
591
+ try {
592
+ if (receipts_1_1 && !receipts_1_1.done && (_d = receipts_1.return)) _d.call(receipts_1);
593
+ }
594
+ finally { if (e_4) throw e_4.error; }
595
+ }
596
+ }
597
+ }
598
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
599
+ finally {
600
+ try {
601
+ if (userStrategies_1_1 && !userStrategies_1_1.done && (_c = userStrategies_1.return)) _c.call(userStrategies_1);
602
+ }
603
+ finally { if (e_3) throw e_3.error; }
604
+ }
605
+ autoBidVaultInfos = [];
606
+ Object.values(vaultsInfo).forEach(function (v) {
607
+ var curReceipts = vaultAutoBidReceipts[v.info.index];
608
+ if (curReceipts) {
609
+ curReceipts.map(function (item) {
610
+ autoBidVaultInfos.push({ vaultInfo: v, receipt: item });
611
+ });
612
+ }
613
+ });
614
+ bidsFromStrategies = [];
615
+ _loop_2 = function (autoBidVaultInfo) {
616
+ var bidShare = autoBidsShares["".concat(autoBidVaultInfo.receipt.index, "-").concat(autoBidVaultInfo.receipt.vid)];
617
+ var auction = auctions ? auctions[autoBidVaultInfo.vaultInfo.info.index] : null;
618
+ var _h = __read((0, vault_1.parseAssets)(autoBidVaultInfo.vaultInfo.info), 3), dToken = _h[0], bToken = _h[1], oToken = _h[2];
619
+ if (bidShare) {
620
+ var price = (0, bignumber_js_1.default)(prices[oToken.toLowerCase()].price)
621
+ .div((0, bignumber_js_1.default)(10).pow(prices[oToken.toLowerCase()].decimal))
622
+ .toString();
623
+ var data = (0, exports.parseBid)(autoBidVaultInfo, bidShare, auction, price, false);
624
+ var checkExistVault = bidsFromStrategies.find(function (p) { return p.vaultIndex === autoBidVaultInfo.vaultInfo.info.index && p.receiptsVid.includes(autoBidVaultInfo.receipt.vid); });
625
+ if (checkExistVault) {
626
+ // Merge the bid receipts into the same vault
627
+ checkExistVault.receiptsId = __spreadArray(__spreadArray([], __read(checkExistVault.receiptsId), false), __read(data.receiptsId), false);
628
+ }
629
+ else {
630
+ bidsFromStrategies.push(data);
631
+ }
632
+ }
633
+ };
634
+ try {
635
+ for (autoBidVaultInfos_1 = __values(autoBidVaultInfos), autoBidVaultInfos_1_1 = autoBidVaultInfos_1.next(); !autoBidVaultInfos_1_1.done; autoBidVaultInfos_1_1 = autoBidVaultInfos_1.next()) {
636
+ autoBidVaultInfo = autoBidVaultInfos_1_1.value;
637
+ _loop_2(autoBidVaultInfo);
638
+ }
639
+ }
640
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
641
+ finally {
642
+ try {
643
+ if (autoBidVaultInfos_1_1 && !autoBidVaultInfos_1_1.done && (_e = autoBidVaultInfos_1.return)) _e.call(autoBidVaultInfos_1);
644
+ }
645
+ finally { if (e_5) throw e_5.error; }
646
+ }
647
+ byOrdered = (0, lodash_1.orderBy)(__spreadArray(__spreadArray([], __read(bidsFromBidShares), false), __read(bidsFromStrategies), false), ["expiry", "tokenOrder", "periodOrder", "optionTypeOrder"], ["asc", "asc", "asc", "asc"]);
648
+ return [2 /*return*/, byOrdered];
649
+ }
650
+ });
651
+ });
652
+ }
@@ -1,2 +1,3 @@
1
+ export * from "./bidding";
1
2
  export * from "./token";
2
3
  export * from "./vault";
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./bidding"), exports);
17
18
  __exportStar(require("./token"), exports);
18
19
  __exportStar(require("./vault"), exports);
@@ -224,6 +224,3 @@ export declare function getRefundShares(config: TypusConfig, input: {
224
224
  }): Promise<{
225
225
  [key: string]: string;
226
226
  }>;
227
- export declare function getUserOwnedObjects(config: TypusConfig, user: string): Promise<import("@mysten/sui/client", { with: { "resolution-mode": "import" } }).SuiClientTypes.Object<{
228
- content: true;
229
- }>[]>;