Integrate wallet connection (MetaMask, WalletConnect)
✓Works with OpenClaudeYou are a Web3 frontend developer. The user wants to integrate wallet connection functionality supporting MetaMask and WalletConnect protocols into a web application.
What to check first
- Verify Node.js version is 16+ with
node --version - Check if
ethers.jsorweb3.jsis already installed:npm list ethersornpm list web3 - Confirm you have a test network RPC endpoint (Infura, Alchemy, or local node)
Steps
- Install required dependencies:
npm install ethers @web3modal/ethereum @web3modal/react(or use wagmi/web3-react depending on framework preference) - Get a WalletConnect Project ID from cloud.walletconnect.com — this is mandatory for WalletConnect v2
- Initialize Web3Modal with your Project ID and supported chains in your root component
- Create a hook that exposes
connect(),disconnect(), andaccountstate using the modal instance - Add a connect button that calls the hook's connect function on click
- Listen to
accountsChangedandchainChangedevents fromwindow.ethereumfor MetaMask - Store the connected wallet address and chain ID in React state or context
- Implement error handling for rejected connections, unsupported networks, and user cancellations
Code
import { useEffect, useState } from 'react';
import { ethers } from 'ethers';
interface WalletState {
address: string | null;
chainId: number | null;
isConnected: boolean;
provider: ethers.BrowserProvider | null;
}
export function useWalletConnect() {
const [wallet, setWallet] = useState<WalletState>({
address: null,
chainId: null,
isConnected: false,
provider: null,
});
const [error, setError] = useState<string | null>(null);
// Connect MetaMask or WalletConnect
const connect = async (walletType: 'metamask' | 'walletconnect') => {
try {
setError(null);
let provider: ethers.BrowserProvider;
if (walletType === 'metamask') {
if (!window.ethereum) {
throw new Error('MetaMask not installed');
}
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts',
});
provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const address = await signer.getAddress();
const network = await provider.getNetwork();
setWallet({
address,
chainId: Number(network.chainId),
isConnected: true,
provider,
});
} else if (
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Web3 & Blockchain Skills
Other Claude Code skills in the same category — free to download.
Smart Contract
Scaffold Solidity smart contract with tests
Web3 Frontend
Build Web3 frontend with wagmi and viem
Hardhat Setup
Set up Hardhat development environment for Solidity
NFT Contract
Create ERC-721/1155 NFT smart contract
DeFi Integration
Integrate DeFi protocols (Uniswap, Aave)
Smart Contract Security Audit
Audit a Solidity smart contract for the most common vulnerabilities
Solidity Gas Optimization
Reduce gas costs in Solidity contracts using storage packing, bitmaps, and efficient patterns
Want a Web3 & Blockchain skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.