Overview
TheusePortfolio hook fetches comprehensive portfolio data for a Stacks wallet address. It automatically refreshes every 30 seconds to keep balances and transaction history up-to-date.
Source: src/hooks/usePortfolio.js
Import
Hook Signature
Parameters
The Stacks wallet address to fetch portfolio data for. If
null or undefined, the hook will not fetch data.Example: "SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5"Return Values
Complete portfolio data object containing all wallet information:
STX token balance formatted as a string (e.g.,
"100.5000"). Shows "--" while loading or if fetch fails.sBTC token balance formatted as a string with full precision (e.g.,
"0.00500000"). Shows "--" while loading.Total portfolio value in USD (e.g.,
"285.50"). Calculated from STX and sBTC balances at current market prices. Shows "--" while loading.Array of recent transaction objects. Each transaction includes:
txid: Transaction hashtx_status:"success","pending", or"failed"tx_type: Type of transaction (e.g.,"token_transfer","contract_call")sender_address: Address that initiated the transactionblock_height: Block number (if confirmed)burn_block_time_iso: Timestamp in ISO format
[] while loading or if no transactions found.Current STX price in USD (e.g.,
2.85). Used to calculate totalUSD. Defaults to 0 while loading.true during initial fetch or refresh, false once data is loaded. Use this to show loading skeletons.Error message if portfolio fetch fails (e.g.,
"Failed to load portfolio data"). null if no error.Usage Example
Auto-Refresh Behavior
The hook automatically refreshes portfolio data every 30 seconds:The auto-refresh ensures users always see up-to-date balances without manual page refreshes. This is especially useful for monitoring pending transactions or deposit confirmations.
Loading States
The hook provides clear loading states throughout the data lifecycle:Initial State
Before
address is provided:loadingisfalse- All portfolio values show
"--" txHistoryis empty[]
Fetching State
While fetching data:
loadingistrue- Previous portfolio values remain visible (or
"--"on first load) - UI should show loading indicators
Success State
After successful fetch:
loadingisfalse- All portfolio values are populated
errorisnull
Error Handling
The hook catches and formats errors from the Stacks API:- Network request timeout
- Invalid address format
- Stacks API rate limiting
- Blockchain node unavailable
Data Format
Portfolio data comes from thegetFullPortfolio service:
Best Practices
Check Address
Always ensure
address is valid before rendering portfolio data. Show a connect prompt if null.Loading Skeletons
Use
loading state to show skeleton loaders instead of empty states during fetch.Error Display
Always display the
error message to users when portfolio fetch fails. Include a retry option.Auto-Refresh Awareness
Remember data refreshes every 30 seconds. Consider showing a “last updated” timestamp.
Common Patterns
Conditional Rendering Based on Connection
Passing Portfolio to Other Hooks
Manual Refresh Trigger
Performance Considerations
Related Hooks
- useWallet - Get connected wallet address
- useAIAdvisor - Pass portfolio data to AI
- useProtocols - Browse DeFi protocols
Related Services
- Stacks API - Underlying portfolio data service
- AI Service - Uses portfolio data for strategy generation