This is a decentralized crowdfunding platform built with React.js for the client-side and Solidity for the smart contract, deployed on the Sepolia testnet using Thirdweb.
- Create Campaigns: Users can create crowdfunding campaigns with a funding goal, description, and deadline.
- Contribute: Users can contribute funds to campaigns securely through the blockchain.
- Campaign Management: Campaign creators can withdraw funds if the goal is met, or refund contributors if not.
- Blockchain Integration: All transactions are secure and transparent, using smart contracts on Ethereum's Sepolia testnet.
- React.js: Frontend framework
- Thirdweb SDK: For interacting with the deployed smart contract
- Tailwind CSS: For styling
- Solidity: For the smart contract
- Thirdweb: For deploying and managing smart contracts
- Sepolia Testnet: Ethereum testnet used for deployment
Make sure you have the following installed on your system:
- Node.js: Download Node.js
- MetaMask: For interacting with the blockchain
- Thirdweb SDK: For contract deployment and interaction
-
Clone the repository:
https://github.com/arnavbansal2764/Crowdfunding-Web3.git
-
Navigate to the project directory:
cd crowdfunding-dapp/client
-
Install the dependencies:
npm install
-
Set up environment variables:
Create a
.env.local
file in the root directory and add the following variables:REACT_APP_THIRDWEB_API_KEY=your-thirdweb-api-key REACT_APP_CONTRACT_ADDRESS=your-contract-address
REACT_APP_THIRDWEB_API_KEY
: API key from ThirdwebREACT_APP_CONTRACT_ADDRESS
: The smart contract address on the Sepolia testnet
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract CrowdFunding {
struct Campaign {
address owner;
string title;
string description;
uint256 target;
uint256 deadline;
uint256 amountCollected;
string image;
address[] donators;
uint256[] donations;
}
mapping(uint256 => Campaign) public campaigns;
uint256 public numberOfCampaigns = 0;
function createCampaign(address _owner, string memory _title, string memory _description, uint256 _target, uint256 _deadline, string memory _image) public returns (uint256) {
Campaign storage campaign = campaigns[numberOfCampaigns];
require(campaign.deadline < block.timestamp, "The deadline should be a date in the future.");
campaign.owner = _owner;
campaign.title = _title;
campaign.description = _description;
campaign.target = _target;
campaign.deadline = _deadline;
campaign.amountCollected = 0;
campaign.image = _image;
numberOfCampaigns++;
return numberOfCampaigns - 1;
}
function donateToCampaign(uint256 _id) public payable {
uint256 amount = msg.value;
Campaign storage campaign = campaigns[_id];
campaign.donators.push(msg.sender);
campaign.donations.push(amount);
(bool sent,) = payable(campaign.owner).call{value: amount}("");
if(sent) {
campaign.amountCollected = campaign.amountCollected + amount;
}
}
function getDonators(uint256 _id) view public returns (address[] memory, uint256[] memory) {
return (campaigns[_id].donators, campaigns[_id].donations);
}
function getCampaigns() public view returns (Campaign[] memory) {
Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);
for(uint i = 0; i < numberOfCampaigns; i++) {
Campaign storage item = campaigns[i];
allCampaigns[i] = item;
}
return allCampaigns;
}
}
-
Deploy the smart contract to Sepolia using Thirdweb:
- Go to Thirdweb and create a project.
- Deploy the smart contract through their platform.
-
After deployment, copy the contract address and paste it into the
.env.local
file.
-
Start the React application:
npm run dev
-
Open your browser and navigate to:
http://localhost:5173
const publishCampaign = async (form) => {
try {
const data = await createCampaign({
args: [
address, // owner
form.title, // title
form.description, // description
form.target,
new Date(form.deadline).getTime(), // deadline,
form.image,
],
});
console.log("contract call success", data)
} catch (error) {
console.log("contract call failure", error)
}
}
const getCampaigns = async () => {
const campaigns = await contract.call('getCampaigns');
const parsedCampaings = campaigns.map((campaign, i) => ({
owner: campaign.owner,
title: campaign.title,
description: campaign.description,
target: ethers.utils.formatEther(campaign.target.toString()),
deadline: campaign.deadline.toNumber(),
amountCollected: ethers.utils.formatEther(campaign.amountCollected.toString()),
image: campaign.image,
pId: i
}));
return parsedCampaings;
}
const getUserCampaigns = async () => {
const allCampaigns = await getCampaigns();
const filteredCampaigns = allCampaigns.filter((campaign) => campaign.owner === address);
return filteredCampaigns;
}
const donate = async (pId, amount) => {
const data = await contract.call('donateToCampaign', [pId], { value: ethers.utils.parseEther(amount)});
return data;
}
const getDonations = async (pId) => {
const donations = await contract.call('getDonators', [pId]);
const numberOfDonations = donations[0].length;
const parsedDonations = [];
for(let i = 0; i < numberOfDonations; i++) {
parsedDonations.push({
donator: donations[0][i],
donation: ethers.utils.formatEther(donations[1][i].toString())
})
}
return parsedDonations;
}
- Test your smart contract using tools like Hardhat or Truffle.
- Use MetaMask to simulate transactions on the Sepolia testnet.
- Check logs for issues using the browser console and smart contract logs.
Deploy the React application to a platform like Vercel or Netlify.
npm run build
This will create an optimized build of the application in the build/
directory, ready to be deployed.