Guest User

Untitled

a guest
May 26th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. pragma solidity ^0.4.21;
  2.  
  3. contract Crowdsale {
  4. using SafeMath for uint256;
  5.  
  6. // The token being sold
  7. ERC20 public token;
  8.  
  9. // Address where funds are collected
  10. address public wallet;
  11.  
  12. // How many token units a buyer gets per wei
  13. uint256 public rate;
  14.  
  15. // Amount of wei raised
  16. uint256 public weiRaised;
  17.  
  18. /**
  19. * Event for token purchase logging
  20. * @param purchaser who paid for the tokens
  21. * @param beneficiary who got the tokens
  22. * @param value weis paid for purchase
  23. * @param amount amount of tokens purchased
  24. */
  25. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  26.  
  27. /**
  28. * @param _rate Number of token units a buyer gets per wei
  29. * @param _wallet Address where collected funds will be forwarded to
  30. * @param _token Address of the token being sold
  31. */
  32. constructor(uint256 _rate, address _wallet, ERC20 _token) public {
  33. require(_rate > 0);
  34. require(_wallet != address(0));
  35. require(_token != address(0));
  36.  
  37. rate = _rate;
  38. wallet = _wallet;
  39. token = _token;
  40. }
  41.  
  42. // -----------------------------------------
  43. // Crowdsale external interface
  44. // -----------------------------------------
  45.  
  46. /**
  47. * @dev fallback function ***DO NOT OVERRIDE***
  48. */
  49. function () external payable {
  50. buyTokens(msg.sender);
  51. }
  52.  
  53. /**
  54. * @dev low level token purchase ***DO NOT OVERRIDE***
  55. * @param _beneficiary Address performing the token purchase
  56. */
  57. function buyTokens(address _beneficiary) public payable {
  58.  
  59. uint256 weiAmount = msg.value;
  60. _preValidatePurchase(_beneficiary, weiAmount);
  61.  
  62. // calculate token amount to be created
  63. uint256 tokens = _getTokenAmount(weiAmount);
  64.  
  65. // update state
  66. weiRaised = weiRaised.add(weiAmount);
  67.  
  68. _processPurchase(_beneficiary, tokens);
  69. emit TokenPurchase(
  70. msg.sender,
  71. _beneficiary,
  72. weiAmount,
  73. tokens
  74. );
  75.  
  76. _updatePurchasingState(_beneficiary, weiAmount);
  77.  
  78. _forwardFunds();
  79. _postValidatePurchase(_beneficiary, weiAmount);
  80. }
  81.  
  82. // -----------------------------------------
  83. // Internal interface (extensible)
  84. // -----------------------------------------
  85.  
  86. /**
  87. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
  88. * @param _beneficiary Address performing the token purchase
  89. * @param _weiAmount Value in wei involved in the purchase
  90. */
  91. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  92. require(_beneficiary != address(0));
  93. require(_weiAmount != 0);
  94. }
  95.  
  96. /**
  97. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  98. * @param _beneficiary Address performing the token purchase
  99. * @param _weiAmount Value in wei involved in the purchase
  100. */
  101. function _postValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  102. // optional override
  103. }
  104.  
  105. /**
  106. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  107. * @param _beneficiary Address performing the token purchase
  108. * @param _tokenAmount Number of tokens to be emitted
  109. */
  110. function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {
  111. token.transfer(_beneficiary, _tokenAmount);
  112. }
  113.  
  114. /**
  115. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  116. * @param _beneficiary Address receiving the tokens
  117. * @param _tokenAmount Number of tokens to be purchased
  118. */
  119. function _processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
  120. _deliverTokens(_beneficiary, _tokenAmount);
  121. }
  122.  
  123. /**
  124. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  125. * @param _beneficiary Address receiving the tokens
  126. * @param _weiAmount Value in wei involved in the purchase
  127. */
  128. function _updatePurchasingState(address _beneficiary, uint256 _weiAmount) internal {
  129. // optional override
  130. }
  131.  
  132. /**
  133. * @dev Override to extend the way in which ether is converted to tokens.
  134. * @param _weiAmount Value in wei to be converted into tokens
  135. * @return Number of tokens that can be purchased with the specified _weiAmount
  136. */
  137. function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
  138. return _weiAmount.mul(rate);
  139. }
  140.  
  141. /**
  142. * @dev Determines how ETH is stored/forwarded on purchases.
  143. */
  144. function _forwardFunds() internal {
  145. wallet.transfer(msg.value);
  146. }
  147. }
  148.  
  149.  
  150. library SafeMath {
  151.  
  152.  
  153. function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
  154. if (a == 0) {
  155. return 0;
  156. }
  157. c = a * b;
  158. assert(c / a == b);
  159. return c;
  160. }
  161.  
  162.  
  163. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  164. // assert(b > 0); // Solidity automatically throws when dividing by 0
  165. // uint256 c = a / b;
  166. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  167. return a / b;
  168. }
  169.  
  170.  
  171. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  172. assert(b <= a);
  173. return a - b;
  174. }
  175.  
  176.  
  177. function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
  178. c = a + b;
  179. assert(c >= a);
  180. return c;
  181. }
  182. }
  183.  
  184.  
  185. library Math {
  186. function max64(uint64 a, uint64 b) internal pure returns (uint64) {
  187. return a >= b ? a : b;
  188. }
  189.  
  190. function min64(uint64 a, uint64 b) internal pure returns (uint64) {
  191. return a < b ? a : b;
  192. }
  193.  
  194. function max256(uint256 a, uint256 b) internal pure returns (uint256) {
  195. return a >= b ? a : b;
  196. }
  197.  
  198. function min256(uint256 a, uint256 b) internal pure returns (uint256) {
  199. return a < b ? a : b;
  200. }
  201. }
  202.  
  203. contract ERC20Basic {
  204. function totalSupply() public view returns (uint256);
  205. function balanceOf(address who) public view returns (uint256);
  206. function transfer(address to, uint256 value) public returns (bool);
  207. event Transfer(address indexed from, address indexed to, uint256 value);
  208. }
  209.  
  210. contract BasicToken is ERC20Basic {
  211. using SafeMath for uint256;
  212.  
  213. mapping(address => uint256) balances;
  214.  
  215. uint256 totalSupply_;
  216.  
  217.  
  218. function totalSupply() public view returns (uint256) {
  219. return totalSupply_;
  220. }
  221.  
  222. function transfer(address _to, uint256 _value) public returns (bool) {
  223. require(_to != address(0));
  224. require(_value <= balances[msg.sender]);
  225.  
  226. balances[msg.sender] = balances[msg.sender].sub(_value);
  227. balances[_to] = balances[_to].add(_value);
  228. emit Transfer(msg.sender, _to, _value);
  229. return true;
  230. }
  231.  
  232.  
  233. function balanceOf(address _owner) public view returns (uint256) {
  234. return balances[_owner];
  235. }
  236.  
  237. }
  238.  
  239.  
  240. contract ERC20 is ERC20Basic {
  241. function allowance(address owner, address spender) public view returns (uint256);
  242. function transferFrom(address from, address to, uint256 value) public returns (bool);
  243. function approve(address spender, uint256 value) public returns (bool);
  244. event Approval(address indexed owner, address indexed spender, uint256 value);
  245. }
  246.  
  247. contract StandardToken is ERC20, BasicToken {
  248.  
  249. mapping (address => mapping (address => uint256)) internal allowed;
  250.  
  251.  
  252.  
  253. function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
  254. require(_to != address(0));
  255. require(_value <= balances[_from]);
  256. require(_value <= allowed[_from][msg.sender]);
  257.  
  258. balances[_from] = balances[_from].sub(_value);
  259. balances[_to] = balances[_to].add(_value);
  260. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
  261. emit Transfer(_from, _to, _value);
  262. return true;
  263. }
  264.  
  265.  
  266. function approve(address _spender, uint256 _value) public returns (bool) {
  267. allowed[msg.sender][_spender] = _value;
  268. emit Approval(msg.sender, _spender, _value);
  269. return true;
  270. }
  271.  
  272.  
  273. function allowance(address _owner, address _spender) public view returns (uint256) {
  274. return allowed[_owner][_spender];
  275. }
  276.  
  277.  
  278. function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
  279. allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
  280. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  281. return true;
  282. }
  283.  
  284.  
  285. function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
  286. uint oldValue = allowed[msg.sender][_spender];
  287. if (_subtractedValue > oldValue) {
  288. allowed[msg.sender][_spender] = 0;
  289. } else {
  290. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  291. }
  292. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  293. return true;
  294. }
  295.  
  296. }
  297.  
  298.  
  299. contract Ownable {
  300. address public owner;
  301.  
  302.  
  303. event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  304.  
  305.  
  306. constructor() public {
  307. owner = msg.sender;
  308. }
  309.  
  310.  
  311. modifier onlyOwner() {
  312. require(msg.sender == owner);
  313. _;
  314. }
  315.  
  316.  
  317. function transferOwnership(address newOwner) public onlyOwner {
  318. require(newOwner != address(0));
  319. emit OwnershipTransferred(owner, newOwner);
  320. owner = newOwner;
  321. }
  322.  
  323. }
  324.  
  325.  
  326.  
  327.  
  328. contract MintableToken is StandardToken, Ownable {
  329. event Mint(address indexed to, uint256 amount);
  330. event MintFinished();
  331.  
  332. bool public mintingFinished = false;
  333.  
  334.  
  335. modifier canMint() {
  336. require(!mintingFinished);
  337. _;
  338. }
  339.  
  340.  
  341. function mint(address _to, uint256 _amount) onlyOwner canMint public returns (bool) {
  342. totalSupply_ = totalSupply_.add(_amount);
  343. balances[_to] = balances[_to].add(_amount);
  344. emit Mint(_to, _amount);
  345. emit Transfer(address(0), _to, _amount);
  346. return true;
  347. }
  348.  
  349.  
  350. function finishMinting() onlyOwner canMint public returns (bool) {
  351. mintingFinished = true;
  352. emit MintFinished();
  353. return true;
  354. }
  355. }
  356.  
  357.  
  358. contract MintedCrowdsale is Crowdsale {
  359.  
  360. constructor(uint256 _rate, address _wallet, ERC20 _token) Crowdsale(_rate, _wallet, _token) public {}
  361.  
  362. function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {
  363. require(MintableToken(token).mint(_beneficiary, _tokenAmount));
  364. }
  365. }
Add Comment
Please, Sign In to add comment