Guest User

Untitled

a guest
Dec 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 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 TOKKA is StandardToken {
  171. string public name = "TOKKA";
  172. string public symbol = "TOK";
  173. uint256 public decimals = 18;
  174.  
  175. uint256 constant INITIAL_SUPPLY = 18000000 * 10**18;
  176. function StandardToken() public {
  177. balances[msg.sender] = INITIAL_SUPPLY;
  178.  
  179. }
  180. }
  181.  
  182. /**
  183. * @title Crowdsale
  184. * @dev Crowdsale is a base contract for managing a token crowdsale.
  185. * Crowdsales have a start and end timestamps, where investors can make
  186. * token purchases and the crowdsale will assign them tokens based
  187. * on a token per ETH rate. Funds collected are forwarded to a wallet
  188. * as they arrive.
  189. */
  190.  
  191. contract Crowdsale {
  192. using SafeMath for uint256;
  193.  
  194. // The token being sold
  195. StandardToken public token;
  196.  
  197. // start and end timestamps where investments are allowed (both inclusive)
  198. uint256 public startTime;
  199. uint256 public endTime;
  200.  
  201. // address where funds are collected
  202. address public wallet;
  203.  
  204. // how many token units a buyer gets per wei
  205. uint256 public rate;
  206.  
  207. // amount of raised money in wei
  208. uint256 public weiRaised;
  209.  
  210. /**
  211. * event for token purchase logging
  212. * @param purchaser who paid for the tokens
  213. * @param beneficiary who got the tokens
  214. * @param value weis paid for purchase
  215. * @param amount amount of tokens purchased
  216. */
  217. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  218.  
  219.  
  220. function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet) public {
  221. require(_startTime >= now);
  222. require(_endTime >= _startTime);
  223. require(_rate > 0);
  224. require(_wallet != address(0));
  225.  
  226. startTime = _startTime;
  227. endTime = _endTime;
  228. rate = _rate;
  229. wallet = _wallet;
  230. }
  231.  
  232. // creates the token to be sold.
  233. // override this method to have crowdsale of a specific mintable token.
  234. function createTokenContract() internal returns (StandardToken) {
  235. return new TOKKA();
  236. }
  237.  
  238.  
  239. // fallback function can be used to buy tokens
  240. function () external payable {
  241. buyTokens(msg.sender);
  242. }
  243.  
  244. // low level token purchase function
  245. function buyTokens(address beneficiary) public payable {
  246. require(beneficiary != address(0));
  247. require(validPurchase());
  248.  
  249. uint256 weiAmount = msg.value;
  250.  
  251. // calculate token amount to be created
  252. uint256 tokens = weiAmount.mul(rate);
  253.  
  254. // update state
  255. weiRaised = weiRaised.add(weiAmount);
  256.  
  257. // transfer tokens purchased
  258. ERC20(token).transfer(this, tokens);
  259.  
  260. TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
  261.  
  262. forwardFunds();
  263. }
  264.  
  265.  
  266. function forwardFunds() internal {
  267. wallet.transfer(msg.value);
  268. }
  269.  
  270.  
  271. function validPurchase() internal view returns (bool) {
  272. bool withinPeriod = now >= startTime && now <= endTime;
  273. bool nonZeroPurchase = msg.value != 0;
  274. return withinPeriod && nonZeroPurchase;
  275. }
  276.  
  277.  
  278. function hasEnded() public view returns (bool) {
  279. return now > endTime;
  280. }
  281.  
  282. }
  283.  
  284. contract CappedCrowdsale is Crowdsale {
  285. using SafeMath for uint256;
  286.  
  287. uint256 public cap;
  288.  
  289. function CappedCrowdsale(uint256 _cap) public {
  290. require(_cap > 0);
  291. cap = _cap;
  292. }
  293.  
  294.  
  295. function validPurchase() internal view returns (bool) {
  296. bool withinCap = weiRaised.add(msg.value) <= cap;
  297. return super.validPurchase() && withinCap;
  298. }
  299.  
  300.  
  301. function hasEnded() public view returns (bool) {
  302. bool capReached = weiRaised >= cap;
  303. return super.hasEnded() || capReached;
  304. }
  305.  
  306. }
Add Comment
Please, Sign In to add comment