Skip to content

Instantly share code, notes, and snippets.

@rob-Hitchens
Last active September 6, 2017 06:54
Show Gist options
  • Select an option

  • Save rob-Hitchens/c2028490fccf6a02751b0970bea6efac to your computer and use it in GitHub Desktop.

Select an option

Save rob-Hitchens/c2028490fccf6a02751b0970bea6efac to your computer and use it in GitHub Desktop.
MODULE 2 - COMPLETE
///////////////////////////////////////////////////////////
// For training purposes.
// Solidity Contract Factory
// MODULE 2 COMPLETED
// Copyright (c) 2017, Rob Hitchens, all rights reserved.
// Not suitable for actual use
//////////////////////////////////////////////////////////
pragma solidity ^0.4.6;
contract Campaign {
address public owner;
uint public deadline;
uint public goal;
uint public fundsRaised;
struct FunderStruct {
uint amountContributed;
uint amountRefunded;
}
mapping (address => FunderStruct) public funderStructs;
event LogContribution(address sender, uint amount);
event LogRefundSent(address funder, uint amount);
event LogWithdrawal(address beneficiary, uint amount);
function Campaign(uint campaignDuration, uint campaignGoal) {
owner = msg.sender;
deadline = block.number + campaignDuration;
goal = campaignGoal;
}
function isSuccess()
public
constant
returns(bool isIndeed)
{
return(fundsRaised >= goal);
}
function hasFailed()
public
constant
returns(bool hasIndeed)
{
return(fundsRaised < goal && block.number > deadline);
}
function contribute()
public
payable
returns(bool success)
{
if(msg.value==0) throw;
if(isSuccess()) throw;
if(hasFailed()) throw;
fundsRaised += msg.value;
funderStructs[msg.sender].amountContributed += msg.value;
LogContribution(msg.sender, msg.value);
return true;
}
function withdrawFunds()
public
returns(bool success)
{
if(msg.sender != owner) throw;
if(!isSuccess()) throw;
uint amount = this.balance;
if(!owner.send(amount)) throw;
LogWithdrawal(owner, amount);
return true;
}
function requestRefund()
public
returns(bool success)
{
uint amountOwed = funderStructs[msg.sender].amountContributed - funderStructs[msg.sender].amountRefunded;
if(amountOwed == 0) throw;
if(!hasFailed()) throw;
funderStructs[msg.sender].amountRefunded += amountOwed;
if(!msg.sender.send(amountOwed)) throw;
LogRefundSent(msg.sender, amountOwed);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment