How it works
OneBalance aggregates your balances by:- Querying your account address across all supported chains
- Identifying tokens you hold on each chain
- Combining balances for the same token across different chains
- Converting values to USD for easy comparison
- Calculating your total portfolio value
Implementation
Fetching balances
Balances are fetched using theuseBalances hook:
lib/hooks/useBalances.ts
API integration
The balances API endpoint returns aggregated data:lib/api/balances.ts
Balance types
The balance response includes detailed type information:lib/types/balances.ts
Displaying balances
Total portfolio value
Show the total value of all holdings:components/BalanceDisplay.tsx
Per-asset balances
Display individual token balances with proper decimal formatting:Balance updates
Balances automatically refresh after transactions:components/SwapForm.tsx
Token input integration
The swap and transfer forms integrate balance display:components/TokenInput.tsx
Balance-based validation
The application validates amounts against available balances:components/SwapForm.tsx
Real-time balance tracking
Balances are tracked dynamically as the user interacts with the application:components/SwapForm.tsx
Aggregated asset IDs
Assets are identified using aggregated IDs that represent the token across all chains:Loading states
Handle loading states gracefully:Error handling
Display clear error messages when balance fetching fails:USD value conversion
Balances include USD values for easy portfolio tracking:Percentage-based operations
Users can quickly select a percentage of their balance:components/SwapForm.tsx
Best practices
1
Fetch on wallet connection
Load balances immediately when the user connects their wallet to provide instant feedback.
2
Refresh after transactions
Always refresh balances after successful swaps or transfers to show updated values.
3
Handle loading states
Show skeleton loaders while balances are being fetched to improve perceived performance.
4
Cache balance data
Store balance data in state to avoid unnecessary API calls when switching between views.
5
Format amounts properly
Always account for token decimals when displaying or parsing amounts.
Balances are aggregated across all chains, but individual chain balances can be queried separately if needed for advanced use cases.