Build Your First Web3 DApp: A Complete Tutorial
To build a first Web3 DApp, set up a Next.js project with wagmi, viem, and ConnectKit, connect a wallet, read contract state with useReadContract, and send transactions with useWriteContract. This tutorial builds a simple voting app, tests it on the Sepolia testnet with MetaMask, and ends with a production checklist covering audits, monitoring, and RPC endpoints.
Introduction
This tutorial walks through building a complete Web3 DApp from scratch. We will create a simple voting app that connects wallets, reads blockchain state, and submits transactions.
Prerequisites
- Node.js 18+
- Basic React knowledge
- MetaMask browser extension
- Sepolia testnet ETH (from faucet)
Project Setup
Create a new Next.js project with TypeScript and Tailwind. Install wagmi, viem, tanstack/react-query, and connectkit.
What Each Library Does
It helps to know which piece does what before you write any code. The Wagmi getting-started guide describes the split: Viem is a TypeScript interface for Ethereum that performs blockchain operations, and TanStack Query is an async state manager that handles requests, caching, and more. Wagmi sits on top of both and turns them into React hooks, so your components ask for contract data and wallet state without managing connections or request lifecycles by hand.
ConnectKit adds the user-facing part: the button, the wallet modal and the account display. Because it plugs into the same Wagmi configuration, you do not wire up wallet connectors twice. When something breaks, this split tells you where to look. A failed contract call is usually a Viem or ABI issue, a stale value on screen is usually query caching, and a missing wallet option is a ConnectKit or connector setting.
Configure Wagmi
Create a wagmi config with your chains (mainnet, sepolia) and transports. Use getDefaultConfig from connectkit for easy setup.
Wrap App with Providers
Wrap your app with WagmiProvider, QueryClientProvider, and ConnectKitProvider.
Connect Wallet Button
Use ConnectKitButton component for easy wallet connection.
Read Contract Data
Use useReadContract hook from wagmi to read blockchain state. Provide contract address, ABI, and function name.
Write to Contract
Use useWriteContract hook for transactions. Handle pending, confirming, and success states. Use useWaitForTransactionReceipt to wait for confirmation.
Reads, Writes and Gas
The two hooks in this tutorial behave differently because the two kinds of contract function behave differently. useReadContract is a hook for calling a read-only function on a contract and returning the response, and the Wagmi docs note that since read-only methods do not change the state of the contract, they do not require any gas to be executed. That is why you can refresh the vote counts as often as you like without a wallet prompt.
Writing is different. useWriteContract is the action for executing a write function on a contract, and it needs a signature from the connected wallet. Ethereum describes gas this way: gas is essential to the Ethereum network, the fuel that allows it to operate, in the same way that a car needs gasoline to run. Every vote your app submits spends gas, so show the user that a transaction is about to happen before you ask the wallet to sign it, and keep your button disabled while a request is pending so a double click does not send two votes.
Tracking a Transaction to Confirmation
Sending a transaction is not the same as having it accepted, so your interface needs three visible states: waiting for the wallet signature, waiting for the network, and finished. useWaitForTransactionReceipt is a hook that waits for the transaction to be included on a block, and then returns the transaction receipt. Pass it the hash returned by the write call, and use its pending and success flags to drive the button label and a short status message.
Plan for failure as well. A user can reject the signature, the contract can revert, or the network can be slow. Show the error message from the hook, leave the form usable so the user can retry, and only refresh the vote counts after the receipt arrives, so the screen never shows a result that the chain has not confirmed.
Testing
- Get Sepolia ETH from a faucet
- Connect MetaMask to Sepolia
- Test wallet connection
- Test reading and writing
- Verify on Etherscan
Why Test on Sepolia First
Do your first full run on a test network. The Ethereum documentation says that Sepolia is a network for contract and application developers to test their applications. Transactions there behave like mainnet transactions, but the ETH has no market value, so a mistake in your voting logic costs nothing.
To fund your test wallet, use a faucet. The same page explains that most faucets are webapps where you can input an address which you request ETH to be sent to. Request a small amount, run through connecting, reading and writing, and look up each transaction on a block explorer to confirm that what the app displays matches what the chain recorded. Only after that should you point the same code at a production network.
What Should You Check Before Going to Production?
- Test on testnet thoroughly
- Audit smart contract
- Set up error monitoring
- Configure proper RPC endpoints
- Deploy to production L2
Conclusion
You have built a functional Web3 DApp! Key concepts: wagmi hooks for blockchain interaction, ConnectKit for wallets, and proper transaction handling. Expand this foundation to build more complex applications.
Sources
The primary sources this article cites, checked in November 2026:
- Getting Started (Wagmi) (wagmi.sh)
- useReadContract (Wagmi) (wagmi.sh)
- useWriteContract (Wagmi) (wagmi.sh)
- useWaitForTransactionReceipt (Wagmi) (wagmi.sh)
- Gas and fees (ethereum.org) (ethereum.org)
- Networks (ethereum.org) (ethereum.org)
Key Takeaways
- Use viem/wagmi for modern Web3 development
- ConnectKit or RainbowKit for wallet connections
- Read operations are free, writes cost gas
- Always handle transaction failures gracefully
- Test on testnets before mainnet deployment
Frequently Asked Questions
Do I need to know Solidity?
Not to build a frontend. A DApp frontend can interact with existing contracts through wagmi hooks like useReadContract and useWriteContract, given the contract address, ABI, and function name, without any Solidity knowledge. To create and deploy your own contracts, you will need to learn Solidity basics, and the contract should be audited before it goes to production.
Which network should I start with?
Start with the Sepolia testnet. It is free, since you get Sepolia ETH from a faucet, and it behaves like mainnet. Connect MetaMask to Sepolia, test wallet connection, reading, and writing, then verify your transactions on Etherscan. Move to mainnet or an L2 such as Base when you are ready for production.
Why do read operations from a smart contract cost nothing while writes cost gas?
Reads only query existing on-chain data without changing it, so no transaction is broadcast and nothing costs gas. Writes modify blockchain state, which requires validators to process and confirm a transaction, so they cost gas. In the app, reads use useReadContract, while writes use useWriteContract and need handling for pending, confirming, and success states.
What are wagmi and viem used for in a DApp?
Viem handles low-level interaction with Ethereum, such as encoding calls and sending transactions. Wagmi layers React hooks on top for reading contract state and managing wallet connections. In this tutorial they sit inside a wagmi config with mainnet and Sepolia chains, and the app is wrapped in WagmiProvider, QueryClientProvider, and ConnectKitProvider.
Which library should I use to add wallet connection?
ConnectKit and RainbowKit are the popular choices. Both provide ready-made interfaces that let users connect wallets like MetaMask with minimal setup, and both integrate cleanly with wagmi and viem. This tutorial uses ConnectKit, whose getDefaultConfig simplifies the wagmi setup and whose ConnectKitButton component adds wallet connection to the page in one step.
About the Author
Marcus Williams
Blockchain & DeFi Editorial Desk
Blockchain & DeFi Editorial Desk · Web3AIBlog
Marcus Williams is a pen name for our blockchain and DeFi editorial desk. Posts under this byline are written and reviewed by contributors with backgrounds in protocol engineering, on-chain analysis, smart contract auditing, tokenomics, and decentralized finance. The desk covers consensus mechanisms, liquidity protocols, MEV, on-chain forensics, regulatory frameworks across jurisdictions, and the operational realities of running and using DeFi at scale. Our coverage is an editorial synthesis of protocol documentation, on-chain data, and audited primary sources, with every figure verified against a primary source before publication.