-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrowdFunding.sol
77 lines (56 loc) · 2.29 KB
/
GrowdFunding.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// This contract allows multiple contributors to pool funds, set funding goals, and
// withdraw or refund funds based on the campaign's success.
contract CrowdFunding {
//State variable
address public owner;
uint public goal;
uint public deadline;
uint public totalFunds;
mapping(address => uint) public contributions;
event FundReceived(address indexed contributor, uint amount);
event GoalReached(uint totalFunds);
event FundsWithdrawn(address indexed owner, uint amount);
event RefundIssued(address indexed contributor, uint amount);
//Constructor to initialize the contract
constructor(uint _goal, uint _duration) {
owner = msg.sender;
goal = _goal;
deadline = block.timestamp + _duration;
}
// Modifier to restrict access to the owner
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can perform this action");
_;
}
// Function to contribute funds
function contribute() public payable {
require(block.timestamp < deadline, "Campaign has ended");
require(msg.value > 0, "Contribution must be greator than zero");
contributions[msg.sender] += msg.value;
totalFunds += msg.value;
emit FundReceived(msg.sender, msg.value);
if (totalFunds >= goal) {
emit GoalReached(totalFunds);
}
}
//Function to withdraw funds if the goal is met
function withdrawFunds() public onlyOwner {
require(totalFunds >= goal, "Goal not reached");
require(block.timestamp >= deadline, "Campaign is still ongoing");
uint amount = address(this).balance;
payable(owner).transfer(amount);
emit FundsWithdrawn(owner, amount);
}
// Function to issue refunds if the goal is not met
function refund() public {
require(block.timestamp >= deadline, "Campaign is still ongoing");
require(totalFunds < goal, "Goal was reached");
uint contributeAmount = contributions[msg.sender];
require(contributeAmount > 0, "No contributions to refund");
contributions[msg.sender] = 0;
payable (msg.sender).transfer(contributeAmount);
emit RefundIssued(msg.sender, contributeAmount);
}
}