Guest User

Untitled

a guest
Dec 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. pragma solidity ^0.4.17;
  2.  
  3. /**
  4. * @title ERC20Basic
  5. * @dev Simpler version of ERC20 interface
  6. * @dev see https://github.com/ethereum/EIPs/issues/179
  7. */
  8. contract ERC20Basic {
  9. uint256 public totalSupply;
  10. function balanceOf(address who) public view returns (uint256);
  11. function transfer(address to, uint256 value) public returns (bool);
  12. event Transfer(address indexed from, address indexed to, uint256 value);
  13. }
  14.  
  15. /**
  16. * @title SafeMath
  17. * @dev Math operations with safety checks that throw on error
  18. */
  19. library SafeMath {
  20. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  21. if (a == 0) {
  22. return 0;
  23. }
  24. uint256 c = a * b;
  25. assert(c / a == b);
  26. return c;
  27. }
  28.  
  29. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  30. // assert(b > 0); // Solidity automatically throws when dividing by 0
  31. uint256 c = a / b;
  32. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  33. return c;
  34. }
  35.  
  36. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  37. assert(b <= a);
  38. return a - b;
  39. }
  40.  
  41. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  42. uint256 c = a + b;
  43. assert(c >= a);
  44. return c;
  45. }
  46. }
  47.  
  48. /**
  49. * @title Basic token
  50. * @dev Basic version of StandardToken, with no allowances.
  51. */
  52. contract BasicToken is ERC20Basic {
  53. using SafeMath for uint256;
  54.  
  55. mapping(address => uint256) balances;
  56.  
  57. /**
  58. * @dev transfer token for a specified address
  59. * @param _to The address to transfer to.
  60. * @param _value The amount to be transferred.
  61. */
  62. function transfer(address _to, uint256 _value) public returns (bool) {
  63. require(_to != address(0));
  64. require(_value <= balances[msg.sender]);
  65.  
  66. // SafeMath.sub will throw if there is not enough balance.
  67. balances[msg.sender] = balances[msg.sender].sub(_value);
  68. balances[_to] = balances[_to].add(_value);
  69. Transfer(msg.sender, _to, _value);
  70. return true;
  71. }
  72.  
  73. /**
  74. * @dev Gets the balance of the specified address.
  75. * @param _owner The address to query the the balance of.
  76. * @return An uint256 representing the amount owned by the passed address.
  77. */
  78. function balanceOf(address _owner) public view returns (uint256 balance) {
  79. return balances[_owner];
  80. }
  81.  
  82. }
  83.  
  84. /**
  85. * @title ERC20 interface
  86. * @dev see https://github.com/ethereum/EIPs/issues/20
  87. */
  88. contract ERC20 is ERC20Basic {
  89. function allowance(address owner, address spender) public view returns (uint256);
  90. function transferFrom(address from, address to, uint256 value) public returns (bool);
  91. function approve(address spender, uint256 value) public returns (bool);
  92. event Approval(address indexed owner, address indexed spender, uint256 value);
  93. }
  94.  
  95. contract StandardToken is ERC20, BasicToken {
  96.  
  97. mapping (address => mapping (address => uint256)) internal allowed;
  98.  
  99.  
  100. /**
  101. * @dev Transfer tokens from one address to another
  102. * @param _from address The address which you want to send tokens from
  103. * @param _to address The address which you want to transfer to
  104. * @param _value uint256 the amount of tokens to be transferred
  105. */
  106. function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
  107. require(_to != address(0));
  108. require(_value <= balances[_from]);
  109. require(_value <= allowed[_from][msg.sender]);
  110.  
  111. balances[_from] = balances[_from].sub(_value);
  112. balances[_to] = balances[_to].add(_value);
  113. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
  114. Transfer(_from, _to, _value);
  115. return true;
  116. }
  117.  
  118. /**
  119. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  120. *
  121. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  122. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  123. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  124. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  125. * @param _spender The address which will spend the funds.
  126. * @param _value The amount of tokens to be spent.
  127. */
  128. function approve(address _spender, uint256 _value) public returns (bool) {
  129. allowed[msg.sender][_spender] = _value;
  130. Approval(msg.sender, _spender, _value);
  131. return true;
  132. }
  133.  
  134. /**
  135. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  136. * @param _owner address The address which owns the funds.
  137. * @param _spender address The address which will spend the funds.
  138. * @return A uint256 specifying the amount of tokens still available for the spender.
  139. */
  140. function allowance(address _owner, address _spender) public view returns (uint256) {
  141. return allowed[_owner][_spender];
  142. }
  143.  
  144. /**
  145. * approve should be called when allowed[_spender] == 0. To increment
  146. * allowed value is better to use this function to avoid 2 calls (and wait until
  147. * the first transaction is mined)
  148. * From MonolithDAO Token.sol
  149. */
  150. function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
  151. allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
  152. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  153. return true;
  154. }
  155.  
  156. function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
  157. uint oldValue = allowed[msg.sender][_spender];
  158. if (_subtractedValue > oldValue) {
  159. allowed[msg.sender][_spender] = 0;
  160. } else {
  161. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  162. }
  163. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  164. return true;
  165. }
  166.  
  167. }
  168.  
  169.  
  170. contract HEY is StandardToken {
  171. string public name = "HEY";
  172. string public symbol = "HEY";
  173. uint256 public decimals = 18;
  174.  
  175. uint256 constant INITIAL_SUPPLY = 1234 * 10**18;
  176.  
  177. function StandardToken() public {
  178. balance[msg.sender] = INITIAL_SUPPLY;
  179.  
  180. }
  181. }
  182.  
  183. /**
  184. * @title Crowdsale
  185. * @dev Crowdsale is a base contract for managing a token crowdsale.
  186. * Crowdsales have a start and end timestamps, where investors can make
  187. * token purchases and the crowdsale will assign them tokens based
  188. * on a token per ETH rate. Funds collected are forwarded to a wallet
  189. * as they arrive.
  190. */
  191.  
  192. contract Crowdsale {
  193. using SafeMath for uint256;
  194.  
  195.  
  196. // start and end timestamps where investments are allowed (both inclusive)
  197. uint256 public startTime;
  198. uint256 public endTime;
  199.  
  200. // address where funds are collected
  201. address public wallet;
  202.  
  203. // how many token units a buyer gets per wei
  204. uint256 public rate;
  205.  
  206. // amount of raised money in wei
  207. uint256 public weiRaised;
  208.  
  209. /**
  210. * event for token purchase logging
  211. * @param purchaser who paid for the tokens
  212. * @param beneficiary who got the tokens
  213. * @param value weis paid for purchase
  214. * @param amount amount of tokens purchased
  215. */
  216. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  217.  
  218.  
  219. function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet) public {
  220. require(_startTime >= now);
  221. require(_endTime >= _startTime);
  222. require(_rate > 0);
  223. require(_wallet != address(0));
  224.  
  225. startTime = _startTime;
  226. endTime = _endTime;
  227. rate = _rate;
  228. wallet = _wallet;
  229. }
  230.  
  231. // creates the token to be sold.
  232. // override this method to have crowdsale of a specific mintable token.
  233. function createTokenContract() internal returns (StandardToken) {
  234. return new Tokka();
  235. }
  236.  
  237.  
  238. // fallback function can be used to buy tokens
  239. function () external payable {
  240. buyTokens(msg.sender);
  241. }
  242.  
  243. // low level token purchase function
  244. function buyTokens(address beneficiary) public payable {
  245. require(beneficiary != address(0));
  246. require(validPurchase());
  247.  
  248. uint256 weiAmount = msg.value;
  249.  
  250. // calculate token amount to be created
  251. uint256 tokens = weiAmount.mul(rate);
  252.  
  253. // update state
  254. weiRaised = weiRaised.add(weiAmount);
  255.  
  256. // transfer tokens purchased
  257. ERC20(token).transfer(this, tokens); // <---- missing transfer
  258.  
  259. TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
  260.  
  261. forwardFunds();
  262. }
  263.  
  264.  
  265. function forwardFunds() internal {
  266. wallet.transfer(msg.value);
  267. }
  268.  
  269.  
  270. function validPurchase() internal view returns (bool) {
  271. bool withinPeriod = now >= startTime && now <= endTime;
  272. bool nonZeroPurchase = msg.value != 0;
  273. return withinPeriod && nonZeroPurchase;
  274. }
  275.  
  276.  
  277. function hasEnded() public view returns (bool) {
  278. return now > endTime;
  279. }
  280.  
  281. }
  282.  
  283. contract CappedCrowdsale is Crowdsale {
  284. using SafeMath for uint256;
  285.  
  286. uint256 public cap;
  287.  
  288. function CappedCrowdsale(uint256 _cap) public {
  289. require(_cap > 0);
  290. cap = _cap;
  291. }
  292.  
  293.  
  294. function validPurchase() internal view returns (bool) {
  295. bool withinCap = weiRaised.add(msg.value) <= cap;
  296. return super.validPurchase() && withinCap;
  297. }
  298.  
  299.  
  300. function hasEnded() public view returns (bool) {
  301. bool capReached = weiRaised >= cap;
  302. return super.hasEnded() || capReached;
  303. }
  304.  
  305. }
Add Comment
Please, Sign In to add comment