@reyaxyz/api-sdk 0.106.1 → 0.107.0
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 +1 -1
- package/dist/clients/modules/isolated-order.simulation/index.js +12 -0
- package/dist/clients/modules/isolated-order.simulation/index.js.map +1 -1
- package/dist/clients/modules/trade.simulation/index.js +10 -0
- package/dist/clients/modules/trade.simulation/index.js.map +1 -1
- package/dist/types/clients/modules/isolated-order.simulation/index.d.ts +2 -0
- package/dist/types/clients/modules/isolated-order.simulation/index.d.ts.map +1 -1
- package/dist/types/clients/modules/trade.simulation/index.d.ts +1 -0
- package/dist/types/clients/modules/trade.simulation/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/clients/modules/isolated-order.simulation/index.ts +14 -0
- package/src/clients/modules/trade.simulation/index.ts +11 -0
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -44,6 +44,7 @@ var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
|
44
44
|
var IsolatedOrderSimulationClient = /** @class */ (function () {
|
|
45
45
|
function IsolatedOrderSimulationClient(accountClient) {
|
|
46
46
|
this.loadedData = null;
|
|
47
|
+
this.marketId = null;
|
|
47
48
|
// Constructor added
|
|
48
49
|
this.accountClient = accountClient;
|
|
49
50
|
}
|
|
@@ -54,6 +55,7 @@ var IsolatedOrderSimulationClient = /** @class */ (function () {
|
|
|
54
55
|
return __generator(this, function (_b) {
|
|
55
56
|
switch (_b.label) {
|
|
56
57
|
case 0:
|
|
58
|
+
this.marketId = params.marketId;
|
|
57
59
|
_a = this;
|
|
58
60
|
return [4 /*yield*/, this.fetchMarketData(params.marketId, params.marginAccountId)];
|
|
59
61
|
case 1:
|
|
@@ -260,6 +262,16 @@ var IsolatedOrderSimulationClient = /** @class */ (function () {
|
|
|
260
262
|
availableMargin: availableMargin,
|
|
261
263
|
};
|
|
262
264
|
};
|
|
265
|
+
IsolatedOrderSimulationClient.prototype.updatePrice = function (price) {
|
|
266
|
+
if (!this.loadedData || !this.marketId) {
|
|
267
|
+
throw new Error('Data not loaded. Call arm() first.');
|
|
268
|
+
}
|
|
269
|
+
// update price
|
|
270
|
+
var marketId = this.marketId;
|
|
271
|
+
this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
|
|
272
|
+
price;
|
|
273
|
+
this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
|
|
274
|
+
};
|
|
263
275
|
return IsolatedOrderSimulationClient;
|
|
264
276
|
}());
|
|
265
277
|
exports.default = IsolatedOrderSimulationClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/isolated-order.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,0CAMyB;AACzB,8DAAqC;AASrC;IAGE,uCAAY,aAA4B;QAFhC,eAAU,GAAgC,IAAI,CAAC;QAGrD,oBAAoB;QACpB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAED,qEAAqE;IAC/D,2CAAG,GAAT,UAAU,MAA6C;;;;;;wBACrD,KAAA,IAAI,CAAA;wBAAc,qBAAM,IAAI,CAAC,eAAe,CAC1C,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,eAAe,CACvB,EAAA;;wBAHD,GAAK,UAAU,GAAG,SAGjB,CAAC;;;;;KACH;IAEM,sDAAwB,GAA/B,UACE,oBAA0C;QAE1C,OAAO,IAAI,wBAAe,CACxB,oBAAoB,CAAC,SAAS,EAC9B,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,sBAAsB,EAC3C,oBAAoB,CAAC,mBAA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C;QAErD,IAAM,eAAe,GAAc,IAAA,yBAAgB,EACjD,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,eAAe,CAAC,aAAa,CAAC,CAC1E,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;QAEhC,IAAM,QAAQ,GAAG,IAAA,sBAAS,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC,QAAQ,EAAE,CAAC;QAEpE,gBAAgB;QAEhB,IAAM,mBAAmB,GACvB,6BAA6B,CAAC,wBAAwB,CACpD,IAAI,CAAC,UAAU,CAAC,mBAAmB,CACpC,CAAC;QAEJ;;;UAGE;QAEF,IAAM,eAAe,GACnB,mBAAmB,CAAC,oBAAoB,CAAC,YAAY,CAAC;QAExD,IAAM,QAAQ,GAAG,IAAA,sBAAS,EAAC,mBAAmB,CAAC;aAC5C,GAAG,EAAE;aACL,SAAS,CAAC,eAAe,CAAC;aAC1B,QAAQ,EAAE,CAAC;QAEd,IAAI,QAAQ,GAAG,QAAQ,EAAE,CAAC;YACxB,MAAM,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACpD,CAAC;QAED,OAAO;YACL,QAAQ,EAAE,QAAQ;YAClB,QAAQ,EAAE,QAAQ;YAClB,eAAe,EAAE,eAAe;SACjC,CAAC;IACJ,CAAC;IACH,oCAAC;AAAD,CAAC,AAzaD,IAyaC","sourcesContent":["import {\n SimulateIsolatedOrderEntity,\n IsolatedOrderSimulationConvertValueParams,\n IsolatedOrderSimulationConvertValueResult,\n IsolatedOrderSimulationLoadDataParams,\n IsolatedOrderSimulationSimulateParams,\n LeverageBoundsAndAvailableMarginResult,\n LeverageBoundsAndAvailableMarginParams,\n} from './types';\nimport AccountClient from '../account';\nimport {\n amountNormalizer,\n ExposureCommand,\n ExposureCommandState,\n RiskMatrix,\n TradeSimulationState,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { EditCollateralAction } from '@reyaxyz/common';\nimport {\n EstimatedPriceParams,\n EstimatedPriceResult,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueResult,\n} from '../trade.simulation/types';\n\nexport default class IsolatedOrderSimulationClient {\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: IsolatedOrderSimulationLoadDataParams): Promise<void> {\n this.loadedData = await this.fetchMarketData(\n params.marketId,\n params.marginAccountId,\n );\n }\n\n static genExposureCommandObject(\n exposureCommandState: ExposureCommandState,\n ): ExposureCommand {\n return new ExposureCommand(\n exposureCommandState.accountId,\n exposureCommandState.rootCollateralPoolId,\n exposureCommandState.oraclePricePerMarket,\n exposureCommandState.accountBalancePerAsset,\n exposureCommandState.groupedByCollateral,\n exposureCommandState.riskMultipliers,\n exposureCommandState.riskMatrices,\n exposureCommandState.exchangeInfoPerAsset,\n exposureCommandState.positionInfoMarketConfiguration,\n exposureCommandState.uniqueTokenAddresses,\n exposureCommandState.uniqueQuoteCollaterals,\n exposureCommandState.tokenMarginInfoPerAsset,\n exposureCommandState.realizedPnLSum,\n exposureCommandState.unrealizedPnLSum,\n exposureCommandState.collateralAddressToExchangePrice,\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\n simulate(\n params: IsolatedOrderSimulationSimulateParams,\n ): SimulateIsolatedOrderEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n // todo: p2: check if it's intended behaviour to not sure snapped amount for simulation calcs e.g. liq. price\n const amount = BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n\n const userAccountExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataAccount,\n );\n\n const passivePoolExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataPassivePool,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amount).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 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 /*\n amount of margin in rUSD terms that needs to be transferred from the source account to the destination account,\n this value is equal to absolute size in rUSD terms / leverage\n */\n\n const requiredMargin = BigNumber(params.amount)\n .abs()\n .div(BigNumber(params.isolatedPositionLeverage))\n .toNumber();\n\n const editCollateralActions: EditCollateralAction[] =\n userAccountExposure.getEditCollateralActionsToCoverMargin(requiredMargin);\n\n const newMarginInfoSource =\n userAccountExposure.getUsdNodeMarginInfoPostEditCollaterals(\n editCollateralActions,\n );\n\n /*\n * Compute Isolated Account Liquidation Margin Requirement Post Transfer + Trade\n * */\n\n const isolatedLMR = this.calculateIsolatedLMR(params.amount);\n\n /*\n * margin balance of the destination account is the requiredMargin which is expected to be transferred\n * to the destination account that performs the isolated trade\n * the liquidation price in this case is trying to estimate what the liquidation price would be all else equal for\n * the market where the trade is being made by the isolated account that is going to be created as part of isolated\n * trade operation\n * */\n const liquidationPrice = ExposureCommand.calculateLiquidation(\n requiredMargin,\n isolatedLMR,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n amount,\n );\n\n // todo: p1: margin ratio seems to be wrong on ui\n const marginRatio = ExposureCommand.getMarginRatio(newMarginInfoSource);\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 spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * spotPrice;\n const xpEarnRangeMin = Math.round(Math.abs(snappedAmount) / 200);\n const xpEarnRangeMax = Math.round((100 * Math.abs(snappedAmount)) / 200);\n\n return {\n estimatedPrice: estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees: fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth: marginRatioHealth,\n snappedAmount,\n snappedAmountInBase,\n requiredMargin: requiredMargin,\n editCollateralActions: editCollateralActions,\n xpEarnRange: {\n min: xpEarnRangeMin,\n max: xpEarnRangeMax,\n },\n maxSlippage: 1,\n } as SimulateIsolatedOrderEntity;\n }\n\n convertValue(\n params: IsolatedOrderSimulationConvertValueParams,\n ): IsolatedOrderSimulationConvertValueResult {\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.collateralAddressToExchangePrice,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).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.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 amountToSnappedAmount(\n amountInRusd: number,\n spotPrice: number,\n baseSpacing: number,\n ): number {\n const amountInBase = BigNumber(amountInRusd)\n .div(BigNumber(spotPrice))\n .toNumber();\n\n return this.roundToBaseSpacing(amountInBase, baseSpacing) * spotPrice;\n }\n\n calculateIsolatedLMR(isolatedExposure: number): number {\n // todo: p2: consider removing the need to load the entire data just to get a few vars to calc leverage bounds\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n if (!this.loadedData.marketStorage) {\n throw new Error('Market storage not loaded');\n }\n\n // todo: p2: carefully test\n const marketRiskMatrix =\n this.loadedData.exposureDataAccount.riskMatrices.find(\n (riskMatrix: RiskMatrix) => {\n return (\n riskMatrix.risk_block_id ===\n BigNumber(\n String(this.loadedData?.marketStorage.risk_block_id),\n ).toNumber()\n );\n },\n );\n\n if (!marketRiskMatrix) {\n throw new Error('Failed to load risk matrix');\n }\n\n const marketDiagonalRiskParam =\n marketRiskMatrix.matrix[\n this.loadedData.marketConfiguration.risk_matrix_index\n ][this.loadedData.marketConfiguration.risk_matrix_index];\n const isolatedRiskMatrix: BigNumber[][] = [[marketDiagonalRiskParam]];\n const isolatedFilledExposures: BigNumber[] = [BigNumber(isolatedExposure)];\n\n return ExposureCommand.computeLiquidationMarginRequirement(\n isolatedRiskMatrix,\n isolatedFilledExposures,\n );\n }\n\n leverageBoundsAndAvailableMargin({\n amountTradedInRusd,\n }: LeverageBoundsAndAvailableMarginParams): LeverageBoundsAndAvailableMarginResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n const baseSpacing = amountNormalizer(\n this.loadedData.marketConfiguration.base_spacing,\n ).toNumber();\n const snappedAmountInRusd = this.amountToSnappedAmount(\n amountTradedInRusd,\n spotPrice,\n baseSpacing,\n );\n\n /*\n todo: p2: consider introducing buffer to the leverage (e.g. to account for the effect of trade on upnl\n and actually depending on the size of the trade the estimated price would change -> different upnl\n as upnl is calculated against oracle prioce + rpnl is also affected through the fees\n * once the trader knows their trade size (in base & rusd terms), they should be able to toggle isolated trade flow\n * which will prompt the user to choose a desired leverage value\n * 0.1 can be hardcoded to be the min bound\n * to get the maximum bound we need to calculate leverage that can be achieved when IMR is reached for position\n * with 1 rUSD exposure in the market -> max leverage = 1/IMR\n * */\n\n // set max bound\n\n const lmrUnitExposure = this.calculateIsolatedLMR(1);\n\n const imrUnitExposure: BigNumber = amountNormalizer(\n String(this.loadedData.exposureDataAccount.riskMultipliers.im_multiplier),\n ).multipliedBy(lmrUnitExposure);\n\n const maxBound = BigNumber(1).dividedBy(imrUnitExposure).toNumber();\n\n // set min bound\n\n const userAccountExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataAccount,\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\n */\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const minBound = BigNumber(snappedAmountInRusd)\n .abs()\n .dividedBy(availableMargin)\n .toNumber();\n\n if (minBound > maxBound) {\n throw Error('Min leverage bound higher than max');\n }\n\n return {\n minBound: minBound,\n maxBound: maxBound,\n availableMargin: availableMargin,\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/isolated-order.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,0CAMyB;AACzB,8DAAqC;AASrC;IAKE,uCAAY,aAA4B;QAJhC,eAAU,GAAgC,IAAI,CAAC;QAE/C,aAAQ,GAAkB,IAAI,CAAC;QAGrC,oBAAoB;QACpB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAED,qEAAqE;IAC/D,2CAAG,GAAT,UAAU,MAA6C;;;;;;wBACrD,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;wBAChC,KAAA,IAAI,CAAA;wBAAc,qBAAM,IAAI,CAAC,eAAe,CAC1C,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,eAAe,CACvB,EAAA;;wBAHD,GAAK,UAAU,GAAG,SAGjB,CAAC;;;;;KACH;IAEM,sDAAwB,GAA/B,UACE,oBAA0C;QAE1C,OAAO,IAAI,wBAAe,CACxB,oBAAoB,CAAC,SAAS,EAC9B,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,sBAAsB,EAC3C,oBAAoB,CAAC,mBAAmB,EACxC,oBAAoB,CAAC,eAAe,EACpC,oBAAoB,CAAC,YAAY,EACjC,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,+BAA+B,EACp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atedOrderSimulationConvertValueResult,\n IsolatedOrderSimulationLoadDataParams,\n IsolatedOrderSimulationSimulateParams,\n LeverageBoundsAndAvailableMarginResult,\n LeverageBoundsAndAvailableMarginParams,\n} from './types';\nimport AccountClient from '../account';\nimport {\n amountNormalizer,\n ExposureCommand,\n ExposureCommandState,\n RiskMatrix,\n TradeSimulationState,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { EditCollateralAction } from '@reyaxyz/common';\nimport {\n EstimatedPriceParams,\n EstimatedPriceResult,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueResult,\n} from '../trade.simulation/types';\n\nexport default class IsolatedOrderSimulationClient {\n private loadedData: TradeSimulationState | null = null;\n private accountClient: AccountClient;\n private marketId: number | null = null;\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: IsolatedOrderSimulationLoadDataParams): Promise<void> {\n this.marketId = params.marketId;\n this.loadedData = await this.fetchMarketData(\n params.marketId,\n params.marginAccountId,\n );\n }\n\n static genExposureCommandObject(\n exposureCommandState: ExposureCommandState,\n ): ExposureCommand {\n return new ExposureCommand(\n exposureCommandState.accountId,\n exposureCommandState.rootCollateralPoolId,\n exposureCommandState.oraclePricePerMarket,\n exposureCommandState.accountBalancePerAsset,\n exposureCommandState.groupedByCollateral,\n exposureCommandState.riskMultipliers,\n exposureCommandState.riskMatrices,\n exposureCommandState.exchangeInfoPerAsset,\n exposureCommandState.positionInfoMarketConfiguration,\n exposureCommandState.uniqueTokenAddresses,\n exposureCommandState.uniqueQuoteCollaterals,\n exposureCommandState.tokenMarginInfoPerAsset,\n exposureCommandState.realizedPnLSum,\n exposureCommandState.unrealizedPnLSum,\n exposureCommandState.collateralAddressToExchangePrice,\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\n simulate(\n params: IsolatedOrderSimulationSimulateParams,\n ): SimulateIsolatedOrderEntity {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n // todo: p2: check if it's intended behaviour to not sure snapped amount for simulation calcs e.g. liq. price\n const amount = BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\n\n const userAccountExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataAccount,\n );\n\n const passivePoolExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataPassivePool,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(amount).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 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 /*\n amount of margin in rUSD terms that needs to be transferred from the source account to the destination account,\n this value is equal to absolute size in rUSD terms / leverage\n */\n\n const requiredMargin = BigNumber(params.amount)\n .abs()\n .div(BigNumber(params.isolatedPositionLeverage))\n .toNumber();\n\n const editCollateralActions: EditCollateralAction[] =\n userAccountExposure.getEditCollateralActionsToCoverMargin(requiredMargin);\n\n const newMarginInfoSource =\n userAccountExposure.getUsdNodeMarginInfoPostEditCollaterals(\n editCollateralActions,\n );\n\n /*\n * Compute Isolated Account Liquidation Margin Requirement Post Transfer + Trade\n * */\n\n const isolatedLMR = this.calculateIsolatedLMR(params.amount);\n\n /*\n * margin balance of the destination account is the requiredMargin which is expected to be transferred\n * to the destination account that performs the isolated trade\n * the liquidation price in this case is trying to estimate what the liquidation price would be all else equal for\n * the market where the trade is being made by the isolated account that is going to be created as part of isolated\n * trade operation\n * */\n const liquidationPrice = ExposureCommand.calculateLiquidation(\n requiredMargin,\n isolatedLMR,\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n amount,\n );\n\n // todo: p1: margin ratio seems to be wrong on ui\n const marginRatio = ExposureCommand.getMarginRatio(newMarginInfoSource);\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 spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * spotPrice;\n const xpEarnRangeMin = Math.round(Math.abs(snappedAmount) / 200);\n const xpEarnRangeMax = Math.round((100 * Math.abs(snappedAmount)) / 200);\n\n return {\n estimatedPrice: estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees: fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth: marginRatioHealth,\n snappedAmount,\n snappedAmountInBase,\n requiredMargin: requiredMargin,\n editCollateralActions: editCollateralActions,\n xpEarnRange: {\n min: xpEarnRangeMin,\n max: xpEarnRangeMax,\n },\n maxSlippage: 1,\n } as SimulateIsolatedOrderEntity;\n }\n\n convertValue(\n params: IsolatedOrderSimulationConvertValueParams,\n ): IsolatedOrderSimulationConvertValueResult {\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.collateralAddressToExchangePrice,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).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.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 amountToSnappedAmount(\n amountInRusd: number,\n spotPrice: number,\n baseSpacing: number,\n ): number {\n const amountInBase = BigNumber(amountInRusd)\n .div(BigNumber(spotPrice))\n .toNumber();\n\n return this.roundToBaseSpacing(amountInBase, baseSpacing) * spotPrice;\n }\n\n calculateIsolatedLMR(isolatedExposure: number): number {\n // todo: p2: consider removing the need to load the entire data just to get a few vars to calc leverage bounds\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n if (!this.loadedData.marketStorage) {\n throw new Error('Market storage not loaded');\n }\n\n // todo: p2: carefully test\n const marketRiskMatrix =\n this.loadedData.exposureDataAccount.riskMatrices.find(\n (riskMatrix: RiskMatrix) => {\n return (\n riskMatrix.risk_block_id ===\n BigNumber(\n String(this.loadedData?.marketStorage.risk_block_id),\n ).toNumber()\n );\n },\n );\n\n if (!marketRiskMatrix) {\n throw new Error('Failed to load risk matrix');\n }\n\n const marketDiagonalRiskParam =\n marketRiskMatrix.matrix[\n this.loadedData.marketConfiguration.risk_matrix_index\n ][this.loadedData.marketConfiguration.risk_matrix_index];\n const isolatedRiskMatrix: BigNumber[][] = [[marketDiagonalRiskParam]];\n const isolatedFilledExposures: BigNumber[] = [BigNumber(isolatedExposure)];\n\n return ExposureCommand.computeLiquidationMarginRequirement(\n isolatedRiskMatrix,\n isolatedFilledExposures,\n );\n }\n\n leverageBoundsAndAvailableMargin({\n amountTradedInRusd,\n }: LeverageBoundsAndAvailableMarginParams): LeverageBoundsAndAvailableMarginResult {\n if (!this.loadedData) {\n throw new Error('Data not loaded. Call arm() first.');\n }\n\n const spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n const baseSpacing = amountNormalizer(\n this.loadedData.marketConfiguration.base_spacing,\n ).toNumber();\n const snappedAmountInRusd = this.amountToSnappedAmount(\n amountTradedInRusd,\n spotPrice,\n baseSpacing,\n );\n\n /*\n todo: p2: consider introducing buffer to the leverage (e.g. to account for the effect of trade on upnl\n and actually depending on the size of the trade the estimated price would change -> different upnl\n as upnl is calculated against oracle prioce + rpnl is also affected through the fees\n * once the trader knows their trade size (in base & rusd terms), they should be able to toggle isolated trade flow\n * which will prompt the user to choose a desired leverage value\n * 0.1 can be hardcoded to be the min bound\n * to get the maximum bound we need to calculate leverage that can be achieved when IMR is reached for position\n * with 1 rUSD exposure in the market -> max leverage = 1/IMR\n * */\n\n // set max bound\n\n const lmrUnitExposure = this.calculateIsolatedLMR(1);\n\n const imrUnitExposure: BigNumber = amountNormalizer(\n String(this.loadedData.exposureDataAccount.riskMultipliers.im_multiplier),\n ).multipliedBy(lmrUnitExposure);\n\n const maxBound = BigNumber(1).dividedBy(imrUnitExposure).toNumber();\n\n // set min bound\n\n const userAccountExposure =\n IsolatedOrderSimulationClient.genExposureCommandObject(\n this.loadedData.exposureDataAccount,\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\n */\n\n const availableMargin =\n userAccountExposure.getUsdNodeMarginInfo.initialDelta;\n\n const minBound = BigNumber(snappedAmountInRusd)\n .abs()\n .dividedBy(availableMargin)\n .toNumber();\n\n if (minBound > maxBound) {\n throw Error('Min leverage bound higher than max');\n }\n\n return {\n minBound: minBound,\n maxBound: maxBound,\n availableMargin: availableMargin,\n };\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"]}
|
|
@@ -190,6 +190,16 @@ var TradeSimulationClient = /** @class */ (function () {
|
|
|
190
190
|
}
|
|
191
191
|
return snappedAmount;
|
|
192
192
|
};
|
|
193
|
+
TradeSimulationClient.prototype.updatePrice = function (price) {
|
|
194
|
+
if (!this.loadedData || !this.marketId) {
|
|
195
|
+
throw new Error('Data not loaded. Call arm() first.');
|
|
196
|
+
}
|
|
197
|
+
// update price
|
|
198
|
+
var marketId = this.marketId;
|
|
199
|
+
this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
|
|
200
|
+
price;
|
|
201
|
+
this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
|
|
202
|
+
};
|
|
193
203
|
return TradeSimulationClient;
|
|
194
204
|
}());
|
|
195
205
|
exports.default = TradeSimulationClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/trade.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,0CAKyB;AACzB,8DAAqC;AAErC;IAKE,+BAAY,aAA4B;QAJhC,aAAQ,GAAkB,IAAI,CAAC;QAC/B,cAAS,GAAkB,IAAI,CAAC;QAChC,eAAU,GAAgC,IAAI,CAAC;QAGrD,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;wBAExC,KAAA,IAAI,CAAA;wBAAc,qBAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,EAAA;;wBAA3E,GAAK,UAAU,GAAG,SAAyD,CAAC;;;;;KAC7E;IAEa,+CAAe,GAA7B,UACE,QAAgB,EAChB,SAAiB;;;gBAEjB,sBAAO,IAAI,CAAC,aAAa,CAAC,mCAAmC,CAAC;wBAC5D,eAAe,EAAE,SAAS;wBAC1B,QAAQ,EAAE,QAAQ;qBACnB,CAAC,EAAC;;;KACJ;IAED,+DAA+D;IAC/D,wCAAQ,GAAR,UAAS,MAAqC;QAA9C,iBA4JC;QA3JC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACxD,CAAC;QAED,IAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,MAAM,CAAC,MAA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Aa,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;IACH,4BAAC;AAAD,CAAC,AAzTD,IAyTC","sourcesContent":["import {\n EstimatedPriceParams,\n EstimatedPriceResult,\n SimulateTradeEntity,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueParams,\n TradeSimulationConvertValueResult,\n TradeSimulationLoadDataParams,\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';\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 const amount = BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\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.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.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 const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n slippage,\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 spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * spotPrice;\n const xpEarnRangeMin = Math.round(Math.abs(snappedAmount) / 200);\n const xpEarnRangeMax = Math.round((100 * Math.abs(snappedAmount)) / 200);\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 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.collateralAddressToExchangePrice,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).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.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"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/trade.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,0CAKyB;AACzB,8DAAqC;AAErC;IAKE,+BAAY,aAA4B;QAJhC,aAAQ,GAAkB,IAAI,CAAC;QAC/B,cAAS,GAAkB,IAAI,CAAC;QAChC,eAAU,GAAgC,IAAI,CAAC;QAGrD,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;wBAExC,KAAA,IAAI,CAAA;wBAAc,qBAAM,IAAI,CAAC,eAAe,C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AAG,KAAK,CAAC;IAC7E,CAAC;IACH,4BAAC;AAAD,CAAC,AApUD,IAoUC","sourcesContent":["import {\n EstimatedPriceParams,\n EstimatedPriceResult,\n SimulateTradeEntity,\n TradeSimulationConvertValueEstimatedPriceParams,\n TradeSimulationConvertValueParams,\n TradeSimulationConvertValueResult,\n TradeSimulationLoadDataParams,\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';\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 const amount = BigNumber(params.amount)\n .div(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n )\n .toNumber();\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.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.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 const estimatedPrice = ExposureCommand.calculateEstimatedPrice(\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ],\n slippage,\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 spotPrice =\n this.loadedData.exposureDataPassivePool.oraclePricePerMarket[\n this.loadedData.marketConfiguration.market_id\n ];\n\n const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);\n const snappedAmount = snappedAmountInBase * spotPrice;\n const xpEarnRangeMin = Math.round(Math.abs(snappedAmount) / 200);\n const xpEarnRangeMax = Math.round((100 * Math.abs(snappedAmount)) / 200);\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 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.collateralAddressToExchangePrice,\n );\n\n const slippage = passivePoolExposure.getSlippage(\n BigNumber(params.amount).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.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"]}
|
|
@@ -5,6 +5,7 @@ import { EstimatedPriceParams, EstimatedPriceResult, TradeSimulationConvertValue
|
|
|
5
5
|
export default class IsolatedOrderSimulationClient {
|
|
6
6
|
private loadedData;
|
|
7
7
|
private accountClient;
|
|
8
|
+
private marketId;
|
|
8
9
|
constructor(accountClient: AccountClient);
|
|
9
10
|
arm(params: IsolatedOrderSimulationLoadDataParams): Promise<void>;
|
|
10
11
|
static genExposureCommandObject(exposureCommandState: ExposureCommandState): ExposureCommand;
|
|
@@ -17,5 +18,6 @@ export default class IsolatedOrderSimulationClient {
|
|
|
17
18
|
amountToSnappedAmount(amountInRusd: number, spotPrice: number, baseSpacing: number): number;
|
|
18
19
|
calculateIsolatedLMR(isolatedExposure: number): number;
|
|
19
20
|
leverageBoundsAndAvailableMargin({ amountTradedInRusd, }: LeverageBoundsAndAvailableMarginParams): LeverageBoundsAndAvailableMarginResult;
|
|
21
|
+
updatePrice(price: number): void;
|
|
20
22
|
}
|
|
21
23
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["clients/modules/isolated-order.simulation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,yCAAyC,EACzC,yCAAyC,EACzC,qCAAqC,EACrC,qCAAqC,EACrC,sCAAsC,EACtC,sCAAsC,EACvC,MAAM,SAAS,CAAC;AACjB,OAAO,aAAa,MAAM,YAAY,CAAC;AACvC,OAAO,EAEL,eAAe,EACf,oBAAoB,EAGrB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,+CAA+C,EAC/C,iCAAiC,EAClC,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,OAAO,OAAO,6BAA6B;IAChD,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,aAAa,CAAgB;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["clients/modules/isolated-order.simulation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,yCAAyC,EACzC,yCAAyC,EACzC,qCAAqC,EACrC,qCAAqC,EACrC,sCAAsC,EACtC,sCAAsC,EACvC,MAAM,SAAS,CAAC;AACjB,OAAO,aAAa,MAAM,YAAY,CAAC;AACvC,OAAO,EAEL,eAAe,EACf,oBAAoB,EAGrB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,+CAA+C,EAC/C,iCAAiC,EAClC,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,OAAO,OAAO,6BAA6B;IAChD,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,QAAQ,CAAuB;gBAE3B,aAAa,EAAE,aAAa;IAMlC,GAAG,CAAC,MAAM,EAAE,qCAAqC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvE,MAAM,CAAC,wBAAwB,CAC7B,oBAAoB,EAAE,oBAAoB,GACzC,eAAe;YAoBJ,eAAe;IAW7B,QAAQ,CACN,MAAM,EAAE,qCAAqC,GAC5C,2BAA2B;IA4H9B,YAAY,CACV,MAAM,EAAE,yCAAyC,GAChD,yCAAyC;IAuB5C,0BAA0B,CACxB,MAAM,EAAE,+CAA+C,GACtD,iCAAiC;IAwCpC,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,oBAAoB;IA8ClE,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IAc/D,qBAAqB,CACnB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,GAClB,MAAM;IAQT,oBAAoB,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM;IAwCtD,gCAAgC,CAAC,EAC/B,kBAAkB,GACnB,EAAE,sCAAsC,GAAG,sCAAsC;IAsElF,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAUjC"}
|
|
@@ -13,5 +13,6 @@ export default class TradeSimulationClient {
|
|
|
13
13
|
convertValueEstimatedPrice(params: TradeSimulationConvertValueEstimatedPriceParams): TradeSimulationConvertValueResult;
|
|
14
14
|
estimatedPrice(params: EstimatedPriceParams): EstimatedPriceResult;
|
|
15
15
|
roundToBaseSpacing(amount: number, baseSpacing: number): number;
|
|
16
|
+
updatePrice(price: number): void;
|
|
16
17
|
}
|
|
17
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -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,mBAAmB,EACnB,+CAA+C,EAC/C,iCAAiC,EACjC,iCAAiC,EACjC,6BAA6B,EAC7B,6BAA6B,EAC9B,MAAM,SAAS,CAAC;AACjB,OAAO,aAAa,MAAM,YAAY,CAAC;AASvC,MAAM,CAAC,OAAO,OA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M,EAAE,+CAA+C,GACtD,iCAAiC;IAwCpC,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,oBAAoB;IA8ClE,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IAc/D,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAUjC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/api-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.107.0",
|
|
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": "
|
|
43
|
+
"gitHead": "b0fbca4dd28789096abde93f62ebd128bcf372a6",
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/ws": "8.5.10"
|
|
46
46
|
}
|
|
@@ -27,6 +27,8 @@ import {
|
|
|
27
27
|
export default class IsolatedOrderSimulationClient {
|
|
28
28
|
private loadedData: TradeSimulationState | null = null;
|
|
29
29
|
private accountClient: AccountClient;
|
|
30
|
+
private marketId: number | null = null;
|
|
31
|
+
|
|
30
32
|
constructor(accountClient: AccountClient) {
|
|
31
33
|
// Constructor added
|
|
32
34
|
this.accountClient = accountClient;
|
|
@@ -34,6 +36,7 @@ export default class IsolatedOrderSimulationClient {
|
|
|
34
36
|
|
|
35
37
|
// Method to asynchronously load data based on marketId and accountId
|
|
36
38
|
async arm(params: IsolatedOrderSimulationLoadDataParams): Promise<void> {
|
|
39
|
+
this.marketId = params.marketId;
|
|
37
40
|
this.loadedData = await this.fetchMarketData(
|
|
38
41
|
params.marketId,
|
|
39
42
|
params.marginAccountId,
|
|
@@ -449,4 +452,15 @@ export default class IsolatedOrderSimulationClient {
|
|
|
449
452
|
availableMargin: availableMargin,
|
|
450
453
|
};
|
|
451
454
|
}
|
|
455
|
+
|
|
456
|
+
updatePrice(price: number): void {
|
|
457
|
+
if (!this.loadedData || !this.marketId) {
|
|
458
|
+
throw new Error('Data not loaded. Call arm() first.');
|
|
459
|
+
}
|
|
460
|
+
// update price
|
|
461
|
+
const marketId = this.marketId;
|
|
462
|
+
this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
|
|
463
|
+
price;
|
|
464
|
+
this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
|
|
465
|
+
}
|
|
452
466
|
}
|
|
@@ -330,4 +330,15 @@ export default class TradeSimulationClient {
|
|
|
330
330
|
}
|
|
331
331
|
return snappedAmount;
|
|
332
332
|
}
|
|
333
|
+
|
|
334
|
+
updatePrice(price: number): void {
|
|
335
|
+
if (!this.loadedData || !this.marketId) {
|
|
336
|
+
throw new Error('Data not loaded. Call arm() first.');
|
|
337
|
+
}
|
|
338
|
+
// update price
|
|
339
|
+
const marketId = this.marketId;
|
|
340
|
+
this.loadedData.exposureDataPassivePool.oraclePricePerMarket[marketId] =
|
|
341
|
+
price;
|
|
342
|
+
this.loadedData.exposureDataAccount.oraclePricePerMarket[marketId] = price;
|
|
343
|
+
}
|
|
333
344
|
}
|