UNPKG

@cranberry-money/shared-utils

Version:

Shared utility functions for Blueberry platform

72 lines 2.62 kB
import { getHoldingAssetTypeLabel, HOLDING_ASSET_TYPE, getChartColor } from '@cranberry-money/shared-constants'; /** * Calculate holdings summary with asset type breakdown. * Groups holdings by asset type and calculates totals. */ export function calculateHoldingsSummary(holdings, walletsCount) { const assetTypeMap = new Map(); let totalValue = 0; for (const holding of holdings) { const value = parseFloat(holding.marketValue) || 0; const assetType = holding.asset?.assetType || HOLDING_ASSET_TYPE.ERC20_TOKEN; totalValue += value; const existing = assetTypeMap.get(assetType) || { totalValue: 0, holdingsCount: 0 }; assetTypeMap.set(assetType, { totalValue: existing.totalValue + value, holdingsCount: existing.holdingsCount + 1, }); } const byAssetType = Array.from(assetTypeMap.entries()) .map(([assetType, data]) => ({ assetType, label: getHoldingAssetTypeLabel(assetType), totalValue: data.totalValue, holdingsCount: data.holdingsCount, })) .sort((a, b) => b.totalValue - a.totalValue); return { totalValue, holdingsCount: holdings.length, walletsCount, byAssetType, }; } /** * Calculate asset allocation from holdings. * Groups holdings by asset UUID (aggregating across wallets) and calculates percentages. */ export function calculateAssetAllocation(holdings, totalValue) { if (totalValue === 0) return []; const assetMap = new Map(); for (const holding of holdings) { const assetUuid = holding.asset?.uuid || holding.assetSymbol; const value = parseFloat(holding.marketValue) || 0; const existing = assetMap.get(assetUuid); if (existing) { existing.totalValue += value; } else { assetMap.set(assetUuid, { symbol: holding.assetSymbol || holding.asset?.symbol || 'Unknown', name: holding.assetName || holding.asset?.name || 'Unknown Asset', totalValue: value, }); } } return Array.from(assetMap.entries()) .map(([assetUuid, data], index) => ({ assetUuid, symbol: data.symbol, name: data.name, totalValue: data.totalValue, percentage: (data.totalValue / totalValue) * 100, color: getChartColor(index), })) .sort((a, b) => b.totalValue - a.totalValue) .map((item, index) => ({ ...item, color: getChartColor(index), })); } //# sourceMappingURL=holdings.js.map