@reyaxyz/api-sdk 0.77.1 → 0.78.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/dist/clients/modules/isolated-order.simulation/index.js +4 -1
- package/dist/clients/modules/isolated-order.simulation/index.js.map +1 -1
- package/dist/clients/socket-client.js +182 -0
- package/dist/clients/socket-client.js.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/types/clients/modules/isolated-order.simulation/index.d.ts.map +1 -1
- package/dist/types/clients/socket-client.d.ts +72 -0
- package/dist/types/clients/socket-client.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +8 -4
- package/src/clients/modules/isolated-order.simulation/index.ts +5 -1
- package/src/clients/socket-client.ts +187 -0
- package/src/index.ts +1 -0
|
@@ -111,6 +111,7 @@ var IsolatedOrderSimulationClient = /** @class */ (function () {
|
|
|
111
111
|
* trade operation
|
|
112
112
|
* */
|
|
113
113
|
var liquidationPrice = common_1.ExposureCommand.calculateLiquidation(requiredMargin, isolatedLMR, this.loadedData.exposureDataPassivePool.oraclePricePerMarket[this.loadedData.marketConfiguration.market_id], amount);
|
|
114
|
+
// todo: p1: margin ratio seems to be wrong on ui
|
|
114
115
|
var marginRatio = common_1.ExposureCommand.getMarginRatio(newMarginInfoSource);
|
|
115
116
|
var marginRatioHealth = common_1.ExposureCommand.evaluateHealthStatus(marginRatio, newMarginInfoSource);
|
|
116
117
|
var baseSpacing = (0, common_1.amountNormalizer)(this.loadedData.marketConfiguration.base_spacing).toNumber();
|
|
@@ -202,10 +203,12 @@ var IsolatedOrderSimulationClient = /** @class */ (function () {
|
|
|
202
203
|
that performs the isolated position trade
|
|
203
204
|
*/
|
|
204
205
|
var availableMargin = userAccountExposure.getUsdNodeMarginInfo.initialDelta;
|
|
205
|
-
// todo: p2: how do we deal with edge cases where e.g. min bound > maxBound
|
|
206
206
|
var minBound = (0, bignumber_js_1.default)(snappedAmountInRusd)
|
|
207
207
|
.dividedBy(availableMargin)
|
|
208
208
|
.toNumber();
|
|
209
|
+
if (minBound > maxBound) {
|
|
210
|
+
throw Error('Min leverage bound higher than max');
|
|
211
|
+
}
|
|
209
212
|
return {
|
|
210
213
|
minBound: minBound,
|
|
211
214
|
maxBound: maxBound,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/isolated-order.simulation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,0CAMyB;AACzB,8DAAqC;AAGrC;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,mBAAmB,EACxC,oBAAoB,CAAC,eAAe,EACpC,oBAAoB,CAAC,YAAY,EACjC,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,+BAA+B,EACpD,oBAAoB,CAAC,oBAAoB,EACzC,oBAAoB,CAAC,sBAAsB,EAC3C,oBAAoB,CAAC,uBAAuB,EAC5C,oBAAoB,CAAC,cAAc,EACnC,oBAAoB,CAAC,gBAAgB,EACrC,oBAAoB,CAAC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AC;QAExD,2EAA2E;QAC3E,IAAM,QAAQ,GAAG,IAAA,sBAAS,EAAC,mBAAmB,CAAC;aAC5C,SAAS,CAAC,eAAe,CAAC;aAC1B,QAAQ,EAAE,CAAC;QAEd,OAAO;YACL,QAAQ,EAAE,QAAQ;YAClB,QAAQ,EAAE,QAAQ;YAClB,eAAe,EAAE,eAAe;SACjC,CAAC;IACJ,CAAC;IACH,oCAAC;AAAD,CAAC,AAvTD,IAuTC","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';\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 size in rUSD terms / leverage\n */\n\n const requiredMargin = BigNumber(params.amount)\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 const marginRatio = ExposureCommand.getMarginRatio(newMarginInfoSource);\n\n const marginRatioHealth = ExposureCommand.evaluateHealthStatus(\n marginRatio,\n newMarginInfoSource,\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 return {\n estimatedPrice: estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees: fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth: marginRatioHealth,\n snappedAmount: this.roundToBaseSpacing(amount, baseSpacing) * spotPrice,\n snappedAmountInBase: this.roundToBaseSpacing(amount, baseSpacing),\n requiredMargin: requiredMargin,\n editCollateralActions: editCollateralActions,\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 roundToBaseSpacing(amount: number, baseSpacing: number): number {\n return Math.floor(amount / baseSpacing) * baseSpacing;\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 // todo: p2: how do we deal with edge cases where e.g. min bound > maxBound\n const minBound = BigNumber(snappedAmountInRusd)\n .dividedBy(availableMargin)\n .toNumber();\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;AAGrC;IAGE,uCAAY,aAA4B;QAFhC,eAAU,GAAgC,IAAI,CAAC;QAGrD,oBAAoB;QACpB,IAAI,CAAC,aAA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ClC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,YAAY,CACjD,CAAC,QAAQ,EAAE,CAAC;QACb,IAAM,mBAAmB,GAAG,IAAI,CAAC,qBAAqB,CACpD,kBAAkB,EAClB,SAAS,EACT,WAAW,CACZ,CAAC;QAEF;;;;;;;;;aASK;QAEL,gBAAgB;QAEhB,IAAM,eAAe,GAAG,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;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,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,AA3TD,IA2TC","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';\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 size in rUSD terms / leverage\n */\n\n const requiredMargin = BigNumber(params.amount)\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,\n newMarginInfoSource,\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 return {\n estimatedPrice: estimatedPrice,\n estimatedSlippage: slippage * 100,\n fees: fees,\n liquidationPrice: liquidationPrice.toNumber(),\n marginRatio: marginRatio * 100,\n marginRatioHealth: marginRatioHealth,\n snappedAmount: this.roundToBaseSpacing(amount, baseSpacing) * spotPrice,\n snappedAmountInBase: this.roundToBaseSpacing(amount, baseSpacing),\n requiredMargin: requiredMargin,\n editCollateralActions: editCollateralActions,\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 roundToBaseSpacing(amount: number, baseSpacing: number): number {\n return Math.floor(amount / baseSpacing) * baseSpacing;\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 .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,2 +1,184 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SocketClient = exports.IncomingMessageTypes = void 0;
|
|
18
|
+
var ws_1 = __importDefault(require("ws"));
|
|
19
|
+
var common_1 = require("@reyaxyz/common");
|
|
20
|
+
var OutgoingMessageTypes;
|
|
21
|
+
(function (OutgoingMessageTypes) {
|
|
22
|
+
OutgoingMessageTypes["PING"] = "ping";
|
|
23
|
+
OutgoingMessageTypes["SUBSCRIBE"] = "subscribe";
|
|
24
|
+
OutgoingMessageTypes["UNSUBSCRIBE"] = "unsubscribe";
|
|
25
|
+
})(OutgoingMessageTypes || (OutgoingMessageTypes = {}));
|
|
26
|
+
var SocketChannels;
|
|
27
|
+
(function (SocketChannels) {
|
|
28
|
+
SocketChannels["CANDLES"] = "candles";
|
|
29
|
+
})(SocketChannels || (SocketChannels = {}));
|
|
30
|
+
var IncomingMessageTypes;
|
|
31
|
+
(function (IncomingMessageTypes) {
|
|
32
|
+
IncomingMessageTypes["CONNECTED"] = "connected";
|
|
33
|
+
IncomingMessageTypes["SUBSCRIBED"] = "subscribed";
|
|
34
|
+
IncomingMessageTypes["ERROR"] = "error";
|
|
35
|
+
IncomingMessageTypes["CHANNEL_DATA"] = "channel_data";
|
|
36
|
+
IncomingMessageTypes["CHANNEL_BATCH_DATA"] = "channel_batch_data";
|
|
37
|
+
IncomingMessageTypes["PONG"] = "pong";
|
|
38
|
+
})(IncomingMessageTypes || (exports.IncomingMessageTypes = IncomingMessageTypes = {}));
|
|
39
|
+
var SocketClient = /** @class */ (function () {
|
|
40
|
+
function SocketClient(environment, onOpenCallback, onCloseCallback, onMessageCallback) {
|
|
41
|
+
this.pingInterval = 30000;
|
|
42
|
+
this.lastMessageTime = Date.now();
|
|
43
|
+
this.onOpenCallback = onOpenCallback;
|
|
44
|
+
this.onCloseCallback = onCloseCallback;
|
|
45
|
+
this.onMessageCallback = onMessageCallback;
|
|
46
|
+
this.url = (common_1.API_CLIENT_CONFIGS[environment] || common_1.API_CLIENT_CONFIGS['test']).webSocketEndpoint;
|
|
47
|
+
}
|
|
48
|
+
SocketClient.prototype.connect = function () {
|
|
49
|
+
this.ws = new ws_1.default(this.url);
|
|
50
|
+
this.ws.addEventListener('open', this.handleOpen.bind(this));
|
|
51
|
+
this.ws.addEventListener('close', this.handleClose.bind(this));
|
|
52
|
+
this.ws.addEventListener('message', this.handleMessage.bind(this));
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* @description Close the websocket connection.
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
SocketClient.prototype.close = function () {
|
|
59
|
+
var _a;
|
|
60
|
+
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
|
|
61
|
+
this.ws = undefined;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* @description Send data to the websocket connection.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
SocketClient.prototype.send = function (data) {
|
|
68
|
+
var _a;
|
|
69
|
+
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(data);
|
|
70
|
+
};
|
|
71
|
+
SocketClient.prototype.handleOpen = function () {
|
|
72
|
+
if (this.onOpenCallback) {
|
|
73
|
+
this.onOpenCallback();
|
|
74
|
+
}
|
|
75
|
+
this.restartPingInterval();
|
|
76
|
+
};
|
|
77
|
+
SocketClient.prototype.handleClose = function () {
|
|
78
|
+
if (this.onCloseCallback) {
|
|
79
|
+
this.onCloseCallback();
|
|
80
|
+
}
|
|
81
|
+
clearInterval(this.pingIntervalId);
|
|
82
|
+
};
|
|
83
|
+
SocketClient.prototype.handleMessage = function (event) {
|
|
84
|
+
if (event.data === 'PING') {
|
|
85
|
+
this.send('PONG');
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.lastMessageTime = Date.now();
|
|
89
|
+
if (this.onMessageCallback) {
|
|
90
|
+
this.onMessageCallback(event);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.restartPingInterval();
|
|
94
|
+
};
|
|
95
|
+
SocketClient.prototype.restartPingInterval = function () {
|
|
96
|
+
var _this = this;
|
|
97
|
+
clearInterval(this.pingIntervalId);
|
|
98
|
+
this.pingIntervalId = setInterval(function () {
|
|
99
|
+
var elapsedTime = Date.now() - _this.lastMessageTime;
|
|
100
|
+
if (elapsedTime > _this.pingInterval) {
|
|
101
|
+
var message = {
|
|
102
|
+
type: OutgoingMessageTypes.PING,
|
|
103
|
+
};
|
|
104
|
+
_this.send(JSON.stringify(message));
|
|
105
|
+
}
|
|
106
|
+
}, this.pingInterval);
|
|
107
|
+
};
|
|
108
|
+
Object.defineProperty(SocketClient.prototype, "onOpen", {
|
|
109
|
+
/**
|
|
110
|
+
* @description Set callback when the socket is opened.
|
|
111
|
+
*
|
|
112
|
+
*/
|
|
113
|
+
set: function (callback) {
|
|
114
|
+
this.onOpenCallback = callback;
|
|
115
|
+
},
|
|
116
|
+
enumerable: false,
|
|
117
|
+
configurable: true
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(SocketClient.prototype, "onClose", {
|
|
120
|
+
/**
|
|
121
|
+
* @description Set callback when the socket is closed.
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
set: function (callback) {
|
|
125
|
+
this.onCloseCallback = callback;
|
|
126
|
+
},
|
|
127
|
+
enumerable: false,
|
|
128
|
+
configurable: true
|
|
129
|
+
});
|
|
130
|
+
Object.defineProperty(SocketClient.prototype, "onMessage", {
|
|
131
|
+
/**
|
|
132
|
+
* @description Set callback when the socket receives a message.
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
set: function (callback) {
|
|
136
|
+
this.onMessageCallback = callback;
|
|
137
|
+
},
|
|
138
|
+
enumerable: false,
|
|
139
|
+
configurable: true
|
|
140
|
+
});
|
|
141
|
+
/**
|
|
142
|
+
* @description Send a subscribe message to the websocket connection.
|
|
143
|
+
*
|
|
144
|
+
*/
|
|
145
|
+
SocketClient.prototype.subscribe = function (channel, params) {
|
|
146
|
+
var message = __assign({ type: OutgoingMessageTypes.SUBSCRIBE, channel: channel }, params);
|
|
147
|
+
this.send(JSON.stringify(message));
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* @description Send an unsubscribe message to the websocket connection.
|
|
151
|
+
*
|
|
152
|
+
*/
|
|
153
|
+
SocketClient.prototype.unsubscribe = function (channel, params) {
|
|
154
|
+
var message = __assign({ type: OutgoingMessageTypes.UNSUBSCRIBE, channel: channel }, params);
|
|
155
|
+
this.send(JSON.stringify(message));
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* @description Subscribe to candles channel
|
|
159
|
+
* for a specific market and resolution.
|
|
160
|
+
*
|
|
161
|
+
*/
|
|
162
|
+
SocketClient.prototype.subscribeToCandles = function (market, resolution) {
|
|
163
|
+
var channel = SocketChannels.CANDLES;
|
|
164
|
+
var params = {
|
|
165
|
+
id: "".concat(market, "/").concat(resolution),
|
|
166
|
+
batched: true,
|
|
167
|
+
};
|
|
168
|
+
this.subscribe(channel, params);
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* @description Unsubscribe from candles channel
|
|
172
|
+
* for a specific market and resolution.
|
|
173
|
+
*/
|
|
174
|
+
SocketClient.prototype.unsubscribeFromCandles = function (market, resolution) {
|
|
175
|
+
var channel = SocketChannels.CANDLES;
|
|
176
|
+
var params = {
|
|
177
|
+
id: "".concat(market, "/").concat(resolution),
|
|
178
|
+
};
|
|
179
|
+
this.unsubscribe(channel, params);
|
|
180
|
+
};
|
|
181
|
+
return SocketClient;
|
|
182
|
+
}());
|
|
183
|
+
exports.SocketClient = SocketClient;
|
|
2
184
|
//# sourceMappingURL=socket-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket-client.js","sourceRoot":"/","sources":["clients/socket-client.ts"],"names":[],"mappings":"","sourcesContent":[""]}
|
|
1
|
+
{"version":3,"file":"socket-client.js","sourceRoot":"/","sources":["clients/socket-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAA6C;AAC7C,0CAIyB;AAEzB,IAAK,oBAIJ;AAJD,WAAK,oBAAoB;IACvB,qCAAa,CAAA;IACb,+CAAuB,CAAA;IACvB,mDAA2B,CAAA;AAC7B,CAAC,EAJI,oBAAoB,KAApB,o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O,EAAE,IAAI;SACd,CAAC;QACF,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,6CAAsB,GAAtB,UAAuB,MAAc,EAAE,UAA6B;QAClE,IAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC;QACvC,IAAM,MAAM,GAAG;YACb,EAAE,EAAE,UAAG,MAAM,cAAI,UAAU,CAAE;SAC9B,CAAC;QACF,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC;IACH,mBAAC;AAAD,CAAC,AAhKD,IAgKC;AAhKY,oCAAY","sourcesContent":["import WebSocket, { MessageEvent } from 'ws';\nimport {\n API_CLIENT_CONFIGS,\n CandlesResolution,\n ServiceConfig,\n} from '@reyaxyz/common';\n\nenum OutgoingMessageTypes {\n PING = 'ping',\n SUBSCRIBE = 'subscribe',\n UNSUBSCRIBE = 'unsubscribe',\n}\n\nenum SocketChannels {\n CANDLES = 'candles',\n}\n\nexport enum IncomingMessageTypes {\n CONNECTED = 'connected',\n SUBSCRIBED = 'subscribed',\n ERROR = 'error',\n CHANNEL_DATA = 'channel_data',\n CHANNEL_BATCH_DATA = 'channel_batch_data',\n PONG = 'pong',\n}\n\nexport class SocketClient {\n private url: string;\n private ws?: WebSocket;\n private onOpenCallback?: () => void;\n private onCloseCallback?: () => void;\n private onMessageCallback?: (event: MessageEvent) => void;\n private pingInterval: number = 30_000;\n private lastMessageTime: number = Date.now();\n private pingIntervalId?: NodeJS.Timeout;\n constructor(\n environment: ServiceConfig['environment'],\n onOpenCallback: () => void,\n onCloseCallback: () => void,\n onMessageCallback: (event: MessageEvent) => void,\n ) {\n this.onOpenCallback = onOpenCallback;\n this.onCloseCallback = onCloseCallback;\n this.onMessageCallback = onMessageCallback;\n this.url = (\n API_CLIENT_CONFIGS[environment] || API_CLIENT_CONFIGS['test']\n ).webSocketEndpoint;\n }\n\n connect(): void {\n this.ws = new WebSocket(this.url);\n this.ws.addEventListener('open', this.handleOpen.bind(this));\n this.ws.addEventListener('close', this.handleClose.bind(this));\n this.ws.addEventListener('message', this.handleMessage.bind(this));\n }\n\n /**\n * @description Close the websocket connection.\n *\n */\n close(): void {\n this.ws?.close();\n this.ws = undefined;\n }\n\n /**\n * @description Send data to the websocket connection.\n *\n */\n send(data: string): void {\n this.ws?.send(data);\n }\n\n private handleOpen(): void {\n if (this.onOpenCallback) {\n this.onOpenCallback();\n }\n this.restartPingInterval();\n }\n\n private handleClose(): void {\n if (this.onCloseCallback) {\n this.onCloseCallback();\n }\n clearInterval(this.pingIntervalId);\n }\n\n private handleMessage(event: MessageEvent): void {\n if (event.data === 'PING') {\n this.send('PONG');\n } else {\n this.lastMessageTime = Date.now();\n if (this.onMessageCallback) {\n this.onMessageCallback(event);\n }\n }\n this.restartPingInterval();\n }\n\n private restartPingInterval(): void {\n clearInterval(this.pingIntervalId);\n this.pingIntervalId = setInterval(() => {\n const elapsedTime = Date.now() - this.lastMessageTime;\n if (elapsedTime > this.pingInterval) {\n const message = {\n type: OutgoingMessageTypes.PING,\n };\n this.send(JSON.stringify(message));\n }\n }, this.pingInterval);\n }\n\n /**\n * @description Set callback when the socket is opened.\n *\n */\n set onOpen(callback: () => void) {\n this.onOpenCallback = callback;\n }\n\n /**\n * @description Set callback when the socket is closed.\n *\n */\n set onClose(callback: () => void) {\n this.onCloseCallback = callback;\n }\n\n /**\n * @description Set callback when the socket receives a message.\n *\n */\n set onMessage(callback: (event: MessageEvent) => void) {\n this.onMessageCallback = callback;\n }\n\n /**\n * @description Send a subscribe message to the websocket connection.\n *\n */\n subscribe(channel: string, params?: object): void {\n const message = {\n type: OutgoingMessageTypes.SUBSCRIBE,\n channel,\n ...params,\n };\n this.send(JSON.stringify(message));\n }\n\n /**\n * @description Send an unsubscribe message to the websocket connection.\n *\n */\n unsubscribe(channel: string, params?: object): void {\n const message = {\n type: OutgoingMessageTypes.UNSUBSCRIBE,\n channel,\n ...params,\n };\n this.send(JSON.stringify(message));\n }\n /**\n * @description Subscribe to candles channel\n * for a specific market and resolution.\n *\n */\n subscribeToCandles(market: string, resolution: CandlesResolution): void {\n const channel = SocketChannels.CANDLES;\n const params = {\n id: `${market}/${resolution}`,\n batched: true,\n };\n this.subscribe(channel, params);\n }\n\n /**\n * @description Unsubscribe from candles channel\n * for a specific market and resolution.\n */\n unsubscribeFromCandles(market: string, resolution: CandlesResolution): void {\n const channel = SocketChannels.CANDLES;\n const params = {\n id: `${market}/${resolution}`,\n };\n this.unsubscribe(channel, params);\n }\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -14,8 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.ApiClient = void 0;
|
|
17
|
+
exports.SocketClient = exports.ApiClient = void 0;
|
|
18
18
|
var api_client_1 = require("./clients/api-client");
|
|
19
19
|
Object.defineProperty(exports, "ApiClient", { enumerable: true, get: function () { return api_client_1.ApiClient; } });
|
|
20
|
+
var socket_client_1 = require("./clients/socket-client");
|
|
21
|
+
Object.defineProperty(exports, "SocketClient", { enumerable: true, get: function () { return socket_client_1.SocketClient; } });
|
|
20
22
|
__exportStar(require("./clients/types"), exports);
|
|
21
23
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAiD;AAAxC,uGAAA,SAAS,OAAA;AAClB,kDAAgC","sourcesContent":["export { ApiClient } from './clients/api-client';\nexport * from './clients/types';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAiD;AAAxC,uGAAA,SAAS,OAAA;AAClB,yDAAuD;AAA9C,6GAAA,YAAY,OAAA;AACrB,kDAAgC","sourcesContent":["export { ApiClient } from './clients/api-client';\nexport { SocketClient } from './clients/socket-client';\nexport * from './clients/types';\n"]}
|
|
@@ -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;AAIzB,MAAM,CAAC,OAAO,OAAO,6BAA6B;IAChD,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,aAAa,CAAgB;gBACzB,aAAa,EAAE,aAAa;IAMlC,GAAG,CAAC,MAAM,EAAE,qCAAqC,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvE,MAAM,CAAC,wBAAwB,CAC7B,oBAAoB,EAAE,oBAAoB,GACzC,eAAe;YAoBJ,eAAe;IAW7B,QAAQ,CACN,MAAM,EAAE,qCAAqC,GAC5C,2BAA2B;
|
|
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;AAIzB,MAAM,CAAC,OAAO,OAAO,6BAA6B;IAChD,OAAO,CAAC,UAAU,CAAqC;IACvD,OAAO,CAAC,aAAa,CAAgB;gBACzB,aAAa,EAAE,aAAa;IAMlC,GAAG,CAAC,MAAM,EAAE,qCAAqC,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvE,MAAM,CAAC,wBAAwB,CAC7B,oBAAoB,EAAE,oBAAoB,GACzC,eAAe;YAoBJ,eAAe;IAW7B,QAAQ,CACN,MAAM,EAAE,qCAAqC,GAC5C,2BAA2B;IAiH9B,YAAY,CACV,MAAM,EAAE,yCAAyC,GAChD,yCAAyC;IAuB5C,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM;IAI/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;CAoEnF"}
|
|
@@ -1 +1,73 @@
|
|
|
1
|
+
import { MessageEvent } from 'ws';
|
|
2
|
+
import { CandlesResolution, ServiceConfig } from '@reyaxyz/common';
|
|
3
|
+
export declare enum IncomingMessageTypes {
|
|
4
|
+
CONNECTED = "connected",
|
|
5
|
+
SUBSCRIBED = "subscribed",
|
|
6
|
+
ERROR = "error",
|
|
7
|
+
CHANNEL_DATA = "channel_data",
|
|
8
|
+
CHANNEL_BATCH_DATA = "channel_batch_data",
|
|
9
|
+
PONG = "pong"
|
|
10
|
+
}
|
|
11
|
+
export declare class SocketClient {
|
|
12
|
+
private url;
|
|
13
|
+
private ws?;
|
|
14
|
+
private onOpenCallback?;
|
|
15
|
+
private onCloseCallback?;
|
|
16
|
+
private onMessageCallback?;
|
|
17
|
+
private pingInterval;
|
|
18
|
+
private lastMessageTime;
|
|
19
|
+
private pingIntervalId?;
|
|
20
|
+
constructor(environment: ServiceConfig['environment'], onOpenCallback: () => void, onCloseCallback: () => void, onMessageCallback: (event: MessageEvent) => void);
|
|
21
|
+
connect(): void;
|
|
22
|
+
/**
|
|
23
|
+
* @description Close the websocket connection.
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
close(): void;
|
|
27
|
+
/**
|
|
28
|
+
* @description Send data to the websocket connection.
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
send(data: string): void;
|
|
32
|
+
private handleOpen;
|
|
33
|
+
private handleClose;
|
|
34
|
+
private handleMessage;
|
|
35
|
+
private restartPingInterval;
|
|
36
|
+
/**
|
|
37
|
+
* @description Set callback when the socket is opened.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
set onOpen(callback: () => void);
|
|
41
|
+
/**
|
|
42
|
+
* @description Set callback when the socket is closed.
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
set onClose(callback: () => void);
|
|
46
|
+
/**
|
|
47
|
+
* @description Set callback when the socket receives a message.
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
set onMessage(callback: (event: MessageEvent) => void);
|
|
51
|
+
/**
|
|
52
|
+
* @description Send a subscribe message to the websocket connection.
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
subscribe(channel: string, params?: object): void;
|
|
56
|
+
/**
|
|
57
|
+
* @description Send an unsubscribe message to the websocket connection.
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
unsubscribe(channel: string, params?: object): void;
|
|
61
|
+
/**
|
|
62
|
+
* @description Subscribe to candles channel
|
|
63
|
+
* for a specific market and resolution.
|
|
64
|
+
*
|
|
65
|
+
*/
|
|
66
|
+
subscribeToCandles(market: string, resolution: CandlesResolution): void;
|
|
67
|
+
/**
|
|
68
|
+
* @description Unsubscribe from candles channel
|
|
69
|
+
* for a specific market and resolution.
|
|
70
|
+
*/
|
|
71
|
+
unsubscribeFromCandles(market: string, resolution: CandlesResolution): void;
|
|
72
|
+
}
|
|
1
73
|
//# sourceMappingURL=socket-client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket-client.d.ts","sourceRoot":"/","sources":["clients/socket-client.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"socket-client.d.ts","sourceRoot":"/","sources":["clients/socket-client.ts"],"names":[],"mappings":"AAAA,OAAkB,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAC7C,OAAO,EAEL,iBAAiB,EACjB,aAAa,EACd,MAAM,iBAAiB,CAAC;AAYzB,oBAAY,oBAAoB;IAC9B,SAAS,cAAc;IACvB,UAAU,eAAe;IACzB,KAAK,UAAU;IACf,YAAY,iBAAiB;IAC7B,kBAAkB,uBAAuB;IACzC,IAAI,SAAS;CACd;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,EAAE,CAAC,CAAY;IACvB,OAAO,CAAC,cAAc,CAAC,CAAa;IACpC,OAAO,CAAC,eAAe,CAAC,CAAa;IACrC,OAAO,CAAC,iBAAiB,CAAC,CAAgC;IAC1D,OAAO,CAAC,YAAY,CAAkB;IACtC,OAAO,CAAC,eAAe,CAAsB;IAC7C,OAAO,CAAC,cAAc,CAAC,CAAiB;gBAEtC,WAAW,EAAE,aAAa,CAAC,aAAa,CAAC,EACzC,cAAc,EAAE,MAAM,IAAI,EAC1B,eAAe,EAAE,MAAM,IAAI,EAC3B,iBAAiB,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI;IAUlD,OAAO,IAAI,IAAI;IAOf;;;OAGG;IACH,KAAK,IAAI,IAAI;IAKb;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAIxB,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,WAAW;IAOnB,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,mBAAmB;IAa3B;;;OAGG;IACH,IAAI,MAAM,CAAC,QAAQ,EAAE,MAAM,IAAI,EAE9B;IAED;;;OAGG;IACH,IAAI,OAAO,CAAC,QAAQ,EAAE,MAAM,IAAI,EAE/B;IAED;;;OAGG;IACH,IAAI,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,EAEpD;IAED;;;OAGG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IASjD;;;OAGG;IACH,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAQnD;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,iBAAiB,GAAG,IAAI;IASvE;;;OAGG;IACH,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,iBAAiB,GAAG,IAAI;CAO5E"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,cAAc,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/api-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.78.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -33,9 +33,13 @@
|
|
|
33
33
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@reyaxyz/common": "0.
|
|
37
|
-
"bignumber.js": "^9.1.2"
|
|
36
|
+
"@reyaxyz/common": "0.90.0",
|
|
37
|
+
"bignumber.js": "^9.1.2",
|
|
38
|
+
"ws": "^8.16.0"
|
|
38
39
|
},
|
|
39
40
|
"packageManager": "pnpm@8.3.1",
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "9a1fa7fed7448ce867f599ab211a8e99ce366f4c",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/ws": "^8.5.10"
|
|
44
|
+
}
|
|
41
45
|
}
|
|
@@ -152,6 +152,7 @@ export default class IsolatedOrderSimulationClient {
|
|
|
152
152
|
amount,
|
|
153
153
|
);
|
|
154
154
|
|
|
155
|
+
// todo: p1: margin ratio seems to be wrong on ui
|
|
155
156
|
const marginRatio = ExposureCommand.getMarginRatio(newMarginInfoSource);
|
|
156
157
|
|
|
157
158
|
const marginRatioHealth = ExposureCommand.evaluateHealthStatus(
|
|
@@ -318,11 +319,14 @@ export default class IsolatedOrderSimulationClient {
|
|
|
318
319
|
const availableMargin =
|
|
319
320
|
userAccountExposure.getUsdNodeMarginInfo.initialDelta;
|
|
320
321
|
|
|
321
|
-
// todo: p2: how do we deal with edge cases where e.g. min bound > maxBound
|
|
322
322
|
const minBound = BigNumber(snappedAmountInRusd)
|
|
323
323
|
.dividedBy(availableMargin)
|
|
324
324
|
.toNumber();
|
|
325
325
|
|
|
326
|
+
if (minBound > maxBound) {
|
|
327
|
+
throw Error('Min leverage bound higher than max');
|
|
328
|
+
}
|
|
329
|
+
|
|
326
330
|
return {
|
|
327
331
|
minBound: minBound,
|
|
328
332
|
maxBound: maxBound,
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import WebSocket, { MessageEvent } from 'ws';
|
|
2
|
+
import {
|
|
3
|
+
API_CLIENT_CONFIGS,
|
|
4
|
+
CandlesResolution,
|
|
5
|
+
ServiceConfig,
|
|
6
|
+
} from '@reyaxyz/common';
|
|
7
|
+
|
|
8
|
+
enum OutgoingMessageTypes {
|
|
9
|
+
PING = 'ping',
|
|
10
|
+
SUBSCRIBE = 'subscribe',
|
|
11
|
+
UNSUBSCRIBE = 'unsubscribe',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
enum SocketChannels {
|
|
15
|
+
CANDLES = 'candles',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum IncomingMessageTypes {
|
|
19
|
+
CONNECTED = 'connected',
|
|
20
|
+
SUBSCRIBED = 'subscribed',
|
|
21
|
+
ERROR = 'error',
|
|
22
|
+
CHANNEL_DATA = 'channel_data',
|
|
23
|
+
CHANNEL_BATCH_DATA = 'channel_batch_data',
|
|
24
|
+
PONG = 'pong',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class SocketClient {
|
|
28
|
+
private url: string;
|
|
29
|
+
private ws?: WebSocket;
|
|
30
|
+
private onOpenCallback?: () => void;
|
|
31
|
+
private onCloseCallback?: () => void;
|
|
32
|
+
private onMessageCallback?: (event: MessageEvent) => void;
|
|
33
|
+
private pingInterval: number = 30_000;
|
|
34
|
+
private lastMessageTime: number = Date.now();
|
|
35
|
+
private pingIntervalId?: NodeJS.Timeout;
|
|
36
|
+
constructor(
|
|
37
|
+
environment: ServiceConfig['environment'],
|
|
38
|
+
onOpenCallback: () => void,
|
|
39
|
+
onCloseCallback: () => void,
|
|
40
|
+
onMessageCallback: (event: MessageEvent) => void,
|
|
41
|
+
) {
|
|
42
|
+
this.onOpenCallback = onOpenCallback;
|
|
43
|
+
this.onCloseCallback = onCloseCallback;
|
|
44
|
+
this.onMessageCallback = onMessageCallback;
|
|
45
|
+
this.url = (
|
|
46
|
+
API_CLIENT_CONFIGS[environment] || API_CLIENT_CONFIGS['test']
|
|
47
|
+
).webSocketEndpoint;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
connect(): void {
|
|
51
|
+
this.ws = new WebSocket(this.url);
|
|
52
|
+
this.ws.addEventListener('open', this.handleOpen.bind(this));
|
|
53
|
+
this.ws.addEventListener('close', this.handleClose.bind(this));
|
|
54
|
+
this.ws.addEventListener('message', this.handleMessage.bind(this));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @description Close the websocket connection.
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
close(): void {
|
|
62
|
+
this.ws?.close();
|
|
63
|
+
this.ws = undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @description Send data to the websocket connection.
|
|
68
|
+
*
|
|
69
|
+
*/
|
|
70
|
+
send(data: string): void {
|
|
71
|
+
this.ws?.send(data);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private handleOpen(): void {
|
|
75
|
+
if (this.onOpenCallback) {
|
|
76
|
+
this.onOpenCallback();
|
|
77
|
+
}
|
|
78
|
+
this.restartPingInterval();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private handleClose(): void {
|
|
82
|
+
if (this.onCloseCallback) {
|
|
83
|
+
this.onCloseCallback();
|
|
84
|
+
}
|
|
85
|
+
clearInterval(this.pingIntervalId);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private handleMessage(event: MessageEvent): void {
|
|
89
|
+
if (event.data === 'PING') {
|
|
90
|
+
this.send('PONG');
|
|
91
|
+
} else {
|
|
92
|
+
this.lastMessageTime = Date.now();
|
|
93
|
+
if (this.onMessageCallback) {
|
|
94
|
+
this.onMessageCallback(event);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
this.restartPingInterval();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private restartPingInterval(): void {
|
|
101
|
+
clearInterval(this.pingIntervalId);
|
|
102
|
+
this.pingIntervalId = setInterval(() => {
|
|
103
|
+
const elapsedTime = Date.now() - this.lastMessageTime;
|
|
104
|
+
if (elapsedTime > this.pingInterval) {
|
|
105
|
+
const message = {
|
|
106
|
+
type: OutgoingMessageTypes.PING,
|
|
107
|
+
};
|
|
108
|
+
this.send(JSON.stringify(message));
|
|
109
|
+
}
|
|
110
|
+
}, this.pingInterval);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @description Set callback when the socket is opened.
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
set onOpen(callback: () => void) {
|
|
118
|
+
this.onOpenCallback = callback;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @description Set callback when the socket is closed.
|
|
123
|
+
*
|
|
124
|
+
*/
|
|
125
|
+
set onClose(callback: () => void) {
|
|
126
|
+
this.onCloseCallback = callback;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @description Set callback when the socket receives a message.
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
set onMessage(callback: (event: MessageEvent) => void) {
|
|
134
|
+
this.onMessageCallback = callback;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @description Send a subscribe message to the websocket connection.
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
subscribe(channel: string, params?: object): void {
|
|
142
|
+
const message = {
|
|
143
|
+
type: OutgoingMessageTypes.SUBSCRIBE,
|
|
144
|
+
channel,
|
|
145
|
+
...params,
|
|
146
|
+
};
|
|
147
|
+
this.send(JSON.stringify(message));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @description Send an unsubscribe message to the websocket connection.
|
|
152
|
+
*
|
|
153
|
+
*/
|
|
154
|
+
unsubscribe(channel: string, params?: object): void {
|
|
155
|
+
const message = {
|
|
156
|
+
type: OutgoingMessageTypes.UNSUBSCRIBE,
|
|
157
|
+
channel,
|
|
158
|
+
...params,
|
|
159
|
+
};
|
|
160
|
+
this.send(JSON.stringify(message));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @description Subscribe to candles channel
|
|
164
|
+
* for a specific market and resolution.
|
|
165
|
+
*
|
|
166
|
+
*/
|
|
167
|
+
subscribeToCandles(market: string, resolution: CandlesResolution): void {
|
|
168
|
+
const channel = SocketChannels.CANDLES;
|
|
169
|
+
const params = {
|
|
170
|
+
id: `${market}/${resolution}`,
|
|
171
|
+
batched: true,
|
|
172
|
+
};
|
|
173
|
+
this.subscribe(channel, params);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @description Unsubscribe from candles channel
|
|
178
|
+
* for a specific market and resolution.
|
|
179
|
+
*/
|
|
180
|
+
unsubscribeFromCandles(market: string, resolution: CandlesResolution): void {
|
|
181
|
+
const channel = SocketChannels.CANDLES;
|
|
182
|
+
const params = {
|
|
183
|
+
id: `${market}/${resolution}`,
|
|
184
|
+
};
|
|
185
|
+
this.unsubscribe(channel, params);
|
|
186
|
+
}
|
|
187
|
+
}
|
package/src/index.ts
CHANGED