Last active
December 18, 2017 18:18
-
-
Save rob-Hitchens/d2bfb7f9506d4601111375d8f09d901d to your computer and use it in GitHub Desktop.
MODULE 4 COMPLETE
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
h3 { | |
font-weight:; normal; | |
color: #777; | |
font-size: 24px; | |
} | |
.black { | |
color: black; | |
} | |
.red { | |
color: red; | |
} | |
.green { | |
color: green; | |
} |
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
////////////////////////////////////////////////////////// | |
// For training purposes. | |
// Solidity Contract Factory | |
// Module 4 | |
// Copyright (c) 2017, Rob Hitchens, all rights reserved. | |
// Not suitable for actual use | |
////////////////////////////////////////////////////////// | |
pragma solidity ^0.4.6; | |
import "Stoppable.sol"; | |
contract Campaign is Stoppable { | |
address public sponsor; | |
uint public deadline; | |
uint public goal; | |
uint public fundsRaised; | |
struct FunderStruct { | |
uint amountContributed; | |
uint amountRefunded; | |
} | |
mapping (address => FunderStruct) public funderStructs; | |
modifier onlySponsor { | |
if(msg.sender != sponsor) throw; | |
_; | |
} | |
event LogContribution(address sender, uint amount); | |
event LogRefundSent(address funder, uint amount); | |
event LogWithdrawal(address beneficiary, uint amount); | |
function Campaign(address campaignSponsor, uint campaignDuration, uint campaignGoal) { | |
sponsor = campaignSponsor; | |
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 | |
onlyIfRunning | |
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() | |
onlySponsor | |
onlyIfRunning | |
returns(bool success) | |
{ | |
if(!isSuccess()) throw; | |
uint amount = this.balance; | |
if(!sponsor.send(amount)) throw; | |
LogWithdrawal(sponsor, amount); | |
return true; | |
} | |
function requestRefund() | |
public | |
onlyIfRunning | |
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; | |
} | |
} |
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
////////////////////////////////////////////////////////// | |
// For training purposes. | |
// Solidity Contract Factory | |
// Module 4 | |
// Copyright (c) 2017, Rob Hitchens, all rights reserved. | |
// Not suitable for actual use | |
////////////////////////////////////////////////////////// | |
pragma solidity ^0.4.6; | |
import "Campaign.sol"; | |
contract Hub is Stoppable { | |
address[] public campaigns; | |
mapping(address => bool) campaignExists; | |
modifier onlyIfCampaign(address campaign) { | |
if(campaignExists[campaign] != true) throw; | |
_; | |
} | |
event LogNewCampaign(address sponsor, address campaign, uint duration, uint goal); | |
event LogCampaignStopped(address sender, address campaign); | |
event LogCampaignStarted(address sender, address campaign); | |
event LogCampaignNewOwner(address sender, address campaign, address newOwner); | |
function getCampaignCount() | |
public | |
constant | |
returns(uint campaignCount) | |
{ | |
return campaigns.length; | |
} | |
function newCampaign(uint campaignDuration, uint campaignGoal) | |
public | |
returns(address campaignContract) | |
{ | |
Campaign trustedCampaign = new Campaign(msg.sender,campaignDuration, campaignGoal); | |
campaigns.push(trustedCampaign); | |
campaignExists[trustedCampaign] = true; | |
LogNewCampaign(msg.sender, trustedCampaign, campaignDuration, campaignGoal); | |
return trustedCampaign; | |
} | |
// Pass-through Admin Controls | |
function stopCampaign(address campaign) | |
onlyOwner | |
onlyIfCampaign(campaign) | |
returns(bool success) | |
{ | |
Campaign trustedCampaign = Campaign(campaign); | |
LogCampaignStopped(msg.sender, campaign); | |
return(trustedCampaign.runSwitch(false)); | |
} | |
function startCampaign(address campaign) | |
onlyOwner | |
onlyIfCampaign(campaign) | |
returns(bool success) | |
{ | |
Campaign trustedCampaign = Campaign(campaign); | |
LogCampaignStarted(msg.sender, campaign); | |
return(trustedCampaign.runSwitch(true)); | |
} | |
function changeCampaignOwner(address campaign, address newOwner) | |
onlyOwner | |
onlyIfCampaign(campaign) | |
returns(bool success) | |
{ | |
Campaign trustedCampaign = Campaign(campaign); | |
LogCampaignNewOwner(msg.sender, campaign, newOwner); | |
return(trustedCampaign.changeOwner(newOwner)); | |
} | |
} |
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
////////////////////////////////////////////////////////// | |
// For training purposes. | |
// Solidity Contract Factory | |
// Module 4 | |
// Copyright (c) 2017, Rob Hitchens, all rights reserved. | |
// Not suitable for actual use | |
////////////////////////////////////////////////////////// | |
pragma solidity ^0.4.6; | |
contract Owned { | |
address public owner; | |
event LogNewOwner(address sender, address oldOwner, address newOwner); | |
modifier onlyOwner { | |
if(msg.sender != owner) throw; | |
_; | |
} | |
function Owned() { | |
owner = msg.sender; | |
} | |
function changeOwner(address newOwner) | |
onlyOwner | |
returns(bool success) | |
{ | |
if(newOwner == 0) throw; | |
LogNewOwner(msg.sender, owner, newOwner); | |
owner = newOwner; | |
return true; | |
} | |
} |
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
////////////////////////////////////////////////////////// | |
// For training purposes. | |
// Solidity Contract Factory | |
// Module 4 | |
// Copyright (c) 2017, Rob Hitchens, all rights reserved. | |
// Not suitable for actual use | |
////////////////////////////////////////////////////////// | |
pragma solidity ^0.4.6; | |
import "Owned.sol"; | |
contract Stoppable is Owned { | |
bool public running; | |
event LogRunSwitch(address sender, bool switchSetting); | |
modifier onlyIfRunning { | |
if(!running) throw; | |
_; | |
} | |
function Stoppable() { | |
running = true; | |
} | |
function runSwitch(bool onOff) | |
onlyOwner | |
returns(bool success) | |
{ | |
running = onOff; | |
LogRunSwitch(msg.sender, onOff); | |
return true; | |
} | |
} |
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
var DefaultBuilder = require("truffle-default-builder"); | |
module.exports = { | |
build: new DefaultBuilder({ | |
"index.html": "index.html", | |
"app.js": [ | |
"javascripts/_vendor/angular.min.js", | |
"javascripts/app.js" | |
], | |
"app.css": [ | |
"stylesheets/app.css" | |
], | |
"images/": "images/" | |
}), | |
networks: { | |
development: { | |
host: "localhost", | |
port: 8545, | |
network_id: "*" // Match any network id | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment