My first blockchain-related toy project. A simple crowdfunding dApp built with Hardhat, Ignition, React, and Tailwind CSS.
- A smart contract for the crowdfunding campaign is deployed to the local Hardhat node.
- The campaign has a target amount and a deadline.
- Accounts can contribute ETH to the campaign.
- Only the owner can withdraw the funds when the campaign is successful.
- Contributors can refund their contributions when the campaign is not successful.
smart-contracts/: Solidity smart contract, deployment script (Hardhat Ignition), and test suite.frontend/: A React-based web application built with Vite and Tailwind CSS to interact with the campaign.
Currently only works on localhost.
Install MetaMask extension in your browser: https://metamask.io/
Install pnpm if not already installed:
npm install -g pnpmThen install dependencies for the smart contracts:
cd smart-contracts
pnpm installIn a new terminal, install dependencies for the frontend:
cd frontend
pnpm installFirst start the Hardhat node:
cd smart-contracts
pnpm run nodeThen in a new terminal, deploy the smart contract:
cd smart-contracts
pnpm run contract:deploySee the output of this command for the contract address, create a .env file in the frontend directory by copying .env.example and set VITE_CROWDFUNDING_ADDRESS to the contract address.
Then, start the frontend:
cd frontend
pnpm run devLook at the output of the pnpm run node command. At the top you will find the server URL and some default accounts. You can use any account to contribute to the campaign. The first account is the owner of the contract (the only person who can withdraw funds when the campaign succeeds).
Go to MetaMask extension in your browser and add a custom network using the server URL from the output of the pnpm run node command. Chain ID is 31337 (default for Hardhat).
Now import an account in the MetaMask extension using a private key from the output of the pnpm run node command. You can import multiple accounts but you need to manually manage which one is currently connected to the frontend.
Then open the frontend in your browser, click "Connect MetaMask" and select an imported account.
You can now try the test cases below. To start fresh each time, you need to restart the Hardhat node and redeploy the contract. After you redeploy, ensure that the contract address has not changed, otherwise you need to update the frontend .env file. It also sometimes happens that MetaMask caches previous data, so you might need to e.g. close and reopen the browser.
- Contribute to the campaign until the goal is reached (before the deadline).
- Locally we can simulate the progress of time by running
cd smart-contracts && pnpm run fast-forward. - Now you should see the campaign status change to "Successful" (maybe you need to refresh the page).
- If you are connected to MetaMask as the owner, you should now be able to withdraw the funds.
- Contribute to the campaign some amount less than the goal.
- Progress time:
cd smart-contracts && pnpm run fast-forward. - Now you should see the campaign status change to "Failed" (maybe you need to refresh the page).
- If you are connected to MetaMask as a contributor, you should now be able to refund your contribution.
- If you are connected to MetaMask as the owner, you should not be able to withdraw the funds.