Skip to content

Commit 2aa635b

Browse files
committed
added a crowd funding project
1 parent b5d0ece commit 2aa635b

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

CrowdFunding.sol

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity >0.5.0 <= 0.9.0;
4+
5+
contract CrowdFunding{
6+
mapping(address => uint) contributors;
7+
address public manager;
8+
uint public minimumContribution;
9+
uint public deadline;
10+
uint public target;
11+
uint public raisedAmount;
12+
uint public noOfContributors;
13+
14+
struct Request{
15+
string description;
16+
address payable recipient;
17+
uint value;
18+
bool completed; // to check the transaction is completed or not
19+
uint noOfVoters; // how many people voted towards it
20+
mapping(address => bool)voters; // this is like voter list remember festival example for this
21+
}
22+
mapping(uint => Request) requests;
23+
uint public numRequest;
24+
25+
constructor(uint _target,uint _deadline)public{
26+
target = _target;
27+
manager = msg.sender;
28+
deadline = block.timestamp + _deadline;
29+
minimumContribution = 100 wei;
30+
}
31+
32+
// users will transfer the ether to contract
33+
34+
function transferEth() payable public{
35+
require(block.timestamp <= deadline,"You have already crossed deadline");
36+
require(msg.value > minimumContribution,"Invalid Funds");
37+
38+
if(contributors[msg.sender] == 0){
39+
noOfContributors++; // checking the user is donating for 1st time
40+
}
41+
42+
raisedAmount = raisedAmount + msg.value;
43+
contributors[msg.sender] = contributors[msg.sender] + msg.value;
44+
}
45+
46+
function getBalance() public view returns(uint){
47+
return address(this).balance;
48+
}
49+
50+
function refund() public{
51+
require(block.timestamp > deadline && raisedAmount < target,"You cannot get refund");
52+
require(contributors[msg.sender] > 0,"You have not contributed");
53+
54+
address payable user = payable(msg.sender);
55+
user.transfer(contributors[msg.sender]); // transferring the money
56+
57+
contributors[msg.sender] = 0; // resetting the amount to 0
58+
}
59+
60+
modifier onlyManager(){
61+
require(msg.sender == manager,"you are not authorised person");
62+
_;
63+
}
64+
65+
// creating the request OR generating the request
66+
function createRequest(string memory _description,address payable _recipient,uint _value) public onlyManager{
67+
Request storage newRequest = requests[numRequest];
68+
69+
numRequest++;
70+
71+
newRequest.description = _description;
72+
newRequest.recipient = _recipient;
73+
newRequest.value = _value;
74+
newRequest.completed = false;
75+
newRequest.noOfVoters = 0;
76+
}
77+
78+
function voteRequest(uint _requestNo) public{
79+
require(contributors[msg.sender] > 0,"You have not contributed the money");
80+
Request storage currRequest = requests[_requestNo];
81+
82+
// checking the voter is not voting twice
83+
currRequest.voters[msg.sender] = true;
84+
currRequest.noOfVoters++;
85+
}
86+
87+
function makePayment(uint _requestNo) public onlyManager{
88+
Request storage currRequest = requests[_requestNo];
89+
90+
require(currRequest.completed == false,"Payment has been done already");
91+
92+
// calculating the majority
93+
94+
require(currRequest.noOfVoters > (noOfContributors / 2),"No majority");
95+
currRequest.recipient.transfer(currRequest.value);
96+
97+
}
98+
}

0 commit comments

Comments
 (0)