How do you build a reliable dApp frontend that reads and writes contracts with viem and wagmi?
What the interviewer wants to hear
- Wallet connection and handling chain and account changes
- Reads via public client or multicall, cached with React Query
- Simulate before writing, then wait for the receipt
- Show clear pending, success and error states, decode revert reasons
- Type-safe ABIs and correct decimal handling
Model answer
I use wagmi for wallet connection and React hooks, with viem underneath. Connectors cover browser wallets, WalletConnect and smart wallets, and I handle account and chain changes, prompting the user to switch if they are on the wrong network. For reads I use useReadContract or batched multicall reads, cached through TanStack Query, and I refetch after relevant transactions or events. For writes I first simulate the call, which catches reverts and gives a decoded error before the user signs. Then I send it, show a pending state with the hash, and wait for the receipt before showing success. I import ABIs as const so viem gives full type safety, and I always format amounts with each token's decimals using formatUnits and parseUnits. I also use my own RPC provider with fallbacks instead of relying only on the wallet's.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Blockchain Developer questions
- What is a reentrancy attack and how do you prevent it?
- What techniques do you use to reduce gas costs in Solidity contracts?
- How do you test a smart contract before deploying it to mainnet?
- How do upgradeable smart contracts work, and what are the trade-offs?
- What is a blockchain oracle and how do you use price data safely?
- How do signature-based approvals like ERC-2612 permit and Permit2 work, and what are their risks?
- How do chain reorganizations affect a dApp or backend service, and how do you handle them?
- How do you design admin control for a protocol so it can be maintained without making users trust a single person?
- Walk me through how you deploy a set of contracts to mainnet safely.