Overview
The Portfolio API provides functions for managing cryptocurrency portfolios in the browser’s localStorage. It handles portfolio creation, asset management, and position tracking with automatic persistence. File location:src/api/portfolio/portfolioApi.js
Configuration
- Storage: Browser localStorage
- Storage key:
crypto_dash_portfolios - Default allocation: $2,000 USD per asset
- Data format: JSON array of portfolio objects
Functions
getOrCreatePrimaryPortfolio
Retrieves the primary portfolio or creates a new one with default assets if none exists.Signature
Parameters
Array of market coin objects from
getTopMarketCoins(). Used to initialize default positions with current prices.Each coin should have:id(string) - Coin identifiercurrent_price(number) - Current price in USD
Returns
Portfolio object
Portfolio identifier (e.g.,
main)Portfolio display name (e.g.,
Portafolio Principal)ISO 8601 timestamp of portfolio creation
Default Assets
When creating a new portfolio, the function attempts to add these preferred assets (if available inmarketCoins):
bitcoinethereumsolanabinancecoinripple
Example Return Value
addAssetToPrimaryPortfolio
Adds a new asset position to the primary portfolio.Signature
Parameters
Coin identifier to add (e.g.,
cardano, polkadot)Current price of the asset in USD. Used to calculate the amount of coins purchased.
Amount to invest in USD. Defaults to
DEFAULT_ALLOCATION_USD (2000).Returns
Updated portfolio object or
null if:- No primary portfolio exists
- Asset already exists in the portfolio
getOrCreatePrimaryPortfolio() return value.Behavior
- Duplicate check: Returns existing portfolio unchanged if asset already exists
- Persistence: Automatically saves to localStorage
- Amount calculation:
amount = allocationUsd / currentPrice - Safe defaults: Uses fallback values if price or allocation are invalid
getDefaultAllocationUsd
Returns the default USD allocation amount used when adding assets.Signature
Returns
Default allocation amount in USD (2000)
Data Storage
Storage Format
Portfolios are stored in localStorage as a JSON array:Storage Key
Error Handling
The API handles storage errors gracefully:- Invalid JSON: Returns empty array
[] - Missing data: Returns empty array
[] - Non-array data: Returns empty array
[]
Implementation Details
Position Calculation
When creating or adding a position, the amount of coins is calculated as:- Allocation: $2,000 USD
- Bitcoin price: $45,000
- Amount: 2000 / 45000 = 0.04444 BTC
Timestamp Format
All timestamps use ISO 8601 format:Primary Portfolio
The application currently supports a single primary portfolio:- Always stored at index 0 in the portfolios array
- ID:
main - Name:
Portafolio Principal
