Skip to content

Commit

Permalink
fix: raw asset structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu committed Dec 20, 2024
1 parent 112ce26 commit 70d074e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/abacus-ts/calculators/assets.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { mapValues } from 'lodash';
import { weakMapMemoize } from 'reselect';

import { MetadataServiceAssetInfo, MetadataServiceInfoResponse } from '@/constants/assetMetadata';
import { getTickSizeDecimalsFromPrice } from '@/lib/numbers';

export const parseAssetInfo = weakMapMemoize(
(assetInfo: MetadataServiceAssetInfo, assetId: string) => ({
assetId,
name: assetInfo.name,
logo: assetInfo.logo,
urls: {
website: assetInfo.urls.website,
technicalDoc: assetInfo.urls.technical_doc,
cmc: assetInfo.urls.cmc,
},
})
);
import { AssetInfo, AssetInfos } from '../rawTypes';

export const transformAssetsInfo = (assetsInfo: MetadataServiceInfoResponse | undefined) => {
export const parseAssetInfo = weakMapMemoize((assetInfo: AssetInfo, assetId: string) => ({
assetId,
name: assetInfo.name,
logo: assetInfo.logo,
price: assetInfo.price,
marketCap: assetInfo.market_cap,
volume24h: assetInfo.volume_24h,
percentChange24h: assetInfo.percent_change_24h,
reportedMarketCap: assetInfo.self_reported_market_cap,
tickSizeDecimals: getTickSizeDecimalsFromPrice(assetInfo.price),
urls: {
website: assetInfo.urls.website,
technicalDoc: assetInfo.urls.technical_doc,
cmc: assetInfo.urls.cmc,
},
}));

export const transformAssetsInfo = (assetsInfo: AssetInfos | undefined) => {
if (assetsInfo == null) {
return assetsInfo;
}
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/rawTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export interface ChildSubaccountData {
assetPositions: { [symbol: string]: IndexerAssetPositionResponseObject };
}

export type AssetInfo = MetadataServiceAssetInfo & { id: string; priceData?: MetadataServicePrice };
export type AssetInfo = MetadataServiceAssetInfo & MetadataServicePrice & { id: string };
export type AssetInfos = Record<string, AssetInfo>;
15 changes: 14 additions & 1 deletion src/abacus-ts/rest/assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { QueryObserver } from '@tanstack/react-query';
import { mapValues } from 'lodash';

import { MetadataServicePrice } from '@/constants/assetMetadata';
import { timeUnits } from '@/constants/time';

import { type RootStore } from '@/state/_store';
Expand Down Expand Up @@ -28,7 +29,19 @@ export function setUpAssetsQuery(store: RootStore) {
setAllAssetsRaw(
mapLoadableData(queryResultToLoadable(result), (map) => {
const [info, prices] = map;
return mapValues(info, (v, id) => ({ ...v, id, priceData: prices[id] }));
return mapValues(info, (assetInfo, id) => {
const priceData =
prices[id] ??
({
price: null,
percent_change_24h: null,
volume_24h: null,
market_cap: null,
self_reported_market_cap: null,
} satisfies MetadataServicePrice);

return { ...assetInfo, ...priceData, id };
});
})
)
);
Expand Down

0 comments on commit 70d074e

Please sign in to comment.