0/5
## Mid-Point Review: Building Your web3 Frontend We've covered a significant amount of ground quickly in building our simple web frontend to interact with a Solidity smart contract. This lesson serves as a mid-point review to consolidate our understanding of the key concepts and steps we've implemented so far. Our goal remains to create a minimal HTML/JavaScript site featuring buttons that trigger functions on our smart contract. Based on our initial plan, we've successfully completed: 1. **Connect:** Implementing the ability for users to connect their wallets (like MetaMask) to our site. 2. **Buy Coffee:** * Adding a button to initiate the action. * Successfully calling the `fund` function on our smart contract to send Ether. 3. **Test Blockchain:** Setting up and utilizing a local test blockchain (`anvil`) for development and interaction. 4. **Get Balance:** Implementing functionality to read and display the Ether balance held by the smart contract. Let's revisit the core concepts behind these completed steps. ## Connecting Your Wallet to the Frontend The first crucial step is establishing communication between our web page and the user's blockchain wallet. * **The Bridge (`window.ethereum`):** We rely on the `window.ethereum` JavaScript object, which EIP-1193 compatible wallets like MetaMask inject into the browser. Our frontend code first checks if this object exists. * **Requesting Connection:** If `window.ethereum` is present, we use a library like `viem` to create a `walletClient`. This client allows us to interact with the user's wallet. We then call a method like `requestAddresses()` on the `walletClient`. This prompts the user (via their wallet extension) to grant permission for our site to view their account address. * **User Interface Feedback:** Once connected, we update the UI (e.g., changing the "Connect" button text to "Connected!") to provide clear feedback. If `window.ethereum` isn't found, we prompt the user to install a compatible wallet. In essence, `window.ethereum` acts as the essential link, enabling our JavaScript to send requests (like connecting or signing transactions) to the wallet, which then communicates with the blockchain. ## Reading Data: Getting the Contract Balance Reading information from the blockchain, such as a contract's balance, is typically a read-only operation. It doesn't change the state of the blockchain and usually doesn't require gas fees or a transaction signature from the user. * **The `publicClient`:** For these operations, we primarily use `viem`'s `publicClient`. This client is configured to read data from the blockchain node we're connected to (in our case, the local `anvil` node). * **Fetching the Balance:** We use the `publicClient.getBalance()` method, providing the address we want to query (the `contractAddress` of our deployed smart contract). * **Handling the Result:** This method returns the balance as a BigInt value denominated in wei (the smallest unit of Ether). We then use a utility function like `formatEther` (also from `viem`) to convert this wei value into a more human-readable Ether string (e.g., "1.5 ETH") for display in the console or UI. ## Writing Data: Sending Transactions to the Contract Interacting with functions that *change* the state of the blockchain (like our `fund` function, which transfers Ether) requires sending a transaction. This involves gas fees and requires the user to authorize the transaction with their private key via their wallet. * **`walletClient` vs. `publicClient`:** While the `publicClient` is used for reading data and simulating transactions, the `walletClient` (initialized with `window.ethereum`) is essential for *sending* transactions because it requires access to the connected user account for signing. * **Step 1: Simulation (`simulateContract`):** Before sending a state-changing transaction, it's crucial best practice to simulate it. Simulation uses the `publicClient` to check if the transaction is likely to succeed *without actually broadcasting it or spending gas*. We provide `simulateContract` with the contract's address and ABI, the function name (`fund`), the connected user account, the target chain, and any necessary parameters (like the amount of Ether to send, using `parseEther` from `viem` to convert the ETH string to wei). If the simulation predicts success, it returns a `request` object. If it predicts failure (e.g., due to insufficient funds, incorrect arguments, or contract logic preventing the call), it throws an error, allowing us to catch issues early. * **Step 2: Execution (`writeContract`):** If the simulation succeeds and returns the `request` object, we proceed to execute the actual transaction. We use the `walletClient.writeContract()` method, passing in the `request` object obtained from the simulation. This action prompts the user in their wallet (MetaMask) to review the transaction details (gas fees, amount being sent) and approve or reject it. * **Transaction Hash:** If the user approves, the wallet signs the transaction and broadcasts it to the network. The `writeContract` function then typically returns the transaction hash, which acts as a receipt and allows us to track the transaction's progress on the blockchain. Remember to use utility functions like `parseEther` to correctly format Ether values into wei (BigInt) before sending them in transactions. ## Your Local Blockchain: Using Anvil for Development To rapidly develop and test our frontend interactions without deploying to public testnets or mainnet, we use `anvil`, a fast local blockchain node provided by the Foundry toolkit. * **Purpose:** Anvil runs an Ethereum node directly on your machine, allowing near-instantaneous transaction confirmation, making the development feedback loop much faster. * **Starting Anvil with State:** We typically start `anvil` using a command like `anvil --load-state fundme-anvil.json`. The crucial `--load-state` flag tells `anvil` to initialize its blockchain state from the specified JSON file. This file ensures our smart contract is already deployed at the expected `contractAddress` and potentially pre-funds test accounts, creating a consistent development environment. * **Connecting the Frontend:** Our frontend application (via MetaMask) is configured to connect to the network provided by `anvil`, usually running at `http://localhost:8545`. All interactions reviewed here (connecting, getting balance, funding) occurred against this local `anvil` instance. * **Backend Dependency:** As demonstrated, if the `anvil` process is stopped, our frontend buttons stop working because they can no longer communicate with the required blockchain node. This highlights the essential role of a running node (whether local like `anvil`, a testnet, or mainnet) for any web3 application. ## Keep Learning: Tips for Success This material is dense, and we've moved quickly. Don't be discouraged if it takes time to fully grasp these concepts. * **Review and Revisit:** Take time to review this lesson and previous steps. Understanding the flow from frontend click to wallet interaction to blockchain confirmation is key. * **Ask Questions:** If you get stuck, reach out! Utilize resources like AI assistants, course discussion forums, developer communities on Discord or Twitter. Many others are learning alongside you. * **Take Breaks:** Learning complex technical subjects requires mental breaks. Your brain needs time to process information. Experiment with study schedules like 30 minutes of focus followed by a 5-minute break, or 55 minutes followed by a 10-15 minute break. Find a sustainable pace that works for you, even if it's just one focused session per day. * **Simulate First:** Remember the crucial pattern for state-changing functions: always try to simulate the transaction before actually sending it with `writeContract`. This practice will save you time and potential frustration by catching errors early. Keep building, keep experimenting, and don't hesitate to revisit the fundamentals. This is an exciting field, and consistent effort is the key to mastering web3 development.
A consolidating review to mid-point review: Building your web3 frontend. Revisit connecting wallets, reading balances (`publicClient`), and sending transactions (`walletClient`, simulate/write). Solidify understanding of `window.ethereum`, `viem` usage, and local testing with `anvil`.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build full-stack web3 applications on ZKsync
JavaScript/TypeScript: viem, wagmi, synpress
Nodejs and pnpm
rindexer
Circle Compliance Engine and USDC
Fleek site hosting and CLI
How to build a static and dynamic React/Next.js site
How to leverage AI to code faster and more securely
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on May 15, 2025
Solidity Developer
Full-Stack Web3 Development Crash CourseDuration: 1h 12min
Duration: 1h 39min
Duration: 3h 08min
Duration: 1h 44min
Course Overview
About the course
How to build full-stack web3 applications on ZKsync
JavaScript/TypeScript: viem, wagmi, synpress
Nodejs and pnpm
rindexer
Circle Compliance Engine and USDC
Fleek site hosting and CLI
How to build a static and dynamic React/Next.js site
How to leverage AI to code faster and more securely
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on May 15, 2025