-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrowdFunding.sol
70 lines (51 loc) · 1.92 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract CrowdFunding {
struct Campaign {
address owner;
string title;
string description;
uint target;
uint deadline;
uint amountCollected;
string image;
address[] donators;
uint[] donations;
}
mapping(uint => Campaign) public campaigns;
uint public numberOfCampaigns;
function createCampaign(address _owner,string memory _title, string memory _description,uint _target,uint _deadline,string memory _image) public returns(uint) {
Campaign storage campaign = campaigns[numberOfCampaigns];
require(campaign.deadline < block.timestamp,"The deadline be a date in the future.");
campaign.owner = _owner;
campaign.title = _title;
campaign.description = _description;
campaign.target = _target;
campaign.deadline = _deadline;
campaign.amountCollected = 0;
campaign.image = _image;
numberOfCampaigns++;
return numberOfCampaigns - 1;
}
function donateToCampaign(uint _id) public payable {
uint amount = msg.value;
Campaign storage campaign = campaigns[_id];
campaign.donators.push(msg.sender);
campaign.donations.push(amount);
(bool sent,) = payable(campaign.owner).call{value:amount}("");
if(sent) {
campaign.amountCollected += amount;
}
}
function getDonators(uint _id) view public returns(address[] memory, uint[] memory) {
return (campaigns[_id].donators,campaigns[_id].donations);
}
function getCampaigns() public view returns(Campaign[] memory) {
Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);
for(uint i = 0; i < numberOfCampaigns; i++) {
Campaign storage item = campaigns[i];
allCampaigns[i] = item;
}
return allCampaigns;
}
}