-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
blockchain_development_integration/pi_network/smart_contracts/Crowdfunding.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |