aftermath-ts-sdk 1.3.23-perps.22 → 1.3.23-wc-0.1

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 (120) hide show
  1. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts +0 -3
  2. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +1 -1
  3. package/dist/general/apiHelpers/transactionsApiHelpers.js +0 -12
  4. package/dist/general/providers/aftermath.d.ts +5 -0
  5. package/dist/general/providers/aftermath.d.ts.map +1 -1
  6. package/dist/general/providers/aftermath.js +6 -1
  7. package/dist/general/providers/aftermathApi.d.ts +5 -0
  8. package/dist/general/providers/aftermathApi.d.ts.map +1 -1
  9. package/dist/general/providers/aftermathApi.js +5 -0
  10. package/dist/general/types/castingTypes.d.ts +2 -2
  11. package/dist/general/types/castingTypes.d.ts.map +1 -1
  12. package/dist/general/types/configTypes.d.ts +10 -0
  13. package/dist/general/types/configTypes.d.ts.map +1 -1
  14. package/dist/general/types/generalTypes.d.ts +0 -4
  15. package/dist/general/types/generalTypes.d.ts.map +1 -1
  16. package/dist/general/types/moveErrorsInterface.d.ts +1 -1
  17. package/dist/general/types/moveErrorsInterface.d.ts.map +1 -1
  18. package/dist/general/utils/caller.d.ts +2 -17
  19. package/dist/general/utils/caller.d.ts.map +1 -1
  20. package/dist/general/utils/caller.js +2 -35
  21. package/dist/general/utils/casting.d.ts +5 -4
  22. package/dist/general/utils/casting.d.ts.map +1 -1
  23. package/dist/general/utils/casting.js +5 -4
  24. package/dist/general/utils/helpers.d.ts +0 -1
  25. package/dist/general/utils/helpers.d.ts.map +1 -1
  26. package/dist/general/utils/helpers.js +6 -35
  27. package/dist/general/wallet/wallet.d.ts +1 -1
  28. package/dist/general/wallet/wallet.d.ts.map +1 -1
  29. package/dist/packages/coin/coin.d.ts +1 -1
  30. package/dist/packages/coin/coin.d.ts.map +1 -1
  31. package/dist/packages/coin/coin.js +1 -3
  32. package/dist/packages/farms/api/farmsApiCasting.js +27 -27
  33. package/dist/packages/farms/farms.d.ts +1 -1
  34. package/dist/packages/farms/farms.d.ts.map +1 -1
  35. package/dist/packages/farms/farmsStakedPosition.d.ts +1 -1
  36. package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
  37. package/dist/packages/farms/farmsStakingPool.d.ts +1 -1
  38. package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
  39. package/dist/packages/faucet/api/faucetApi.d.ts +17 -9
  40. package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
  41. package/dist/packages/faucet/api/faucetApi.js +49 -44
  42. package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +1 -1
  43. package/dist/packages/faucet/api/faucetApiCasting.js +7 -10
  44. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +4 -1
  45. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts.map +1 -1
  46. package/dist/packages/faucet/faucet.d.ts +1 -1
  47. package/dist/packages/faucet/faucet.d.ts.map +1 -1
  48. package/dist/packages/faucet/faucet.js +1 -1
  49. package/dist/packages/faucet/faucetTypes.d.ts +3 -2
  50. package/dist/packages/faucet/faucetTypes.d.ts.map +1 -1
  51. package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.js +3 -3
  52. package/dist/packages/multisig/multisig.d.ts +1 -1
  53. package/dist/packages/multisig/multisig.d.ts.map +1 -1
  54. package/dist/packages/nftAmm/nftAmm.d.ts +1 -1
  55. package/dist/packages/nftAmm/nftAmm.d.ts.map +1 -1
  56. package/dist/packages/oracle/api/oracleApi.d.ts +21 -0
  57. package/dist/packages/oracle/api/oracleApi.d.ts.map +1 -0
  58. package/dist/packages/oracle/api/oracleApi.js +76 -0
  59. package/dist/packages/oracle/api/oracleApiCasting.d.ts +6 -0
  60. package/dist/packages/oracle/api/oracleApiCasting.d.ts.map +1 -0
  61. package/dist/packages/oracle/api/oracleApiCasting.js +25 -0
  62. package/dist/packages/oracle/oracle.d.ts +15 -0
  63. package/dist/packages/oracle/oracle.d.ts.map +1 -0
  64. package/dist/packages/oracle/oracle.js +46 -0
  65. package/dist/packages/oracle/oracleCastingTypes.d.ts +11 -0
  66. package/dist/packages/oracle/oracleCastingTypes.d.ts.map +1 -0
  67. package/dist/packages/oracle/oracleCastingTypes.js +2 -0
  68. package/dist/packages/oracle/oracleTypes.d.ts +10 -0
  69. package/dist/packages/oracle/oracleTypes.d.ts.map +1 -0
  70. package/dist/packages/oracle/oracleTypes.js +2 -0
  71. package/dist/packages/perpetuals/api/perpetualsApi.d.ts +203 -11
  72. package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
  73. package/dist/packages/perpetuals/api/perpetualsApi.js +615 -924
  74. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +21 -10
  75. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
  76. package/dist/packages/perpetuals/api/perpetualsApiCasting.js +238 -260
  77. package/dist/packages/perpetuals/index.d.ts +0 -1
  78. package/dist/packages/perpetuals/index.d.ts.map +1 -1
  79. package/dist/packages/perpetuals/index.js +0 -1
  80. package/dist/packages/perpetuals/perpetuals.d.ts +14 -83
  81. package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
  82. package/dist/packages/perpetuals/perpetuals.js +59 -158
  83. package/dist/packages/perpetuals/perpetualsAccount.d.ts +86 -158
  84. package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
  85. package/dist/packages/perpetuals/perpetualsAccount.js +148 -489
  86. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +149 -82
  87. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +1 -1
  88. package/dist/packages/perpetuals/perpetualsMarket.d.ts +20 -55
  89. package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
  90. package/dist/packages/perpetuals/perpetualsMarket.js +57 -114
  91. package/dist/packages/perpetuals/perpetualsTypes.d.ts +280 -586
  92. package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
  93. package/dist/packages/perpetuals/perpetualsTypes.js +81 -17
  94. package/dist/packages/pools/api/poolsApiCasting.js +3 -3
  95. package/dist/packages/pools/pool.d.ts +1 -1
  96. package/dist/packages/pools/pool.d.ts.map +1 -1
  97. package/dist/packages/pools/pools.d.ts +1 -1
  98. package/dist/packages/pools/pools.d.ts.map +1 -1
  99. package/dist/packages/referralVault/referralVault.d.ts +1 -1
  100. package/dist/packages/referralVault/referralVault.d.ts.map +1 -1
  101. package/dist/packages/router/api/routerApiCasting.js +1 -1
  102. package/dist/packages/staking/api/stakingApiCasting.js +3 -3
  103. package/dist/packages/staking/staking.d.ts +1 -1
  104. package/dist/packages/staking/staking.d.ts.map +1 -1
  105. package/dist/packages/sui/sui.d.ts +1 -1
  106. package/dist/packages/sui/sui.d.ts.map +1 -1
  107. package/dist/packages/suiFrens/api/suiFrensApiCasting.js +4 -4
  108. package/dist/packages/suiFrens/stakedSuiFren.d.ts +1 -1
  109. package/dist/packages/suiFrens/stakedSuiFren.d.ts.map +1 -1
  110. package/dist/packages/suiFrens/suiFren.d.ts +1 -1
  111. package/dist/packages/suiFrens/suiFren.d.ts.map +1 -1
  112. package/dist/packages/suiFrens/suiFrens.d.ts +1 -1
  113. package/dist/packages/suiFrens/suiFrens.d.ts.map +1 -1
  114. package/dist/types.d.ts +1 -0
  115. package/dist/types.d.ts.map +1 -1
  116. package/dist/types.js +1 -0
  117. package/package.json +2 -2
  118. package/dist/packages/perpetuals/perpetualsVault.d.ts +0 -109
  119. package/dist/packages/perpetuals/perpetualsVault.d.ts.map +0 -1
  120. package/dist/packages/perpetuals/perpetualsVault.js +0 -290
@@ -23,39 +23,29 @@ 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");
26
27
  const utils_1 = require("../../general/utils");
27
28
  const perpetuals_1 = require("./perpetuals");
28
29
  const __1 = require("..");
29
- const transactions_1 = require("@mysten/sui/transactions");
30
- // TODO: create refresh account positions function ?
31
30
  class PerpetualsAccount extends caller_1.Caller {
32
31
  // =========================================================================
33
32
  // Constructor
34
33
  // =========================================================================
35
- constructor(account, accountCap, config, Provider) {
36
- const vaultId = "vaultId" in accountCap ? accountCap.vaultId : undefined;
34
+ constructor(account, accountCap, config) {
37
35
  super(config, "perpetuals");
38
36
  this.account = account;
39
37
  this.accountCap = accountCap;
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
- // }
49
38
  // =========================================================================
50
39
  // Calculations
51
40
  // =========================================================================
52
41
  this.calcFreeCollateralForPosition = (inputs) => {
53
42
  var _a, _b;
54
43
  const marketId = inputs.market.marketId;
55
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
44
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
56
45
  const funding = this.calcUnrealizedFundingsForPosition(inputs);
57
46
  const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
58
- let collateralUsd = position.collateral * inputs.collateralPrice;
47
+ let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
48
+ inputs.collateralPrice;
59
49
  collateralUsd += funding;
60
50
  let cappedMargin;
61
51
  if (pnl < 0) {
@@ -74,12 +64,14 @@ class PerpetualsAccount extends caller_1.Caller {
74
64
  var _a, _b;
75
65
  const { market, indexPrice, collateralPrice } = inputs;
76
66
  const marketId = market.marketId;
77
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : market.emptyPosition();
67
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market });
78
68
  const funding = this.calcUnrealizedFundingsForPosition({
79
69
  market,
80
70
  position,
81
71
  });
82
- const collateralUsd = (position === null || position === void 0 ? void 0 : position.collateral) * collateralPrice + funding;
72
+ const collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position === null || position === void 0 ? void 0 : position.collateral) *
73
+ collateralPrice +
74
+ funding;
83
75
  const { pnl, netAbsBaseValue } = this.calcPnLAndMarginForPosition({
84
76
  market,
85
77
  indexPrice,
@@ -104,31 +96,31 @@ class PerpetualsAccount extends caller_1.Caller {
104
96
  this.calcUnrealizedFundingsForPosition = (inputs) => {
105
97
  var _a, _b;
106
98
  const marketId = inputs.market.marketId;
107
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
108
- const baseAmount = position.baseAssetAmount;
99
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
100
+ const baseAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
109
101
  const isLong = Math.sign(baseAmount);
110
102
  if (isLong < 0) {
111
- const fundingShort = position.cumFundingRateShort;
112
- const marketFundingShort = inputs.market.marketState.cumFundingRateShort;
103
+ const fundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateShort);
104
+ const marketFundingShort = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateShort);
113
105
  return -baseAmount * (marketFundingShort - fundingShort);
114
106
  }
115
107
  else {
116
- const fundingLong = position.cumFundingRateLong;
117
- const marketFundingLong = inputs.market.marketState.cumFundingRateLong;
108
+ const fundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.cumFundingRateLong);
109
+ const marketFundingLong = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketState.cumFundingRateLong);
118
110
  return -baseAmount * (marketFundingLong - fundingLong);
119
111
  }
120
112
  };
121
113
  this.calcPnLAndMarginForPosition = (inputs) => {
122
114
  var _a, _b;
123
115
  const marketId = inputs.market.marketId;
124
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
125
- const marginRatioInitial = 1 / (position.leverage || 1);
116
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
117
+ const marginRatioInitial = 1 / position.leverage;
126
118
  // const marginRatioInitial = inputs.market.initialMarginRatio();
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;
119
+ const marginRatioMaintenance = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
120
+ const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
121
+ const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
122
+ const bidsQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.bidsQuantity);
123
+ const asksQuantity = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.asksQuantity);
132
124
  const pnl = baseAssetAmount * inputs.indexPrice - quoteAssetAmount;
133
125
  const netAbs = Math.max(Math.abs(baseAssetAmount + bidsQuantity), Math.abs(baseAssetAmount - asksQuantity));
134
126
  const netAbsBaseValue = netAbs * inputs.indexPrice;
@@ -139,30 +131,33 @@ class PerpetualsAccount extends caller_1.Caller {
139
131
  this.calcLiquidationPriceForPosition = (inputs) => {
140
132
  var _a, _b;
141
133
  const marketId = inputs.market.marketId;
142
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
134
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
143
135
  const funding = this.calcUnrealizedFundingsForPosition(inputs);
144
- const baseAssetAmount = position.baseAssetAmount;
145
- const quoteAssetAmount = position.quoteAssetNotionalAmount;
146
- const numerator = position.collateral * inputs.collateralPrice +
147
- funding -
148
- quoteAssetAmount;
149
- const MMR = inputs.market.marketParams.marginRatioMaintenance;
150
- const bidsQuantity = position.bidsQuantity;
151
- const asksQuantity = position.asksQuantity;
152
- const netAbs = Math.max(Math.abs(baseAssetAmount + bidsQuantity), Math.abs(baseAssetAmount - asksQuantity));
153
- const denominator = netAbs * MMR - baseAssetAmount;
154
- if (!denominator)
155
- return 0;
156
- const liquidationPrice = numerator / denominator;
157
- return liquidationPrice <= 0 ? 0 : liquidationPrice;
136
+ const collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position === null || position === void 0 ? void 0 : position.collateral) *
137
+ inputs.collateralPrice +
138
+ funding;
139
+ const baseAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.baseAssetAmount);
140
+ const quoteAssetAmount = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.quoteAssetNotionalAmount);
141
+ const MMR = iFixedUtils_1.IFixedUtils.numberFromIFixed(inputs.market.marketParams.marginRatioMaintenance);
142
+ const numerator = collateralUsd - quoteAssetAmount;
143
+ const price = (() => {
144
+ if (baseAssetAmount > 0) {
145
+ return numerator / ((1 - MMR) * -baseAssetAmount);
146
+ }
147
+ else {
148
+ return numerator / ((1 + MMR) * -baseAssetAmount);
149
+ }
150
+ })();
151
+ return price < 0 ? 0 : price;
158
152
  };
159
153
  this.calcFreeMarginUsdForPosition = (inputs) => {
160
154
  var _a, _b;
161
155
  const marketId = inputs.market.marketId;
162
- const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : inputs.market.emptyPosition();
156
+ const position = (_b = (_a = inputs.position) !== null && _a !== void 0 ? _a : this.positionForMarketId({ marketId })) !== null && _b !== void 0 ? _b : this.emptyPosition({ market: inputs.market });
163
157
  const totalFunding = this.calcUnrealizedFundingsForPosition(inputs);
164
158
  const { pnl, minInitialMargin } = this.calcPnLAndMarginForPosition(inputs);
165
- let collateralUsd = position.collateral * inputs.collateralPrice;
159
+ let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
160
+ inputs.collateralPrice;
166
161
  const margin = collateralUsd + totalFunding + pnl;
167
162
  if (margin >= minInitialMargin) {
168
163
  return margin - minInitialMargin;
@@ -179,7 +174,7 @@ class PerpetualsAccount extends caller_1.Caller {
179
174
  zipped.forEach(([market, indexPrice]) => {
180
175
  var _a;
181
176
  const marketId = market.marketId;
182
- const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : market.emptyPosition();
177
+ const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : this.emptyPosition({ market });
183
178
  const funding = this.calcUnrealizedFundingsForPosition({
184
179
  market,
185
180
  position,
@@ -189,7 +184,8 @@ class PerpetualsAccount extends caller_1.Caller {
189
184
  indexPrice,
190
185
  position,
191
186
  });
192
- let collateralUsd = position.collateral * inputs.collateralPrice;
187
+ let collateralUsd = iFixedUtils_1.IFixedUtils.numberFromIFixed(position.collateral) *
188
+ inputs.collateralPrice;
193
189
  totalPnL += pnl;
194
190
  totalFunding += funding;
195
191
  totalCollateralAllocated += collateralUsd;
@@ -204,38 +200,52 @@ class PerpetualsAccount extends caller_1.Caller {
204
200
  };
205
201
  this.closePositionTxInputs = (inputs) => {
206
202
  var _a;
207
- const { size, market, orderDatas, collateralPrice } = inputs;
203
+ const { size, market, walletAddress, orderDatas, collateralPrice } = inputs;
208
204
  const marketId = market.marketId;
209
- const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : market.emptyPosition();
205
+ const position = (_a = this.positionForMarketId({ marketId })) !== null && _a !== void 0 ? _a : this.emptyPosition({ market });
210
206
  // TODO: move conversion to helper function, since used often
211
- const ordersCollateral = utils_1.Helpers.sum(orderDatas
207
+ const ordersCollateral = __1.Coin.normalizeBalance(utils_1.Helpers.sum(orderDatas
212
208
  .filter((orderData) => orderData.marketId === market.marketId)
213
- .map((orderData) => market.calcCollateralUsedForOrder(Object.assign(Object.assign({}, inputs), { orderData, leverage: position.leverage })).collateral));
214
- const fullPositionCollateralChange = Math.max(this.calcFreeMarginUsdForPosition(inputs) / collateralPrice -
215
- ordersCollateral *
216
- (1 -
217
- PerpetualsAccount.constants
218
- .closePositionMarginOfError), 0) * -1;
219
- // NOTE: is this safe / correct ?
220
- const collateralChange = Number(fullPositionCollateralChange) *
221
- (Number(size) /
222
- utils_1.Casting.Fixed.fixedOneN9 /
223
- position.baseAssetAmount);
209
+ .map((orderData) => market.calcCollateralUsedForOrder(Object.assign(Object.assign({}, inputs), { orderData, leverage: position.leverage })).collateral)), this.collateralDecimals());
210
+ const fullPositionCollateralChange = utils_1.Helpers.maxBigInt(BigInt(Math.floor(Number(__1.Coin.normalizeBalance(this.calcFreeMarginUsdForPosition(inputs) *
211
+ collateralPrice, this.collateralDecimals()) - ordersCollateral) *
212
+ (1 -
213
+ PerpetualsAccount.constants
214
+ .closePositionMarginOfError))), BigInt(0)) * BigInt(-1);
215
+ const positionSize = BigInt(Math.round(Math.abs(utils_1.Casting.IFixed.numberFromIFixed(position.baseAssetAmount) /
216
+ market.lotSize())));
217
+ const collateralChange = BigInt(Math.round(Number(fullPositionCollateralChange) *
218
+ (Number(size) / Number(positionSize))));
224
219
  const positionSide = perpetuals_1.Perpetuals.positionSide(position);
225
220
  return {
226
221
  size,
227
222
  marketId,
223
+ walletAddress,
228
224
  collateralChange,
229
- // leverage: position.leverage || 1,
230
- // leverage: undefined,
231
225
  side: positionSide === types_1.PerpetualsOrderSide.Bid
232
226
  ? types_1.PerpetualsOrderSide.Ask
233
227
  : types_1.PerpetualsOrderSide.Bid,
234
- // hasPosition: this.positionForMarketId({ marketId }) !== undefined,
235
- reduceOnly: true,
228
+ hasPosition: this.positionForMarketId({ marketId }) !== undefined,
229
+ };
230
+ };
231
+ this.emptyPosition = (inputs) => {
232
+ const { market } = inputs;
233
+ return {
234
+ marketId: market.marketId,
235
+ collateralCoinType: this.accountCap.collateralCoinType,
236
+ collateral: BigInt(0),
237
+ baseAssetAmount: BigInt(0),
238
+ quoteAssetNotionalAmount: BigInt(0),
239
+ cumFundingRateLong: market.marketState.cumFundingRateLong,
240
+ cumFundingRateShort: market.marketState.cumFundingRateShort,
241
+ asksQuantity: BigInt(0),
242
+ bidsQuantity: BigInt(0),
243
+ pendingOrders: [],
244
+ makerFee: BigInt(1000000000000000000), // 100%
245
+ takerFee: BigInt(1000000000000000000), // 100%
246
+ leverage: 1,
236
247
  };
237
248
  };
238
- this.vaultId = vaultId;
239
249
  }
240
250
  // =========================================================================
241
251
  // Transactions
@@ -245,98 +255,29 @@ class PerpetualsAccount extends caller_1.Caller {
245
255
  // =========================================================================
246
256
  getDepositCollateralTx(inputs) {
247
257
  return __awaiter(this, void 0, void 0, function* () {
248
- var _a;
249
- const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
250
- // TODO: add vault support
251
- if (this.vaultId)
252
- throw new Error("this function is not supported for vaults");
253
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
254
- "transactions/deposit-collateral", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }), ("vaultId" in this.accountCap
255
- ? {
256
- vaultId: this.accountCap.vaultId,
257
- }
258
- : {
259
- accountId: this.accountCap.accountId,
260
- })), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
261
- tx: tx !== null && tx !== void 0 ? tx : new transactions_1.Transaction(),
262
- })) }), undefined, {
263
- txKind: true,
264
- });
258
+ return this.fetchApiTransaction("transactions/deposit-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
265
259
  });
266
260
  }
267
261
  getWithdrawCollateralTx(inputs) {
268
262
  return __awaiter(this, void 0, void 0, function* () {
269
- var _a;
270
- const { withdrawAmount, recipientAddress, tx: txFromInputs } = inputs;
271
- // TODO: add vault support
272
- if (this.vaultId)
273
- throw new Error("this function is not supported for vaults");
274
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
275
- "transactions/withdraw-collateral", Object.assign(Object.assign({ withdrawAmount,
276
- recipientAddress, walletAddress: this.ownerAddress(), collateralCoinType: this.accountCap.collateralCoinType }, ("vaultId" in this.accountCap
277
- ? {
278
- vaultId: this.accountCap.vaultId,
279
- }
280
- : {
281
- accountId: this.accountCap.accountId,
282
- })), { txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({
283
- tx: txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction(),
284
- })) }), undefined, {
285
- txKind: true,
286
- });
263
+ return this.fetchApiTransaction("transactions/withdraw-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
287
264
  });
288
265
  }
289
266
  getAllocateCollateralTx(inputs) {
290
267
  return __awaiter(this, void 0, void 0, function* () {
291
- var _a;
292
- const { tx, allocateAmount, marketId } = inputs;
293
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
294
- "transactions/allocate-collateral", Object.assign(Object.assign({ marketId,
295
- allocateAmount }, ("vaultId" in this.accountCap
296
- ? {
297
- vaultId: this.accountCap.vaultId,
298
- }
299
- : {
300
- accountId: this.accountCap.accountId,
301
- })), { 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
- txKind: true,
303
- });
268
+ const { market } = inputs;
269
+ return this.fetchApiTransaction("transactions/allocate-collateral", Object.assign(Object.assign({}, inputs), { packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
304
270
  });
305
271
  }
306
272
  getDeallocateCollateralTx(inputs) {
307
273
  return __awaiter(this, void 0, void 0, function* () {
308
- var _a;
309
- const { tx, deallocateAmount, marketId } = inputs;
310
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
311
- "transactions/deallocate-collateral", Object.assign(Object.assign({ marketId,
312
- deallocateAmount }, ("vaultId" in this.accountCap
313
- ? {
314
- vaultId: this.accountCap.vaultId,
315
- }
316
- : {
317
- accountId: this.accountCap.accountId,
318
- })), { 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, {
319
- txKind: true,
320
- });
274
+ const { market } = inputs;
275
+ return this.fetchApiTransaction("transactions/deallocate-collateral", Object.assign(Object.assign({}, inputs), { packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, basePriceFeedId: market.marketParams.basePriceFeedId, collateralPriceFeedId: market.marketParams.collateralPriceFeedId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
321
276
  });
322
277
  }
323
278
  getTransferCollateralTx(inputs) {
324
279
  return __awaiter(this, void 0, void 0, function* () {
325
- var _a;
326
- const { transferAmount, toAccountId, tx } = inputs;
327
- if ("vaultId" in this.accountCap)
328
- throw new Error("`getTransferCollateralTx` not supported by vault accounts");
329
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
330
- "transactions/transfer-collateral", {
331
- transferAmount,
332
- toAccountId,
333
- walletAddress: this.ownerAddress(),
334
- collateralCoinType: this.accountCap.collateralCoinType,
335
- fromAccountId: this.accountCap.accountId,
336
- 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() })),
337
- }, undefined, {
338
- txKind: true,
339
- });
280
+ return this.fetchApiTransaction("transactions/transfer-collateral", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, fromAccountCapId: this.accountCap.objectId }));
340
281
  });
341
282
  }
342
283
  // =========================================================================
@@ -344,167 +285,33 @@ class PerpetualsAccount extends caller_1.Caller {
344
285
  // =========================================================================
345
286
  getPlaceMarketOrderTx(inputs) {
346
287
  return __awaiter(this, void 0, void 0, function* () {
347
- var _a;
348
- const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
349
- const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
350
- // tx.setSender(this.ownerAddress());
351
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
352
- "transactions/place-market-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
353
- ? 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
354
- ? {
355
- vaultId: this.accountCap.vaultId,
356
- }
357
- : {
358
- accountId: this.accountCap.accountId,
359
- })), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
360
- txKind: true,
361
- });
288
+ return this.fetchApiTransaction("transactions/market-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest, hasPosition: this.positionForMarketId(inputs) !== undefined }));
362
289
  });
363
290
  }
364
291
  getPlaceLimitOrderTx(inputs) {
365
292
  return __awaiter(this, void 0, void 0, function* () {
366
- var _a;
367
- const { tx: txFromInputs, slTp } = inputs, otherInputs = __rest(inputs, ["tx", "slTp"]);
368
- const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
369
- // tx.setSender(this.ownerAddress());
370
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
371
- "transactions/place-limit-order", Object.assign(Object.assign(Object.assign(Object.assign({}, otherInputs), { slTp: slTp
372
- ? 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
373
- ? {
374
- vaultId: this.accountCap.vaultId,
375
- }
376
- : {
377
- accountId: this.accountCap.accountId,
378
- })), { hasPosition: this.positionForMarketId(otherInputs) !== undefined }), undefined, {
379
- txKind: true,
380
- });
381
- });
382
- }
383
- getCancelOrdersTx(inputs) {
384
- return __awaiter(this, void 0, void 0, function* () {
385
- var _a;
386
- const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
387
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
388
- "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
389
- ? {
390
- vaultId: this.accountCap.vaultId,
391
- }
392
- : {
393
- accountId: this.accountCap.accountId,
394
- })), undefined, {
395
- txKind: true,
396
- });
293
+ return this.fetchApiTransaction("transactions/limit-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest, hasPosition: this.positionForMarketId(inputs) !== undefined }));
397
294
  });
398
295
  }
399
- getCancelStopOrdersTx(inputs) {
296
+ getPlaceSLTPOrder(inputs) {
400
297
  return __awaiter(this, void 0, void 0, function* () {
401
- var _a;
402
- const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
403
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
404
- "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
405
- ? {
406
- vaultId: this.accountCap.vaultId,
407
- }
408
- : {
409
- accountId: this.accountCap.accountId,
410
- })), undefined, {
411
- txKind: true,
412
- });
298
+ return this.fetchApiTransaction("transactions/sltp-order", Object.assign(Object.assign({}, inputs), { accountObjectId: this.accountCap.objectId, accountObjectVersion: this.accountCap.objectVersion, accountObjectDigest: this.accountCap.objectDigest }));
413
299
  });
414
300
  }
415
- getPlaceStopOrdersTx(inputs) {
301
+ getCancelOrderTx(inputs) {
416
302
  return __awaiter(this, void 0, void 0, function* () {
417
- var _a;
418
- const { tx: txFromInputs, isSponsoredTx, stopOrders, gasCoinArg, } = inputs;
419
- const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
420
- // tx.setSender(this.ownerAddress());
421
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
422
- "transactions/place-stop-orders", Object.assign({ stopOrders,
423
- gasCoinArg,
424
- isSponsoredTx, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
425
- ? {
426
- vaultId: this.accountCap.vaultId,
427
- }
428
- : {
429
- accountId: this.accountCap.accountId,
430
- })), undefined, {
431
- txKind: true,
432
- });
433
- });
434
- }
435
- getPlaceSlTpOrdersTx(inputs) {
436
- return __awaiter(this, void 0, void 0, function* () {
437
- var _a;
438
- const { tx: txFromInputs, isSponsoredTx, marketId } = inputs, slTpInputs = __rest(inputs, ["tx", "isSponsoredTx", "marketId"]);
439
- const position = this.positionForMarketId({ marketId });
440
- if (!position)
441
- throw new Error("you have no position for this market");
442
- const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
443
- // tx.setSender(this.ownerAddress());
444
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
445
- "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
446
- ? {
447
- vaultId: this.accountCap.vaultId,
448
- }
449
- : {
450
- accountId: this.accountCap.accountId,
451
- })), { positionSide: perpetuals_1.Perpetuals.positionSide({
452
- baseAssetAmount: position.baseAssetAmount,
453
- }) }), undefined, {
454
- txKind: true,
455
- });
456
- });
457
- }
458
- getEditStopOrdersTx(inputs) {
459
- return __awaiter(this, void 0, void 0, function* () {
460
- var _a;
461
- const { tx: txFromInputs, stopOrders } = inputs;
462
- const tx = txFromInputs !== null && txFromInputs !== void 0 ? txFromInputs : new transactions_1.Transaction();
463
- // tx.setSender(this.ownerAddress());
464
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
465
- "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
466
- ? {
467
- vaultId: this.accountCap.vaultId,
468
- }
469
- : {
470
- accountId: this.accountCap.accountId,
471
- })), undefined, {
472
- txKind: true,
473
- });
303
+ return this.fetchApiTransaction("transactions/cancel-order", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
474
304
  });
475
305
  }
476
- getReduceOrderTx(inputs) {
306
+ getCancelOrdersTx(inputs) {
477
307
  return __awaiter(this, void 0, void 0, function* () {
478
- var _a;
479
- const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
480
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
481
- "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
482
- ? {
483
- vaultId: this.accountCap.vaultId,
484
- }
485
- : {
486
- accountId: this.accountCap.accountId,
487
- })), undefined, {
488
- txKind: true,
489
- });
308
+ return this.fetchApiTransaction("transactions/cancel-orders", Object.assign(Object.assign({}, inputs), { collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
490
309
  });
491
310
  }
492
- getSetLeverageTx(inputs) {
311
+ getReduceOrdersTx(inputs) {
493
312
  return __awaiter(this, void 0, void 0, function* () {
494
- var _a;
495
- const { leverage, tx, collateralChange, marketId } = inputs;
496
- return this.fetchApiTxObject(`${this.vaultId ? "vault" : "account"}/` +
497
- "transactions/set-leverage", Object.assign({ leverage,
498
- marketId,
499
- collateralChange, txKind: yield ((_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Transactions().fetchBase64TxKindFromTx({ tx })), walletAddress: this.ownerAddress() }, ("vaultId" in this.accountCap
500
- ? {
501
- vaultId: this.accountCap.vaultId,
502
- }
503
- : {
504
- accountId: this.accountCap.accountId,
505
- })), undefined, {
506
- txKind: true,
507
- });
313
+ const { market, orderDatas } = inputs;
314
+ return this.fetchApiTransaction("transactions/reduce-orders", Object.assign(Object.assign({}, inputs), { orderIds: orderDatas.map((order) => order.orderId), sizesToSubtract: orderDatas.map((order) => order.sizeToSubtract), packageId: market.marketData.packageId, marketInitialSharedVersion: market.marketData.initialSharedVersion, marketId: market.marketId, basePriceFeedId: market.marketParams.basePriceFeedId, collateralPriceFeedId: market.marketParams.collateralPriceFeedId, collateralCoinType: this.accountCap.collateralCoinType, accountCapId: this.accountCap.objectId }));
508
315
  });
509
316
  }
510
317
  // =========================================================================
@@ -512,117 +319,77 @@ class PerpetualsAccount extends caller_1.Caller {
512
319
  // =========================================================================
513
320
  getClosePositionTx(inputs) {
514
321
  return __awaiter(this, void 0, void 0, function* () {
515
- return this.getPlaceMarketOrderTx(Object.assign({}, this.closePositionTxInputs(inputs)));
322
+ return this.getPlaceMarketOrderTx(this.closePositionTxInputs(inputs));
516
323
  });
517
324
  }
518
325
  // =========================================================================
519
- // Interactions
326
+ // Inspections
520
327
  // =========================================================================
521
- getStopOrdersMessageToSign(inputs) {
522
- var _a;
523
- return {
524
- action: "GET_STOP_ORDERS",
525
- account_id: this.accountCap.accountId
526
- .toString()
527
- .replaceAll("n", ""),
528
- clearing_house_ids: (_a = inputs === null || inputs === void 0 ? void 0 : inputs.marketIds) !== null && _a !== void 0 ? _a : [],
529
- };
328
+ setPositionLeverage(inputs) {
329
+ return __awaiter(this, void 0, void 0, function* () {
330
+ return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/set-position-leverage`, inputs);
331
+ });
530
332
  }
531
- // public async getPlaceOrderPreview(
532
- // inputs: SdkPerpetualsPlaceOrderPreviewInputs,
533
- // abortSignal?: AbortSignal
534
- // ): Promise<
535
- // | {
536
- // error: string;
537
- // }
538
- // | {
539
- // positionAfterOrder: PerpetualsPosition;
540
- // priceSlippage: number;
541
- // percentSlippage: Percentage;
542
- // filledSize: number;
543
- // filledSizeUsd: number;
544
- // postedSize: number;
545
- // postedSizeUsd: number;
546
- // collateralChange: number;
547
- // executionPrice: number;
548
- // }
333
+ // public async getAllPositionLeverages(): Promise<
334
+ // {
335
+ // marketId: PerpetualsMarketId;
336
+ // leverage: number;
337
+ // }[]
549
338
  // > {
550
- // return this.fetchApi<
551
- // ApiPerpetualsPreviewOrderResponse,
552
- // ApiPerpetualsPreviewOrderBody
553
- // >(
554
- // `${this.vaultId ? "vault" : "account"}/` +"previews/place-order",
555
- // {
556
- // ...inputs,
557
- // accountId: this.accountCap.accountId,
558
- // collateralCoinType: this.accountCap.collateralCoinType,
559
- // },
560
- // abortSignal
339
+ // return this.fetchApi(
340
+ // `${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/position-leverages`
561
341
  // );
562
342
  // }
563
- getPlaceMarketOrderPreview(inputs, abortSignal) {
343
+ getPositionLeverages(inputs) {
564
344
  return __awaiter(this, void 0, void 0, function* () {
565
- return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` +
566
- "previews/place-market-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
567
- ? {
568
- vaultId: this.accountCap.vaultId,
569
- }
570
- : {
571
- accountId: this.accountCap.accountId,
572
- })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
345
+ return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/position-leverages/${JSON.stringify(inputs.marketIds)}`);
573
346
  });
574
347
  }
575
- getPlaceLimitOrderPreview(inputs, abortSignal) {
348
+ setPositionLeverageMessageToSign(inputs) {
349
+ return {
350
+ account_id: Number(this.accountCap.accountId),
351
+ market_id: inputs.marketId,
352
+ leverage: inputs.leverage,
353
+ };
354
+ }
355
+ getOrderPreview(inputs, abortSignal) {
576
356
  return __awaiter(this, void 0, void 0, function* () {
577
- return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` +
578
- "previews/place-limit-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
579
- ? {
580
- vaultId: this.accountCap.vaultId,
581
- }
582
- : {
583
- accountId: this.accountCap.accountId,
584
- })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
357
+ const response = yield this.fetchApi("preview-order", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
358
+ if ("error" in response)
359
+ return response;
360
+ const { collateralChange } = response, remainingResponse = __rest(response, ["collateralChange"]);
361
+ return Object.assign(Object.assign({}, remainingResponse), { collateralChange: __1.Coin.normalizeBalance(collateralChange, this.collateralDecimals()) });
585
362
  });
586
363
  }
587
- getCancelOrdersPreview(inputs, abortSignal) {
364
+ getCancelOrdersPreview(inputs) {
588
365
  return __awaiter(this, void 0, void 0, function* () {
589
366
  // NOTE: should this case return an error instead ?
590
367
  if (Object.keys(inputs.marketIdsToData).length <= 0)
591
368
  return {
592
- collateralChange: 0,
369
+ collateralChange: BigInt(0),
593
370
  marketIdsToPositionAfterCancelOrders: {},
594
371
  };
595
- return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/cancel-orders", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
596
- ? {
597
- vaultId: this.accountCap.vaultId,
598
- }
599
- : {
600
- accountId: this.accountCap.accountId,
601
- })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
602
- });
603
- }
604
- getReduceOrderPreview(inputs, abortSignal) {
605
- return __awaiter(this, void 0, void 0, function* () {
606
- return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/reduce-order", Object.assign(Object.assign(Object.assign({}, inputs), ("vaultId" in this.accountCap
607
- ? {
608
- vaultId: this.accountCap.vaultId,
609
- }
610
- : {
611
- accountId: this.accountCap.accountId,
612
- })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
372
+ const response = yield this.fetchApi("preview-cancel-orders", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }));
373
+ if ("error" in response)
374
+ return response;
375
+ return {
376
+ marketIdsToPositionAfterCancelOrders: response.marketIdsToPositionAfterCancelOrders,
377
+ collateralChange: __1.Coin.normalizeBalance(response.collateralChange, this.collateralDecimals()),
378
+ };
613
379
  });
614
380
  }
615
- getSetLeveragePreview(inputs, abortSignal) {
381
+ getReduceOrdersPreview(inputs) {
616
382
  return __awaiter(this, void 0, void 0, function* () {
617
- const { marketId, leverage } = inputs;
618
- return this.fetchApi(`${this.vaultId ? "vault" : "account"}/` + "previews/set-leverage", Object.assign(Object.assign({ marketId,
619
- leverage }, ("vaultId" in this.accountCap
620
- ? {
621
- vaultId: this.accountCap.vaultId,
622
- }
623
- : {
624
- accountId: this.accountCap.accountId,
625
- })), { collateralCoinType: this.accountCap.collateralCoinType }), abortSignal);
383
+ // NOTE: should this case not throw an error instead ?
384
+ if (Object.keys(inputs.orderDatas).length <= 0)
385
+ throw new Error("no orderDatas provided");
386
+ const response = yield this.fetchApi("preview-reduce-orders", Object.assign(Object.assign({}, inputs), { orderIds: inputs.orderDatas.map((order) => order.orderId), sizesToSubtract: inputs.orderDatas.map((order) => order.sizeToSubtract), accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }));
387
+ if ("error" in response)
388
+ return response;
389
+ return {
390
+ positionAfterReduceOrders: response.positionAfterReduceOrders,
391
+ collateralChange: __1.Coin.normalizeBalance(response.collateralChange, this.collateralDecimals()),
392
+ };
626
393
  });
627
394
  }
628
395
  getOrderDatas() {
@@ -636,40 +403,19 @@ class PerpetualsAccount extends caller_1.Caller {
636
403
  ], []);
637
404
  if (orderDatas.length <= 0)
638
405
  return [];
639
- return this.fetchApi("account/order-datas", {
640
- accountId: this.accountCap.accountId,
406
+ return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/order-datas`, {
641
407
  orderDatas,
642
408
  });
643
409
  });
644
410
  }
645
- getStopOrderDatas(inputs) {
646
- return __awaiter(this, void 0, void 0, function* () {
647
- const { bytes, signature, marketIds } = inputs;
648
- return this.fetchApi("account/stop-order-datas", {
649
- bytes,
650
- signature,
651
- accountId: this.accountCap.accountId,
652
- walletAddress: this.ownerAddress(),
653
- marketIds: marketIds !== null && marketIds !== void 0 ? marketIds : [],
654
- });
655
- });
656
- }
657
411
  getCollateralHistory(inputs) {
658
412
  return __awaiter(this, void 0, void 0, function* () {
659
- return this.fetchApi("account/collateral-history", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId, collateralCoinType: this.accountCap.collateralCoinType }));
413
+ return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/collateral-history`, inputs);
660
414
  });
661
415
  }
662
416
  getOrderHistory(inputs) {
663
417
  return __awaiter(this, void 0, void 0, function* () {
664
- return this.fetchApi("account/trade-history", Object.assign(Object.assign({}, inputs), { accountId: this.accountCap.accountId }));
665
- });
666
- }
667
- getMarginHistory() {
668
- return __awaiter(this, void 0, void 0, function* () {
669
- return this.fetchApi("account/margin-history", {
670
- accountId: this.accountCap.accountId,
671
- collateralCoinType: this.accountCap.collateralCoinType,
672
- });
418
+ return this.fetchApi(`${this.accountCap.collateralCoinType}/accounts/${this.accountCap.accountId}/order-history`, inputs);
673
419
  });
674
420
  }
675
421
  // =========================================================================
@@ -683,74 +429,8 @@ class PerpetualsAccount extends caller_1.Caller {
683
429
  return undefined;
684
430
  }
685
431
  }
686
- nonSlTpStopOrderDatas(inputs) {
687
- const { stopOrderDatas } = inputs;
688
- const slTpOrders = this.slTpStopOrderDatas(inputs);
689
- const stopOrders = stopOrderDatas.filter((stopOrder) => !(slTpOrders !== null && slTpOrders !== void 0 ? slTpOrders : [])
690
- .map((slTpOrder) => JSON.stringify(slTpOrder))
691
- .includes(JSON.stringify(stopOrder)));
692
- return stopOrders.length <= 0 ? undefined : stopOrders;
693
- }
694
- slTpStopOrderDatas(inputs) {
695
- const { stopOrderDatas } = inputs;
696
- let slTpOrders = [];
697
- for (const { marketId } of this.account.positions) {
698
- const { fullSlTpOrder, partialSlTpOrders } = this.slTpStopOrderDatasForMarketId({
699
- marketId,
700
- stopOrderDatas,
701
- });
702
- slTpOrders = [
703
- ...slTpOrders,
704
- ...(fullSlTpOrder ? [fullSlTpOrder] : []),
705
- ...(partialSlTpOrders !== null && partialSlTpOrders !== void 0 ? partialSlTpOrders : []),
706
- ];
707
- }
708
- return slTpOrders.length <= 0 ? undefined : slTpOrders;
709
- }
710
- nonSlTpStopOrderDatasForMarketId(inputs) {
711
- const { marketId, stopOrderDatas } = inputs;
712
- const position = this.positionForMarketId({ marketId });
713
- if (!position)
714
- return undefined;
715
- const { fullSlTpOrder, partialSlTpOrders } = this.slTpStopOrderDatasForMarketId(inputs);
716
- const stopOrders = stopOrderDatas.filter((stopOrder) => ![
717
- ...(fullSlTpOrder ? [fullSlTpOrder] : []),
718
- ...(partialSlTpOrders !== null && partialSlTpOrders !== void 0 ? partialSlTpOrders : []),
719
- ]
720
- .map((slTpOrder) => JSON.stringify(slTpOrder))
721
- .includes(JSON.stringify(stopOrder)));
722
- return stopOrders.length <= 0 ? undefined : stopOrders;
723
- }
724
- slTpStopOrderDatasForMarketId(inputs) {
725
- const { marketId, stopOrderDatas } = inputs;
726
- const position = this.positionForMarketId({ marketId });
727
- if (!position || position.baseAssetAmount === 0) {
728
- return {
729
- fullSlTpOrder: undefined,
730
- partialSlTpOrders: undefined,
731
- };
732
- }
733
- const side = !position ? undefined : perpetuals_1.Perpetuals.positionSide(position);
734
- // TODO: clean this up
735
- const fullSlTpOrder = stopOrderDatas.find((order) => order.marketId === marketId &&
736
- order.slTp &&
737
- order.side !== side &&
738
- (order.slTp.stopLossIndexPrice ||
739
- order.slTp.takeProfitIndexPrice) &&
740
- order.size >= utils_1.Casting.i64MaxBigInt);
741
- const partialSlTpOrders = stopOrderDatas.filter((order) => order.marketId === marketId &&
742
- order.slTp &&
743
- order.side !== side &&
744
- (order.slTp.stopLossIndexPrice ||
745
- order.slTp.takeProfitIndexPrice) &&
746
- order.size < utils_1.Casting.i64MaxBigInt);
747
- return {
748
- fullSlTpOrder,
749
- partialSlTpOrders: partialSlTpOrders.length <= 0 ? undefined : partialSlTpOrders,
750
- };
751
- }
752
432
  collateral() {
753
- return this.account.availableCollateral;
433
+ return utils_1.Casting.IFixed.numberFromIFixed(this.accountCap.collateral);
754
434
  }
755
435
  collateralDecimals() {
756
436
  return this.accountCap.collateralDecimals;
@@ -758,27 +438,6 @@ class PerpetualsAccount extends caller_1.Caller {
758
438
  collateralBalance() {
759
439
  return __1.Coin.normalizeBalance(this.collateral(), this.collateralDecimals());
760
440
  }
761
- isVault() {
762
- return this.isVault !== undefined;
763
- }
764
- ownerAddress() {
765
- return "walletAddress" in this.accountCap
766
- ? // TODO: change to ownerAddress ?
767
- this.accountCap.walletAddress
768
- : this.accountCap.ownerAddress;
769
- }
770
- accountObjectId() {
771
- return this.accountCap.accountObjectId;
772
- }
773
- accountId() {
774
- return this.accountCap.accountId;
775
- }
776
- // TODO: make this work with vaults
777
- accountCapId() {
778
- if ("vaultId" in this.accountCap)
779
- throw new Error("not account cap id present on vault owned account");
780
- return this.accountCap.objectId;
781
- }
782
441
  }
783
442
  exports.PerpetualsAccount = PerpetualsAccount;
784
443
  // =========================================================================