Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bonsai-core): migrate market header #1398

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/abacus-ts/calculators/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,32 @@ 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)
.toFixed(USD_DECIMALS),
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
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),
})
);
9 changes: 6 additions & 3 deletions src/abacus-ts/selectors/assets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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 = (assetId: string) =>
createAppSelector([selectAllAssetsInfo], (assets) => assets?.[assetId]);
jaredvu marked this conversation as resolved.
Show resolved Hide resolved

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);
jaredvu marked this conversation as resolved.
Show resolved Hide resolved
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: string;
percentChange24h: number | null;
stepSizeDecimals: number;
tickSizeDecimals: number;
};
Expand Down
Loading