From 457c2cc625b8fbf05b5eba3ae16c8ba1ea93ecc8 Mon Sep 17 00:00:00 2001 From: filipzeta <103913117+filipzeta@users.noreply.github.com> Date: Thu, 27 Jul 2023 13:50:10 +1000 Subject: [PATCH] Clean up some risk calculations (#255) --- CHANGELOG.md | 1 + src/exchange.ts | 8 -------- src/risk-utils.ts | 17 +++++++++++++---- src/subexchange.ts | 24 ------------------------ 4 files changed, 14 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d25307ef8..f70f4bf9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Version changes are pinned to SDK releases. ## [1.5.0] 2023-07-25 +- Clean up some risk calculations under the hood. ([#255](https://github.com/zetamarkets/sdk/pull/255)) - Add fee to TradeEventV3. ([#253](https://github.com/zetamarkets/sdk/pull/253)) - Add header to event queue fetch. ([#250](https://github.com/zetamarkets/sdk/pull/250)) diff --git a/src/exchange.ts b/src/exchange.ts index 59285028f..b9f6a4669 100644 --- a/src/exchange.ts +++ b/src/exchange.ts @@ -950,10 +950,6 @@ export class Exchange { await this.getSubExchange(asset).updatePricingParameters(args); } - public getMarginParams(asset: Asset) { - return this.getSubExchange(asset).marginParams; - } - public async updateMarginParameters( asset: Asset, args: instructions.UpdateMarginParametersArgs @@ -1089,10 +1085,6 @@ export class Exchange { } } - public updateMarginParams(asset: Asset) { - this.getSubExchange(asset).updateMarginParams(); - } - public async halt(asset: Asset) { await this.getSubExchange(asset).halt(); } diff --git a/src/risk-utils.ts b/src/risk-utils.ts index 0bc63189b..9bb7161e2 100644 --- a/src/risk-utils.ts +++ b/src/risk-utils.ts @@ -7,7 +7,7 @@ import { MarginAccount, ProductLedger, } from "./program-types"; -import { getProductLedger } from "./utils"; +import { getProductLedger, convertNativeBNToDecimal } from "./utils"; export function collectRiskMaps( imMap: Map, @@ -107,10 +107,19 @@ export function calculateFutureMargin( asset: Asset, spotPrice: number ): types.MarginRequirement { - let subExchange = Exchange.getSubExchange(asset); - let initial = spotPrice * subExchange.marginParams.futureMarginInitial; + let assetIndex = assets.assetToIndex(asset); + let initial = + spotPrice * + convertNativeBNToDecimal( + Exchange.pricing.marginParameters[assetIndex].futureMarginInitial, + constants.MARGIN_PRECISION + ); let maintenance = - spotPrice * subExchange.marginParams.futureMarginMaintenance; + spotPrice * + convertNativeBNToDecimal( + Exchange.pricing.marginParameters[assetIndex].futureMarginMaintenance, + constants.MARGIN_PRECISION + ); return { initialLong: initial, initialShort: initial, diff --git a/src/subexchange.ts b/src/subexchange.ts index a850a30bf..97f8d6a7e 100644 --- a/src/subexchange.ts +++ b/src/subexchange.ts @@ -94,11 +94,6 @@ export class SubExchange { } private _perpSyncQueueAddress: PublicKey; - public get marginParams(): types.MarginParams { - return this._marginParams; - } - private _marginParams: types.MarginParams; - public get halted(): boolean { return Exchange.state.haltStates[assetToIndex(this._asset)].halted; } @@ -151,7 +146,6 @@ export class SubExchange { } this._perpSyncQueue = fetchedAccs[0] as PerpSyncQueue; - this.updateMarginParams(); this._markets = await ZetaGroupMarkets.load( asset, @@ -609,24 +603,6 @@ export class SubExchange { await utils.processTransaction(Exchange.provider, tx); } - public updateMarginParams() { - if (Exchange.pricing === undefined) { - return; - } - this._marginParams = { - futureMarginInitial: utils.convertNativeBNToDecimal( - Exchange.pricing.marginParameters[assetToIndex(this._asset)] - .futureMarginInitial, - constants.MARGIN_PRECISION - ), - futureMarginMaintenance: utils.convertNativeBNToDecimal( - Exchange.pricing.marginParameters[assetToIndex(this._asset)] - .futureMarginMaintenance, - constants.MARGIN_PRECISION - ), - }; - } - /** * Halt zeta group functionality. */