Last active
October 6, 2017 13:33
-
-
Save vincentlg/2457265050e6c7cc8ffe51ec5efe59ff to your computer and use it in GitHub Desktop.
simplified crowdfunding solidity
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.8; | |
contract Crowdfunding { | |
enum Stages { | |
Initialization, | |
CampaignInprogress, | |
CampaignFailure, | |
CampaignSuccess | |
} | |
enum Actors { | |
Contributor | |
} | |
mapping (address => uint) public contributorList; | |
// current stage | |
Stages public stage = Stages.Initialization; | |
// contract creator is beneficiary | |
address public beneficiary; | |
// (in wei) | |
uint256 public fundingGoal; | |
// (in wei) | |
uint256 public amountRaised; | |
// the current campaign expiry (future block number) | |
uint256 public expiry; | |
// the contributions data store, where all contributions are notated | |
address[] public contributorsList; | |
// all contribution a specific sender | |
mapping(address => uint256) public contributions; | |
// the contract constructor | |
function Campaign(uint256 _fundingGoal, uint256 _expiry) public { | |
expiry = _expiry; | |
fundingGoal = _fundingGoal; | |
beneficiary = msg.sender; | |
contributorList[0xADc997Fe0Bf47f50df2F2AA56d042ee4ab74cc11] = 1; | |
from_initialisation_to_campaigninprogress(); | |
} | |
function from_initialisation_to_campaigninprogress() { | |
if (block.number < expiry && amountRaised < fundingGoal) { | |
stage = Stages.CampaignInprogress; | |
} | |
} | |
function contribute() payable atStage(Stages.CampaignInprogress) onlyContributor { | |
if (msg.value == 0) { | |
throw; | |
} | |
bool isKwnownContributor = (contributions[msg.sender] > 0); | |
if(!isKwnownContributor) | |
{ | |
contributorsList.push(msg.sender); | |
} | |
contributions[msg.sender] += msg.value; | |
amountRaised += msg.value; | |
from_campaigninprogress_to_campaignsuccess(); | |
from_campaigninprogress_to_campaignfailure(); | |
} | |
function from_campaigninprogress_to_campaignsuccess() { | |
//if(block.number >= expiry && amountRaised >= fundingGoal) { | |
if(amountRaised >= fundingGoal) { | |
stage = Stages.CampaignSuccess; | |
payout(); | |
} | |
} | |
function from_campaigninprogress_to_campaignfailure() { | |
if(block.number >= expiry && amountRaised < fundingGoal) { | |
stage = Stages.CampaignFailure; | |
refundcontributors(); | |
} | |
} | |
function payout() atStage(Stages.CampaignSuccess) internal{ | |
// send funds to the benerifiary | |
if (!beneficiary.call.value(this.balance)()) { | |
throw; | |
} | |
} | |
function refundcontributors() atStage(Stages.CampaignFailure) internal{ | |
for (var i = 0; i < contributorsList.length; i++) { | |
address contrib = contributorsList[i]; | |
contrib.send(contributions[contrib]); | |
} | |
} | |
// check the campaign state | |
modifier atStage(Stages _expectedStage) { | |
require(stage == _expectedStage); | |
_; | |
} | |
modifier onlyContributor { | |
require(contributorList[msg.sender] != 0); | |
_; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
remove unused actors