Created
May 27, 2018 11:04
-
-
Save dima91/719419b3c32a230fa9d17d883cecd989 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
This file contains 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
pragma solidity ^0.4.24; | |
import "./mortal.sol"; | |
contract Crowdfund { | |
struct Funder { | |
address addr; | |
uint amount; | |
} | |
struct Project { | |
bool created; | |
bool opened; | |
string name; | |
address owner; | |
mapping (uint => Funder) funders; | |
uint fundersSize; | |
uint amount; | |
uint fundingGoal; | |
} | |
event ProjectCreated (string _name, uint _fundingGoal); | |
event FundTransferred (address _backer, string _project, uint _amount, uint _remainingAmount); | |
event fundingGoalReached (string project); | |
address private owner; | |
mapping (string => Project) private projects; | |
modifier ProjectNotExists (string _name) { | |
require (projects[_name].created == false); | |
_; | |
} | |
modifier ProjectExists (string _name) { | |
require (projects[_name].created == true); | |
_; | |
} | |
constructor () public { | |
owner= msg.sender; | |
} | |
function createProject (string _projectName, uint _amount) public ProjectNotExists (_projectName) { | |
projects[_projectName]= Project (true, true, _projectName, msg.sender, 0, 0, _amount); | |
emit ProjectCreated (_projectName, _amount); | |
} | |
function sendMoney (string _projectName, uint _sum) public ProjectExists (_projectName) { | |
Project storage p= projects[_projectName]; | |
p.funders[p.fundersSize]= Funder (msg.sender, _sum); | |
p.fundersSize++; | |
p.amount += _sum; | |
uint remaining= p.fundingGoal - _sum; | |
emit FundTransferred (msg.sender, _projectName, _sum, remaining); | |
checkGoalReached (_projectName); | |
} | |
function checkGoalReached (string _projectName) private returns (bool) { | |
Project storage p= projects[_projectName]; | |
if (p.fundingGoal <= p.amount) { | |
emit fundingGoalReached (_projectName); | |
p.opened= false; | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains 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
pragma solidity ^0.4.24; | |
contract Mortal { | |
address owner; | |
constructor () public { | |
owner= msg.sender; | |
} | |
function kill () public { | |
if (msg.sender == owner) | |
selfdestruct (owner); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment