Guest User

Untitled

a guest
Oct 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. pragma solidity ^0.4.11;
  2.  
  3. contract CrowdFunding {
  4. // Defines a new type with two fields.
  5. struct Funder {
  6. address addr;
  7. uint amount;
  8. }
  9.  
  10. struct Campaign {
  11. address beneficiary;
  12. uint fundingGoal;
  13. uint numFunders;
  14. uint amount;
  15. mapping (uint => Funder) funders;
  16. }
  17.  
  18. uint numCampaigns;
  19. mapping (uint => Campaign) campaigns;
  20.  
  21. function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
  22. campaignID = numCampaigns++; // campaignID is return variable
  23. // Creates new struct and saves in storage. We leave out the mapping type.
  24. campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
  25. }
  26.  
  27. function contribute(uint campaignID) payable {
  28. Campaign storage c = campaigns[campaignID];
  29. // Creates a new temporary memory struct, initialised with the given values
  30. // and copies it over to storage.
  31. // Note that you can also use Funder(msg.sender, msg.value) to initialise.
  32. c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
  33. c.amount += msg.value;
  34. }
  35.  
  36. function checkGoalReached(uint campaignID) returns (bool reached) {
  37. Campaign storage c = campaigns[campaignID];
  38. if (c.amount < c.fundingGoal)
  39. return false;
  40. uint amount = c.amount;
  41. c.amount = 0;
  42. c.beneficiary.transfer(amount);
  43. return true;
  44. }
  45. }
  46.  
  47. > contract.newCampaign(eth.accounts[0], 100)
  48. "0x0122973cbcb7df227e8d625c6ee4a831d716b8b69398e4431ec14f89951fdc25"
  49.  
  50. > n_cont.checkGoalReached.call(0)
  51. true
  52. > n_cont.checkGoalReached.call(1)
  53. true
  54.  
  55. > contract.checkGoalReached(0, function(e, result) {console.log(result)})
  56. 0xe1bf7a275a62fb3bc2cdd12ff280cde5e9064ddbfeb51ba6713a713cd356c8cc
  57. undefined
  58. > contract.checkGoalReached(1, function(e, result){console.log(result)})
  59. 0x613c5973733cde57d4b7325bbab2d841d2fa6c95e371e77ca24a2f5a54768fd3
  60. undefined
Add Comment
Please, Sign In to add comment