Skip to content

Commit

Permalink
fix(bonsai-core): unopened isolated position struct change (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu authored Jan 15, 2025
1 parent 95fc2ff commit 800dd22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"error",
{
"patterns": [
"@/abacus-ts/*",
"@/abacus-ts/*/*",
"!@/abacus-ts/ontology",
"!@/abacus-ts/lib",
"!@/abacus-ts/summaryTypes"
"!@/abacus-ts/lib/*",
"!@/abacus-ts/types/summaryTypes"
]
}
],
Expand Down
75 changes: 47 additions & 28 deletions src/abacus-ts/calculators/subaccount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigNumber from 'bignumber.js';
import { mapValues, orderBy } from 'lodash';
import { groupBy, map, mapValues, orderBy, pickBy } from 'lodash';
import { weakMapMemoize } from 'reselect';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';
import {
Expand All @@ -9,7 +10,11 @@ import {
} from '@/types/indexer/indexerApiGen';
import { IndexerWsBaseMarketObject } from '@/types/indexer/indexerManual';

import { getAssetFromMarketId } from '@/lib/assetUtils';
import {
getAssetFromMarketId,
getDisplayableAssetFromBaseAsset,
getDisplayableTickerFromMarket,
} from '@/lib/assetUtils';
import { calc } from '@/lib/do';
import { isTruthy } from '@/lib/isTruthy';
import { BIG_NUMBERS, MaybeBigNumber, MustBigNumber, ToBigNumber } from '@/lib/numbers';
Expand All @@ -18,6 +23,7 @@ import { isPresent } from '@/lib/typeUtils';
import { ChildSubaccountData, MarketsData, ParentSubaccountData } from '../types/rawTypes';
import {
GroupedSubaccountSummary,
PendingIsolatedPosition,
SubaccountOrder,
SubaccountPosition,
SubaccountPositionBase,
Expand Down Expand Up @@ -76,16 +82,15 @@ export function calculateMarketsNeededForSubaccount(parent: Omit<ParentSubaccoun
);
}

function calculateSubaccountSummary(
subaccountData: ChildSubaccountData,
markets: MarketsData
): SubaccountSummary {
const core = calculateSubaccountSummaryCore(subaccountData, markets);
return {
...core,
...calculateSubaccountSummaryDerived(core),
};
}
const calculateSubaccountSummary = weakMapMemoize(
(subaccountData: ChildSubaccountData, markets: MarketsData): SubaccountSummary => {
const core = calculateSubaccountSummaryCore(subaccountData, markets);
return {
...core,
...calculateSubaccountSummaryDerived(core),
};
}
);

function calculateSubaccountSummaryCore(
subaccountData: ChildSubaccountData,
Expand Down Expand Up @@ -295,37 +300,51 @@ export function calculateChildSubaccountSummaries(
parent: Omit<ParentSubaccountData, 'live'>,
markets: MarketsData
): Record<string, SubaccountSummary> {
return Object.fromEntries(
Object.values(parent.childSubaccounts)
.map((subaccount) => {
if (!subaccount) return undefined;
const summary = calculateSubaccountSummary(subaccount, markets);
return [subaccount.subaccountNumber, summary];
})
.filter(isTruthy)
return pickBy(
mapValues(
parent.childSubaccounts,
(subaccount) => subaccount && calculateSubaccountSummary(subaccount, markets)
),
isTruthy
);
}

/**
*
* UnopenedIsolatedPosition is a modified SubaccountOrder that meets the following criterias:
* @returns a list of pending isolated positions
* PendingIsolatedPosition is exists if there are any orders that meet the following criteria:
* - marginMode is ISOLATED
* - no existing position exists
* - equity of the childSubaccount is returned with the order
* - childSubaccount has equity
*/
export function calculateUnopenedIsolatedPositions(
childSubaccounts: Record<string, SubaccountSummary>,
orders: SubaccountOrder[],
positions: SubaccountPosition[]
): Array<SubaccountOrder & Pick<SubaccountSummary, 'equity'>> {
): PendingIsolatedPosition[] {
const setOfOpenPositionMarkets = new Set(positions.map(({ market }) => market));

const filteredOrders = orders.filter(
(o) => !setOfOpenPositionMarkets.has(o.marketId) && o.marginMode === 'ISOLATED'
);

return filteredOrders.map((o) => ({
...o,
equity: childSubaccounts[o.subaccountNumber]?.equity ?? BIG_NUMBERS.ZERO,
}));
const filteredOrdersMap = groupBy(filteredOrders, 'marketId');
const marketIdToSubaccountNumber = mapValues(
filteredOrdersMap,
(filteredOrder) => filteredOrder[0]?.subaccountNumber
);

return map(filteredOrdersMap, (orderList, marketId) => {
const subaccountNumber = marketIdToSubaccountNumber[marketId];
if (subaccountNumber == null) return undefined;
const assetId = getAssetFromMarketId(marketId);

return {
assetId,
displayableAsset: getDisplayableAssetFromBaseAsset(assetId),
marketId,
displayId: getDisplayableTickerFromMarket(marketId),
equity: childSubaccounts[subaccountNumber]?.equity ?? BIG_NUMBERS.ZERO,
orders: orderList,
};
}).filter(isTruthy);
}
9 changes: 9 additions & 0 deletions src/abacus-ts/types/summaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ export type SubaccountOrder = {
marginMode: MarginMode | undefined;
};

export type PendingIsolatedPosition = {
marketId: string;
displayId: string;
assetId: string;
displayableAsset: string;
equity: BigNumber;
orders: SubaccountOrder[];
};

export type LiveTrade = BaseTrade;

0 comments on commit 800dd22

Please sign in to comment.