wagmi v1 → v2 Codemod
Automates the wagmi v1 to v2 migration.
Built with the Codemod JSSG engine.
Published: waleedbhattiii-wagmi-v1-to-v2
Quick Start
bash
Or run manually on a specific directory:
bash
What gets migrated automatically (~85% coverage)
| Category | v1 | v2 |
|---|---|---|
| Provider | <WagmiConfig> | <WagmiProvider> |
| Hook | useContractRead | useReadContract |
| Hook | useContractWrite | useWriteContract |
| Hook | useContractEvent | useWatchContractEvent |
| Hook | useContractReads | useReadContracts |
| Hook | useContractInfiniteReads | useInfiniteReadContracts |
| Hook | useWaitForTransaction | useWaitForTransactionReceipt |
| Hook | useSwitchNetwork | useSwitchChain |
| Hook | useSigner | useWalletClient |
| Hook | useProvider | usePublicClient |
| Hook | useWebSocketProvider | usePublicClient |
| Hook | useFeeData | useEstimateFeesPerGas |
| Prepare hooks | usePrepareContractWrite | useSimulateContract |
| Prepare hooks | usePrepareSendTransaction | useEstimateGas |
| Query params | useReadContract({ enabled }) | useReadContract({ query: { enabled } }) |
| Watch prop | useBalance({ watch: true }) | Removed + TODO added |
| Connectors | new MetaMaskConnector({ chains }) | metaMask() |
| Connectors | new WalletConnectConnector(...) | walletConnect(...) |
| Connectors | new InjectedConnector(...) | injected(...) |
| Connectors | new CoinbaseWalletConnector(...) | coinbaseWallet(...) |
| Config | createClient | createConfig |
| Network | useNetwork() | useChainId() + useChains() |
| Imports | Stale v1 specifiers | Cleaned up |
Key safety guarantees:
- Import source verified before any rename — user-defined hooks with same names are never touched
- Exact text match prevents substring collisions (e.g. useContractRead never matches useContractReads)
- String-aware comma splitting — commas inside string values never cause incorrect splits
- watch: true preserved on useBlockNumber and useBlock (still valid in v2)
- Non-overlapping edits — TODO comments inserted before statements, never inside renamed nodes
- useSwitchNetwork renamed in-place by transform 01, not removed by transform 08
What needs manual work (flagged with TODO)
| Pattern | Why manual |
|---|---|
| configureChains | Requires Viem transport setup specific to each RPC provider |
| watch: true removal | Replacement pattern depends on component structure |
| usePrepareContractWrite destructuring | config → data.request |
| useNetwork().chain | Returns chainId (number not Chain object) |
| useWriteContract args | Contract config moves from hook to writeContract() call site |
| useSwitchChain result | switchNetwork → switchChain |
| useWalletClient result | Viem WalletClient not ethers Signer |
| useWaitForTransactionReceipt hash | Type changes from string to 0x${string} |
Install wagmi v2 peer dependencies
bash
Wrap your app with QueryClientProvider
tsx
Running tests
bash
Or individually:
bash
Real-world benchmark
Tested on the official wagmi v1 examples (wevm/wagmi@1.x branch):
- 27 files scanned
- 10 files changed automatically
- ~85% of migration patterns automated
- 6 TODO comments added for AI/manual resolution
- 0 false positives
Project structure
plaintext