Guest User

Untitled

a guest
Dec 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. pragma solidity ^0.4.16;
  2.  
  3. contract owned {
  4. address public owner;
  5.  
  6. function owned() public {
  7. owner = msg.sender;
  8. }
  9.  
  10. modifier onlyOwner {
  11. require(msg.sender == owner);
  12. _;
  13. }
  14.  
  15. function transferOwnership(address newOwner) onlyOwner public {
  16. owner = newOwner;
  17. }
  18. }
  19.  
  20. interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }
  21.  
  22. contract MyCoin {
  23. // Public variables of the token
  24. string public name = "MyCoin";
  25. string public symbol = "MIM";
  26. uint8 public decimals = 18;
  27. // 18 decimals is the strongly suggested default, avoid changing it
  28. uint256 public totalSupply;
  29. uint256 initialSupply = 50000000;
  30.  
  31. // This creates an array with all balances
  32. mapping (address => uint256) public balanceOf;
  33. mapping (address => mapping (address => uint256)) public allowance;
  34.  
  35. // This generates a public event on the blockchain that will notify clients
  36. event Transfer(address indexed from, address indexed to, uint256 value);
  37.  
  38. // This notifies clients about the amount burnt
  39. event Burn(address indexed from, uint256 value);
  40.  
  41. /**
  42. * Constrctor function
  43. *
  44. * Initializes contract with initial supply tokens to the creator of the contract
  45. */
  46. function MyCoin() public {
  47. totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
  48. balanceOf[this] = totalSupply; // Give the creator all initial tokens
  49. //name = tokenName; // Set the name for display purposes
  50. //symbol = tokenSymbol; // Set the symbol for display purposes
  51. }
  52.  
  53. /**
  54. * Internal transfer, only can be called by this contract
  55. */
  56. function _transfer(address _from, address _to, uint _value) internal {
  57. // Prevent transfer to 0x0 address. Use burn() instead
  58. require(_to != 0x0);
  59. // Check if the sender has enough
  60. require(balanceOf[_from] >= _value);
  61. // Check for overflows
  62. require(balanceOf[_to] + _value > balanceOf[_to]);
  63. // Save this for an assertion in the future
  64. uint previousBalances = balanceOf[_from] + balanceOf[_to];
  65. // Subtract from the sender
  66. balanceOf[_from] -= _value;
  67. // Add the same to the recipient
  68. balanceOf[_to] += _value;
  69. Transfer(_from, _to, _value);
  70. // Asserts are used to use static analysis to find bugs in your code. They should never fail
  71. assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
  72. }
  73.  
  74. /**
  75. * Transfer tokens
  76. *
  77. * Send `_value` tokens to `_to` from your account
  78. *
  79. * @param _to The address of the recipient
  80. * @param _value the amount to send
  81. */
  82. function transfer(address _to, uint256 _value) public {
  83. _transfer(this, _to, _value);
  84. }
  85.  
  86. /**
  87. * Transfer tokens from other address
  88. *
  89. * Send `_value` tokens to `_to` in behalf of `_from`
  90. *
  91. * @param _from The address of the sender
  92. * @param _to The address of the recipient
  93. * @param _value the amount to send
  94. */
  95. function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
  96. require(_value <= allowance[_from][msg.sender]); // Check allowance
  97. allowance[_from][msg.sender] -= _value;
  98. _transfer(_from, _to, _value);
  99. return true;
  100. }
  101.  
  102. /**
  103. * Set allowance for other address
  104. *
  105. * Allows `_spender` to spend no more than `_value` tokens in your behalf
  106. *
  107. * @param _spender The address authorized to spend
  108. * @param _value the max amount they can spend
  109. */
  110. function approve(address _spender, uint256 _value) public
  111. returns (bool success) {
  112. allowance[msg.sender][_spender] = _value;
  113. return true;
  114. }
  115.  
  116. /**
  117. * Set allowance for other address and notify
  118. *
  119. * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
  120. *
  121. * @param _spender The address authorized to spend
  122. * @param _value the max amount they can spend
  123. * @param _extraData some extra information to send to the approved contract
  124. */
  125. function approveAndCall(address _spender, uint256 _value, bytes _extraData)
  126. public
  127. returns (bool success) {
  128. tokenRecipient spender = tokenRecipient(_spender);
  129. if (approve(_spender, _value)) {
  130. spender.receiveApproval(msg.sender, _value, this, _extraData);
  131. return true;
  132. }
  133. }
  134.  
  135. /**
  136. * Destroy tokens
  137. *
  138. * Remove `_value` tokens from the system irreversibly
  139. *
  140. * @param _value the amount of money to burn
  141. */
  142. function burn(uint256 _value) public returns (bool success) {
  143. require(balanceOf[this] >= _value); // Check if the sender has enough
  144. balanceOf[this] -= _value; // Subtract from the sender
  145. totalSupply -= _value; // Updates totalSupply
  146. Burn(this, _value);
  147. return true;
  148. }
  149.  
  150. /**
  151. * Destroy tokens from other account
  152. *
  153. * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
  154. *
  155. * @param _from the address of the sender
  156. * @param _value the amount of money to burn
  157. */
  158. function burnFrom(address _from, uint256 _value) public returns (bool success) {
  159. require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
  160. require(_value <= allowance[_from][msg.sender]); // Check allowance
  161. balanceOf[_from] -= _value; // Subtract from the targeted balance
  162. allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
  163. totalSupply -= _value; // Update totalSupply
  164. Burn(_from, _value);
  165. return true;
  166. }
  167. }
  168.  
  169. /******************************************/
  170. /* ADVANCED TOKEN STARTS HERE */
  171. /******************************************/
  172.  
  173. contract MyAdvancedToken is owned, MyCoin {
  174.  
  175. uint256 public sellPrice = 500;
  176. uint256 public buyPrice = 500;
  177. uint256 private endTime = 1516060799; //@TODO
  178.  
  179. uint256 private phaseOneStart = 14512864000; //@TODO
  180. uint256 private phaseOneEnd = 1413036800; //@TODO
  181.  
  182. uint256 private phaseTwoStart = 1513067359; //@TODO
  183. uint256 private phaseTwoEnd = 1513110559; //@TODO
  184.  
  185. uint256 private xmasStart = 1513081759; //25.12.2017 00:00:00
  186. uint256 private xmasEnd = 1513114159; //25.12.2017 23:59:59
  187. uint256 private newyearStart = 1514764800; //1.1.2018 00:00:00
  188. uint256 private newyearEnd = 1514851199; //1.1.2018 23:59:59
  189.  
  190. mapping (address => bool) public frozenAccount;
  191.  
  192. /* This generates a public event on the blockchain that will notify clients */
  193. event FrozenFunds(address target, bool frozen);
  194.  
  195. /* Initializes contract with initial supply tokens to the creator of the contract */
  196. function MyAdvancedToken() MyCoin() public {}
  197.  
  198. /* Internal transfer, only can be called by this contract */
  199. function _transfer(address _from, address _to, uint _value) internal {
  200. require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
  201. require (balanceOf[_from] > _value); // Check if the sender has enough
  202. require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows
  203. require(!frozenAccount[_from]); // Check if sender is frozen
  204. require(!frozenAccount[_to]); // Check if recipient is frozen
  205. balanceOf[_from] -= _value; // Subtract from the sender
  206. balanceOf[_to] += _value; // Add the same to the recipient
  207. Transfer(_from, _to, _value);
  208. owner.transfer(this.balance); //send balance to the owner
  209. }
  210.  
  211. /// @notice Create `mintedAmount` tokens and send it to `target`
  212. /// @param target Address to receive the tokens
  213. /// @param mintedAmount the amount of tokens it will receive
  214. function mintToken(address target, uint256 mintedAmount) onlyOwner public {
  215. balanceOf[target] += mintedAmount;
  216. totalSupply += mintedAmount;
  217. Transfer(0, this, mintedAmount);
  218. Transfer(this, target, mintedAmount);
  219. }
  220.  
  221. /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
  222. /// @param target Address to be frozen
  223. /// @param freeze either to freeze it or not
  224. function freezeAccount(address target, bool freeze) onlyOwner public {
  225. frozenAccount[target] = freeze;
  226. FrozenFunds(target, freeze);
  227. }
  228.  
  229. /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
  230. /// @param newSellPrice Price the users can sell to the contract
  231. /// @param newBuyPrice Price users can buy from the contract
  232. function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
  233. sellPrice = newSellPrice;
  234. buyPrice = newBuyPrice;
  235. }
  236.  
  237. /// @notice Buy tokens from contract by sending ether
  238. function () payable public {
  239.  
  240. //this is minimum what you can buy
  241. require(msg.value >= 1 / buyPrice);
  242.  
  243. //ICO has to be active
  244. require(now < endTime);
  245.  
  246. //get amount of tokens to send to the seller
  247. uint amount = calculateAmount(msg.value);
  248. uint bonus = getBonus(amount);
  249.  
  250. //Xmas or NewYear give them 50% more tokens
  251. //uint bonus = ;
  252.  
  253. _transfer(this, msg.sender, amount + bonus);
  254. }
  255.  
  256. /**
  257. * Get bonus for Xmas and New Year
  258. */
  259. function getBonus(uint _amount) constant private returns (uint256) {
  260. if((now >= xmasStart && now <= xmasEnd) || (now >= newyearStart && now <= newyearEnd)) { //@TODO pripremi vrijeme, dodaj jos koji random dan za popuste
  261. return _amount * 50 / 100;
  262. } else {
  263. return 0;
  264. }
  265.  
  266.  
  267. }
  268.  
  269. /**
  270. * Calculate how mush MIM you need to send
  271. */
  272. function calculateAmount(uint _amount) constant private returns (uint256) {
  273. //phase 1 (10 days)
  274. if(now >= phaseOneStart && now <= phaseOneEnd) { //@TODO
  275. // calculates the amount
  276. return _amount * buyPrice + (_amount * buyPrice * 50 / 100);
  277. }
  278. //phase 2 (15 days)
  279. else if (now >= phaseTwoStart && now <= phaseTwoEnd) { //@TODO
  280. return _amount * buyPrice;
  281. }
  282. //phase 3 (20 days)
  283. else {
  284. return _amount * buyPrice - (_amount * buyPrice * 50 / 100);
  285. }
  286. }
  287.  
  288. /// @notice Sell `amount` tokens to contract
  289. /// @param amount amount of tokens to be sold
  290. function sell(uint256 amount) public {
  291. require(this.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
  292. _transfer(msg.sender, this, amount); // makes the transfers
  293. msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
  294. }
  295. }
Add Comment
Please, Sign In to add comment