useWallet
Hook for accessing wallet state, account information, provider details, and connection methods.
Overview
The useWallet hook provides access to the current wallet state and methods to connect, disconnect, and switch networks.
Return Value
Terminal
interface UseWalletReturn {
wallet: {
isConnected: boolean;
account: string;
chainId: string | null;
provider: ethers.BrowserProvider | null;
signer: ethers.Signer | null;
isLoading: boolean;
error: string | null;
};
connectWallet: () => Promise<void>;
disconnectWallet: () => void;
switchNetwork: (networkKey: string) => Promise<boolean>;
isCorrectNetwork: (networkKey?: string) => boolean;
}Usage
UseWalletExample.tsx
import { useWallet } from 'hyperkit';
function Profile() {
const { wallet, connectWallet, disconnectWallet } = useWallet();
if (!wallet.isConnected) {
return (
<button onClick={connectWallet}>
Connect Wallet
</button>
);
}
return (
<div>
<p>Connected: {wallet.account}</p>
<p>Chain ID: {wallet.chainId}</p>
<button onClick={disconnectWallet}>
Disconnect
</button>
</div>
);
}Methods
connectWallet
Connects to the user's wallet. Prompts for connection if not already connected.
Terminal
const { connectWallet } = useWallet();
await connectWallet();disconnectWallet
Disconnects the current wallet and clears wallet state.
Terminal
const { disconnectWallet } = useWallet();
disconnectWallet();switchNetwork
Switches to a different network. Returns true if successful.
Terminal
const { switchNetwork } = useWallet();
const success = await switchNetwork('metis-hyperion-testnet');isCorrectNetwork
Checks if the current network matches the specified network key.
Terminal
const { isCorrectNetwork } = useWallet();
const isCorrect = isCorrectNetwork('metis-hyperion-testnet');