From 5e34d45b231374ad31cf472c404cd3e75c0a7333 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Mon, 2 Dec 2024 13:15:44 +0700 Subject: [PATCH] Create Crowdfunding.sol --- .../smart_contracts/Crowdfunding.sol | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 blockchain_development_integration/pi_network/smart_contracts/Crowdfunding.sol diff --git a/blockchain_development_integration/pi_network/smart_contracts/Crowdfunding.sol b/blockchain_development_integration/pi_network/smart_contracts/Crowdfunding.sol new file mode 100644 index 000000000..11b21c834 --- /dev/null +++ b/blockchain_development_integration/pi_network/smart_contracts/Crowdfunding.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract Crowdfunding { + struct Campaign { + address creator; + uint256 goal; + uint256 raisedAmount; + uint256 deadline; + bool isCompleted; + } + + mapping(uint256 => Campaign) public campaigns; + uint256 public campaignCount; + + function createCampaign(uint256 goal, uint256 duration) external { + require(goal > 0, "Goal must be greater than 0"); + campaignCount++; + campaigns[campaignCount] = Campaign(msg.sender, goal, 0, block.timestamp + duration, false); + } + + function contribute(uint256 campaignId) external payable { + Campaign storage campaign = campaigns[campaignId]; + require(block.timestamp < campaign.deadline, "Campaign has ended"); + require(!campaign.isCompleted, "Campaign already completed"); + + campaign.raisedAmount += msg.value; + } + + function finalizeCampaign(uint256 campaignId) external { + Campaign storage campaign = campaigns[campaignId]; + require(block.timestamp >= campaign.deadline, "Campaign is still ongoing"); + require(!campaign.isCompleted, "Campaign already completed"); + + campaign.isCompleted = true; + if (campaign.raisedAmount >= campaign.goal) { + payable(campaign.creator).transfer(campaign.raisedAmount); + } + } +}