aftermath-ts-sdk 1.3.12-perps.20 → 1.3.12-perps.21

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.
Files changed (27) hide show
  1. package/dist/general/types/generalTypes.d.ts +3 -0
  2. package/dist/general/types/generalTypes.d.ts.map +1 -1
  3. package/dist/general/utils/caller.d.ts +14 -2
  4. package/dist/general/utils/caller.d.ts.map +1 -1
  5. package/dist/general/utils/caller.js +25 -0
  6. package/dist/packages/perpetuals/api/perpetualsApi.js +2 -2
  7. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +2 -6
  8. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
  9. package/dist/packages/perpetuals/api/perpetualsApiCasting.js +74 -66
  10. package/dist/packages/perpetuals/index.d.ts +1 -0
  11. package/dist/packages/perpetuals/index.d.ts.map +1 -1
  12. package/dist/packages/perpetuals/index.js +1 -0
  13. package/dist/packages/perpetuals/perpetuals.d.ts +50 -14
  14. package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
  15. package/dist/packages/perpetuals/perpetuals.js +73 -45
  16. package/dist/packages/perpetuals/perpetualsAccount.d.ts +54 -21
  17. package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
  18. package/dist/packages/perpetuals/perpetualsAccount.js +227 -111
  19. package/dist/packages/perpetuals/perpetualsMarket.d.ts +14 -15
  20. package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
  21. package/dist/packages/perpetuals/perpetualsMarket.js +45 -57
  22. package/dist/packages/perpetuals/perpetualsTypes.d.ts +324 -150
  23. package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
  24. package/dist/packages/perpetuals/perpetualsVault.d.ts +109 -0
  25. package/dist/packages/perpetuals/perpetualsVault.d.ts.map +1 -0
  26. package/dist/packages/perpetuals/perpetualsVault.js +290 -0
  27. package/package.json +1 -1
@@ -23,7 +23,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.PerpetualsAccount = void 0;
24
24
  const caller_1 = require("../../general/utils/caller");
25
25
  const types_1 = require("../../types");
26
- const iFixedUtils_1 = require("../../general/utils/iFixedUtils");
27
26
  const utils_1 = require("../../general/utils");
28
27
  const perpetuals_1 = require("./perpetuals");
29
28
  const __1 = require("..");
@@ -34,10 +33,19 @@ class PerpetualsAccount extends caller_1.Caller {
34
33
  // Constructor
35
34
  // =========================================================================
36
35
  constructor(account, accountCap, config, Provider) {
36
+ const vaultId = "vaultId" in accountCap ? accountCap.vaultId : undefined;
37
37
  super(config, "perpetuals");
38
38
  this.account = account;
39
39
  this.accountCap = accountCap;
40
40
  this.Provider = Provider;
41
+ // public async getOwnedWithdrawRequests() {
42
+ // return new Perpetuals(
43
+ // this.config,
44
+ // this.Provider
45
+ // ).getOwnedWithdrawRequests({
46
+ // walletAddress: this.ownerAddress(),
47
+ // });
48
+ // }
41
49
  // =========================================================================
42
50
  // Calculations
43
51
  // =========================================================================
@@ -47,8 +55,7 @@ class PerpetualsAccount extends caller_1.Caller {
47
55
  const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
48
56
  const funding = this.calcUnrealizedFundingsForPosition(inputs);
49
57
  const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
50
- let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
51
- inputs.collateralPrice;
58
+ let collateralUsd = position.collateral * inputs.collateralPrice;
52
59
  collateralUsd += funding;
53
60
  let cappedMargin;
54
61
  if (pnl < 0) {
@@ -72,9 +79,7 @@ class PerpetualsAccount extends caller_1.Caller {
72
79
  market,
73
80
  position,
74
81
  });
75
- const collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position === null || position === void 0 ? void 0 : position.collateral) *
76
- collateralPrice +
77
- funding;
82
+ const collateralUsd = (position === null || position === void 0 ? void 0 : position.collateral) * collateralPrice + funding;
78
83
  const { pnl, netAbsBaseValue } = this.calcPnLAndMarginForPosition({
79
84
  market,
80
85
  indexPrice,
@@ -100,16 +105,16 @@ class PerpetualsAccount extends caller_1.Caller {
100
105
  var _a, _b;
101
106
  const marketId = inputs.market.marketId;
102
107
  const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
103
- const baseAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
108
+ const baseAmount = position.baseAssetAmount;
104
109
  const isLong = Math.sign(baseAmount);
105
110
  if (isLong < 0) {
106
- const fundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateShort);
107
- const marketFundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateShort);
111
+ const fundingShort = position.cumFundingRateShort;
112
+ const marketFundingShort = inputs.market.marketState.cumFundingRateShort;
108
113
  return -baseAmount * (marketFundingShort - fundingShort);
109
114
  }
110
115
  else {
111
- const fundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateLong);
112
- const marketFundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateLong);
116
+ const fundingLong = position.cumFundingRateLong;
117
+ const marketFundingLong = inputs.market.marketState.cumFundingRateLong;
113
118
  return -baseAmount * (marketFundingLong - fundingLong);
114
119
  }
115
120
  };
@@ -119,11 +124,11 @@ class PerpetualsAccount extends caller_1.Caller {
119
124
  const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
120
125
  const marginRatioInitial = 1 / (position.leverage || 1);
121
126
  // const marginRatioInitial = inputs.market.initialMarginRatio();
122
- const marginRatioMaintenance = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
123
- const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
124
- const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
125
- const bidsQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.bidsQuantity);
126
- const asksQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.asksQuantity);
127
+ const marginRatioMaintenance = inputs.market.marketParams.marginRatioMaintenance;
128
+ const baseAssetAmount = position.baseAssetAmount;
129
+ const quoteAssetAmount = position.quoteAssetNotionalAmount;
130
+ const bidsQuantity = position.bidsQuantity;
131
+ const asksQuantity = position.asksQuantity;
127
132
  const pnl = baseAssetAmount * inputs.indexPrice - quoteAssetAmount;
128
133
  const netAbs = Math.max(Math.abs(baseAssetAmount + bidsQuantity), Math.abs(baseAssetAmount - asksQuantity));
129
134
  const netAbsBaseValue = netAbs * inputs.indexPrice;
@@ -136,15 +141,14 @@ class PerpetualsAccount extends caller_1.Caller {
136
141
  const marketId = inputs.market.marketId;
137
142
  const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
138
143
  const funding = this.calcUnrealizedFundingsForPosition(inputs);
139
- const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
140
- const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
141
- const numerator = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
142
- inputs.collateralPrice +
144
+ const baseAssetAmount = position.baseAssetAmount;
145
+ const quoteAssetAmount = position.quoteAssetNotionalAmount;
146
+ const numerator = position.collateral * inputs.collateralPrice +
143
147
  funding -
144
148
  quoteAssetAmount;
145
- const MMR = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
146
- const bidsQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.bidsQuantity);
147
- const asksQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.asksQuantity);
149
+ const MMR = inputs.market.marketParams.marginRatioMaintenance;
150
+ const bidsQuantity = position.bidsQuantity;
151
+ const asksQuantity = position.asksQuantity;
148
152
  const netAbs = Math.max(Math.abs(baseAssetAmount + bidsQuantity), Math.abs(baseAssetAmount - asksQuantity));
149
153
  const denominator = netAbs * MMR - baseAssetAmount;
150
154
  if (!denominator)
@@ -158,8 +162,7 @@ class PerpetualsAccount extends caller_1.Caller {
158
162
  const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
159
163
  const totalFunding = this.calcUnrealizedFundingsForPosition(inputs);
160
164
  const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
161
- let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
162
- inputs.collateralPrice;
165
+ let collateralUsd = position.collateral * inputs.collateralPrice;
163
166
  const margin = collateralUsd + totalFunding + pnl;
164
167
  if (margin >= minInitialMargin) {
165
168
  return margin - minInitialMargin;
@@ -186,8 +189,7 @@ class PerpetualsAccount extends caller_1.Caller {
186
189
  indexPrice,
187
190
  position,
188
191
  });
189
- let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
190
- inputs.collateralPrice;
192
+ let collateralUsd = position.collateral * inputs.collateralPrice;
191
193
  totalPnL += pnl;
192
194
  totalFunding += funding;
193
195
  totalCollateralAllocated += collateralUsd;
@@ -214,8 +216,7 @@ class PerpetualsAccount extends caller_1.Caller {
214
216
  (1 -
215
217
  PerpetualsAccount.constants
216
218
  .closePositionMarginOfError))), BigInt(0)) * BigInt(-1);
217
- const positionSize = BigInt(Math.round(Math.abs(utils_1.Casting.IFixed.numberFromIFixed(position.baseAssetAmount) /
218
- market.lotSize())));
219
+ const positionSize = BigInt(Math.round(Math.abs(position.baseAssetAmount / market.lotSize())));
219
220
  // NOTE: is this safe / correct ?
220
221
  const collateralChange = __1.Coin.balanceWithDecimals(BigInt(Math.round(Number(fullPositionCollateralChange) *
221
222
  (Number(size) / Number(positionSize)))), this.collateralDecimals());
@@ -233,6 +234,7 @@ class PerpetualsAccount extends caller_1.Caller {
233
234
  reduceOnly: true,
234
235
  };
235
236
  };
237
+ this.vaultId = vaultId;
236
238
  }
237
239
  // =========================================================================
238
240
  // Transactions
@@ -244,7 +246,17 @@ class PerpetualsAccount extends caller_1.Caller {
244
246
  return __awaiter(this, void 0, void 0, function* () {
245
247
  var _a;
246
248
  const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
247
- return this.fetchApiTransaction("transactions/deposit-collateral", Object.assign(Object.assign({}, otherInputs), { walletAddress: this.accountCap.walletAddress, collateralCoinType: this.accountCap.collateralCoinType, accountObjectId: this.accountCap.objectId, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
249
+ // TODO: add vault support
250
+ if (this.vaultId)
251
+ throw new Error("this function is not supported for vaults");
252
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
253
+ "transactions/deposit-collateral", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }), ("vaultId" in this.accountCap
254
+ ? {
255
+ vaultId: this.accountCap.vaultId,
256
+ }
257
+ : {
258
+ accountId: this.accountCap.accountId,
259
+ })), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
248
260
  tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction(),
249
261
  })) }), undefined, {
250
262
  txKind: true,
@@ -255,35 +267,37 @@ class PerpetualsAccount extends caller_1.Caller {
255
267
  return __awaiter(this, void 0, void 0, function* () {
256
268
  var _a;
257
269
  const { withdrawAmount, recipientAddress, tx: txFromInputs } = inputs;
258
- const { txKind, coinOutArg } = yield this.fetchApi("transactions/withdraw-collateral", {
259
- withdrawAmount,
260
- recipientAddress,
261
- walletAddress: this.accountCap.walletAddress,
262
- collateralCoinType: this.accountCap.collateralCoinType,
263
- accountObjectId: this.accountCap.objectId,
264
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
270
+ // TODO: add vault support
271
+ if (this.vaultId)
272
+ throw new Error("this function is not supported for vaults");
273
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
274
+ "transactions/withdraw-collateral", Object.assign(Object.assign({ withdrawAmount,
275
+ recipientAddress, walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }, ("vaultId" in this.accountCap
276
+ ? {
277
+ vaultId: this.accountCap.vaultId,
278
+ }
279
+ : {
280
+ accountId: this.accountCap.accountId,
281
+ })), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
265
282
  tx: txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction(),
266
- })),
283
+ })) }), undefined, {
284
+ txKind: true,
267
285
  });
268
- const tx = transactions_1.Transaction.fromKind(txKind);
269
- tx.setSender(this.accountCap.walletAddress);
270
- return {
271
- tx,
272
- coinOutArg,
273
- };
274
286
  });
275
287
  }
276
288
  getAllocateCollateralTx(inputs) {
277
289
  return __awaiter(this, void 0, void 0, function* () {
278
290
  var _a;
279
291
  const { tx, allocateAmount, marketId } = inputs;
280
- return this.fetchApiTransaction("transactions/allocate-collateral", {
281
- marketId,
282
- allocateAmount,
283
- accountObjectId: this.accountCap.objectId,
284
- walletAddress: this.accountCap.walletAddress,
285
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })),
286
- }, undefined, {
292
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
293
+ "transactions/allocate-collateral", Object.assign(Object.assign({ marketId,
294
+ allocateAmount }, ("vaultId" in this.accountCap
295
+ ? {
296
+ vaultId: this.accountCap.vaultId,
297
+ }
298
+ : {
299
+ accountId: this.accountCap.accountId,
300
+ })), { walletAddress: this.ownerAddress(), txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })) }), undefined, {
287
301
  txKind: true,
288
302
  });
289
303
  });
@@ -292,13 +306,15 @@ class PerpetualsAccount extends caller_1.Caller {
292
306
  return __awaiter(this, void 0, void 0, function* () {
293
307
  var _a;
294
308
  const { tx, deallocateAmount, marketId } = inputs;
295
- return this.fetchApiTransaction("transactions/deallocate-collateral", {
296
- marketId,
297
- deallocateAmount,
298
- accountObjectId: this.accountCap.objectId,
299
- walletAddress: this.accountCap.walletAddress,
300
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })),
301
- }, undefined, {
309
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
310
+ "transactions/deallocate-collateral", Object.assign(Object.assign({ marketId,
311
+ deallocateAmount }, ("vaultId" in this.accountCap
312
+ ? {
313
+ vaultId: this.accountCap.vaultId,
314
+ }
315
+ : {
316
+ accountId: this.accountCap.accountId,
317
+ })), { walletAddress: this.ownerAddress(), txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })) }), undefined, {
302
318
  txKind: true,
303
319
  });
304
320
  });
@@ -306,13 +322,16 @@ class PerpetualsAccount extends caller_1.Caller {
306
322
  getTransferCollateralTx(inputs) {
307
323
  return __awaiter(this, void 0, void 0, function* () {
308
324
  var _a;
309
- const { transferAmount, toAccountObjectId, tx } = inputs;
310
- return this.fetchApiTransaction("transactions/transfer-collateral", {
325
+ const { transferAmount, toAccountId, tx } = inputs;
326
+ if ("vaultId" in this.accountCap)
327
+ throw new Error("`getTransferCollateralTx` not supported by vault accounts");
328
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
329
+ "transactions/transfer-collateral", {
311
330
  transferAmount,
312
- toAccountObjectId,
313
- walletAddress: this.accountCap.walletAddress,
331
+ toAccountId,
332
+ walletAddress: this.ownerAddress(),
314
333
  collateralCoinType: this.accountCap.collateralCoinType,
315
- fromAccountObjectId: this.accountCap.objectId,
334
+ fromAccountId: this.accountCap.accountId,
316
335
  txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction() })),
317
336
  }, undefined, {
318
337
  txKind: true,
@@ -327,9 +346,16 @@ class PerpetualsAccount extends caller_1.Caller {
327
346
  var _a;
328
347
  const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
329
348
  const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
330
- // tx.setSender(this.accountCap.walletAddress);
331
- return this.fetchApiTransaction("transactions/place-market-order", Object.assign(Object.assign({}, otherInputs), { slTp: slTp
332
- ? Object.assign({ walletAddress: this.accountCap.walletAddress }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId, hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
349
+ // tx.setSender(this.ownerAddress());
350
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
351
+ "transactions/place-market-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
352
+ ? Object.assign({ walletAddress: this.ownerAddress() }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
353
+ ? {
354
+ vaultId: this.accountCap.vaultId,
355
+ }
356
+ : {
357
+ accountId: this.accountCap.accountId,
358
+ })), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
333
359
  txKind: true,
334
360
  });
335
361
  });
@@ -339,9 +365,16 @@ class PerpetualsAccount extends caller_1.Caller {
339
365
  var _a;
340
366
  const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
341
367
  const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
342
- // tx.setSender(this.accountCap.walletAddress);
343
- return this.fetchApiTransaction("transactions/place-limit-order", Object.assign(Object.assign({}, otherInputs), { slTp: slTp
344
- ? Object.assign({ walletAddress: this.accountCap.walletAddress }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId, hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
368
+ // tx.setSender(this.ownerAddress());
369
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
370
+ "transactions/place-limit-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
371
+ ? Object.assign({ walletAddress: this.ownerAddress() }, slTp) : undefined, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
372
+ ? {
373
+ vaultId: this.accountCap.vaultId,
374
+ }
375
+ : {
376
+ accountId: this.accountCap.accountId,
377
+ })), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
345
378
  txKind: true,
346
379
  });
347
380
  });
@@ -350,7 +383,14 @@ class PerpetualsAccount extends caller_1.Caller {
350
383
  return __awaiter(this, void 0, void 0, function* () {
351
384
  var _a;
352
385
  const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
353
- return this.fetchApiTransaction("transactions/cancel-orders", Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId }), undefined, {
386
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
387
+ "transactions/cancel-orders", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
388
+ ? {
389
+ vaultId: this.accountCap.vaultId,
390
+ }
391
+ : {
392
+ accountId: this.accountCap.accountId,
393
+ })), undefined, {
354
394
  txKind: true,
355
395
  });
356
396
  });
@@ -359,7 +399,14 @@ class PerpetualsAccount extends caller_1.Caller {
359
399
  return __awaiter(this, void 0, void 0, function* () {
360
400
  var _a;
361
401
  const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
362
- return this.fetchApiTransaction("transactions/cancel-stop-orders", Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId }), undefined, {
402
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
403
+ "transactions/cancel-stop-orders", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
404
+ ? {
405
+ vaultId: this.accountCap.vaultId,
406
+ }
407
+ : {
408
+ accountId: this.accountCap.accountId,
409
+ })), undefined, {
363
410
  txKind: true,
364
411
  });
365
412
  });
@@ -369,15 +416,17 @@ class PerpetualsAccount extends caller_1.Caller {
369
416
  var _a;
370
417
  const { tx: txFromInputs, isSponsoredTx, stopOrders, gasCoinArg, } = inputs;
371
418
  const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
372
- // tx.setSender(this.accountCap.walletAddress);
373
- return this.fetchApiTransaction("transactions/place-stop-orders", {
374
- stopOrders,
419
+ // tx.setSender(this.ownerAddress());
420
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
421
+ "transactions/place-stop-orders", Object.assign({ stopOrders,
375
422
  gasCoinArg,
376
- isSponsoredTx,
377
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })),
378
- walletAddress: this.accountCap.walletAddress,
379
- accountObjectId: this.accountCap.objectId,
380
- }, undefined, {
423
+ isSponsoredTx, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
424
+ ? {
425
+ vaultId: this.accountCap.vaultId,
426
+ }
427
+ : {
428
+ accountId: this.accountCap.accountId,
429
+ })), undefined, {
381
430
  txKind: true,
382
431
  });
383
432
  });
@@ -390,8 +439,15 @@ class PerpetualsAccount extends caller_1.Caller {
390
439
  if (!position)
391
440
  throw new Error("you have no position for this market");
392
441
  const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
393
- // tx.setSender(this.accountCap.walletAddress);
394
- return this.fetchApiTransaction("transactions/place-sl-tp-orders", Object.assign(Object.assign({}, slTpInputs), { marketId, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId, positionSide: perpetuals_1.Perpetuals.positionSide({
442
+ // tx.setSender(this.ownerAddress());
443
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
444
+ "transactions/place-sl-tp-orders", Object.assign(Object.assign(Object.assign(Object.assign({}, slTpInputs), { marketId, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
445
+ ? {
446
+ vaultId: this.accountCap.vaultId,
447
+ }
448
+ : {
449
+ accountId: this.accountCap.accountId,
450
+ })), { positionSide: perpetuals_1.Perpetuals.positionSide({
395
451
  baseAssetAmount: position.baseAssetAmount,
396
452
  }) }), undefined, {
397
453
  txKind: true,
@@ -403,13 +459,15 @@ class PerpetualsAccount extends caller_1.Caller {
403
459
  var _a;
404
460
  const { tx: txFromInputs, stopOrders } = inputs;
405
461
  const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
406
- // tx.setSender(this.accountCap.walletAddress);
407
- return this.fetchApiTransaction("transactions/edit-stop-orders", {
408
- stopOrders,
409
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })),
410
- walletAddress: this.accountCap.walletAddress,
411
- accountObjectId: this.accountCap.objectId,
412
- }, undefined, {
462
+ // tx.setSender(this.ownerAddress());
463
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
464
+ "transactions/edit-stop-orders", Object.assign({ stopOrders, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
465
+ ? {
466
+ vaultId: this.accountCap.vaultId,
467
+ }
468
+ : {
469
+ accountId: this.accountCap.accountId,
470
+ })), undefined, {
413
471
  txKind: true,
414
472
  });
415
473
  });
@@ -418,7 +476,14 @@ class PerpetualsAccount extends caller_1.Caller {
418
476
  return __awaiter(this, void 0, void 0, function* () {
419
477
  var _a;
420
478
  const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
421
- return this.fetchApiTransaction("transactions/reduce-order", Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.accountCap.walletAddress, accountObjectId: this.accountCap.objectId }), undefined, {
479
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
480
+ "transactions/reduce-order", Object.assign(Object.assign(Object.assign({}, otherInputs), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }), ("vaultId" in this.accountCap
481
+ ? {
482
+ vaultId: this.accountCap.vaultId,
483
+ }
484
+ : {
485
+ accountId: this.accountCap.accountId,
486
+ })), undefined, {
422
487
  txKind: true,
423
488
  });
424
489
  });
@@ -427,14 +492,16 @@ class PerpetualsAccount extends caller_1.Caller {
427
492
  return __awaiter(this, void 0, void 0, function* () {
428
493
  var _a;
429
494
  const { leverage, tx, collateralChange, marketId } = inputs;
430
- return this.fetchApiTransaction("transactions/set-leverage", {
431
- leverage,
495
+ return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
496
+ "transactions/set-leverage", Object.assign({ leverage,
432
497
  marketId,
433
- collateralChange,
434
- txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })),
435
- walletAddress: this.accountCap.walletAddress,
436
- accountObjectId: this.accountCap.objectId,
437
- }, undefined, {
498
+ collateralChange, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
499
+ ? {
500
+ vaultId: this.accountCap.vaultId,
501
+ }
502
+ : {
503
+ accountId: this.accountCap.accountId,
504
+ })), undefined, {
438
505
  txKind: true,
439
506
  });
440
507
  });
@@ -483,10 +550,10 @@ class PerpetualsAccount extends caller_1.Caller {
483
550
  // ApiPerpetualsPreviewOrderResponse,
484
551
  // ApiPerpetualsPreviewOrderBody
485
552
  // >(
486
- // "previews/place-order",
553
+ // `${this.vaultId ? "vault" : "account"}/` +"previews/place-order",
487
554
  // {
488
555
  // ...inputs,
489
- // accountObjectId: this.accountCap.objectId,
556
+ // accountId: this.accountCap.accountId,
490
557
  // collateralCoinType: this.accountCap.collateralCoinType,
491
558
  // },
492
559
  // abortSignal
@@ -494,12 +561,26 @@ class PerpetualsAccount extends caller_1.Caller {
494
561
  // }
495
562
  getPlaceMarketOrderPreview(inputs, abortSignal) {
496
563
  return __awaiter(this, void 0, void 0, function* () {
497
- return this.fetchApi("previews/place-market-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
564
+ return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` +
565
+ "previews/place-market-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
566
+ ? {
567
+ vaultId: this.accountCap.vaultId,
568
+ }
569
+ : {
570
+ accountId: this.accountCap.accountId,
571
+ })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
498
572
  });
499
573
  }
500
574
  getPlaceLimitOrderPreview(inputs, abortSignal) {
501
575
  return __awaiter(this, void 0, void 0, function* () {
502
- return this.fetchApi("previews/place-limit-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
576
+ return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` +
577
+ "previews/place-limit-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
578
+ ? {
579
+ vaultId: this.accountCap.vaultId,
580
+ }
581
+ : {
582
+ accountId: this.accountCap.accountId,
583
+ })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
503
584
  });
504
585
  }
505
586
  getCancelOrdersPreview(inputs, abortSignal) {
@@ -510,23 +591,37 @@ class PerpetualsAccount extends caller_1.Caller {
510
591
  collateralChange: 0,
511
592
  marketIdsToPositionAfterCancelOrders: {},
512
593
  };
513
- return this.fetchApi("previews/cancel-orders", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
594
+ return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/cancel-orders", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
595
+ ? {
596
+ vaultId: this.accountCap.vaultId,
597
+ }
598
+ : {
599
+ accountId: this.accountCap.accountId,
600
+ })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
514
601
  });
515
602
  }
516
603
  getReduceOrderPreview(inputs, abortSignal) {
517
604
  return __awaiter(this, void 0, void 0, function* () {
518
- return this.fetchApi("previews/reduce-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
605
+ return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/reduce-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
606
+ ? {
607
+ vaultId: this.accountCap.vaultId,
608
+ }
609
+ : {
610
+ accountId: this.accountCap.accountId,
611
+ })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
519
612
  });
520
613
  }
521
614
  getSetLeveragePreview(inputs, abortSignal) {
522
615
  return __awaiter(this, void 0, void 0, function* () {
523
616
  const { marketId, leverage } = inputs;
524
- return this.fetchApi("previews/set-leverage", {
525
- marketId,
526
- leverage,
527
- accountObjectId: this.accountCap.objectId,
528
- collateralCoinType: this.accountCap.collateralCoinType,
529
- }, abortSignal);
617
+ return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/set-leverage", Object.assign(Object.assign({ marketId,
618
+ leverage }, ("vaultId" in this.accountCap
619
+ ? {
620
+ vaultId: this.accountCap.vaultId,
621
+ }
622
+ : {
623
+ accountId: this.accountCap.accountId,
624
+ })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
530
625
  });
531
626
  }
532
627
  getOrderDatas() {
@@ -553,7 +648,7 @@ class PerpetualsAccount extends caller_1.Caller {
553
648
  bytes,
554
649
  signature,
555
650
  accountId: this.accountCap.accountId,
556
- walletAddress: this.accountCap.walletAddress,
651
+ walletAddress: this.ownerAddress(),
557
652
  marketIds: marketIds !== null && marketIds !== void 0 ? marketIds : [],
558
653
  });
559
654
  });
@@ -570,7 +665,7 @@ class PerpetualsAccount extends caller_1.Caller {
570
665
  }
571
666
  getMarginHistory() {
572
667
  return __awaiter(this, void 0, void 0, function* () {
573
- return this.fetchApi("account/trade-history", {
668
+ return this.fetchApi("account/margin-history", {
574
669
  accountId: this.accountCap.accountId,
575
670
  collateralCoinType: this.accountCap.collateralCoinType,
576
671
  });
@@ -628,7 +723,7 @@ class PerpetualsAccount extends caller_1.Caller {
628
723
  slTpStopOrderDatasForMarketId(inputs) {
629
724
  const { marketId, stopOrderDatas } = inputs;
630
725
  const position = this.positionForMarketId({ marketId });
631
- if (!position || position.baseAssetAmount === BigInt(0)) {
726
+ if (!position || position.baseAssetAmount === 0) {
632
727
  return {
633
728
  fullSlTpOrder: undefined,
634
729
  partialSlTpOrders: undefined,
@@ -662,6 +757,27 @@ class PerpetualsAccount extends caller_1.Caller {
662
757
  collateralBalance() {
663
758
  return __1.Coin.normalizeBalance(this.collateral(), this.collateralDecimals());
664
759
  }
760
+ isVault() {
761
+ return this.isVault !== undefined;
762
+ }
763
+ ownerAddress() {
764
+ return "walletAddress" in this.accountCap
765
+ ? // TODO: change to ownerAddress ?
766
+ this.accountCap.walletAddress
767
+ : this.accountCap.ownerAddress;
768
+ }
769
+ accountObjectId() {
770
+ return this.accountCap.accountObjectId;
771
+ }
772
+ accountId() {
773
+ return this.accountCap.accountId;
774
+ }
775
+ // TODO: make this work with vaults
776
+ accountCapId() {
777
+ if ("vaultId" in this.accountCap)
778
+ throw new Error("not account cap id present on vault owned account");
779
+ return this.accountCap.objectId;
780
+ }
665
781
  }
666
782
  exports.PerpetualsAccount = PerpetualsAccount;
667
783
  // =========================================================================