Skip to main content

Overview

The useWalletProtocols hook provides a React-friendly interface to detect which DeFi protocols a connected wallet has active positions in. It automatically fetches and updates when the wallet address changes. Location: src/hooks/useWalletProtocols.js

Import

Signature

Parameters

string | null
required
Stacks wallet address to analyze. Pass null or empty string when wallet is disconnected.Example: SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7

Return Values

Array<WalletProtocol>
required
Array of detected protocol positions
boolean
required
true while fetching wallet data, false once complete
string | null
required
Error message if detection failed, otherwise null

Usage Examples

Features

Auto-Update

Automatically re-scans when wallet address changes

Empty State

Returns empty array when no address provided (graceful handling)

Error Recovery

Captures API errors without crashing the component

Two-Factor Detection

Checks both token balances AND transaction history for accuracy

Behavior

Address Changes

The hook automatically re-runs detection when the address parameter changes:

No Address Provided

When address is null, undefined, or empty string:
  • Returns [] for walletProtocols
  • Sets loading to false
  • No API calls made

Loading States

  1. Initial: loading = false, walletProtocols = []
  2. Fetching: loading = true
  3. Success: loading = false, walletProtocols = [...]
  4. Error: loading = false, error = "message"

Detection Methods

Token Balance Check

Scans wallet for protocol-specific LP or receipt tokens:
Result: confidence = "confirmed"

Transaction History Check

Analyzes last 50 transactions for protocol contract interactions:
Result: confidence = "likely"

Implementation Details

Source Code

Supported Protocols

The hook can detect positions in these Stacks DeFi protocols:
Type: Stacking
Token: stSTX (liquid staking receipt)
Contract: SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.ststx-token
Type: Lending
Token: zsBTC (lending position)
Contract: SP2VCQJGH7PHP2DJK7Z0V48AGBHQAW3R3ZW1QF4N.zest-reward-dist
Type: DEX / Yield
Token: atALEX (auto-compounding)
Contract: SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v3
Type: DEX LP
Token: stxSTX-LP (liquidity pool)
Contract: STTWD9SPRQVD3P733V89SV0P8EP8QSB5B00ZBZQ.stxstx-lp-token-v-1-2
Type: Yield
Token: USDh (yield-bearing stablecoin)
Contract: SP2XD7417HGPRTREMKF748VNEQPDRR0RMANB7X1N.token-usdh
Type: DEX LP
Token: WELSH-LP (liquidity pool)
Contract: SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.wstx-welsh-lp-token
Type: Borrowing
Token: sBTC Collateral
Contract: SP2XD7417HGPRTREMKF748VNEQPDRR0RMANB7X1N.granite-vault

Performance

Initial Detection: ~1-2s for parallel API calls (balances + transactions)
Subsequent Updates: Triggered only when address changes
No Auto-Refresh: Hook does not poll for updates (call with new address to refresh)

Comparison with Service

Common Patterns

Wallet Connection Integration

Portfolio Value Calculation

Portfolio Protocols Service

Underlying service that detects wallet positions

useProtocolData Hook

Fetch protocol TVL and APY data