useWalletContext
Retrieves the currently connected wallet context from the Drift Common store.Signature
Returns
The current wallet context state, orundefined if no wallet is connected.
Example
Source
source/react/src/hooks/useWalletContext.tsx:5
useWallet
Retrieves the Drift wallet context directly from React context.Signature
Returns
The Drift wallet context from theDriftWalletContext provider.
Example
Source
source/react/src/hooks/useWalletContext.tsx:13
useSyncWalletToStore
Keeps the wallet authority and connection state synchronized with the application store. This hook listens to wallet connect/disconnect events and updates the store accordingly.Signature
Parameters
Optional callback function to clear application-specific data from the store when the wallet disconnects or changes.
Behavior
- On Connect: Sets the wallet authority in the store, resets SOL balance to 0 (unloaded state), and calls
handleWalletConnectaction - On Disconnect: Clears the authority, resets SOL balance, calls
handleWalletDisconnectaction, and invokes the optionalclearDataFromStorecallback - On Adapter Change: Removes previous event listeners and sets up new ones
Example
Implementation Details
- Resets
currentSolBalancetoBigNum(0, BASE_PRECISION_EXP)withloaded: falseon both connect and disconnect - Updates
authorityandauthorityStringin the store - Cleans up event listeners when the wallet adapter changes
Source
source/react/src/hooks/useSyncWalletToStore.tsx:12
useSolBalance
Tracks the connected wallet’s SOL balance and keeps it updated in the application store. UsesonAccountChange subscription for real-time updates and polling as a fallback.
Signature
Parameters
When
true, disables balance tracking. Useful for conditionally enabling the hook.Behavior
- Initial Load: Fetches the current balance when wallet connects
- Real-time Updates: Subscribes to account changes using
connection.onAccountChange - Polling Fallback: Polls for balance at
basePollingRateMsinterval until initial balance is loaded - On Disconnect: Sets balance to zero and removes the account change listener
Example
Important Notes
Store Updates
UpdatescurrentSolBalance in the store with:
value:BigNumrepresentation of the balance in lamportsloaded: Boolean indicating whether the initial balance has been fetched
Source
source/react/src/hooks/useSolBalance.tsx:10