-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrowdFunding.sol
66 lines (46 loc) · 1.64 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
pragma solidity >=0.6.0 < 0.7.0;
struct Funder{
address addr;
uint amount;
}
contract CrowdFunding{
struct Campaign {
address payable beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping(uint => Funder) funders;
}
uint numCampaigns;
mapping(uint => Campaign) campaigns;
// create a new campaign
function newCampaign(address payable beneficiary, uint goal)
public returns(uint campaignID) {
campaignID = numCampaigns++;
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
}
//contribute to the Campaign
function contribute(uint campaignID)
public payable {
Campaign storage c = campaigns[campaignID];
c.funders[c.numFunders++] = Funder(
{
addr: msg.sender,
amount: msg.value
}
);
c.amount +=msg.value;
}
//check fundingGoal has reached
function checkGoalReached(uint campaignID)
public returns(bool reached) {
Campaign storage c = campaigns[campaignID];
if(c.amount < c.fundingGoal)
return false;
//reached the goal
uint amount = c.amount;
c.amount = 0;
c.beneficiary.transfer(amount);
return true;
}
}