Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.11;
- contract CrowdFunding {
- // Defines a new type with two fields.
- struct Funder {
- address addr;
- uint amount;
- }
- struct Campaign {
- address beneficiary;
- uint fundingGoal;
- uint numFunders;
- uint amount;
- mapping (uint => Funder) funders;
- }
- uint numCampaigns;
- mapping (uint => Campaign) campaigns;
- function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
- campaignID = numCampaigns++; // campaignID is return variable
- // Creates new struct and saves in storage. We leave out the mapping type.
- campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
- }
- function contribute(uint campaignID) payable {
- Campaign storage c = campaigns[campaignID];
- // Creates a new temporary memory struct, initialised with the given values
- // and copies it over to storage.
- // Note that you can also use Funder(msg.sender, msg.value) to initialise.
- c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
- c.amount += msg.value;
- }
- function checkGoalReached(uint campaignID) returns (bool reached) {
- Campaign storage c = campaigns[campaignID];
- if (c.amount < c.fundingGoal)
- return false;
- uint amount = c.amount;
- c.amount = 0;
- c.beneficiary.transfer(amount);
- return true;
- }
- }
- > contract.newCampaign(eth.accounts[0], 100)
- "0x0122973cbcb7df227e8d625c6ee4a831d716b8b69398e4431ec14f89951fdc25"
- > n_cont.checkGoalReached.call(0)
- true
- > n_cont.checkGoalReached.call(1)
- true
- > contract.checkGoalReached(0, function(e, result) {console.log(result)})
- 0xe1bf7a275a62fb3bc2cdd12ff280cde5e9064ddbfeb51ba6713a713cd356c8cc
- undefined
- > contract.checkGoalReached(1, function(e, result){console.log(result)})
- 0x613c5973733cde57d4b7325bbab2d841d2fa6c95e371e77ca24a2f5a54768fd3
- undefined
Add Comment
Please, Sign In to add comment