useSolanaWallet
Hook to retrieve Solana accounts, RPC client, and wallet from Web3Auth.
Import
import { useSolanaWallet } from '@web3auth/modal/react/solana'
Usage
import { useSolanaWallet } from '@web3auth/modal/react/solana'
function SolanaWalletInfo() {
const { accounts, rpc, solanaWallet, getPrivateKey } = useSolanaWallet()
return (
<div>
<div>Accounts: {accounts ? accounts.join(', ') : 'No accounts'}</div>
<div>Solana wallet: {solanaWallet ? 'Available' : 'Not available'}</div>
<div>RPC: {rpc ? 'Available' : 'Not available'}</div>
</div>
)
}
Return type
import { type IUseSolanaWallet } from '@web3auth/modal/react/solana'
accounts
string[] | null
Base58 Solana account addresses, or null if not available.
solanaWallet
Wallet | null
The wallet-standard Solana wallet instance, or null if not available.
rpc
Rpc<SolanaRpcApi> | null
The @solana/kit RPC client for making Solana RPC calls, or null if not available.
getPrivateKey
() => Promise<string>
Returns the user's private key when available (in-app adapters only).
Example: fetching SOL balance
getBalance.tsx
import { address } from '@solana/kit'
import { useSolanaWallet } from '@web3auth/modal/react/solana'
import { useEffect, useState } from 'react'
export function Balance() {
const { accounts, rpc } = useSolanaWallet()
const [balance, setBalance] = useState<number | null>(null)
const fetchBalance = async () => {
if (!rpc || !accounts?.length) return
const { value } = await rpc.getBalance(address(accounts[0])).send()
setBalance(Number(value) / 1e9)
}
useEffect(() => {
fetchBalance()
}, [rpc, accounts])
return (
<div>
{balance !== null && <p>{balance} SOL</p>}
<button onClick={fetchBalance} type="button">
Fetch balance
</button>
</div>
)
}