Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo committed Dec 12, 2024
1 parent e27402c commit 73d52e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
28 changes: 20 additions & 8 deletions src/abacus-ts/calculators/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export function calculateSubaccountSummaryCore(
if (market == null) {
return acc;
}
const { notionalTotal: positionValue, initialRiskTotal: positionRisk } =
getDerivedPositionInfo(position, market);
const { value: positionValue, initialRiskTotal: positionRisk } = getDerivedPositionInfo(
position,
market
);
return {
valueTotal: acc.valueTotal.plus(positionValue),
initialRiskTotal: acc.initialRiskTotal.plus(positionRisk),
Expand All @@ -59,25 +61,35 @@ function getDerivedPositionInfo(
market: IndexerPerpetualMarketResponseObject
): SubaccountPositionDerivedCore {
const marginMode = position.subaccountNumber < NUM_PARENT_SUBACCOUNTS ? 'CROSS' : 'ISOLATED';
const effectiveImf = getMarketEffectiveInitialMargin(market) ?? MustBigNumber(0);
const effectiveImf = getMarketEffectiveInitialMarginForMarket(market) ?? MustBigNumber(0);

const size = MustBigNumber(position.size);
// indexer position size is already signed I think but we will be extra sure
const size = MustBigNumber(position.size).abs();
const oracle = MustBigNumber(market.oraclePrice);
const signedSize = position.side === IndexerPositionSide.SHORT ? size.negated() : size;

const notional = size.times(oracle);
const value = signedSize.times(oracle);

return {
marginMode,
valueTotal: value,
notionalTotal: notional,
size,
signedSize,
value,
notional,
initialRiskTotal: notional.times(effectiveImf),
adjustedImf: effectiveImf,
adjustedMmf: MaybeBigNumber(market.maintenanceMarginFraction) ?? MustBigNumber(0),
maxLeverage: calc(() => {
if (effectiveImf.isZero()) {
return null;
}
return MustBigNumber(1).div(effectiveImf);
}),
};
}

function getMarketEffectiveInitialMargin(config: IndexerPerpetualMarketResponseObject) {
function getMarketEffectiveInitialMarginForMarket(config: IndexerPerpetualMarketResponseObject) {
const initialMarginFraction = MaybeBigNumber(config.initialMarginFraction);
const openInterest = MaybeBigNumber(config.openInterest);
const openInterestLowerCap = MaybeBigNumber(config.openInterestLowerCap);
Expand All @@ -95,7 +107,7 @@ function getMarketEffectiveInitialMargin(config: IndexerPerpetualMarketResponseO
}

// if these are equal we can throw an error from dividing by zero
if (openInterestUpperCap === openInterestLowerCap) {
if (openInterestUpperCap.eq(openInterestLowerCap)) {
return initialMarginFraction;
}

Expand Down
17 changes: 10 additions & 7 deletions src/abacus-ts/summaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,25 @@ export type SubaccountPositionDerivedArgs = {

export type SubaccountPositionDerivedCore = {
marginMode: 'ISOLATED' | 'CROSS';
notionalTotal: BigNumber; // always positive
valueTotal: BigNumber; // can be negative

signedSize: BigNumber;
size: BigNumber;
notional: BigNumber; // always positive
value: BigNumber; // can be negative

adjustedImf: BigNumber;
adjustedMmf: BigNumber;

initialRiskTotal: BigNumber;
maxLeverage: BigNumber | null;
};

export type SubaccountPositionDerivedExtra = {
maxLeverage: BigNumber;

marginValue: BigNumber;
// all these depend on the subaccount being calculated
leverage: BigNumber;
marginValue: BigNumber;
liquidationPrice: BigNumber;

updatedUnrealizedPnl: BigNumber;
updatedUnrealizedPnlPercent: BigNumber;

liquidationPrice: BigNumber;
};

0 comments on commit 73d52e8

Please sign in to comment.