Wagmi v2 - How to Generate Instant, Type-Safe Hooks for Your Smart Contracts

In the fast-moving world of Web3, developer experience (DX) used to be an afterthought. We spent hours copy-pasting JSON ABIs, manually typing contract addresses, and wrestling with hex-encoded data.
Then came Wagmi.
Wagmi v2, paired with the Wagmi CLI, has fundamentally changed the game. Instead of manual setup, you can now generate type-safe React hooks that "know" your smart contract's functions before you even finish typing them.
Step 1: Installation
First, we need the core Wagmi library, its peer dependency Viem (the lightweight alternative to ethers.js), and TanStack Query for state management.
Next, install the Wagmi CLI as a development dependency. This is the magic tool that will handle your ABIs.
Step 2: Configure Wagmi
Before we get to the CLI, we need a standard configuration for our app. Create a file named wagmi.ts (or config.ts).
Step 3: Initialize the Wagmi CLI
The CLI needs its own configuration file to know where to fetch your ABIs and where to output the generated code. Run the following command:
This creates a wagmi.config.ts file. This is where you tell Wagmi about your smart contracts.
Automating ABI Generation
You have two main ways to get your ABIs:
- From a local project (Hardhat/Foundry)
- From a block explorer (Etherscan)
Here is a powerhouse configuration that fetches an ABI directly from Etherscan and generates React hooks for it:
Step 4: Run the Generator
Once your config is ready, run the generation command:
Wagmi will now:
- Reach out to Etherscan
- Grab the ABI for the
MyContract - Create a file at
lib/generated.tscontaining custom hooks likeuseReadEnsRegistryOwner
Step 5: Put it All Together
Create a config/providers.tsx file, and wrap your application with the WagmiProvider and the QueryClientProvider.
Using Your Generated Hook
Now, instead of writing useReadContract and passing in a messy ABI object, you can do this:
Why This Matters
By using the CLI to generate your ABIs and hooks:
- Type Safety: If a function name changes or you pass the wrong number of arguments, TypeScript will throw an error immediately.
- Speed: No more manual JSON imports.
- Maintainability: If you update your contract, just run
npx wagmi generateagain, and your entire frontend updates its types.
References
-
Wagmi Documentation
- https://wagmi.sh
- Official Wagmi library documentation and guides
-
Wagmi CLI Plugin Documentation
- https://wagmi.sh/cli
- Complete reference for CLI configuration and plugins
-
Viem Documentation
- https://viem.sh
- Lightweight Ethereum library as alternative to ethers.js
-
TanStack Query (React Query)
- https://tanstack.com/query/latest
- State management library for fetching and caching data
-
Etherscan API
- https://docs.etherscan.io
- Reference for retrieving smart contract ABIs from Etherscan