@reyaxyz/api-sdk 0.117.0 → 0.117.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.
package/README.md CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-1.61%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-7.2%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-0.64%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-1.62%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-1.57%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-7.14%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-0.64%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-1.58%25-red.svg?style=flat) |
10
10
 
@@ -53,16 +53,22 @@ var TradeSimulationClient = /** @class */ (function () {
53
53
  // Method to asynchronously load data based on marketId and accountId
54
54
  TradeSimulationClient.prototype.arm = function (params) {
55
55
  return __awaiter(this, void 0, void 0, function () {
56
- var _a;
57
- return __generator(this, function (_b) {
58
- switch (_b.label) {
56
+ var tradeSimulationState, userAccountExposure, passivePoolExposure;
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
59
  case 0:
60
60
  this.marketId = params.marketId;
61
61
  this.accountId = params.marginAccountId;
62
- _a = this;
63
62
  return [4 /*yield*/, this.fetchMarketData(this.marketId, this.accountId)];
64
63
  case 1:
65
- _a.loadedData = _b.sent();
64
+ tradeSimulationState = _a.sent();
65
+ userAccountExposure = new common_1.ExposureCommand(tradeSimulationState.exposureDataAccount.accountId, tradeSimulationState.exposureDataAccount.rootCollateralPoolId, tradeSimulationState.exposureDataAccount.oraclePricePerMarket, tradeSimulationState.exposureDataAccount.accountBalancePerAsset, tradeSimulationState.exposureDataAccount.groupedByCollateral, tradeSimulationState.exposureDataAccount.riskMultipliers, tradeSimulationState.exposureDataAccount.riskMatrices, tradeSimulationState.exposureDataAccount.exchangeInfoPerAsset, tradeSimulationState.exposureDataAccount.positionInfoMarketConfiguration, tradeSimulationState.exposureDataAccount.uniqueTokenAddresses, tradeSimulationState.exposureDataAccount.uniqueQuoteCollaterals, tradeSimulationState.exposureDataAccount.tokenMarginInfoPerAsset, tradeSimulationState.exposureDataAccount.realizedPnLSum, tradeSimulationState.exposureDataAccount.unrealizedPnLSum, tradeSimulationState.exposureDataAccount.mtmRpnlSum, tradeSimulationState.exposureDataAccount.collateralAddressToExchangePrice);
66
+ passivePoolExposure = new common_1.ExposureCommand(tradeSimulationState.exposureDataPassivePool.accountId, tradeSimulationState.exposureDataPassivePool.rootCollateralPoolId, tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket, tradeSimulationState.exposureDataPassivePool.accountBalancePerAsset, tradeSimulationState.exposureDataPassivePool.groupedByCollateral, tradeSimulationState.exposureDataPassivePool.riskMultipliers, tradeSimulationState.exposureDataPassivePool.riskMatrices, tradeSimulationState.exposureDataPassivePool.exchangeInfoPerAsset, tradeSimulationState.exposureDataPassivePool.positionInfoMarketConfiguration, tradeSimulationState.exposureDataPassivePool.uniqueTokenAddresses, tradeSimulationState.exposureDataPassivePool.uniqueQuoteCollaterals, tradeSimulationState.exposureDataPassivePool.tokenMarginInfoPerAsset, tradeSimulationState.exposureDataPassivePool.realizedPnLSum, tradeSimulationState.exposureDataPassivePool.unrealizedPnLSum, tradeSimulationState.exposureDataPassivePool.mtmRpnlSum, tradeSimulationState.exposureDataPassivePool.collateralAddressToExchangePrice);
67
+ this.loadedData = {
68
+ tradeSimulationState: tradeSimulationState,
69
+ userAccountExposure: userAccountExposure,
70
+ passivePoolExposure: passivePoolExposure,
71
+ };
66
72
  return [2 /*return*/];
67
73
  }
68
74
  });
@@ -80,35 +86,32 @@ var TradeSimulationClient = /** @class */ (function () {
80
86
  };
81
87
  // Synchronous method to simulate operations based on an amount
82
88
  TradeSimulationClient.prototype.simulate = function (params) {
83
- var _this = this;
84
89
  if (!this.loadedData) {
85
90
  throw new Error('Data not loaded. Call arm() first.');
86
91
  }
92
+ var _a = this.loadedData, tradeSimulationState = _a.tradeSimulationState, userAccountExposure = _a.userAccountExposure, passivePoolExposure = _a.passivePoolExposure;
87
93
  var amount;
88
94
  if (params.fromBase) {
89
95
  amount = params.amount;
90
96
  }
91
97
  else {
92
98
  amount = (0, bignumber_js_1.default)(params.amount)
93
- .div(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id])
99
+ .div(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id])
94
100
  .toNumber();
95
101
  }
96
- var userAccountExposure = new common_1.ExposureCommand(this.loadedData.exposureDataAccount.accountId, this.loadedData.exposureDataAccount.rootCollateralPoolId, this.loadedData.exposureDataAccount.oraclePricePerMarket, this.loadedData.exposureDataAccount.accountBalancePerAsset, this.loadedData.exposureDataAccount.groupedByCollateral, this.loadedData.exposureDataAccount.riskMultipliers, this.loadedData.exposureDataAccount.riskMatrices, this.loadedData.exposureDataAccount.exchangeInfoPerAsset, this.loadedData.exposureDataAccount.positionInfoMarketConfiguration, this.loadedData.exposureDataAccount.uniqueTokenAddresses, this.loadedData.exposureDataAccount.uniqueQuoteCollaterals, this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset, this.loadedData.exposureDataAccount.realizedPnLSum, this.loadedData.exposureDataAccount.unrealizedPnLSum, this.loadedData.exposureDataAccount.mtmRpnlSum, this.loadedData.exposureDataAccount.collateralAddressToExchangePrice);
97
- var passivePoolExposure = new common_1.ExposureCommand(this.loadedData.exposureDataPassivePool.accountId, this.loadedData.exposureDataPassivePool.rootCollateralPoolId, this.loadedData.exposureDataPassivePool.oraclePricePerMarket, this.loadedData.exposureDataPassivePool.accountBalancePerAsset, this.loadedData.exposureDataPassivePool.groupedByCollateral, this.loadedData.exposureDataPassivePool.riskMultipliers, this.loadedData.exposureDataPassivePool.riskMatrices, this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset, this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration, this.loadedData.exposureDataPassivePool.uniqueTokenAddresses, this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals, this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset, this.loadedData.exposureDataPassivePool.realizedPnLSum, this.loadedData.exposureDataPassivePool.unrealizedPnLSum, this.loadedData.exposureDataPassivePool.mtmRpnlSum, this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice);
98
102
  /*
99
103
  max amount of margin in rUSD terms that can be transferred from the source account to the destination account
100
104
  that performs the isolated position trade (PRE TRADE)
101
105
  */
102
106
  var availableMargin = userAccountExposure.getUsdNodeMarginInfo.initialDelta;
103
107
  var marginBalance = userAccountExposure.getUsdNodeMarginInfo.marginBalance;
104
- var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(amount).negated().toNumber(), this.loadedData.marketConfiguration, this.loadedData.marketStorage);
105
- var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], slippage);
106
- var fees = common_1.ExposureCommand.calculateFee(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], amount, this.loadedData.feeParameter);
107
- var _a = userAccountExposure.getUsdNodeMarginInfoPostTrade(amount, this.loadedData.marketStorage.quote_collateral, this.loadedData.marketConfiguration, this.loadedData.marketStorage.risk_block_id), newMarginInfo = _a.usdNodeMarginInfo, tokenMarginInfoPerAsset = _a.tokenMarginInfoPerAsset;
108
+ var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(amount).negated().toNumber(), tradeSimulationState.marketConfiguration, tradeSimulationState.marketStorage);
109
+ var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id], slippage);
110
+ var fees = common_1.ExposureCommand.calculateFee(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id], amount, tradeSimulationState.feeParameter);
111
+ var _b = userAccountExposure.getUsdNodeMarginInfoPostTrade(amount, tradeSimulationState.marketStorage.quote_collateral, tradeSimulationState.marketConfiguration, tradeSimulationState.marketStorage.risk_block_id), newMarginInfo = _b.usdNodeMarginInfo, tokenMarginInfoPerAsset = _b.tokenMarginInfoPerAsset;
108
112
  var newQuoteTokenMarginInfo = tokenMarginInfoPerAsset.find(function (marginInfo) {
109
- var _a, _b;
110
113
  return (marginInfo.assetAddress ===
111
- ((_b = (_a = _this.loadedData) === null || _a === void 0 ? void 0 : _a.marketStorage) === null || _b === void 0 ? void 0 : _b.quote_collateral));
114
+ tradeSimulationState.marketStorage.quote_collateral);
112
115
  });
113
116
  if (!newQuoteTokenMarginInfo) {
114
117
  throw new Error('Error performing simulation');
@@ -119,10 +122,10 @@ var TradeSimulationClient = /** @class */ (function () {
119
122
  * */
120
123
  var requiredMargin = newQuoteTokenMarginInfo.marginBalance -
121
124
  newQuoteTokenMarginInfo.initialDelta;
122
- var liquidationPrice = common_1.ExposureCommand.calculateLiquidation(newMarginInfo.marginBalance, newQuoteTokenMarginInfo.liquidationMarginRequirement, this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], amount);
125
+ var liquidationPrice = common_1.ExposureCommand.calculateLiquidation(newMarginInfo.marginBalance, newQuoteTokenMarginInfo.liquidationMarginRequirement, tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id], amount);
123
126
  var marginRatio = common_1.ExposureCommand.getMarginRatio(newMarginInfo);
124
127
  var marginRatioHealth = common_1.ExposureCommand.evaluateHealthStatus(marginRatio * 100);
125
- var baseSpacing = (0, common_1.amountNormalizer)(this.loadedData.marketConfiguration.base_spacing).toNumber();
128
+ var baseSpacing = (0, common_1.amountNormalizer)(tradeSimulationState.marketConfiguration.base_spacing).toNumber();
126
129
  var snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
127
130
  var snappedAmount = snappedAmountInBase * estimatedPrice;
128
131
  var xpEarnRangeMin = Math.round(Math.abs(snappedAmount) / common_2.INSTANT_TRADING_RATE_XP);
@@ -146,26 +149,26 @@ var TradeSimulationClient = /** @class */ (function () {
146
149
  maxSlippage: 1,
147
150
  };
148
151
  };
149
- TradeSimulationClient.prototype.computeLimitOrderLMR = function (exposure) {
152
+ TradeSimulationClient.prototype.getRiskMatrixElement = function () {
150
153
  if (!this.loadedData) {
151
154
  throw new Error('Data not loaded. Call arm() first.');
152
155
  }
153
- var riskBlockId = this.loadedData.marketStorage.risk_block_id;
154
- var riskMatrices = this.loadedData.exposureDataAccount.riskMatrices;
156
+ var tradeSimulationState = this.loadedData.tradeSimulationState;
157
+ var riskBlockId = tradeSimulationState.marketStorage.risk_block_id;
158
+ var riskMatrices = tradeSimulationState.exposureDataAccount.riskMatrices;
155
159
  var riskMatrix = riskMatrices.find(function (riskMatrix) { return riskMatrix.risk_block_id === riskBlockId; });
156
160
  if (!riskMatrix) {
157
161
  throw new Error('Risk matrix not found');
158
162
  }
159
- var riskMatrixIndex = this.loadedData.marketConfiguration.risk_matrix_index;
160
- var riskMatrixElement = riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex];
161
- var lmr = common_1.ExposureCommand.computeLiquidationMarginRequirement([[riskMatrixElement]], [(0, common_1.amountDenormalizer)(exposure)]);
162
- return (0, common_1.amountNormalizer)(lmr).toNumber();
163
+ var riskMatrixIndex = tradeSimulationState.marketConfiguration.risk_matrix_index;
164
+ return Number(riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex]);
163
165
  };
164
166
  // Synchronous method to simulate operations based on an amount
165
167
  TradeSimulationClient.prototype.simulateLimit = function (params) {
166
168
  if (!this.loadedData) {
167
169
  throw new Error('Data not loaded. Call arm() first.');
168
170
  }
171
+ var _a = this.loadedData, tradeSimulationState = _a.tradeSimulationState, userAccountExposure = _a.userAccountExposure;
169
172
  var amount;
170
173
  if (params.fromBase) {
171
174
  amount = params.amount;
@@ -173,17 +176,18 @@ var TradeSimulationClient = /** @class */ (function () {
173
176
  else {
174
177
  amount = (0, bignumber_js_1.default)(params.amount).div(params.triggerPrice).toNumber();
175
178
  }
176
- var baseSpacing = (0, common_1.amountNormalizer)(this.loadedData.marketConfiguration.base_spacing).toNumber();
179
+ var baseSpacing = (0, common_1.amountNormalizer)(tradeSimulationState.marketConfiguration.base_spacing).toNumber();
177
180
  var snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
178
181
  var snappedAmount = snappedAmountInBase * params.triggerPrice;
179
- var fees = common_1.ExposureCommand.calculateFee(params.triggerPrice, amount, this.loadedData.feeParameter);
180
- var userAccountExposure = new common_1.ExposureCommand(this.loadedData.exposureDataAccount.accountId, this.loadedData.exposureDataAccount.rootCollateralPoolId, this.loadedData.exposureDataAccount.oraclePricePerMarket, this.loadedData.exposureDataAccount.accountBalancePerAsset, this.loadedData.exposureDataAccount.groupedByCollateral, this.loadedData.exposureDataAccount.riskMultipliers, this.loadedData.exposureDataAccount.riskMatrices, this.loadedData.exposureDataAccount.exchangeInfoPerAsset, this.loadedData.exposureDataAccount.positionInfoMarketConfiguration, this.loadedData.exposureDataAccount.uniqueTokenAddresses, this.loadedData.exposureDataAccount.uniqueQuoteCollaterals, this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset, this.loadedData.exposureDataAccount.realizedPnLSum, this.loadedData.exposureDataAccount.unrealizedPnLSum, this.loadedData.exposureDataAccount.mtmRpnlSum, this.loadedData.exposureDataAccount.collateralAddressToExchangePrice);
182
+ var fees = common_1.ExposureCommand.calculateFee(params.triggerPrice, amount, tradeSimulationState.feeParameter);
181
183
  var marginBalance = userAccountExposure.getUsdNodeMarginInfo.marginBalance;
182
184
  var availableMargin = userAccountExposure.getUsdNodeMarginInfo.initialDelta;
185
+ var imrMultiplier = (0, common_1.amountNormalizer)(String(userAccountExposure.riskMultipliers.im_multiplier)).toNumber();
186
+ var riskMatrixElement = this.getRiskMatrixElement();
183
187
  var accountLMR = userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement;
184
- var accountIMR = accountLMR * userAccountExposure.riskMultipliers.im_multiplier;
185
- var limitOrderLMR = this.computeLimitOrderLMR(snappedAmount);
186
- var limitOrderIMR = limitOrderLMR * userAccountExposure.riskMultipliers.im_multiplier;
188
+ var accountIMR = accountLMR * imrMultiplier;
189
+ var limitOrderLMR = Math.sqrt(riskMatrixElement) * Math.abs(snappedAmount);
190
+ var limitOrderIMR = limitOrderLMR * imrMultiplier;
187
191
  var requiredMargin = accountIMR + limitOrderIMR;
188
192
  var liquidationMarginRequirement = userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement +
189
193
  limitOrderLMR;
@@ -204,31 +208,47 @@ var TradeSimulationClient = /** @class */ (function () {
204
208
  requiredMargin: requiredMargin,
205
209
  };
206
210
  };
211
+ TradeSimulationClient.prototype.getMaxAmountForLimitOrder = function (params) {
212
+ if (!this.loadedData) {
213
+ throw new Error('Data not loaded. Call arm() first.');
214
+ }
215
+ var userAccountExposure = this.loadedData.userAccountExposure;
216
+ var riskMatrixElement = this.getRiskMatrixElement();
217
+ var imrMultiplier = (0, common_1.amountNormalizer)(String(userAccountExposure.riskMultipliers.im_multiplier)).toNumber();
218
+ var availableMargin = userAccountExposure.getUsdNodeMarginInfo.initialDelta;
219
+ var maxAmountSize = availableMargin / Math.sqrt(riskMatrixElement) / imrMultiplier;
220
+ var maxAmountBase = maxAmountSize / params.triggerPrice;
221
+ return {
222
+ maxAmountBase: maxAmountBase,
223
+ maxAmountSize: maxAmountSize,
224
+ };
225
+ };
207
226
  TradeSimulationClient.prototype.convertValue = function (params) {
208
227
  if (!this.loadedData) {
209
228
  throw new Error('Data not loaded. Call arm() first.');
210
229
  }
230
+ var tradeSimulationState = this.loadedData.tradeSimulationState;
211
231
  if (!params.fromBase)
212
232
  return (0, bignumber_js_1.default)(params.amount)
213
- .div(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id])
233
+ .div(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id])
214
234
  .toNumber();
215
235
  else
216
236
  return (0, bignumber_js_1.default)(params.amount)
217
- .times(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id])
237
+ .times(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id])
218
238
  .toNumber();
219
239
  };
220
240
  TradeSimulationClient.prototype.convertValueEstimatedPrice = function (params) {
221
241
  if (!this.loadedData) {
222
242
  throw new Error('Data not loaded. Call arm() first.');
223
243
  }
224
- var passivePoolExposure = new common_1.ExposureCommand(this.loadedData.exposureDataPassivePool.accountId, this.loadedData.exposureDataPassivePool.rootCollateralPoolId, this.loadedData.exposureDataPassivePool.oraclePricePerMarket, this.loadedData.exposureDataPassivePool.accountBalancePerAsset, this.loadedData.exposureDataPassivePool.groupedByCollateral, this.loadedData.exposureDataPassivePool.riskMultipliers, this.loadedData.exposureDataPassivePool.riskMatrices, this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset, this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration, this.loadedData.exposureDataPassivePool.uniqueTokenAddresses, this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals, this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset, this.loadedData.exposureDataPassivePool.realizedPnLSum, this.loadedData.exposureDataPassivePool.unrealizedPnLSum, this.loadedData.exposureDataPassivePool.mtmRpnlSum, this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice);
244
+ var _a = this.loadedData, tradeSimulationState = _a.tradeSimulationState, passivePoolExposure = _a.passivePoolExposure;
225
245
  var amountForSlippage = params.fromBase
226
246
  ? params.amount
227
247
  : (0, bignumber_js_1.default)(params.amount)
228
- .div(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id])
248
+ .div(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id])
229
249
  .toNumber();
230
- var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(amountForSlippage).negated().toNumber(), this.loadedData.marketConfiguration, this.loadedData.marketStorage);
231
- var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], slippage);
250
+ var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(amountForSlippage).negated().toNumber(), tradeSimulationState.marketConfiguration, tradeSimulationState.marketStorage);
251
+ var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id], slippage);
232
252
  if (!params.fromBase)
233
253
  return (0, bignumber_js_1.default)(params.amount).div(estimatedPrice).toNumber();
234
254
  else
@@ -238,10 +258,10 @@ var TradeSimulationClient = /** @class */ (function () {
238
258
  if (!this.loadedData) {
239
259
  throw new Error('Data not loaded. Call arm() first.');
240
260
  }
241
- var passivePoolExposure = new common_1.ExposureCommand(this.loadedData.exposureDataPassivePool.accountId, this.loadedData.exposureDataPassivePool.rootCollateralPoolId, this.loadedData.exposureDataPassivePool.oraclePricePerMarket, this.loadedData.exposureDataPassivePool.accountBalancePerAsset, this.loadedData.exposureDataPassivePool.groupedByCollateral, this.loadedData.exposureDataPassivePool.riskMultipliers, this.loadedData.exposureDataPassivePool.riskMatrices, this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset, this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration, this.loadedData.exposureDataPassivePool.uniqueTokenAddresses, this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals, this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset, this.loadedData.exposureDataPassivePool.realizedPnLSum, this.loadedData.exposureDataPassivePool.unrealizedPnLSum, this.loadedData.exposureDataPassivePool.mtmRpnlSum, this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice);
242
- var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(params.amount).negated().toNumber(), this.loadedData.marketConfiguration, this.loadedData.marketStorage);
243
- var price = this.loadedData.exposureDataAccount.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id];
244
- var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], slippage);
261
+ var _a = this.loadedData, tradeSimulationState = _a.tradeSimulationState, passivePoolExposure = _a.passivePoolExposure;
262
+ var slippage = passivePoolExposure.getSlippage((0, bignumber_js_1.default)(params.amount).negated().toNumber(), tradeSimulationState.marketConfiguration, tradeSimulationState.marketStorage);
263
+ var price = tradeSimulationState.exposureDataAccount.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id];
264
+ var estimatedPrice = common_1.ExposureCommand.calculateEstimatedPrice(tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[tradeSimulationState.marketConfiguration.market_id], slippage);
245
265
  return {
246
266
  estimatedPrice: estimatedPrice,
247
267
  markPrice: price,
@@ -263,11 +283,12 @@ var TradeSimulationClient = /** @class */ (function () {
263
283
  if (!this.loadedData || !this.marketId) {
264
284
  throw new Error('Data not loaded. Call arm() first.');
265
285
  }
286
+ var tradeSimulationState = this.loadedData.tradeSimulationState;
266
287
  // update price
267
288
  var marketId = this.marketId;
268
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
289
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[marketId] = price;
290
+ tradeSimulationState.exposureDataAccount.oraclePricePerMarket[marketId] =
269
291
  price;
270
- this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
271
292
  };
272
293
  return TradeSimulationClient;
273
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,cAAc,GAAG,wBAAe,CAAC,uBAAuB,CAC5D,IAAI,CAAC,UAAU,CAAC,uBAAuB,CAAC,oBAAoB,CAC1D,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,SAAS,CAC9C,EACD,QAAQ,CACT,CAAC;QAEF,OAAO;YACL,cAAc,gBAAA;YACd,SAAS,EAAE,KAAK;SACjB,CAAC;IACJ,CAAC;IAED,kDAAkB,GAAlB,UAAmB,MAAc,EAAE,WAAmB;QACpD,IAAM,aAAa,GAAG,IAAA,sBAAS,EAAC,MAAM,CAAC;aACpC,GAAG,EAAE;aACL,SAAS,CAAC,WAAW,CAAC;aACtB,YAAY,CAAC,sBAAS,CAAC,WAAW,CAAC;aACnC,YAAY,CAAC,WAAW,CAAC;aACzB,QAAQ,EAAE,CAAC;QAEd,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;YACf,OAAO,CAAC,aAAa,CAAC;QACxB,CAAC;QACD,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,2CAAW,GAAX,UAAY,KAAa;QACvB,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACxD,CAAC;QACD,eAAe;QACf,IAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC/B,IAAI,CAAC,UAAU,CAAC,uBAAuB,CAAC,oBAAoB,CAAC,QAAQ,CAAC;YACpE,KAAK,CAAC;QACR,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,oBAAoB,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;IAC7E,CAAC;IACH,4BAAC;AAAD,CAAC,AAhdD,IAgdC","sourcesContent":["import {\n EstimatedPriceParams,\n EstimatedPriceResult,\n SimulateLimitTradeEntity,\n SimulateTradeEntity,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueParams,\n TradeSimulationConvertValueResult,\n TradeSimulationLoadDataParams,\n TradeSimulationSimulateLimitParams,\n TradeSimulationSimulateParams,\n} from './types';\nimport AccountClient from '../account';\nimport {\n amountDenormalizer,\n amountNormalizer,\n ExposureCommand,\n MarginInfo,\n TradeSimulationState,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { INSTANT_TRADING_RATE_XP } from '@reyaxyz/common';\n\nexport default class TradeSimulationClient {\n private marketId: number | null = null;\n private accountId: number | null = null;\n private loadedData: TradeSimulationState | null = null;\n private accountClient: AccountClient;\n constructor(accountClient: AccountClient) {\n // Constructor added\n this.accountClient = accountClient;\n }\n\n // Method to asynchronously load data based on marketId and accountId\n async arm(params: TradeSimulationLoadDataParams): Promise<void> {\n this.marketId = params.marketId;\n this.accountId = params.marginAccountId;\n\n this.loadedData = await this.fetchMarketData(this.marketId, this.accountId);\n }\n\n private async fetchMarketData(\n marketId: number,\n accountId: number,\n ): Promise<TradeSimulationState> {\n return this.accountClient.getTransactionSimulationInitialData({\n marginAccountId: accountId,\n marketId: marketId,\n });\n }\n\n // Synchronous method to simulate operations based on an amount\n simulate(params: TradeSimulationSimulateParams): SimulateTradeEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n let amount;\n if (params.fromBase) {\n amount = params.amount;\n } else {\n amount = BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n }\n\n const userAccountExposure = new ExposureCommand(\n this.loadedData.exposureDataAccount.accountId,\n this.loadedData.exposureDataAccount.rootCollateralPoolId,\n this.loadedData.exposureDataAccount.oraclePricePerMarket,\n this.loadedData.exposureDataAccount.accountBalancePerAsset,\n this.loadedData.exposureDataAccount.groupedByCollateral,\n this.loadedData.exposureDataAccount.riskMultipliers,\n this.loadedData.exposureDataAccount.riskMatrices,\n this.loadedData.exposureDataAccount.exchangeInfoPerAsset,\n this.loadedData.exposureDataAccount.positionInfoMarketConfiguration,\n this.loadedData.exposureDataAccount.uniqueTokenAddresses,\n this.loadedData.exposureDataAccount.uniqueQuoteCollaterals,\n this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset,\n this.loadedData.exposureDataAccount.realizedPnLSum,\n this.loadedData.exposureDataAccount.unrealizedPnLSum,\n this.loadedData.exposureDataAccount.mtmRpnlSum,\n this.loadedData.exposureDataAccount.collateralAddressToExchangePrice,\n );\n\n const passivePoolExposure = new ExposureCommand(\n this.loadedData.exposureDataPassivePool.accountId,\n this.loadedData.exposureDataPassivePool.rootCollateralPoolId,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket,\n this.loadedData.exposureDataPassivePool.accountBalancePerAsset,\n this.loadedData.exposureDataPassivePool.groupedByCollateral,\n this.loadedData.exposureDataPassivePool.riskMultipliers,\n this.loadedData.exposureDataPassivePool.riskMatrices,\n this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,\n this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,\n this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,\n this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,\n this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,\n this.loadedData.exposureDataPassivePool.realizedPnLSum,\n this.loadedData.exposureDataPassivePool.unrealizedPnLSum,\n this.loadedData.exposureDataPassivePool.mtmRpnlSum,\n this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,\n );\n\n /*\n max amount of margin in rUSD terms that can be transferred from the source account to the destination account\n that performs the isolated position trade (PRE TRADE)\n */\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const marginBalance =\n userAccountExposure.getUsdNodeMarginInfo.marginBalance;\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amount).negated().toNumber(),\n this.loadedData.marketConfiguration,\n this.loadedData.marketStorage,\n );\n\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n slippage,\n );\n\n const fees = ExposureCommand.calculateFee(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n amount,\n this.loadedData.feeParameter,\n );\n\n const { usdNodeMarginInfo: newMarginInfo, tokenMarginInfoPerAsset } =\n userAccountExposure.getUsdNodeMarginInfoPostTrade(\n amount,\n this.loadedData.marketStorage.quote_collateral,\n this.loadedData.marketConfiguration,\n this.loadedData.marketStorage.risk_block_id,\n );\n\n const newQuoteTokenMarginInfo = tokenMarginInfoPerAsset.find(\n (marginInfo: MarginInfo) => {\n return (\n marginInfo.assetAddress ===\n this.loadedData?.marketStorage?.quote_collateral\n );\n },\n );\n\n if (!newQuoteTokenMarginInfo) {\n throw new Error('Error performing simulation');\n }\n\n /*\n * Note, required margin is the initial margin requirement in rUSD terms of the account after the trade.\n * margin balance rusd - initial delta rusd = margin balance rusd - (margin balance rusd - imr rusd) = imr rusd\n * */\n\n const requiredMargin =\n newQuoteTokenMarginInfo.marginBalance -\n newQuoteTokenMarginInfo.initialDelta;\n\n const liquidationPrice = ExposureCommand.calculateLiquidation(\n newMarginInfo.marginBalance,\n newQuoteTokenMarginInfo.liquidationMarginRequirement,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n amount,\n );\n\n const marginRatio = ExposureCommand.getMarginRatio(newMarginInfo);\n\n const marginRatioHealth = ExposureCommand.evaluateHealthStatus(\n marginRatio * 100,\n );\n\n const baseSpacing = amountNormalizer(\n this.loadedData.marketConfiguration.base_spacing,\n ).toNumber();\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * estimatedPrice;\n const xpEarnRangeMin = Math.round(\n Math.abs(snappedAmount) / INSTANT_TRADING_RATE_XP,\n );\n const xpEarnRangeMax = Math.round(\n (100 * Math.abs(snappedAmount)) / INSTANT_TRADING_RATE_XP,\n );\n\n return {\n estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth,\n marginBalance,\n availableMargin,\n requiredMargin,\n snappedAmount,\n snappedAmountInBase,\n xpEarnRange: {\n min: xpEarnRangeMin,\n max: xpEarnRangeMax,\n },\n maxSlippage: 1,\n } as SimulateTradeEntity;\n }\n\n private computeLimitOrderLMR(exposure: number): number {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const riskBlockId = this.loadedData.marketStorage.risk_block_id;\n const riskMatrices = this.loadedData.exposureDataAccount.riskMatrices;\n\n const riskMatrix = riskMatrices.find(\n (riskMatrix) => riskMatrix.risk_block_id === riskBlockId,\n );\n\n if (!riskMatrix) {\n throw new Error('Risk matrix not found');\n }\n\n const riskMatrixIndex =\n this.loadedData.marketConfiguration.risk_matrix_index;\n\n const riskMatrixElement =\n riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex];\n\n const lmr = ExposureCommand.computeLiquidationMarginRequirement(\n [[riskMatrixElement]],\n [amountDenormalizer(exposure)],\n );\n\n return amountNormalizer(lmr).toNumber();\n }\n\n // Synchronous method to simulate operations based on an amount\n simulateLimit(\n params: TradeSimulationSimulateLimitParams,\n ): SimulateLimitTradeEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n let amount;\n if (params.fromBase) {\n amount = params.amount;\n } else {\n amount = BigNumber(params.amount).div(params.triggerPrice).toNumber();\n }\n\n const baseSpacing = amountNormalizer(\n this.loadedData.marketConfiguration.base_spacing,\n ).toNumber();\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * params.triggerPrice;\n\n const fees = ExposureCommand.calculateFee(\n params.triggerPrice,\n amount,\n this.loadedData.feeParameter,\n );\n\n const userAccountExposure = new ExposureCommand(\n this.loadedData.exposureDataAccount.accountId,\n this.loadedData.exposureDataAccount.rootCollateralPoolId,\n this.loadedData.exposureDataAccount.oraclePricePerMarket,\n this.loadedData.exposureDataAccount.accountBalancePerAsset,\n this.loadedData.exposureDataAccount.groupedByCollateral,\n this.loadedData.exposureDataAccount.riskMultipliers,\n this.loadedData.exposureDataAccount.riskMatrices,\n this.loadedData.exposureDataAccount.exchangeInfoPerAsset,\n this.loadedData.exposureDataAccount.positionInfoMarketConfiguration,\n this.loadedData.exposureDataAccount.uniqueTokenAddresses,\n this.loadedData.exposureDataAccount.uniqueQuoteCollaterals,\n this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset,\n this.loadedData.exposureDataAccount.realizedPnLSum,\n this.loadedData.exposureDataAccount.unrealizedPnLSum,\n this.loadedData.exposureDataAccount.mtmRpnlSum,\n this.loadedData.exposureDataAccount.collateralAddressToExchangePrice,\n );\n\n const marginBalance =\n userAccountExposure.getUsdNodeMarginInfo.marginBalance;\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const accountLMR =\n userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement;\n const accountIMR =\n accountLMR * userAccountExposure.riskMultipliers.im_multiplier;\n\n const limitOrderLMR = this.computeLimitOrderLMR(snappedAmount);\n const limitOrderIMR =\n limitOrderLMR * userAccountExposure.riskMultipliers.im_multiplier;\n\n const requiredMargin = accountIMR + limitOrderIMR;\n\n const liquidationMarginRequirement =\n userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement +\n limitOrderLMR;\n\n const marginRatio = ExposureCommand.getMarginRatio({\n marginBalance,\n liquidationMarginRequirement,\n });\n\n const marginRatioHealth = ExposureCommand.evaluateHealthStatus(\n marginRatio * 100,\n );\n\n return {\n fees,\n snappedAmount,\n snappedAmountInBase,\n estimatedPrice: params.triggerPrice,\n marginRatio,\n marginRatioHealth,\n availableMargin,\n marginBalance,\n requiredMargin,\n };\n }\n\n convertValue(\n params: TradeSimulationConvertValueParams,\n ): TradeSimulationConvertValueResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n if (!params.fromBase)\n return BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n else\n return BigNumber(params.amount)\n .times(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n }\n\n convertValueEstimatedPrice(\n params: TradeSimulationConvertValueEstimatedPriceParams,\n ): TradeSimulationConvertValueResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const passivePoolExposure = new ExposureCommand(\n this.loadedData.exposureDataPassivePool.accountId,\n this.loadedData.exposureDataPassivePool.rootCollateralPoolId,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket,\n this.loadedData.exposureDataPassivePool.accountBalancePerAsset,\n this.loadedData.exposureDataPassivePool.groupedByCollateral,\n this.loadedData.exposureDataPassivePool.riskMultipliers,\n this.loadedData.exposureDataPassivePool.riskMatrices,\n this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,\n this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,\n this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,\n this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,\n this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,\n this.loadedData.exposureDataPassivePool.realizedPnLSum,\n this.loadedData.exposureDataPassivePool.unrealizedPnLSum,\n this.loadedData.exposureDataPassivePool.mtmRpnlSum,\n this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,\n );\n\n const amountForSlippage = params.fromBase\n ? params.amount\n : BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amountForSlippage).negated().toNumber(),\n this.loadedData.marketConfiguration,\n this.loadedData.marketStorage,\n );\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n slippage,\n );\n\n if (!params.fromBase)\n return BigNumber(params.amount).div(estimatedPrice).toNumber();\n else return BigNumber(params.amount).times(estimatedPrice).toNumber();\n }\n\n estimatedPrice(params: EstimatedPriceParams): EstimatedPriceResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const passivePoolExposure = new ExposureCommand(\n this.loadedData.exposureDataPassivePool.accountId,\n this.loadedData.exposureDataPassivePool.rootCollateralPoolId,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket,\n this.loadedData.exposureDataPassivePool.accountBalancePerAsset,\n this.loadedData.exposureDataPassivePool.groupedByCollateral,\n this.loadedData.exposureDataPassivePool.riskMultipliers,\n this.loadedData.exposureDataPassivePool.riskMatrices,\n this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,\n this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,\n this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,\n this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,\n this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,\n this.loadedData.exposureDataPassivePool.realizedPnLSum,\n this.loadedData.exposureDataPassivePool.unrealizedPnLSum,\n this.loadedData.exposureDataPassivePool.mtmRpnlSum,\n this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,\n );\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).negated().toNumber(),\n this.loadedData.marketConfiguration,\n this.loadedData.marketStorage,\n );\n\n const price =\n this.loadedData.exposureDataAccount.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n slippage,\n );\n\n return {\n estimatedPrice,\n markPrice: price,\n };\n }\n\n roundToBaseSpacing(amount: number, baseSpacing: number): number {\n const snappedAmount = BigNumber(amount)\n .abs()\n .dividedBy(baseSpacing)\n .integerValue(BigNumber.ROUND_FLOOR)\n .multipliedBy(baseSpacing)\n .toNumber();\n\n if (amount < 0) {\n return -snappedAmount;\n }\n return snappedAmount;\n }\n\n updatePrice(price: number): void {\n if (!this.loadedData || !this.marketId) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n // update price\n const marketId = this.marketId;\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =\n price;\n this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/trade.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,0CAKyB;AACzB,8DAAqC;AACrC,0CAA0D;AAE1D;IAUE,+BAAY,aAA4B;QAThC,aAAQ,GAAkB,IAAI,CAAC;QAC/B,cAAS,GAAkB,IAAI,CAAC;QAChC,eAAU,GAIP,IAAI,CAAC;QAId,oBAAoB;QACpB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAED,qEAAqE;IAC/D,mCAAG,GAAT,UAAU,MAAqC;;;;;;wBAC7C,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;wBAChC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,eAAe,CAAC;wBAEX,qBAAM,IAAI,CAAC,eAAe,CACrD,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,SAAS,CACf,EAAA;;wBAHK,oBAAoB,GAAG,SAG5B;wBAEK,mBAAmB,GAAG,IAAI,wBAAe,CAC7C,oBAAoB,CAAC,mBAAmB,CAAC,SAAS,EAClD,oBAAoB,CAAC,mBAAmB,CAAC,oBAAoB,EAC7D,oBAAoB,CAAC,mBAAmB,CAAC,oBAAoB,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a,CAAC;IACvB,CAAC;IAED,2CAAW,GAAX,UAAY,KAAa;QACvB,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACxD,CAAC;QAEO,IAAA,oBAAoB,GAAK,IAAI,CAAC,UAAU,qBAApB,CAAqB;QAEjD,eAAe;QACf,IAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC/B,oBAAoB,CAAC,uBAAuB,CAAC,oBAAoB,CAC/D,QAAQ,CACT,GAAG,KAAK,CAAC;QACV,oBAAoB,CAAC,mBAAmB,CAAC,oBAAoB,CAAC,QAAQ,CAAC;YACrE,KAAK,CAAC;IACV,CAAC;IACH,4BAAC;AAAD,CAAC,AAjdD,IAidC","sourcesContent":["import {\n EstimatedPriceParams,\n EstimatedPriceResult,\n LimitTradeMaxOrderSizeParams,\n LimitTradeMaxOrderSizeResult,\n SimulateLimitTradeEntity,\n SimulateTradeEntity,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueParams,\n TradeSimulationConvertValueResult,\n TradeSimulationLoadDataParams,\n TradeSimulationSimulateLimitParams,\n TradeSimulationSimulateParams,\n} from './types';\nimport AccountClient from '../account';\nimport {\n amountNormalizer,\n ExposureCommand,\n MarginInfo,\n TradeSimulationState,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { INSTANT_TRADING_RATE_XP } from '@reyaxyz/common';\n\nexport default class TradeSimulationClient {\n private marketId: number | null = null;\n private accountId: number | null = null;\n private loadedData: {\n tradeSimulationState: TradeSimulationState;\n userAccountExposure: ExposureCommand;\n passivePoolExposure: ExposureCommand;\n } | null = null;\n private accountClient: AccountClient;\n\n constructor(accountClient: AccountClient) {\n // Constructor added\n this.accountClient = accountClient;\n }\n\n // Method to asynchronously load data based on marketId and accountId\n async arm(params: TradeSimulationLoadDataParams): Promise<void> {\n this.marketId = params.marketId;\n this.accountId = params.marginAccountId;\n\n const tradeSimulationState = await this.fetchMarketData(\n this.marketId,\n this.accountId,\n );\n\n const userAccountExposure = new ExposureCommand(\n tradeSimulationState.exposureDataAccount.accountId,\n tradeSimulationState.exposureDataAccount.rootCollateralPoolId,\n tradeSimulationState.exposureDataAccount.oraclePricePerMarket,\n tradeSimulationState.exposureDataAccount.accountBalancePerAsset,\n tradeSimulationState.exposureDataAccount.groupedByCollateral,\n tradeSimulationState.exposureDataAccount.riskMultipliers,\n tradeSimulationState.exposureDataAccount.riskMatrices,\n tradeSimulationState.exposureDataAccount.exchangeInfoPerAsset,\n tradeSimulationState.exposureDataAccount.positionInfoMarketConfiguration,\n tradeSimulationState.exposureDataAccount.uniqueTokenAddresses,\n tradeSimulationState.exposureDataAccount.uniqueQuoteCollaterals,\n tradeSimulationState.exposureDataAccount.tokenMarginInfoPerAsset,\n tradeSimulationState.exposureDataAccount.realizedPnLSum,\n tradeSimulationState.exposureDataAccount.unrealizedPnLSum,\n tradeSimulationState.exposureDataAccount.mtmRpnlSum,\n tradeSimulationState.exposureDataAccount.collateralAddressToExchangePrice,\n );\n\n const passivePoolExposure = new ExposureCommand(\n tradeSimulationState.exposureDataPassivePool.accountId,\n tradeSimulationState.exposureDataPassivePool.rootCollateralPoolId,\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket,\n tradeSimulationState.exposureDataPassivePool.accountBalancePerAsset,\n tradeSimulationState.exposureDataPassivePool.groupedByCollateral,\n tradeSimulationState.exposureDataPassivePool.riskMultipliers,\n tradeSimulationState.exposureDataPassivePool.riskMatrices,\n tradeSimulationState.exposureDataPassivePool.exchangeInfoPerAsset,\n tradeSimulationState.exposureDataPassivePool.positionInfoMarketConfiguration,\n tradeSimulationState.exposureDataPassivePool.uniqueTokenAddresses,\n tradeSimulationState.exposureDataPassivePool.uniqueQuoteCollaterals,\n tradeSimulationState.exposureDataPassivePool.tokenMarginInfoPerAsset,\n tradeSimulationState.exposureDataPassivePool.realizedPnLSum,\n tradeSimulationState.exposureDataPassivePool.unrealizedPnLSum,\n tradeSimulationState.exposureDataPassivePool.mtmRpnlSum,\n tradeSimulationState.exposureDataPassivePool.collateralAddressToExchangePrice,\n );\n\n this.loadedData = {\n tradeSimulationState,\n userAccountExposure,\n passivePoolExposure,\n };\n }\n\n private async fetchMarketData(\n marketId: number,\n accountId: number,\n ): Promise<TradeSimulationState> {\n return this.accountClient.getTransactionSimulationInitialData({\n marginAccountId: accountId,\n marketId: marketId,\n });\n }\n\n // Synchronous method to simulate operations based on an amount\n simulate(params: TradeSimulationSimulateParams): SimulateTradeEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState, userAccountExposure, passivePoolExposure } =\n this.loadedData;\n\n let amount;\n if (params.fromBase) {\n amount = params.amount;\n } else {\n amount = BigNumber(params.amount)\n .div(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n )\n .toNumber();\n }\n\n /*\n max amount of margin in rUSD terms that can be transferred from the source account to the destination account\n that performs the isolated position trade (PRE TRADE)\n */\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const marginBalance =\n userAccountExposure.getUsdNodeMarginInfo.marginBalance;\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amount).negated().toNumber(),\n tradeSimulationState.marketConfiguration,\n tradeSimulationState.marketStorage,\n );\n\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n slippage,\n );\n\n const fees = ExposureCommand.calculateFee(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n amount,\n tradeSimulationState.feeParameter,\n );\n\n const { usdNodeMarginInfo: newMarginInfo, tokenMarginInfoPerAsset } =\n userAccountExposure.getUsdNodeMarginInfoPostTrade(\n amount,\n tradeSimulationState.marketStorage.quote_collateral,\n tradeSimulationState.marketConfiguration,\n tradeSimulationState.marketStorage.risk_block_id,\n );\n\n const newQuoteTokenMarginInfo = tokenMarginInfoPerAsset.find(\n (marginInfo: MarginInfo) => {\n return (\n marginInfo.assetAddress ===\n tradeSimulationState.marketStorage.quote_collateral\n );\n },\n );\n\n if (!newQuoteTokenMarginInfo) {\n throw new Error('Error performing simulation');\n }\n\n /*\n * Note, required margin is the initial margin requirement in rUSD terms of the account after the trade.\n * margin balance rusd - initial delta rusd = margin balance rusd - (margin balance rusd - imr rusd) = imr rusd\n * */\n\n const requiredMargin =\n newQuoteTokenMarginInfo.marginBalance -\n newQuoteTokenMarginInfo.initialDelta;\n\n const liquidationPrice = ExposureCommand.calculateLiquidation(\n newMarginInfo.marginBalance,\n newQuoteTokenMarginInfo.liquidationMarginRequirement,\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n amount,\n );\n\n const marginRatio = ExposureCommand.getMarginRatio(newMarginInfo);\n\n const marginRatioHealth = ExposureCommand.evaluateHealthStatus(\n marginRatio * 100,\n );\n\n const baseSpacing = amountNormalizer(\n tradeSimulationState.marketConfiguration.base_spacing,\n ).toNumber();\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * estimatedPrice;\n const xpEarnRangeMin = Math.round(\n Math.abs(snappedAmount) / INSTANT_TRADING_RATE_XP,\n );\n const xpEarnRangeMax = Math.round(\n (100 * Math.abs(snappedAmount)) / INSTANT_TRADING_RATE_XP,\n );\n\n return {\n estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth,\n marginBalance,\n availableMargin,\n requiredMargin,\n snappedAmount,\n snappedAmountInBase,\n xpEarnRange: {\n min: xpEarnRangeMin,\n max: xpEarnRangeMax,\n },\n maxSlippage: 1,\n } as SimulateTradeEntity;\n }\n\n getRiskMatrixElement(): number {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState } = this.loadedData;\n\n const riskBlockId = tradeSimulationState.marketStorage.risk_block_id;\n const riskMatrices = tradeSimulationState.exposureDataAccount.riskMatrices;\n\n const riskMatrix = riskMatrices.find(\n (riskMatrix) => riskMatrix.risk_block_id === riskBlockId,\n );\n\n if (!riskMatrix) {\n throw new Error('Risk matrix not found');\n }\n\n const riskMatrixIndex =\n tradeSimulationState.marketConfiguration.risk_matrix_index;\n\n return Number(riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex]);\n }\n\n // Synchronous method to simulate operations based on an amount\n simulateLimit(\n params: TradeSimulationSimulateLimitParams,\n ): SimulateLimitTradeEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState, userAccountExposure } = this.loadedData;\n\n let amount;\n if (params.fromBase) {\n amount = params.amount;\n } else {\n amount = BigNumber(params.amount).div(params.triggerPrice).toNumber();\n }\n\n const baseSpacing = amountNormalizer(\n tradeSimulationState.marketConfiguration.base_spacing,\n ).toNumber();\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * params.triggerPrice;\n\n const fees = ExposureCommand.calculateFee(\n params.triggerPrice,\n amount,\n tradeSimulationState.feeParameter,\n );\n\n const marginBalance =\n userAccountExposure.getUsdNodeMarginInfo.marginBalance;\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const imrMultiplier = amountNormalizer(\n String(userAccountExposure.riskMultipliers.im_multiplier),\n ).toNumber();\n\n const riskMatrixElement = this.getRiskMatrixElement();\n\n const accountLMR =\n userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement;\n const accountIMR = accountLMR * imrMultiplier;\n\n const limitOrderLMR =\n Math.sqrt(riskMatrixElement) * Math.abs(snappedAmount);\n const limitOrderIMR = limitOrderLMR * imrMultiplier;\n\n const requiredMargin = accountIMR + limitOrderIMR;\n\n const liquidationMarginRequirement =\n userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement +\n limitOrderLMR;\n\n const marginRatio = ExposureCommand.getMarginRatio({\n marginBalance,\n liquidationMarginRequirement,\n });\n\n const marginRatioHealth = ExposureCommand.evaluateHealthStatus(\n marginRatio * 100,\n );\n\n return {\n fees,\n snappedAmount,\n snappedAmountInBase,\n estimatedPrice: params.triggerPrice,\n marginRatio,\n marginRatioHealth,\n availableMargin,\n marginBalance,\n requiredMargin,\n };\n }\n\n getMaxAmountForLimitOrder(\n params: LimitTradeMaxOrderSizeParams,\n ): LimitTradeMaxOrderSizeResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { userAccountExposure } = this.loadedData;\n\n const riskMatrixElement = this.getRiskMatrixElement();\n\n const imrMultiplier = amountNormalizer(\n String(userAccountExposure.riskMultipliers.im_multiplier),\n ).toNumber();\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const maxAmountSize =\n availableMargin / Math.sqrt(riskMatrixElement) / imrMultiplier;\n const maxAmountBase = maxAmountSize / params.triggerPrice;\n\n return {\n maxAmountBase,\n maxAmountSize,\n };\n }\n\n convertValue(\n params: TradeSimulationConvertValueParams,\n ): TradeSimulationConvertValueResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState } = this.loadedData;\n\n if (!params.fromBase)\n return BigNumber(params.amount)\n .div(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n )\n .toNumber();\n else\n return BigNumber(params.amount)\n .times(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n )\n .toNumber();\n }\n\n convertValueEstimatedPrice(\n params: TradeSimulationConvertValueEstimatedPriceParams,\n ): TradeSimulationConvertValueResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState, passivePoolExposure } = this.loadedData;\n\n const amountForSlippage = params.fromBase\n ? params.amount\n : BigNumber(params.amount)\n .div(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n )\n .toNumber();\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amountForSlippage).negated().toNumber(),\n tradeSimulationState.marketConfiguration,\n tradeSimulationState.marketStorage,\n );\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n slippage,\n );\n\n if (!params.fromBase)\n return BigNumber(params.amount).div(estimatedPrice).toNumber();\n else return BigNumber(params.amount).times(estimatedPrice).toNumber();\n }\n\n estimatedPrice(params: EstimatedPriceParams): EstimatedPriceResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState, passivePoolExposure } = this.loadedData;\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).negated().toNumber(),\n tradeSimulationState.marketConfiguration,\n tradeSimulationState.marketStorage,\n );\n\n const price =\n tradeSimulationState.exposureDataAccount.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ];\n\n const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n tradeSimulationState.marketConfiguration.market_id\n ],\n slippage,\n );\n\n return {\n estimatedPrice,\n markPrice: price,\n };\n }\n\n roundToBaseSpacing(amount: number, baseSpacing: number): number {\n const snappedAmount = BigNumber(amount)\n .abs()\n .dividedBy(baseSpacing)\n .integerValue(BigNumber.ROUND_FLOOR)\n .multipliedBy(baseSpacing)\n .toNumber();\n\n if (amount < 0) {\n return -snappedAmount;\n }\n return snappedAmount;\n }\n\n updatePrice(price: number): void {\n if (!this.loadedData || !this.marketId) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const { tradeSimulationState } = this.loadedData;\n\n // update price\n const marketId = this.marketId;\n tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[\n marketId\n ] = price;\n tradeSimulationState.exposureDataAccount.oraclePricePerMarket[marketId] =\n price;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"/","sources":["clients/modules/trade.simulation/types.ts"],"names":[],"mappings":"","sourcesContent":["import { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';\n\nexport type TradeSimulationLoadDataParams = {\n marketId: MarketEntity['id'];\n marginAccountId: MarginAccountEntity['id'];\n};\n\nexport type TradeSimulationSimulateParams = {\n amount: number; // position size / base, + for long | - for short\n fromBase?: boolean;\n};\n\nexport type TradeSimulationConvertValueParams = {\n amount: number;\n fromBase: boolean;\n};\n\nexport type EstimatedPriceParams = {\n amount: number; // amount in base\n};\n\nexport type EstimatedPriceResult = {\n estimatedPrice: number;\n markPrice: number;\n};\n\nexport type TradeSimulationConvertValueEstimatedPriceParams = {\n amount: number;\n fromBase: boolean;\n};\n\nexport type TradeSimulationConvertValueEstimatedPriceResult = number;\n\nexport type SimulateTradeEntity = {\n liquidationPrice: number;\n fees: number;\n estimatedPrice: number;\n estimatedSlippage: number;\n marginRatio: MarginAccountEntity['marginRatioPercentage'];\n marginRatioHealth: MarginAccountEntity['marginRatioHealth'];\n availableMargin: number;\n marginBalance: number;\n requiredMargin: number;\n snappedAmount: number;\n snappedAmountInBase: number;\n xpEarnRange?: {\n min: number;\n max: number;\n };\n maxSlippage: number;\n};\n\nexport type TradeSimulationSimulateLimitParams = {\n triggerPrice: number;\n amount: number; // position size / base, + for long | - for short\n fromBase?: boolean;\n};\n\nexport type SimulateLimitTradeEntity = {\n fees: number;\n snappedAmount: number;\n snappedAmountInBase: number;\n estimatedPrice: number;\n marginRatio: MarginAccountEntity['marginRatioPercentage'];\n marginRatioHealth: MarginAccountEntity['marginRatioHealth'];\n availableMargin: number;\n marginBalance: number;\n requiredMargin: number;\n};\n\nexport type TradeSimulationConvertValueResult = number;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"/","sources":["clients/modules/trade.simulation/types.ts"],"names":[],"mappings":"","sourcesContent":["import { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';\n\nexport type TradeSimulationLoadDataParams = {\n marketId: MarketEntity['id'];\n marginAccountId: MarginAccountEntity['id'];\n};\n\nexport type TradeSimulationSimulateParams = {\n amount: number; // position size / base, + for long | - for short\n fromBase?: boolean;\n};\n\nexport type TradeSimulationConvertValueParams = {\n amount: number;\n fromBase: boolean;\n};\n\nexport type EstimatedPriceParams = {\n amount: number; // amount in base\n};\n\nexport type EstimatedPriceResult = {\n estimatedPrice: number;\n markPrice: number;\n};\n\nexport type TradeSimulationConvertValueEstimatedPriceParams = {\n amount: number;\n fromBase: boolean;\n};\n\nexport type TradeSimulationConvertValueEstimatedPriceResult = number;\n\nexport type SimulateTradeEntity = {\n liquidationPrice: number;\n fees: number;\n estimatedPrice: number;\n estimatedSlippage: number;\n marginRatio: MarginAccountEntity['marginRatioPercentage'];\n marginRatioHealth: MarginAccountEntity['marginRatioHealth'];\n availableMargin: number;\n marginBalance: number;\n requiredMargin: number;\n snappedAmount: number;\n snappedAmountInBase: number;\n xpEarnRange?: {\n min: number;\n max: number;\n };\n maxSlippage: number;\n};\n\nexport type TradeSimulationSimulateLimitParams = {\n triggerPrice: number;\n amount: number; // position size / base, + for long | - for short\n fromBase?: boolean;\n};\n\nexport type SimulateLimitTradeEntity = {\n fees: number;\n snappedAmount: number;\n snappedAmountInBase: number;\n estimatedPrice: number;\n marginRatio: MarginAccountEntity['marginRatioPercentage'];\n marginRatioHealth: MarginAccountEntity['marginRatioHealth'];\n availableMargin: number;\n marginBalance: number;\n requiredMargin: number;\n};\n\nexport type LimitTradeMaxOrderSizeParams = {\n triggerPrice: number;\n};\n\nexport type LimitTradeMaxOrderSizeResult = {\n maxAmountBase: number;\n maxAmountSize: number;\n};\n\nexport type TradeSimulationConvertValueResult = number;\n"]}
@@ -1,4 +1,4 @@
1
- import { EstimatedPriceParams, EstimatedPriceResult, SimulateLimitTradeEntity, SimulateTradeEntity, TradeSimulationConvertValueEstimatedPriceParams, TradeSimulationConvertValueParams, TradeSimulationConvertValueResult, TradeSimulationLoadDataParams, TradeSimulationSimulateLimitParams, TradeSimulationSimulateParams } from './types';
1
+ import { EstimatedPriceParams, EstimatedPriceResult, LimitTradeMaxOrderSizeParams, LimitTradeMaxOrderSizeResult, SimulateLimitTradeEntity, SimulateTradeEntity, TradeSimulationConvertValueEstimatedPriceParams, TradeSimulationConvertValueParams, TradeSimulationConvertValueResult, TradeSimulationLoadDataParams, TradeSimulationSimulateLimitParams, TradeSimulationSimulateParams } from './types';
2
2
  import AccountClient from '../account';
3
3
  export default class TradeSimulationClient {
4
4
  private marketId;
@@ -9,8 +9,9 @@ export default class TradeSimulationClient {
9
9
  arm(params: TradeSimulationLoadDataParams): Promise<void>;
10
10
  private fetchMarketData;
11
11
  simulate(params: TradeSimulationSimulateParams): SimulateTradeEntity;
12
- private computeLimitOrderLMR;
12
+ getRiskMatrixElement(): number;
13
13
  simulateLimit(params: TradeSimulationSimulateLimitParams): SimulateLimitTradeEntity;
14
+ getMaxAmountForLimitOrder(params: LimitTradeMaxOrderSizeParams): LimitTradeMaxOrderSizeResult;
14
15
  convertValue(params: TradeSimulationConvertValueParams): TradeSimulationConvertValueResult;
15
16
  convertValueEstimatedPrice(params: TradeSimulationConvertValueEstimatedPriceParams): TradeSimulationConvertValueResult;
16
17
  estimatedPrice(params: EstimatedPriceParams): EstimatedPriceResult;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["clients/modules/trade.simulation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,wBAAwB,EACxB,mBAAmB,EACnB,+CA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AAO,CAAC,QAAQ,CAAuB;IACvC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,UAAU,CAIF;IAChB,OAAO,CAAC,aAAa,CAAgB;gBAEzB,aAAa,EAAE,aAAa;IAMlC,GAAG,CAAC,MAAM,EAAE,6BAA6B,GAAG,OAAO,CAAC,IAAI,CAAC;YAsDjD,eAAe;IAW7B,QAAQ,CAAC,MAAM,EAAE,6BAA6B,GAAG,mBAAmB;IAmIpE,oBAAoB,IAAI,MAAM;IAyB9B,aAAa,CACX,MAAM,EAAE,kCAAkC,GACzC,wBAAwB;IA2E3B,yBAAyB,CACvB,MAAM,EAAE,4BAA4B,GACnC,4BAA4B;IA0B/B,YAAY,CACV,MAAM,EAAE,iCAAiC,GACxC,iCAAiC;IAyBpC,0BAA0B,CACxB,MAAM,EAAE,+CAA+C,GACtD,iCAAiC;IAkCpC,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,oBAAoB;IA+BlE,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IAc/D,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAejC"}
@@ -57,5 +57,12 @@ export type SimulateLimitTradeEntity = {
57
57
  marginBalance: number;
58
58
  requiredMargin: number;
59
59
  };
60
+ export type LimitTradeMaxOrderSizeParams = {
61
+ triggerPrice: number;
62
+ };
63
+ export type LimitTradeMaxOrderSizeResult = {
64
+ maxAmountBase: number;
65
+ maxAmountSize: number;
66
+ };
60
67
  export type TradeSimulationConvertValueResult = number;
61
68
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"/","sources":["clients/modules/trade.simulation/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpE,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,EAAE,YAAY,CAAC,IAAI,CA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MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG,MAAM,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/api-sdk",
3
- "version": "0.117.0",
3
+ "version": "0.117.2",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -40,7 +40,7 @@
40
40
  "ws": "^8.16.0"
41
41
  },
42
42
  "packageManager": "pnpm@8.3.1",
43
- "gitHead": "567e5d63578fbe6fc816eafcdd490a7bd36ad366",
43
+ "gitHead": "ff90530cbb5c9ca0dc3b92fa3eb060b3ec4b790d",
44
44
  "devDependencies": {
45
45
  "@types/ws": "8.5.10"
46
46
  }
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  EstimatedPriceParams,
3
3
  EstimatedPriceResult,
4
+ LimitTradeMaxOrderSizeParams,
5
+ LimitTradeMaxOrderSizeResult,
4
6
  SimulateLimitTradeEntity,
5
7
  SimulateTradeEntity,
6
8
  TradeSimulationConvertValueEstimatedPriceParams,
@@ -12,7 +14,6 @@ import {
12
14
  } from './types';
13
15
  import AccountClient from '../account';
14
16
  import {
15
- amountDenormalizer,
16
17
  amountNormalizer,
17
18
  ExposureCommand,
18
19
  MarginInfo,
@@ -24,8 +25,13 @@ import { INSTANT_TRADING_RATE_XP } from '@reyaxyz/common';
24
25
  export default class TradeSimulationClient {
25
26
  private marketId: number | null = null;
26
27
  private accountId: number | null = null;
27
- private loadedData: TradeSimulationState | null = null;
28
+ private loadedData: {
29
+ tradeSimulationState: TradeSimulationState;
30
+ userAccountExposure: ExposureCommand;
31
+ passivePoolExposure: ExposureCommand;
32
+ } | null = null;
28
33
  private accountClient: AccountClient;
34
+
29
35
  constructor(accountClient: AccountClient) {
30
36
  // Constructor added
31
37
  this.accountClient = accountClient;
@@ -36,7 +42,54 @@ export default class TradeSimulationClient {
36
42
  this.marketId = params.marketId;
37
43
  this.accountId = params.marginAccountId;
38
44
 
39
- this.loadedData = await this.fetchMarketData(this.marketId, this.accountId);
45
+ const tradeSimulationState = await this.fetchMarketData(
46
+ this.marketId,
47
+ this.accountId,
48
+ );
49
+
50
+ const userAccountExposure = new ExposureCommand(
51
+ tradeSimulationState.exposureDataAccount.accountId,
52
+ tradeSimulationState.exposureDataAccount.rootCollateralPoolId,
53
+ tradeSimulationState.exposureDataAccount.oraclePricePerMarket,
54
+ tradeSimulationState.exposureDataAccount.accountBalancePerAsset,
55
+ tradeSimulationState.exposureDataAccount.groupedByCollateral,
56
+ tradeSimulationState.exposureDataAccount.riskMultipliers,
57
+ tradeSimulationState.exposureDataAccount.riskMatrices,
58
+ tradeSimulationState.exposureDataAccount.exchangeInfoPerAsset,
59
+ tradeSimulationState.exposureDataAccount.positionInfoMarketConfiguration,
60
+ tradeSimulationState.exposureDataAccount.uniqueTokenAddresses,
61
+ tradeSimulationState.exposureDataAccount.uniqueQuoteCollaterals,
62
+ tradeSimulationState.exposureDataAccount.tokenMarginInfoPerAsset,
63
+ tradeSimulationState.exposureDataAccount.realizedPnLSum,
64
+ tradeSimulationState.exposureDataAccount.unrealizedPnLSum,
65
+ tradeSimulationState.exposureDataAccount.mtmRpnlSum,
66
+ tradeSimulationState.exposureDataAccount.collateralAddressToExchangePrice,
67
+ );
68
+
69
+ const passivePoolExposure = new ExposureCommand(
70
+ tradeSimulationState.exposureDataPassivePool.accountId,
71
+ tradeSimulationState.exposureDataPassivePool.rootCollateralPoolId,
72
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket,
73
+ tradeSimulationState.exposureDataPassivePool.accountBalancePerAsset,
74
+ tradeSimulationState.exposureDataPassivePool.groupedByCollateral,
75
+ tradeSimulationState.exposureDataPassivePool.riskMultipliers,
76
+ tradeSimulationState.exposureDataPassivePool.riskMatrices,
77
+ tradeSimulationState.exposureDataPassivePool.exchangeInfoPerAsset,
78
+ tradeSimulationState.exposureDataPassivePool.positionInfoMarketConfiguration,
79
+ tradeSimulationState.exposureDataPassivePool.uniqueTokenAddresses,
80
+ tradeSimulationState.exposureDataPassivePool.uniqueQuoteCollaterals,
81
+ tradeSimulationState.exposureDataPassivePool.tokenMarginInfoPerAsset,
82
+ tradeSimulationState.exposureDataPassivePool.realizedPnLSum,
83
+ tradeSimulationState.exposureDataPassivePool.unrealizedPnLSum,
84
+ tradeSimulationState.exposureDataPassivePool.mtmRpnlSum,
85
+ tradeSimulationState.exposureDataPassivePool.collateralAddressToExchangePrice,
86
+ );
87
+
88
+ this.loadedData = {
89
+ tradeSimulationState,
90
+ userAccountExposure,
91
+ passivePoolExposure,
92
+ };
40
93
  }
41
94
 
42
95
  private async fetchMarketData(
@@ -55,57 +108,22 @@ export default class TradeSimulationClient {
55
108
  throw new Error('Data not loaded. Call arm() first.');
56
109
  }
57
110
 
111
+ const { tradeSimulationState, userAccountExposure, passivePoolExposure } =
112
+ this.loadedData;
113
+
58
114
  let amount;
59
115
  if (params.fromBase) {
60
116
  amount = params.amount;
61
117
  } else {
62
118
  amount = BigNumber(params.amount)
63
119
  .div(
64
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
65
- this.loadedData.marketConfiguration.market_id
120
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
121
+ tradeSimulationState.marketConfiguration.market_id
66
122
  ],
67
123
  )
68
124
  .toNumber();
69
125
  }
70
126
 
71
- const userAccountExposure = new ExposureCommand(
72
- this.loadedData.exposureDataAccount.accountId,
73
- this.loadedData.exposureDataAccount.rootCollateralPoolId,
74
- this.loadedData.exposureDataAccount.oraclePricePerMarket,
75
- this.loadedData.exposureDataAccount.accountBalancePerAsset,
76
- this.loadedData.exposureDataAccount.groupedByCollateral,
77
- this.loadedData.exposureDataAccount.riskMultipliers,
78
- this.loadedData.exposureDataAccount.riskMatrices,
79
- this.loadedData.exposureDataAccount.exchangeInfoPerAsset,
80
- this.loadedData.exposureDataAccount.positionInfoMarketConfiguration,
81
- this.loadedData.exposureDataAccount.uniqueTokenAddresses,
82
- this.loadedData.exposureDataAccount.uniqueQuoteCollaterals,
83
- this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset,
84
- this.loadedData.exposureDataAccount.realizedPnLSum,
85
- this.loadedData.exposureDataAccount.unrealizedPnLSum,
86
- this.loadedData.exposureDataAccount.mtmRpnlSum,
87
- this.loadedData.exposureDataAccount.collateralAddressToExchangePrice,
88
- );
89
-
90
- const passivePoolExposure = new ExposureCommand(
91
- this.loadedData.exposureDataPassivePool.accountId,
92
- this.loadedData.exposureDataPassivePool.rootCollateralPoolId,
93
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket,
94
- this.loadedData.exposureDataPassivePool.accountBalancePerAsset,
95
- this.loadedData.exposureDataPassivePool.groupedByCollateral,
96
- this.loadedData.exposureDataPassivePool.riskMultipliers,
97
- this.loadedData.exposureDataPassivePool.riskMatrices,
98
- this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,
99
- this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,
100
- this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,
101
- this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,
102
- this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,
103
- this.loadedData.exposureDataPassivePool.realizedPnLSum,
104
- this.loadedData.exposureDataPassivePool.unrealizedPnLSum,
105
- this.loadedData.exposureDataPassivePool.mtmRpnlSum,
106
- this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,
107
- );
108
-
109
127
  /*
110
128
  max amount of margin in rUSD terms that can be transferred from the source account to the destination account
111
129
  that performs the isolated position trade (PRE TRADE)
@@ -119,38 +137,38 @@ export default class TradeSimulationClient {
119
137
 
120
138
  const slippage = passivePoolExposure.getSlippage(
121
139
  BigNumber(amount).negated().toNumber(),
122
- this.loadedData.marketConfiguration,
123
- this.loadedData.marketStorage,
140
+ tradeSimulationState.marketConfiguration,
141
+ tradeSimulationState.marketStorage,
124
142
  );
125
143
 
126
144
  const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
127
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
128
- this.loadedData.marketConfiguration.market_id
145
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
146
+ tradeSimulationState.marketConfiguration.market_id
129
147
  ],
130
148
  slippage,
131
149
  );
132
150
 
133
151
  const fees = ExposureCommand.calculateFee(
134
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
135
- this.loadedData.marketConfiguration.market_id
152
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
153
+ tradeSimulationState.marketConfiguration.market_id
136
154
  ],
137
155
  amount,
138
- this.loadedData.feeParameter,
156
+ tradeSimulationState.feeParameter,
139
157
  );
140
158
 
141
159
  const { usdNodeMarginInfo: newMarginInfo, tokenMarginInfoPerAsset } =
142
160
  userAccountExposure.getUsdNodeMarginInfoPostTrade(
143
161
  amount,
144
- this.loadedData.marketStorage.quote_collateral,
145
- this.loadedData.marketConfiguration,
146
- this.loadedData.marketStorage.risk_block_id,
162
+ tradeSimulationState.marketStorage.quote_collateral,
163
+ tradeSimulationState.marketConfiguration,
164
+ tradeSimulationState.marketStorage.risk_block_id,
147
165
  );
148
166
 
149
167
  const newQuoteTokenMarginInfo = tokenMarginInfoPerAsset.find(
150
168
  (marginInfo: MarginInfo) => {
151
169
  return (
152
170
  marginInfo.assetAddress ===
153
- this.loadedData?.marketStorage?.quote_collateral
171
+ tradeSimulationState.marketStorage.quote_collateral
154
172
  );
155
173
  },
156
174
  );
@@ -171,8 +189,8 @@ export default class TradeSimulationClient {
171
189
  const liquidationPrice = ExposureCommand.calculateLiquidation(
172
190
  newMarginInfo.marginBalance,
173
191
  newQuoteTokenMarginInfo.liquidationMarginRequirement,
174
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
175
- this.loadedData.marketConfiguration.market_id
192
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
193
+ tradeSimulationState.marketConfiguration.market_id
176
194
  ],
177
195
  amount,
178
196
  );
@@ -184,7 +202,7 @@ export default class TradeSimulationClient {
184
202
  );
185
203
 
186
204
  const baseSpacing = amountNormalizer(
187
- this.loadedData.marketConfiguration.base_spacing,
205
+ tradeSimulationState.marketConfiguration.base_spacing,
188
206
  ).toNumber();
189
207
 
190
208
  const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
@@ -216,13 +234,15 @@ export default class TradeSimulationClient {
216
234
  } as SimulateTradeEntity;
217
235
  }
218
236
 
219
- private computeLimitOrderLMR(exposure: number): number {
237
+ getRiskMatrixElement(): number {
220
238
  if (!this.loadedData) {
221
239
  throw new Error('Data not loaded. Call arm() first.');
222
240
  }
223
241
 
224
- const riskBlockId = this.loadedData.marketStorage.risk_block_id;
225
- const riskMatrices = this.loadedData.exposureDataAccount.riskMatrices;
242
+ const { tradeSimulationState } = this.loadedData;
243
+
244
+ const riskBlockId = tradeSimulationState.marketStorage.risk_block_id;
245
+ const riskMatrices = tradeSimulationState.exposureDataAccount.riskMatrices;
226
246
 
227
247
  const riskMatrix = riskMatrices.find(
228
248
  (riskMatrix) => riskMatrix.risk_block_id === riskBlockId,
@@ -233,17 +253,9 @@ export default class TradeSimulationClient {
233
253
  }
234
254
 
235
255
  const riskMatrixIndex =
236
- this.loadedData.marketConfiguration.risk_matrix_index;
237
-
238
- const riskMatrixElement =
239
- riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex];
256
+ tradeSimulationState.marketConfiguration.risk_matrix_index;
240
257
 
241
- const lmr = ExposureCommand.computeLiquidationMarginRequirement(
242
- [[riskMatrixElement]],
243
- [amountDenormalizer(exposure)],
244
- );
245
-
246
- return amountNormalizer(lmr).toNumber();
258
+ return Number(riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex]);
247
259
  }
248
260
 
249
261
  // Synchronous method to simulate operations based on an amount
@@ -254,6 +266,8 @@ export default class TradeSimulationClient {
254
266
  throw new Error('Data not loaded. Call arm() first.');
255
267
  }
256
268
 
269
+ const { tradeSimulationState, userAccountExposure } = this.loadedData;
270
+
257
271
  let amount;
258
272
  if (params.fromBase) {
259
273
  amount = params.amount;
@@ -262,7 +276,7 @@ export default class TradeSimulationClient {
262
276
  }
263
277
 
264
278
  const baseSpacing = amountNormalizer(
265
- this.loadedData.marketConfiguration.base_spacing,
279
+ tradeSimulationState.marketConfiguration.base_spacing,
266
280
  ).toNumber();
267
281
 
268
282
  const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
@@ -271,26 +285,7 @@ export default class TradeSimulationClient {
271
285
  const fees = ExposureCommand.calculateFee(
272
286
  params.triggerPrice,
273
287
  amount,
274
- this.loadedData.feeParameter,
275
- );
276
-
277
- const userAccountExposure = new ExposureCommand(
278
- this.loadedData.exposureDataAccount.accountId,
279
- this.loadedData.exposureDataAccount.rootCollateralPoolId,
280
- this.loadedData.exposureDataAccount.oraclePricePerMarket,
281
- this.loadedData.exposureDataAccount.accountBalancePerAsset,
282
- this.loadedData.exposureDataAccount.groupedByCollateral,
283
- this.loadedData.exposureDataAccount.riskMultipliers,
284
- this.loadedData.exposureDataAccount.riskMatrices,
285
- this.loadedData.exposureDataAccount.exchangeInfoPerAsset,
286
- this.loadedData.exposureDataAccount.positionInfoMarketConfiguration,
287
- this.loadedData.exposureDataAccount.uniqueTokenAddresses,
288
- this.loadedData.exposureDataAccount.uniqueQuoteCollaterals,
289
- this.loadedData.exposureDataAccount.tokenMarginInfoPerAsset,
290
- this.loadedData.exposureDataAccount.realizedPnLSum,
291
- this.loadedData.exposureDataAccount.unrealizedPnLSum,
292
- this.loadedData.exposureDataAccount.mtmRpnlSum,
293
- this.loadedData.exposureDataAccount.collateralAddressToExchangePrice,
288
+ tradeSimulationState.feeParameter,
294
289
  );
295
290
 
296
291
  const marginBalance =
@@ -299,14 +294,19 @@ export default class TradeSimulationClient {
299
294
  const availableMargin =
300
295
  userAccountExposure.getUsdNodeMarginInfo.initialDelta;
301
296
 
297
+ const imrMultiplier = amountNormalizer(
298
+ String(userAccountExposure.riskMultipliers.im_multiplier),
299
+ ).toNumber();
300
+
301
+ const riskMatrixElement = this.getRiskMatrixElement();
302
+
302
303
  const accountLMR =
303
304
  userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement;
304
- const accountIMR =
305
- accountLMR * userAccountExposure.riskMultipliers.im_multiplier;
305
+ const accountIMR = accountLMR * imrMultiplier;
306
306
 
307
- const limitOrderLMR = this.computeLimitOrderLMR(snappedAmount);
308
- const limitOrderIMR =
309
- limitOrderLMR * userAccountExposure.riskMultipliers.im_multiplier;
307
+ const limitOrderLMR =
308
+ Math.sqrt(riskMatrixElement) * Math.abs(snappedAmount);
309
+ const limitOrderIMR = limitOrderLMR * imrMultiplier;
310
310
 
311
311
  const requiredMargin = accountIMR + limitOrderIMR;
312
312
 
@@ -336,6 +336,34 @@ export default class TradeSimulationClient {
336
336
  };
337
337
  }
338
338
 
339
+ getMaxAmountForLimitOrder(
340
+ params: LimitTradeMaxOrderSizeParams,
341
+ ): LimitTradeMaxOrderSizeResult {
342
+ if (!this.loadedData) {
343
+ throw new Error('Data not loaded. Call arm() first.');
344
+ }
345
+
346
+ const { userAccountExposure } = this.loadedData;
347
+
348
+ const riskMatrixElement = this.getRiskMatrixElement();
349
+
350
+ const imrMultiplier = amountNormalizer(
351
+ String(userAccountExposure.riskMultipliers.im_multiplier),
352
+ ).toNumber();
353
+
354
+ const availableMargin =
355
+ userAccountExposure.getUsdNodeMarginInfo.initialDelta;
356
+
357
+ const maxAmountSize =
358
+ availableMargin / Math.sqrt(riskMatrixElement) / imrMultiplier;
359
+ const maxAmountBase = maxAmountSize / params.triggerPrice;
360
+
361
+ return {
362
+ maxAmountBase,
363
+ maxAmountSize,
364
+ };
365
+ }
366
+
339
367
  convertValue(
340
368
  params: TradeSimulationConvertValueParams,
341
369
  ): TradeSimulationConvertValueResult {
@@ -343,19 +371,21 @@ export default class TradeSimulationClient {
343
371
  throw new Error('Data not loaded. Call arm() first.');
344
372
  }
345
373
 
374
+ const { tradeSimulationState } = this.loadedData;
375
+
346
376
  if (!params.fromBase)
347
377
  return BigNumber(params.amount)
348
378
  .div(
349
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
350
- this.loadedData.marketConfiguration.market_id
379
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
380
+ tradeSimulationState.marketConfiguration.market_id
351
381
  ],
352
382
  )
353
383
  .toNumber();
354
384
  else
355
385
  return BigNumber(params.amount)
356
386
  .times(
357
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
358
- this.loadedData.marketConfiguration.market_id
387
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
388
+ tradeSimulationState.marketConfiguration.market_id
359
389
  ],
360
390
  )
361
391
  .toNumber();
@@ -368,43 +398,26 @@ export default class TradeSimulationClient {
368
398
  throw new Error('Data not loaded. Call arm() first.');
369
399
  }
370
400
 
371
- const passivePoolExposure = new ExposureCommand(
372
- this.loadedData.exposureDataPassivePool.accountId,
373
- this.loadedData.exposureDataPassivePool.rootCollateralPoolId,
374
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket,
375
- this.loadedData.exposureDataPassivePool.accountBalancePerAsset,
376
- this.loadedData.exposureDataPassivePool.groupedByCollateral,
377
- this.loadedData.exposureDataPassivePool.riskMultipliers,
378
- this.loadedData.exposureDataPassivePool.riskMatrices,
379
- this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,
380
- this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,
381
- this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,
382
- this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,
383
- this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,
384
- this.loadedData.exposureDataPassivePool.realizedPnLSum,
385
- this.loadedData.exposureDataPassivePool.unrealizedPnLSum,
386
- this.loadedData.exposureDataPassivePool.mtmRpnlSum,
387
- this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,
388
- );
401
+ const { tradeSimulationState, passivePoolExposure } = this.loadedData;
389
402
 
390
403
  const amountForSlippage = params.fromBase
391
404
  ? params.amount
392
405
  : BigNumber(params.amount)
393
406
  .div(
394
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
395
- this.loadedData.marketConfiguration.market_id
407
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
408
+ tradeSimulationState.marketConfiguration.market_id
396
409
  ],
397
410
  )
398
411
  .toNumber();
399
412
 
400
413
  const slippage = passivePoolExposure.getSlippage(
401
414
  BigNumber(amountForSlippage).negated().toNumber(),
402
- this.loadedData.marketConfiguration,
403
- this.loadedData.marketStorage,
415
+ tradeSimulationState.marketConfiguration,
416
+ tradeSimulationState.marketStorage,
404
417
  );
405
418
  const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
406
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
407
- this.loadedData.marketConfiguration.market_id
419
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
420
+ tradeSimulationState.marketConfiguration.market_id
408
421
  ],
409
422
  slippage,
410
423
  );
@@ -419,38 +432,22 @@ export default class TradeSimulationClient {
419
432
  throw new Error('Data not loaded. Call arm() first.');
420
433
  }
421
434
 
422
- const passivePoolExposure = new ExposureCommand(
423
- this.loadedData.exposureDataPassivePool.accountId,
424
- this.loadedData.exposureDataPassivePool.rootCollateralPoolId,
425
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket,
426
- this.loadedData.exposureDataPassivePool.accountBalancePerAsset,
427
- this.loadedData.exposureDataPassivePool.groupedByCollateral,
428
- this.loadedData.exposureDataPassivePool.riskMultipliers,
429
- this.loadedData.exposureDataPassivePool.riskMatrices,
430
- this.loadedData.exposureDataPassivePool.exchangeInfoPerAsset,
431
- this.loadedData.exposureDataPassivePool.positionInfoMarketConfiguration,
432
- this.loadedData.exposureDataPassivePool.uniqueTokenAddresses,
433
- this.loadedData.exposureDataPassivePool.uniqueQuoteCollaterals,
434
- this.loadedData.exposureDataPassivePool.tokenMarginInfoPerAsset,
435
- this.loadedData.exposureDataPassivePool.realizedPnLSum,
436
- this.loadedData.exposureDataPassivePool.unrealizedPnLSum,
437
- this.loadedData.exposureDataPassivePool.mtmRpnlSum,
438
- this.loadedData.exposureDataPassivePool.collateralAddressToExchangePrice,
439
- );
435
+ const { tradeSimulationState, passivePoolExposure } = this.loadedData;
436
+
440
437
  const slippage = passivePoolExposure.getSlippage(
441
438
  BigNumber(params.amount).negated().toNumber(),
442
- this.loadedData.marketConfiguration,
443
- this.loadedData.marketStorage,
439
+ tradeSimulationState.marketConfiguration,
440
+ tradeSimulationState.marketStorage,
444
441
  );
445
442
 
446
443
  const price =
447
- this.loadedData.exposureDataAccount.oraclePricePerMarket[
448
- this.loadedData.marketConfiguration.market_id
444
+ tradeSimulationState.exposureDataAccount.oraclePricePerMarket[
445
+ tradeSimulationState.marketConfiguration.market_id
449
446
  ];
450
447
 
451
448
  const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
452
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[
453
- this.loadedData.marketConfiguration.market_id
449
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
450
+ tradeSimulationState.marketConfiguration.market_id
454
451
  ],
455
452
  slippage,
456
453
  );
@@ -479,10 +476,15 @@ export default class TradeSimulationClient {
479
476
  if (!this.loadedData || !this.marketId) {
480
477
  throw new Error('Data not loaded. Call arm() first.');
481
478
  }
479
+
480
+ const { tradeSimulationState } = this.loadedData;
481
+
482
482
  // update price
483
483
  const marketId = this.marketId;
484
- this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
484
+ tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
485
+ marketId
486
+ ] = price;
487
+ tradeSimulationState.exposureDataAccount.oraclePricePerMarket[marketId] =
485
488
  price;
486
- this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
487
489
  }
488
490
  }
@@ -68,4 +68,13 @@ export type SimulateLimitTradeEntity = {
68
68
  requiredMargin: number;
69
69
  };
70
70
 
71
+ export type LimitTradeMaxOrderSizeParams = {
72
+ triggerPrice: number;
73
+ };
74
+
75
+ export type LimitTradeMaxOrderSizeResult = {
76
+ maxAmountBase: number;
77
+ maxAmountSize: number;
78
+ };
79
+
71
80
  export type TradeSimulationConvertValueResult = number;