Build Web3 frontend with wagmi and viem
✓Works with OpenClaudeYou are a Web3 frontend engineer. The user wants to build a Web3 frontend using wagmi (React hooks for Ethereum) and viem (TypeScript Ethereum library).
What to check first
- Run
npm list wagmi viemto confirm both libraries are installed in your project - Verify you have a React 18+ project with TypeScript support
- Check that you have a wallet connector library installed (e.g.,
@rainbow-me/rainbowkitor@web3modal/wagmi)
Steps
- Set up the WagmiConfig with viem chains and connectors using
createConfig()from wagmi - Wrap your app with
<WagmiConfig>provider at the root level - Use the
useAccount()hook to check wallet connection status and getaddress,isConnected,chainId - Implement
useConnect()hook to access available connectors and trigger connection viaconnect() - Use
useBalance()hook to fetch ETH or token balances, passing the account address and optional token contract - Implement
useReadContract()(or legacyuseContractRead()) to call contract read functions with ABI and function arguments - Use
useWriteContract()anduseWaitForTransactionReceipt()for state mutations and transaction confirmation - Handle chain switching with
useSwitchChain()hook to change networks at runtime
Code
import { useAccount, useConnect, useBalance, useWriteContract, useWaitForTransactionReceipt, useSwitchChain } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'
import { createConfig, http } from 'wagmi'
import { injected } from 'wagmi/connectors'
import { parseEther } from 'viem'
// 1. Configure wagmi with viem transport
export const config = createConfig({
chains: [mainnet, sepolia],
connectors: [injected()],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})
// 2. Component that uses wagmi hooks
export function WalletConnection() {
const { address, isConnected, chainId } = useAccount()
const { connectors, connect } = useConnect()
const { switchChain } = useSwitchChain()
const { data: balance } = useBalance({ address })
return (
<div>
{isConnected ? (
<>
<p>Connected: {address}</p>
<p>Balance: {balance?.formatted} {balance?.symbol}</p>
<button onClick={() => switchChain({ chainId: sepolia.id })}>
Switch to Sepolia
</button>
</>
) : (
<button onClick={() => connect({ connector: connectors[0] })}>
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
Hardhat Setup
Set up Hardhat development environment for Solidity
Wallet Connect
Integrate wallet connection (MetaMask, WalletConnect)
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.