-
Notifications
You must be signed in to change notification settings - Fork 0
/
fundings.sol
81 lines (64 loc) · 2.18 KB
/
fundings.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
78
79
80
81
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract CrowdFunding {
struct Campaign {
address owner;
string title;
string description;
uint256 targetAmount;
uint256 deadline;
uint256 amountCollected;
string image;
address[] donaters;
uint256[] donations;
}
mapping(uint256 => Campaign) public campaigns;
uint256 public numberOfCampaigns = 0;
function createCampaigns(
address _owner,
string memory _title,
string memory _description,
uint256 _targetAmount,
uint256 _deadline,
string memory _image
) public returns (uint256) {
Campaign storage campaign = campaigns[numberOfCampaigns];
// is everythings is okay
require(campaign.deadline < block.timestamp, "deadline reached :(");
campaign.owner = _owner;
campaign.title = _title;
campaign.description = _description;
campaign.targetAmount = _targetAmount;
campaign.deadline = _deadline;
campaign.amountCollected = 0;
campaign.image = _image;
numberOfCampaigns++;
return numberOfCampaigns;
}
function donateToCampaign(uint256 _id) public payable {
uint256 amount = msg.value;
Campaign storage campaign = campaigns[_id];
campaign.donaters.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].donaters, campaigns[_id].donations);
}
function getCampaigns() public view returns (Campaign[] memory) {
// making an array of struct with lenght of number of campaign
Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);
for (uint256 i = 0; i < numberOfCampaigns; i++) {
Campaign storage item = campaigns[i];
allCampaigns[i] = item;
}
return allCampaigns;
}
}