Skip to content

Commit

Permalink
feat(bonsai-core): migrate market header (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu authored and tinaszheng committed Jan 7, 2025
1 parent b23bf35 commit f6c6b53
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
24 changes: 20 additions & 4 deletions src/abacus-ts/calculators/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,30 @@ export function getMarketEffectiveInitialMarginForMarket(
return effectiveIMF;
}

const calculateMarket = weakMapMemoize(
(market: IndexerPerpetualMarketResponseObject): MarketInfo => ({
...market,
function calculateDerivedMarketDisplayItems(market: IndexerPerpetualMarketResponseObject) {
return {
assetId: getAssetFromMarketId(market.ticker),
displayableAsset: getDisplayableAssetFromTicker(market.ticker),
effectiveInitialMarginFraction: getMarketEffectiveInitialMarginForMarket(market),
displayableTicker: getDisplayableTickerFromMarket(market.ticker),
};
}

function calculateDerivedMarketCore(market: IndexerPerpetualMarketResponseObject) {
return {
effectiveInitialMarginFraction: getMarketEffectiveInitialMarginForMarket(market),
openInterestUSDC: MustBigNumber(market.openInterest).times(market.oraclePrice).toNumber(),
percentChange24h: MustBigNumber(market.oraclePrice).isZero()
? null
: MustBigNumber(market.priceChange24H).div(market.oraclePrice).toNumber(),
stepSizeDecimals: MaybeBigNumber(market.stepSize)?.decimalPlaces() ?? TOKEN_DECIMALS,
tickSizeDecimals: MaybeBigNumber(market.tickSize)?.decimalPlaces() ?? USD_DECIMALS,
};
}

const calculateMarket = weakMapMemoize(
(market: IndexerPerpetualMarketResponseObject): MarketInfo => ({
...market,
...calculateDerivedMarketDisplayItems(market),
...calculateDerivedMarketCore(market),
})
);
12 changes: 9 additions & 3 deletions src/abacus-ts/selectors/assets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { createSelector } from 'reselect';
import { createAppSelector } from '@/state/appTypes';

import { transformAssetsInfo } from '../calculators/assets';
import { selectRawAssetsData } from './base';
import { selectCurrentMarketInfo } from './markets';

export const selectAllAssetsInfo = createSelector([selectRawAssetsData], (assets) =>
export const selectAllAssetsInfo = createAppSelector([selectRawAssetsData], (assets) =>
transformAssetsInfo(assets)
);

export const selectCurrentMarketAssetInfo = createSelector(
export const selectAssetInfo = () =>
createAppSelector(
[selectAllAssetsInfo, (_s, assetId: string) => assetId],
(assets, assetId) => assets?.[assetId]
);

export const selectCurrentMarketAssetInfo = createAppSelector(
[selectCurrentMarketInfo, selectAllAssetsInfo],
(currentMarketInfo, assets) => {
if (currentMarketInfo == null || assets == null) {
Expand Down
5 changes: 4 additions & 1 deletion src/abacus-ts/selectors/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { createAppSelector } from '@/state/appTypes';
import { getCurrentMarketId } from '@/state/perpetualsSelectors';

import { calculateAllMarkets } from '../calculators/markets';
import { selectRawMarketsData } from './base';
import { mergeLoadableStatus } from '../lib/mapLoadable';
import { selectRawMarkets, selectRawMarketsData } from './base';

export const selectAllMarketsInfo = createAppSelector([selectRawMarketsData], (markets) =>
calculateAllMarkets(markets)
Expand All @@ -12,3 +13,5 @@ export const selectCurrentMarketInfo = createAppSelector(
[selectAllMarketsInfo, getCurrentMarketId],
(markets, currentMarketId) => (currentMarketId ? markets?.[currentMarketId] : undefined)
);

export const selectMarketsInfoLoading = createAppSelector([selectRawMarkets], mergeLoadableStatus);
2 changes: 2 additions & 0 deletions src/abacus-ts/summaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export type MarketInfo = IndexerPerpetualMarketResponseObject & {
displayableAsset: string;
displayableTicker: string;
effectiveInitialMarginFraction: BigNumber | null;
openInterestUSDC: number;
percentChange24h: number | null;
stepSizeDecimals: number;
tickSizeDecimals: number;
};
Expand Down

0 comments on commit f6c6b53

Please sign in to comment.