Skip to main content

Overview

The usePortfolio 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

string | null
required
The Stacks wallet address to fetch portfolio data for. If null or undefined, the hook will not fetch data.Example: "SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5"

Return Values

object
Complete portfolio data object containing all wallet information:
string
STX token balance formatted as a string (e.g., "100.5000"). Shows "--" while loading or if fetch fails.
string
sBTC token balance formatted as a string with full precision (e.g., "0.00500000"). Shows "--" while loading.
string
Total portfolio value in USD (e.g., "285.50"). Calculated from STX and sBTC balances at current market prices. Shows "--" while loading.
array
Array of recent transaction objects. Each transaction includes:
  • txid: Transaction hash
  • tx_status: "success", "pending", or "failed"
  • tx_type: Type of transaction (e.g., "token_transfer", "contract_call")
  • sender_address: Address that initiated the transaction
  • block_height: Block number (if confirmed)
  • burn_block_time_iso: Timestamp in ISO format
Empty array [] while loading or if no transactions found.
number
Current STX price in USD (e.g., 2.85). Used to calculate totalUSD. Defaults to 0 while loading.
boolean
true during initial fetch or refresh, false once data is loaded. Use this to show loading skeletons.
string | null
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:
1

Initial State

Before address is provided:
  • loading is false
  • All portfolio values show "--"
  • txHistory is empty []
2

Fetching State

While fetching data:
  • loading is true
  • Previous portfolio values remain visible (or "--" on first load)
  • UI should show loading indicators
3

Success State

After successful fetch:
  • loading is false
  • All portfolio values are populated
  • error is null
4

Error State

If fetch fails:
  • loading is false
  • error contains error message
  • Portfolio values show "--"

Error Handling

The hook catches and formats errors from the Stacks API:
Common errors:
  • Network request timeout
  • Invalid address format
  • Stacks API rate limiting
  • Blockchain node unavailable

Data Format

Portfolio data comes from the getFullPortfolio 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

The hook refreshes every 30 seconds. If you have multiple components using usePortfolio with the same address, consider lifting the hook to a parent component and passing data as props to avoid duplicate API calls.
  • Stacks API - Underlying portfolio data service
  • AI Service - Uses portfolio data for strategy generation