#100DaysOfSolidity #063 Decentralized Crowd Funding with Solidity 🚀💰

Solidity Academy
7 min readAug 8, 2023

Welcome to the 63rd article of the #100DaysOfSolidity series! In this post, we are going to delve into the fascinating world of “Crowd Fund” applications built on the Ethereum blockchain using Solidity language. Crowd funding has transformed the way projects and startups raise funds, and by implementing it in a decentralized and secure manner, we can open up even more possibilities for creative ventures.

#100DaysOfSolidity #063 Decentralized Crowd Funding with Solidity 🚀💰

What is Crowd Funding?

Crowd funding is a method of raising capital from a large number of individuals, typically through small contributions. Traditionally, this was done through centralized platforms where a company or individual would pitch their idea or project, and interested parties would invest money in exchange for potential rewards or equity. However, the emergence of blockchain technology has paved the way for decentralized crowd funding, eliminating the need for intermediaries and providing greater transparency and trust.

Advantages of Decentralized Crowd Funding 🎯

Decentralized crowd funding brings several key advantages to the table:

1. Transparency: The blockchain ensures that all transactions and contributions are visible to everyone, promoting transparency and accountability.

2. Global Accessibility: Anyone with an internet connection can participate in decentralized crowd funding, regardless of their location or background.

3. Lower Fees: By removing intermediaries, decentralized crowd funding reduces fees and allows more funds to reach the project creators.

4. Smart Contracts: With Solidity smart contracts, we can automate the entire crowd funding process, ensuring that funds are released only when certain conditions are met.

5. Trustless Environment: Participants can engage in crowd funding without having to trust a central authority, as the rules are enforced by the smart contract code.

Designing the Crowd Fund Smart Contract 🔧

Let’s dive into the technical aspects of designing a decentralized crowd funding smart contract using Solidity.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CrowdFund {
address public projectCreator;
uint public goalAmount;
uint public deadline;
mapping(address => uint) public contributions;
uint public totalContributions;
bool public fundingComplete;
// Events to emit status changes
event GoalReached(uint totalContributions);
event FundTransfer(address backer, uint amount);
constructor(uint _goalAmount, uint _durationInDays) {
projectCreator = msg.sender;
goalAmount = _goalAmount * 1 ether; // Convert to wei (1 ether = 1e18 wei)
deadline = block.timestamp + (_durationInDays * 1 days);
}
modifier onlyProjectCreator() {
require(msg.sender == projectCreator, "Only the project creator can perform this action.");
_;
}
modifier goalNotReached() {
require(!fundingComplete && block.timestamp < deadline, "Goal already reached or deadline passed.");
_;
}
function contribute() external payable goalNotReached {
contributions[msg.sender] += msg.value;
totalContributions += msg.value;
if (totalContributions >= goalAmount) {
fundingComplete = true;
emit GoalReached(totalContributions);
}
emit FundTransfer(msg.sender, msg.value);
}
function withdrawFunds() external onlyProjectCreator {
require(fundingComplete, "Funding goal not reached yet.");
payable(projectCreator).transfer(address(this).balance);
}
function refundContribution() external goalNotReached {
require(contributions[msg.sender] > 0, "You have not contributed to the crowd fund.");
uint amountToRefund = contributions[msg.sender];
contributions[msg.sender] = 0;
totalContributions -= amountToRefund;
payable(msg.sender).transfer(amountToRefund);
}
function getRemainingTime() external view returns (uint) {
if (block.timestamp >= deadline) {
return 0;
} else {
return deadline - block.timestamp;
}
}
}

Understanding the Smart Contract 🤓

1. `CrowdFund`: This is our main smart contract where crowd funding logic is implemented.
2. `projectCreator`: The address of the account that deployed the contract and created the crowd fund campaign.
3. `goalAmount`: The total amount of funds (in wei) required to successfully complete the crowd fund.
4. `deadline`: The timestamp until which the crowd fund will be open for contributions.
5. `contributions`: A mapping that stores the contribution amount of each participant.
6. `totalContributions`: The total amount of funds (in wei) contributed by all participants.
7. `fundingComplete`: A boolean flag indicating whether the crowd fund goal has been reached or not.

The contract consists of the following functions:

1. `constructor`: Initializes the crowd fund campaign with the desired `goalAmount` and `deadline`.

2. `contribute`: Allows anyone to contribute funds to the crowd fund as long as the deadline has not passed and the goal has not been reached. The contributed amount is added to the `contributions` mapping and `totalContributions`.

3. `withdrawFunds`: The project creator can withdraw the funds once the crowd fund goal has been reached. The total amount raised is transferred to the project creator’s address.

4. `refundContribution`: In case the crowd fund goal is not reached before the deadline, contributors can request a refund of their contributions.

5. `getRemainingTime`: A helper function to check how much time is remaining until the crowd fund deadline.

Report: Crowd Fund ERC20 Token Smart Contract 🚀

Crowd Fund ERC20 Token Smart Contract 🚀🤝

In this report, we will explore the functionalities and features of the smart contract, which enables users to create and participate in crowd funding campaigns using an ERC20 token.

Smart Contract Overview

The “Crowd Fund ERC20 Token” smart contract is designed to facilitate decentralized crowd funding campaigns on the Ethereum blockchain. It leverages the power of ERC20 tokens, allowing users to pledge their tokens to support various projects and initiatives. Let’s dive into the key components of the smart contract:

1.Campaign Creation: Users can create new crowd funding campaigns by calling the `launch` function. When creating a campaign, the user specifies the funding goal, the start and end timestamps for the campaign duration.

2. Campaign Structure: Each campaign is represented by a `Campaign` struct, containing essential information such as the campaign creator’s address, the funding goal, the total amount pledged, the start and end timestamps, and a flag to indicate if the goal has been claimed.

3. Campaign ID and Mapping: The smart contract maintains a mapping from campaign IDs to their respective `Campaign` struct. Additionally, the contract keeps track of the total number of campaigns created.

4. Pledging and Unpledging: Participants can pledge their ERC20 tokens to a campaign by calling the `pledge` function. The pledged tokens are held in the contract until the campaign ends. If a participant wishes to cancel their pledge, they can do so by calling the `unpledge` function before the campaign end timestamp.

5. Claiming and Refunding: After the campaign ends, the campaign creator can claim the funds if the total amount pledged exceeds the campaign goal. This is done by calling the `claim` function. If the campaign goal is not met, participants can request a refund of their pledged tokens by calling the `refund` function.

6. Events: The smart contract emits various events, such as `Launch`, `Cancel`, `Pledge`, `Unpledge`, `Claim`, and `Refund`, to provide transparency and enable external applications to listen for important contract state changes.

Functionalities and Security Measures

1. Timestamp Checks: The smart contract ensures that campaign start and end timestamps are appropriately set, and that a campaign cannot end before it even starts.

2. Campaign Cancellation: A campaign creator has the option to cancel a campaign as long as it has not started yet, preventing unnecessary token locking.

3. Pledge and Unpledge Mechanism: The smart contract enables participants to pledge and unpledge tokens seamlessly, fostering a user-friendly experience.

4. Funds Handling: The smart contract safely handles the transfer of pledged tokens to the contract during the campaign and the transfer of funds to the campaign creator upon successful completion.

5. Claiming and Refunding Logic: The contract implements the logic to ensure that funds are released only when specific conditions are met, either through claiming or refunding.

Advantages of ERC20 Token Integration

By incorporating an ERC20 token into the crowd funding process, the smart contract benefits from various advantages:

1. Interoperability: ERC20 tokens are compatible with numerous wallets and exchanges, ensuring seamless integration with the broader Ethereum ecosystem.

2. Standardization: The ERC20 standard defines a set of rules that enable developers to predict how tokens behave, enhancing security and ease of use.

3. Decentralization: By using an ERC20 token, the crowd funding process becomes more decentralized, reducing reliance on centralized entities.

4. Economic Incentives: ERC20 tokens can facilitate various economic incentives, such as discounts, bonuses, or governance rights for contributors.

In Conclusion; the “Crowd Fund ERC20 Token” smart contract is a powerful tool that empowers users to participate in decentralized crowd funding campaigns using ERC20 tokens. With its secure and transparent functionalities, the smart contract promotes trust and accountability, enabling users to support exciting projects and initiatives with confidence.

By integrating ERC20 tokens, the smart contract takes advantage of the standardization and interoperability offered by the ERC20 standard, providing a seamless user experience. The world of decentralized crowd funding is now more accessible and dynamic, opening doors for innovative projects and collaborations.

As the blockchain ecosystem continues to evolve, the “Crowd Fund ERC20 Token” smart contract serves as an essential building block in the realm of decentralized finance, fostering creativity, inclusivity, and innovation within the blockchain community.

Conclusion 🏁

In this article, we explored the exciting world of decentralized crowd funding with Solidity. We discussed the advantages of using blockchain for crowd funding and designed a fully functional crowd funding smart contract. By leveraging smart contracts, we can create trustless and transparent crowd funding campaigns that empower both project creators and contributors.

Decentralized crowd funding has the potential to revolutionize the way we support creative projects and bring innovative ideas to life. By eliminating intermediaries, reducing fees, and providing a global platform for funding, blockchain-based crowd funding takes fundraising to a whole new level.

Remember, this is just the beginning, and there are endless possibilities to explore in the world of blockchain and Solidity. Happy coding! 🚀💡

📚 Resources 📚

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/