-
Notifications
You must be signed in to change notification settings - Fork 387
updated tutorial contracts #29
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,61 @@ | ||
| contract token { | ||
| mapping (address => uint) public balance; | ||
| function token() {} | ||
| function sendToken(address receiver, uint amount) returns(bool sufficient) { } | ||
| } | ||
|
|
||
| contract CrowdSale { | ||
| contract token { mapping (address => uint) public coinBalanceOf; function token() {} function sendCoin(address receiver, uint amount) returns(bool sufficient) { } } | ||
|
|
||
| address public admin; | ||
| address public beneficiary; | ||
| uint public fundingGoal; | ||
| uint public amountRaised; | ||
| uint public deadline; | ||
| uint public price; | ||
| token public tokenReward; | ||
| Funder[] public funders; | ||
|
|
||
| /* data structure to hold information about campaign contributors */ | ||
| struct Funder { | ||
| address addr; | ||
| uint amount; | ||
| } | ||
|
|
||
| /* at initialization, setup the owner */ | ||
| function CrowdSale() { | ||
| admin = msg.sender; | ||
| } | ||
|
|
||
| function setup(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, address _reward) returns (bytes32 response){ | ||
| if (msg.sender == admin && !(beneficiary > 0 && fundingGoal > 0 && deadline > 0)) { | ||
| contract CrowdSale { | ||
|
|
||
| address public beneficiary; | ||
| uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; | ||
| token public tokenReward; | ||
| Funder[] public funders; | ||
| event FundTransfer(address backer, uint amount, bool isContribution); | ||
|
|
||
| /* data structure to hold information about campaign contributors */ | ||
| struct Funder { | ||
| address addr; | ||
| uint amount; | ||
| } | ||
|
|
||
| /* at initialization, setup the owner */ | ||
| function CrowdSale(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, address _reward) { | ||
| beneficiary = _beneficiary; | ||
| fundingGoal = _fundingGoal; | ||
| deadline = now + _duration * 1 days; | ||
| deadline = now + _duration * 1 minutes; | ||
| price = _price; | ||
| tokenReward = token(_reward); | ||
| return "campaign is set"; | ||
| } else if (msg.sender != admin) { | ||
| return "not authorized"; | ||
| } else { | ||
| return "campaign cannot be changed"; | ||
| } | ||
| } | ||
|
|
||
| /* The function without name is the default function that is called whenever anyone sends funds to a contract without specifying any extra data or if the data does not match any of the function signatures */ | ||
| function () returns (bytes32 response) { | ||
| if (msg.data.length != 0) return; | ||
| var numFunders = funders.length; | ||
| Funder f = funders[numFunders++]; | ||
| f.addr = msg.sender; | ||
| f.amount = msg.value; | ||
| amountRaised += f.amount; | ||
| tokenReward.sendToken(msg.sender, f.amount/price); | ||
| return "thanks for your contribution"; | ||
| } | ||
| tokenReward = token(_reward); | ||
| } | ||
|
|
||
| /* checks if the goal or time limit has been reached and ends the campaign */ | ||
| function checkGoalReached() returns (bytes32 response) { | ||
| if (amountRaised >= fundingGoal){ | ||
| beneficiary.send(amountRaised); | ||
| suicide(beneficiary); | ||
| /* The function without name is the default function that is called whenever anyone sends funds to a contract */ | ||
| function () { | ||
| Funder f = funders[funders.length++]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps more intuitive: |
||
| f.addr = msg.sender; | ||
| f.amount = msg.value; | ||
| amountRaised += f.amount; | ||
| tokenReward.sendCoin(msg.sender, f.amount/price); | ||
| FundTransfer(f.addr, f.amount, true); | ||
| if (now >= deadline) { | ||
| FundTransfer('0x00100000fe219aaaa8b1fe83adc99d59b807f6f9', 2, true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you check that this compiles?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah those fund transfer events where just added as a poor man debugging tool. Forgot to take them out |
||
| } else { | ||
| FundTransfer('0x00200000fe219aaaa8b1fe83adc99d59b807f6f9', 3, true); | ||
| } | ||
| } | ||
| else if (deadline <= block.number){ | ||
| for (uint i = 0; i < funders.length; i++) { | ||
| funders[i].addr.send(funders[i].amount); | ||
| funders[i].addr = 0; | ||
| funders[i].amount = 0; | ||
|
|
||
| modifier afterDeadline() { if (now >= deadline) _ } | ||
|
|
||
| /* checks if the goal or time limit has been reached and ends the campaign */ | ||
| function checkGoalReached() afterDeadline { | ||
| FundTransfer('0x00300000fe219aaaa8b1fe83adc99d59b807f6f9', 2, true); | ||
| if (amountRaised >= fundingGoal){ | ||
| FundTransfer('0x00400000fe219aaaa8b1fe83adc99d59b807f6f9', 1, false); | ||
| beneficiary.send(amountRaised); | ||
| FundTransfer(beneficiary, amountRaised, false); | ||
| } else { | ||
| FundTransfer(0, 11, false); | ||
| for (uint i = 0; i < funders.length; ++i) { | ||
| funders[i].addr.send(funders[i].amount); | ||
| FundTransfer(funders[i].addr, funders[i].amount, false); | ||
| } | ||
| } | ||
| FundTransfer('0x00500000fe219aaaa8b1fe83adc99d59b807f6f9', 111, false); | ||
| suicide(beneficiary); | ||
| return "Deadline passed"; | ||
| } | ||
| return "Not reached yet"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,92 +1,106 @@ | ||
| contract token{ | ||
| function sendToken(address receiver,uint256 amount)returns(bool sufficient){} | ||
| function getBalance(address account)returns(uint256 balance){} | ||
| } | ||
|
|
||
| contract Democracy { | ||
|
|
||
| uint public minimumQuorum = 10; | ||
| uint public debatingPeriod = 7 days; | ||
| token public voterShare; | ||
| address public founder; | ||
| Proposal[] public proposals; | ||
|
|
||
| struct Proposal { | ||
| address recipient; | ||
| uint amount; | ||
| bytes32 data; | ||
| bytes32 descriptionHash; | ||
| uint creationDate; | ||
| uint quorum; | ||
| bool active; | ||
| Vote[] votes; | ||
| mapping (address => bool) voted; | ||
| } | ||
|
|
||
| struct Vote { | ||
| int position; | ||
| address voter; | ||
| } | ||
|
|
||
| function Democracy() { | ||
| founder = msg.sender; | ||
| } | ||
|
|
||
| function setup(address _voterShareAddress){ | ||
| if (msg.sender == founder && proposals.length == 0) { | ||
| voterShare = token(_voterShareAddress); | ||
| } | ||
| } | ||
|
|
||
| contract token { mapping (address => uint) public coinBalanceOf; function token() { } function sendCoin(address receiver, uint amount) returns(bool sufficient) { } } | ||
|
|
||
|
|
||
| contract Democracy { | ||
|
|
||
| function newProposal(address _recipient, uint _amount, bytes32 _data, bytes32 _descriptionHash) returns (uint proposalID) { | ||
| if (voterShare.balances(msg.sender)>0) { | ||
| proposalID = proposals.length++; | ||
| Proposal p = proposals[proposalID]; | ||
| p.recipient = _recipient; | ||
| p.amount = _amount; | ||
| p.data = _data; | ||
| p.descriptionHash = _descriptionHash; | ||
| p.creationDate = now; | ||
| p.active = true; | ||
| } else { | ||
| return 0; | ||
| uint public minimumQuorum = 10; | ||
| uint public debatingPeriod = 7 minutes; | ||
| token public voterShare; | ||
| address public founder; | ||
| Proposal[] public proposals; | ||
| uint public numProposals; | ||
|
|
||
| event ProposalAdded(uint proposalID, address recipient, uint amount, bytes32 data, string description); | ||
| event Voted(uint proposalID, int position, address voter); | ||
| event ProposalTallied(uint proposalID, int result, uint quorum, bool active); | ||
| event LineCounter(uint line); /* This event should be taken out in the future */ | ||
|
|
||
| struct Proposal { | ||
| address recipient; | ||
| uint amount; | ||
| bytes32 data; | ||
| string description; | ||
| uint creationDate; | ||
| uint quorum; | ||
| bool active; | ||
| Vote[] votes; | ||
| mapping (address => bool) voted; | ||
| } | ||
| } | ||
|
|
||
| function vote(uint _proposalID, int _position) returns (uint voteID){ | ||
| if (voterShare.balances(msg.sender)>0 && (_position >= -1 || _position <= 1 )) { | ||
| Proposal p = proposals[_proposalID]; | ||
| if (p.voted[msg.sender] != true) { | ||
| voteID = p.votes.length++; | ||
| Vote v = p.votes[voteID]; | ||
| v.position = _position; | ||
| v.voter = msg.sender; | ||
| p.voted[msg.sender] = true; | ||
|
|
||
| struct Vote { | ||
| int position; | ||
| address voter; | ||
| } | ||
|
|
||
| function Democracy() { | ||
| founder = msg.sender; | ||
| } | ||
|
|
||
| function setup(address _voterShareAddress){ | ||
| if (msg.sender == founder && proposals.length == 0) { | ||
| voterShare = token(_voterShareAddress); | ||
| } | ||
| } | ||
|
|
||
| function newProposal(address _recipient, uint _amount, bytes32 _data, string _description) returns (uint proposalID) { | ||
| if (voterShare.coinBalanceOf(msg.sender)>0) { | ||
| proposalID = proposals.length++; | ||
| Proposal p = proposals[proposalID]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about initialising structs also applies here. |
||
| p.recipient = _recipient; | ||
| p.amount = _amount; | ||
| p.data = _data; | ||
| p.description = _description; | ||
| p.creationDate = now; | ||
| p.active = true; | ||
| ProposalAdded(proposalID, _recipient, _amount, _data, _description); | ||
| numProposals = proposalID+1; | ||
| } else { | ||
| return 0; | ||
| } | ||
| } else { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| function executeProposal(uint _proposalID) returns (int result) { | ||
| Proposal p = proposals[_proposalID]; | ||
| /* Check if debating period is over */ | ||
| if (now > (p.creationDate + debatingPeriod) && p.active){ | ||
|
|
||
| /* tally the votes */ | ||
| for (uint i = 0; i <= p.votes.length; i++) { | ||
| Vote v = p.votes[i]; | ||
| int voteWeight = int(voterShare.balances(v.voter)); | ||
| p.quorum += uint(voteWeight); | ||
| result += voteWeight * v.position; | ||
|
|
||
| function vote(uint _proposalID, int _position) returns (uint voteID){ | ||
| LineCounter(83); | ||
| if (voterShare.coinBalanceOf(msg.sender)>0 && (_position >= -1 || _position <= 1 )) { | ||
| LineCounter(85); | ||
| Proposal p = proposals[_proposalID]; | ||
| if (p.voted[msg.sender] != true) { | ||
| LineCounter(88); | ||
| voteID = p.votes.length++; | ||
| Vote v = p.votes[voteID]; | ||
| v.position = _position; | ||
| v.voter = msg.sender; | ||
| p.voted[msg.sender] = true; | ||
| Voted(_proposalID, _position, msg.sender); | ||
| } | ||
| } else { | ||
| LineCounter(97); | ||
| return 0; | ||
| } | ||
| /* execute result */ | ||
| if (p.quorum > minimumQuorum && result > 0 ) { | ||
| p.recipient.call.value(p.amount)(p.data); | ||
| p.active = false; | ||
| } else if (p.quorum > minimumQuorum && result < 0) { | ||
| p.active = false; | ||
| } | ||
|
|
||
| function executeProposal(uint _proposalID) returns (int result) { | ||
| Proposal p = proposals[_proposalID]; | ||
| /* Check if debating period is over */ | ||
| if (now > (p.creationDate + debatingPeriod) && p.active){ | ||
|
|
||
| /* tally the votes */ | ||
| for (uint i = 0; i <= p.votes.length; i++) { | ||
| Vote v = p.votes[i]; | ||
| int voteWeight = int(voterShare.coinBalanceOf(v.voter)); | ||
| p.quorum += uint(voteWeight); | ||
|
||
| result += voteWeight * v.position; | ||
| } | ||
| /* execute result */ | ||
| if (p.quorum > minimumQuorum && result > 0 ) { | ||
| p.recipient.call.value(p.amount)(p.data); | ||
| p.active = false; | ||
| } else if (p.quorum > minimumQuorum && result < 0) { | ||
| p.active = false; | ||
| } | ||
| } | ||
| ProposalTallied(_proposalID, result, p.quorum, p.active); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,24 @@ | ||
| contract greeter { | ||
| /* Declare variable admin which will store an address */ | ||
| address public admin; | ||
|
|
||
| /* this function is executed at initialization and sets the owner of the contract */ | ||
| function greeter() { | ||
| admin = msg.sender; | ||
| } | ||
|
|
||
| /* main function */ | ||
| function greet(bytes32 input) returns (bytes32) { | ||
| if (input == "") { return "Hello, World"; } | ||
| contract mortal { | ||
| address owner; | ||
|
|
||
| /* Try it yourself: the joker | ||
| if (input=="Who's there?") { | ||
| // insert a joke here | ||
| } else if (msg.value > 1000) { | ||
| // a trillionth of an ether. It's a cheap joke. | ||
| return "Knock knock!"; | ||
| } | ||
| */ | ||
| /* this function is executed at initialization and sets the owner of the contract */ | ||
| function mortal() { owner = msg.sender; } | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| /* Function to recover the funds on the contract */ | ||
| function kill() { | ||
| if (msg.sender == admin) { | ||
| suicide(admin); | ||
| } | ||
| } | ||
| function kill() { if (msg.sender == owner) suicide(owner); } | ||
| } | ||
|
|
||
| contract greeter is mortal { | ||
| string greeting; | ||
|
|
||
| /* this runs when the contract is executed */ | ||
| function greeter(string _greeting) public { | ||
| greeting = _greeting; | ||
| } | ||
|
|
||
| /* main function */ | ||
| function greet() constant returns (string) { | ||
| return greeting; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can directly use
token _reward, it will be converted toaddressfor the ABI.