Created
May 7, 2020 11:08
-
-
Save emmaodia/9866843961157bfcc1e401d7e70108a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author: @emma_odia on Twitter | |
// We will be using Solidity version 0.5.4 | |
pragma solidity ^0.6.0; | |
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; | |
contract CommunityHealthSupportDapp { | |
using SafeMath for uint256; | |
// List of existing projects | |
Project[] private projects; | |
// Event that will be emitted whenever a new project is started | |
event ProjectStarted( | |
address contractAddress, | |
address projectStarter, | |
string projectTitle, | |
string projectDesc, | |
string email, | |
uint256 phone, | |
uint256 goalAmount | |
); | |
function startProject( | |
string calldata title, | |
string calldata description, | |
string calldata email, | |
uint phone, | |
uint amountToRaise | |
) external { | |
Project newProject = new Project(msg.sender, title, description, email, phone, amountToRaise); | |
projects.push(newProject); | |
emit ProjectStarted( | |
address(newProject), | |
msg.sender, | |
title, | |
description, | |
email, | |
phone, | |
amountToRaise | |
); | |
} | |
function returnAllProjects() external view returns(Project[] memory){ | |
return projects; | |
} | |
} | |
contract Project { | |
using SafeMath for uint256; | |
// Data structures | |
enum State { | |
Fundraising, | |
Successful | |
} | |
// State variables | |
address payable public creator; | |
uint public amountGoal; // required to reach at least this much, else everyone gets refund | |
uint public completeAt; | |
uint256 public currentBalance; | |
uint public raiseBy; | |
string public title; | |
string public description; | |
string public email; | |
uint256 public phone; | |
State public state = State.Fundraising; // initialize on create | |
mapping (address => uint) public contributions; | |
// Event that will be emitted whenever funding will be received | |
event FundingReceived(address contributor, uint amount, uint currentTotal); | |
// Event that will be emitted whenever the project starter has received the funds | |
event CreatorPaid(address recipient); | |
// Modifier to check current state | |
modifier inState(State _state) { | |
require(state == _state); | |
_; | |
} | |
// Modifier to check if the function caller is the project creator | |
modifier isCreator() { | |
require(msg.sender == creator); | |
_; | |
} | |
constructor | |
( | |
address payable projectStarter, | |
string memory projectTitle, | |
string memory projectDesc, | |
string memory projectEmail, | |
uint projectPhone, | |
uint goalAmount | |
) public { | |
creator = projectStarter; | |
title = projectTitle; | |
description = projectDesc; | |
email = projectEmail; | |
phone = projectPhone; | |
amountGoal = goalAmount; | |
currentBalance = 0; | |
} | |
function contribute() external inState(State.Fundraising) payable { | |
require(msg.sender != creator); | |
contributions[msg.sender] = contributions[msg.sender].add(msg.value); | |
currentBalance = currentBalance.add(msg.value); | |
emit FundingReceived(msg.sender, msg.value, currentBalance); | |
} | |
function payOut() internal inState(State.Successful) returns (bool) { | |
uint256 totalRaised = currentBalance; | |
currentBalance = 0; | |
if (creator.send(totalRaised)) { | |
emit CreatorPaid(creator); | |
return true; | |
} else { | |
currentBalance = totalRaised; | |
state = State.Successful; | |
} | |
return false; | |
} | |
function getDetails() public view returns | |
( | |
address payable projectStarter, | |
string memory projectTitle, | |
string memory projectDesc, | |
string memory projectEmail, | |
uint256 projectPhone, | |
State currentState, | |
uint256 currentAmount, | |
uint256 goalAmount | |
) { | |
projectStarter = creator; | |
projectTitle = title; | |
projectDesc = description; | |
projectEmail = email; | |
projectPhone = phone; | |
currentState = state; | |
currentAmount = currentBalance; | |
goalAmount = amountGoal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment