-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowdfunding.sol
51 lines (48 loc) · 1.73 KB
/
crowdfunding.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
//crowdfunding
pragma solidity ^0.8.0;
//contract declaration
contract CrowdFunding{
address public creator;
ERC20 public tokenContract;
uint n = 1;
struct Campaign{
uint256 goal;
uint id;
uint256 duration;
uint256 deadline;
uint256 totalAmount;
mapping(address => uint256) contributions;
bool isOpen;
}
Campaign[] public campaigns;
constructor(address token){
creator = msg.sender;
tokenContract = ERC20(token);
}
function createCampaign(uint256 _goal,uint256 _duration) public{
campaigns.push(Campaign({
goal: _goal,
duration: _duration,
id: n++,
deadline: block.timestamp + _duration,
isOpen: true }));
}
function contribute(uint _id, uint256 amount) public{
campaigns[_id+1].totalAmount += amount;
}
function cancelContribution(uint _id) public{
require(campaigns[_id+1],"This camapign doesn't exist!!!")
require(campaigns[_id+1].isOpen = true,"The campaign is closed!!");
require(campaigns.[_id+1].contributions > 0,"You haven't contributed to this campaign!!!");
uint refund = campaigns[_id+1].contributions[msg.sender];
campaigns[_id+1].totalAmount -= refund;
campaigns[_id+1].contributions[msg.sender] = 0;
require(tokenContract.transfer(msg.sender,refund),"Transfer failed!!");
}
function withdrawFunds(uint _id){
require(msg.sender = creator,"You're not the creator of this Campaign!!");
require(campaigns[_id+1].totalAmount > campaigns[_id+1].goal,"Goal not met yet!!");
uint256 amount = campaigns[_id].totalAmount;
campaigns[_id].totalAmount = 0;
}
}