Skip to content

Commit

Permalink
Clean up some risk calculations (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipzeta authored Jul 27, 2023
1 parent a9a17c6 commit 457c2cc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
8 changes: 0 additions & 8 deletions src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
17 changes: 13 additions & 4 deletions src/risk-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MarginAccount,
ProductLedger,
} from "./program-types";
import { getProductLedger } from "./utils";
import { getProductLedger, convertNativeBNToDecimal } from "./utils";

export function collectRiskMaps(
imMap: Map<Asset, Number>,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 0 additions & 24 deletions src/subexchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -151,7 +146,6 @@ export class SubExchange {
}

this._perpSyncQueue = fetchedAccs[0] as PerpSyncQueue;
this.updateMarginParams();

this._markets = await ZetaGroupMarkets.load(
asset,
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 457c2cc

Please sign in to comment.