-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrowdFunding.sol
54 lines (51 loc) · 1.93 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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract CrowddFunding {
struct Campaign {
address owner;
string title;
string description;
uint256 target;
uint256 deadline;
uint256 amountCollected;
string image;
address[] donators;
uint256[] donations;
}
mapping(uint256 => Campaign) public campaigns;
uint256 public noOfCampaigns = 0;
function createCampaign(address _owner,string memory _title,string memory _description,uint256 _target, uint256 _deadline, string memory _image) public returns (uint256) {
Campaign storage campaign = campaigns[noOfCampaigns];
require(campaign.deadline < block.timestamp, "date must be in future");
campaign.owner = _owner;
campaign.title = _title;
campaign.description = _description;
campaign.image = _image;
campaign.target = _target;
campaign.deadline = _deadline;
campaign.amountCollected = 0;
noOfCampaigns++;
return noOfCampaigns - 1;
}
function donateToCompaign(uint256 _id) public payable{
uint256 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 = campaign.amountCollected + amount;
}
}
function getDonators(uint256 _id) public view returns(address[] memory,uint256[] memory){
return (campaigns[_id].donators, campaigns[_id].donations);
}
function getCampaigns() public view returns(Campaign[] memory){
Campaign[] memory allCampaigns = new Campaign[](noOfCampaigns);
for(uint i=0;i<noOfCampaigns;i++){
Campaign storage item = campaigns[i];
allCampaigns[i] = item;
}
return allCampaigns;
}
}