Guest User

Untitled

a guest
Dec 12th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. pragma solidity ^0.4.24;
  2.  
  3. import "./IERC20.sol";
  4. import "./SafeMath.sol";
  5.  
  6. contract Crowdsale {
  7. using SafeMath for uint256;
  8.  
  9. uint256 private cap; // maximum amount of ether to be raised
  10. uint256 private weiRaised; // current amount of wei raised
  11.  
  12. uint256 private rate; // price in wei per smallest unit of token (e.g. 1 wei = 10 smallet unit of a token)
  13. address private wallet; // wallet to hold the ethers
  14. IERC20 private token; // address of erc20 tokens
  15.  
  16. /**
  17. * Event for token purchase logging
  18. * @param purchaser who paid for the tokens
  19. * @param beneficiary who got the tokens
  20. * @param value weis paid for purchase
  21. * @param amount amount of tokens purchased
  22. */
  23. event TokensPurchased(
  24. address indexed purchaser,
  25. address indexed beneficiary,
  26. uint256 value,
  27. uint256 amount
  28. );
  29.  
  30. // -----------------------------------------
  31. // Public functions (DO NOT change the interface!)
  32. // -----------------------------------------
  33. /**
  34. * @param _rate Number of token units a buyer gets per wei
  35. * @dev The rate is the conversion between wei and the smallest and indivisible token unit.
  36. * @param _wallet Address where collected funds will be forwarded to
  37. * @param _token Address of the token being sold
  38. */
  39. constructor(uint256 _rate, address _wallet, IERC20 _token, uint256 _cap) public {
  40. // TODO: Your Code Here
  41. require(_rate > 0);
  42. require(_wallet != address(0));
  43. require(_token != address(0));
  44. require(_cap > 0);
  45.  
  46. rate = _rate;
  47. wallet = _wallet;
  48. token = _token;
  49. cap = _cap;
  50. }
  51.  
  52. /**
  53. * @dev Fallback function for users to send ether directly to contract address
  54. */
  55. function() external payable {
  56. // TODO: Your Code Here
  57. buyTokens(msg.sender);
  58. }
  59.  
  60. function buyTokens(address beneficiary) public payable {
  61. // Below are some general steps that should be done.
  62. // You need to decide the right order to do them in.
  63. // - Validate any conditions
  64. // - Calculate number of tokens
  65. // - Update any states
  66. // - Transfer tokens and emit event
  67. // - Forward funds to wallet
  68.  
  69. // TODO: Your Code Here
  70. uint256 weiAmount = msg.value;
  71. _preValidatePurchase(beneficiary, weiAmount);
  72.  
  73. // calculate token amount to be created
  74. uint256 tokens = _getTokenAmount(weiAmount);
  75.  
  76. // update state
  77. weiRaised = weiRaised.add(weiAmount);
  78.  
  79. _processPurchase(beneficiary, tokens);
  80. emit TokensPurchased(
  81. msg.sender,
  82. beneficiary,
  83. weiAmount,
  84. tokens
  85. );
  86.  
  87. _forwardFunds();
  88.  
  89. }
  90.  
  91. /**
  92. * @dev Checks whether the cap has been reached.
  93. * @return Whether the cap was reached
  94. */
  95. function capReached() public view returns (bool) {
  96. // TODO: Your Code Here
  97. return weiRaised >= cap;
  98. }
  99.  
  100. // -----------------------------------------
  101. // Internal functions (you can write any other internal helper functions here)
  102. // -----------------------------------------
  103. /**
  104. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use `super` in contracts that inherit from Crowdsale to extend their validations.
  105. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  106. * super._preValidatePurchase(beneficiary, weiAmount);
  107. * require(weiRaised().add(weiAmount) <= cap);
  108. * @param beneficiary Address performing the token purchase
  109. * @param weiAmount Value in wei involved in the purchase
  110. */
  111. function _preValidatePurchase(
  112. address beneficiary,
  113. uint256 weiAmount
  114. )
  115. view internal
  116. {
  117. require(beneficiary != address(0));
  118. require(weiAmount != 0);
  119. require(weiRaised.add(weiAmount) <= cap);
  120. }
  121.  
  122. /**
  123. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  124. * @param beneficiary Address performing the token purchase
  125. * @param tokenAmount Number of tokens to be emitted
  126. */
  127. function _processPurchase(
  128. address beneficiary,
  129. uint256 tokenAmount
  130. )
  131. internal
  132. {
  133. token.transfer(beneficiary, tokenAmount);
  134. }
  135.  
  136. /**
  137. * @dev Override to extend the way in which ether is converted to tokens.
  138. * @param weiAmount Value in wei to be converted into tokens
  139. * @return Number of tokens that can be purchased with the specified _weiAmount
  140. */
  141. function _getTokenAmount(uint256 weiAmount)
  142. internal view returns (uint256)
  143. {
  144. return weiAmount.mul(rate);
  145. }
  146.  
  147. /**
  148. * @dev Determines how ETH is stored/forwarded on purchases.
  149. */
  150. function _forwardFunds() internal {
  151. wallet.transfer(msg.value);
  152. }
  153.  
  154. }
Add Comment
Please, Sign In to add comment