1/5
## Introduction to the HTML/JS Buy Me A Coffee dApp Welcome to the first project section of our full-stack web3 development journey! Here, we'll dive straight into building a tangible application: a simple "Buy Me A Coffee" dApp (Decentralized Application) using fundamental web technologies – HTML and JavaScript – alongside core blockchain concepts. The goal of this project is to demystify how a standard web frontend can interact with a smart contract deployed on a blockchain. We'll build everything from scratch, providing a clear view of the essential components and their interplay. By the end, you'll have a functional dApp that allows users to connect their crypto wallet and send simulated cryptocurrency ("buy coffee") to a smart contract. ## Project Overview: Building a Minimal web3 Application Our "Buy Me A Coffee" application serves as a practical introduction to web3 interactions. While simple, it encapsulates key actions found in many dApps: 1. **Connecting a Wallet:** Allowing users to link their browser-based wallet (like MetaMask) to the web application. 2. **Sending Transactions:** Enabling users to send cryptocurrency (in our case, test Ether) to a smart contract function. This simulates the "buying coffee" action. 3. **Reading Blockchain Data:** Fetching information stored within the smart contract, such as its current Ether balance. 4. **Executing State-Changing Functions:** Triggering functions on the smart contract that modify its state, like withdrawing the accumulated funds (typically restricted to the contract owner). We will focus on the core mechanics, using minimal frontend tooling (plain HTML and JavaScript) to keep the focus squarely on the web3 integration aspects. ## Core Components and Their Roles Understanding the different pieces involved is crucial. Our application consists of four main parts: 1. **Frontend (HTML/JavaScript):** This is what the user sees and interacts with in their browser. * **`index.html`**: Defines the structure and elements of the user interface, including buttons for actions (Connect, Get Balance, Withdraw, Buy Coffee) and an input field for the amount. * **`index-js.js`**: Contains the JavaScript code that handles user interactions (button clicks), manages the wallet connection, and orchestrates communication with the smart contract. It sends requests to the blockchain and updates the UI or logs information based on the responses. 2. **Backend (Solidity Smart Contract):** This is the logic that lives on the blockchain. * **`FundMe.sol`**: A smart contract written in Solidity. It defines the rules for receiving funds, storing them, tracking the balance, and allowing withdrawals. Key functions include: * `fund()`: A `payable` function designed to accept Ether sent from the frontend. * `withdraw()`: A function, typically restricted, allowing the contract owner to transfer the contract's entire Ether balance out. * Balance inquiry logic (implicitly accessed via calls from the frontend). 3. **Blockchain (Anvil):** The decentralized ledger where the smart contract resides and transactions are recorded. * **Anvil**: For development, we use Anvil, a local blockchain simulator included in the Foundry toolkit. Anvil runs in your terminal, providing a controlled environment with pre-funded test accounts and their private keys, mimicking a real blockchain without requiring real funds or extensive setup. Our `FundMe.sol` contract will be deployed to this local Anvil instance. 4. **Wallet (MetaMask):** The bridge facilitating interaction between the user's browser and the blockchain. * **MetaMask**: A browser extension wallet that holds the user's accounts and keys. It allows the user to authorize the frontend's connection request and prompts them to sign and approve transactions (like sending Ether or executing contract functions) before they are sent to the blockchain. We'll configure MetaMask to connect to our local Anvil network and import the test accounts provided by Anvil. ## Understanding the Interaction Flow How do these components work together? Let's trace a typical user journey: 1. **Load & Connect:** The user opens `index.html` in their browser. They click the "Connect" button. 2. **Wallet Approval:** The frontend JavaScript prompts MetaMask. The user selects an account and approves the connection request within the MetaMask extension. The frontend UI updates to show "Connected". 3. **Send Funds ("Buy Coffee"):** The user enters an amount (e.g., `1` ETH) into the input field and clicks "Buy Coffee". 4. **Transaction Initiation:** The frontend JavaScript constructs a transaction request to call the `fund()` function on the `FundMe.sol` contract, specifying the Ether amount. 5. **Transaction Confirmation:** MetaMask pops up, showing the transaction details (sending 1 ETH to the contract address). The user confirms the transaction. 6. **Blockchain Interaction:** MetaMask signs the transaction with the user's private key and sends it to the connected blockchain node (our local Anvil instance). 7. **Contract Execution:** Anvil processes the transaction, executing the `fund()` function within the `FundMe.sol` contract, which increases the contract's Ether balance. 8. **Read Data ("Get Balance"):** The user clicks "Get Balance". 9. **Data Request:** The frontend JavaScript makes a read-only call (no transaction cost beyond potential RPC provider fees on a live network) to the blockchain via the wallet/provider to fetch the contract's current balance. 10. **Display Result:** The balance is returned from the blockchain, and the JavaScript logs it to the browser's developer console. 11. **Withdraw Funds (Owner Action):** Assuming the connected user is the contract owner, they click "Withdraw". 12. **Withdrawal Transaction:** Similar to funding, JavaScript initiates a transaction to call the `withdraw()` function. MetaMask prompts the user to confirm (this transaction sends no Ether but requires gas fees). 13. **Withdrawal Execution:** The transaction is sent to Anvil, the `withdraw()` function executes, transferring the contract's balance to the owner's address. 14. **Verify Withdrawal:** Clicking "Get Balance" again would now show `0`. ## Demonstrated Functionality Walkthrough In the accompanying video demonstration, we see these steps in action: 1. **Setup:** The `index.html` page is served locally (e.g., `http://127.0.0.1:5500`) using a tool like VS Code's "Go Live" extension. The Anvil local blockchain is shown running in a terminal window. 2. **Connection:** Clicking "Connect" triggers MetaMask, where the pre-configured "Anvil" network and an imported Anvil test account are selected and connected. 3. **Initial State:** Clicking "getBalance" initially logs `0` to the browser console. 4. **Funding:** Entering `1` ETH and clicking "Buy Coffee" triggers a MetaMask confirmation popup for sending 1 ETH to the `fund` function. After confirmation, console logs indicate transaction processing and eventually show a transaction hash. 5. **Updated Balance:** Clicking "getBalance" now logs `1` (after potentially a brief delay for the local block to be mined). 6. **Second Funding:** Repeating the funding step adds another 1 ETH to the contract. 7. **Final Funded Balance:** "getBalance" now correctly logs `2`. 8. **Withdrawal:** Clicking "Withdraw" prompts MetaMask confirmation for the `withdraw` transaction (no ETH value sent). After confirmation and processing, the withdrawal is complete. 9. **Empty Balance:** "getBalance" finally logs `0`, confirming the successful withdrawal. Throughout this process, the browser's developer console is heavily used to display status messages, balances, and transaction hashes, serving as our primary feedback mechanism in this minimal setup. ## Key Tools and Technologies To build and run this project, we rely on several tools: * **Code Editor:** VS Code (or your preferred editor). * **Local Web Server:** VS Code "Go Live" extension (or any simple HTTP server). * **Local Blockchain:** Anvil (part of the Foundry toolkit). * **Wallet:** MetaMask browser extension. * **Debugging:** Browser Developer Tools (specifically the Console). * **Core Languages:** HTML, JavaScript, Solidity. ## Learning Objectives and Next Steps This project establishes the foundational pattern for frontend-smart contract interaction. Mastering these steps – connecting, sending transactions, reading data, handling wallet interactions – is essential before moving on to more complex dApps. Pay close attention to how JavaScript uses libraries (like Ethers.js or Viem, which will be introduced later) to communicate with MetaMask, which in turn communicates with the blockchain. Remember that local development environments like Anvil are invaluable for rapid testing and iteration without incurring real-world costs or delays. Familiarize yourself with using the developer console for logging and debugging. While we build this from scratch, note that AI tools can assist in generating boilerplate code, but understanding the underlying principles remains paramount for building robust and secure web3 applications.
A foundational project to Introduction to the HTML/JS Buy Me A Coffee dApp. Discover how plain HTML and JavaScript can interact with a Solidity smart contract to create a simple dApp. Practice connecting wallets, sending test Ether, reading contract state, and triggering withdrawals on a local blockchain.
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