Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.81 KB | None | 0 0
  1. pragma solidity ^0.5.8;
  2. pragma experimental ABIEncoderV2;
  3.  
  4. library SafeMath {
  5.  
  6. /**
  7. * @dev Returns the lowest value of the two integers
  8. */
  9. function min(uint256 a, uint256 b) internal pure returns (uint256) {
  10. return a < b ? a : b;
  11. }
  12.  
  13. /**
  14. * @dev Multiplies two numbers, throws on overflow.
  15. */
  16. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  17. if (a == 0) {
  18. return 0;
  19. }
  20. uint256 c = a * b;
  21. assert(c / a == b);
  22. return c;
  23. }
  24.  
  25. /**
  26. * @dev Integer division of two numbers, truncating the quotient.
  27. */
  28. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  29. // assert(b > 0); // Solidity automatically throws when dividing by 0
  30. uint256 c = a / b;
  31. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  32. return c;
  33. }
  34.  
  35. /**
  36. * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  37. */
  38. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  39. assert(b <= a);
  40. return a - b;
  41. }
  42.  
  43. /**
  44. * @dev Adds two numbers, throws on overflow.
  45. */
  46. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  47. uint256 c = a + b;
  48. assert(c >= a);
  49. return c;
  50. }
  51.  
  52. function percentageOf(uint256 total, uint256 percentage) internal pure returns (uint256) {
  53. return div(mul(total, percentage), 100);
  54. }
  55.  
  56. function getPercentage(uint256 total, uint256 piece) internal pure returns (uint256) {
  57. return div(piece, total);
  58. }
  59. }
  60.  
  61. contract Protected {
  62. bool public active = false;
  63. address payable public ceoAddress;
  64. mapping (address => bool) public admin;
  65.  
  66. function isAdmin (address _adr) public view returns (bool) {
  67. return admin[_adr];
  68. }
  69.  
  70. function addAdmin (address _adr) external adminOnly {
  71. admin[_adr] = true;
  72. }
  73.  
  74. function removeAdmin (address _adr) external adminOnly {
  75. require(_adr != msg.sender, "Action not allowed");
  76. admin[_adr] = false;
  77. }
  78.  
  79. modifier adminOnly () {
  80. require(admin[msg.sender] == true, "Unauthorized");
  81. _;
  82. }
  83.  
  84. modifier activeOnly () {
  85. require(active == true, "Inactive");
  86. _;
  87. }
  88.  
  89. constructor () public {
  90. ceoAddress = msg.sender;
  91. admin[msg.sender] = true;
  92. }
  93.  
  94. function toggleActive () external adminOnly returns (bool) {
  95. active = !active;
  96. return active;
  97. }
  98.  
  99. function getCEOAddress () public view returns (address payable) {
  100. return ceoAddress;
  101. }
  102. }
  103.  
  104. contract Moon is Protected {
  105.  
  106. // ----------------------- Unit System
  107. struct Unit {
  108. string name;
  109. uint256 cookies_per_second;
  110. uint256 starting_price;
  111. uint256 price_inc;
  112. uint256 shares;
  113. uint256 buy_fee;
  114. uint256 sell_fee;
  115. bool unlocked;
  116. }
  117. mapping (uint256 => Unit) private units;
  118. mapping (uint256 => uint256) private unit_price;
  119. uint256 private total_units;
  120.  
  121. // Dividend System
  122. mapping (address => uint256) public last_withdraw; // Should probably rename to something that reflects "last withdraw/unclaimed update"
  123. mapping (address => uint256) public shares;
  124. mapping (address => uint256) public unclaimed;
  125.  
  126. // Dividend System
  127. uint256 public pot = 0; // We'll get back to this later
  128. uint256 public legacy_pot = 0;
  129. uint256 public total_shares = 0;
  130. uint256 private precision_multiplier = 1000000;
  131.  
  132. // Buying
  133. address payable private dev;
  134. address payable private dev2;
  135. address payable private dev3;
  136. uint256 private dfp = 2;
  137. uint256 private dfp3 = 1;
  138. uint256 private rfp = 5;
  139.  
  140.  
  141. // --------------------- Unit Data System
  142. mapping (uint256 => mapping (address => uint256)) private unit;
  143. mapping (uint256 => uint256) private unit_count;
  144. mapping (address => uint256) private cookies;
  145. mapping (address => uint256) private production_start;
  146. uint256 public total_produced_cookies = 0;
  147. uint256 public total_sold_cookies = 0;
  148. uint256 public last_update = 0;
  149.  
  150. // Dividend System
  151. event Withdraw (address user, uint256 value, uint256 timestamp);
  152. event Deposit (address user, uint256 value, uint256 bought_shares, uint256 timestamp);
  153. event Remove (address user, uint256 sold_shares, uint256 timestamp);
  154.  
  155. // -------------------- Dividend System
  156.  
  157. function update (address user) internal {
  158. // Update share related stuff
  159. unclaimed[user] = SafeMath.add(unclaimed[user], calculateGains(user));
  160. last_withdraw[user] = legacy_pot;
  161.  
  162. // Update cookie production stuff
  163. cookies[user] = SafeMath.add(cookies[user], getProducedCookiesForPlayer(user));
  164. production_start[user] = now;
  165.  
  166. // Tracking max cookies
  167. total_produced_cookies = SafeMath.add(total_produced_cookies, calculateProducedCookiesSinceLastUpdate());
  168. last_update = now;
  169. }
  170.  
  171. function getTotalUnits () public view returns (uint256) {
  172. return total_units;
  173. }
  174.  
  175. function calculateProducedCookiesSinceLastUpdate() internal view returns (uint256) {
  176. uint256 cps = 0;
  177. uint256 total_unit_count = getTotalUnits();
  178.  
  179. for (uint8 i = 0; i < total_unit_count; i++) {
  180. if (unit_count[i] > 0) {
  181. uint256 cookies_per_second = getUnitCookiesPerSecond(i);
  182. uint256 total_cookies_per_second = SafeMath.mul(cookies_per_second, unit_count[i]);
  183. cps = SafeMath.add(cps, total_cookies_per_second);
  184. }
  185. }
  186.  
  187. uint256 produced_time = SafeMath.sub(now, last_update);
  188. uint256 produced = SafeMath.mul(cps, produced_time);
  189. return produced;
  190. }
  191.  
  192. function getTotalProducedCookies() internal view returns (uint256) {
  193. return SafeMath.add(total_produced_cookies, calculateProducedCookiesSinceLastUpdate());
  194. }
  195.  
  196. function getTotalCookiesInExistence() internal view returns (uint256) {
  197. return SafeMath.sub(getTotalProducedCookies(), total_sold_cookies);
  198. }
  199.  
  200. function calculateGains (address user) public view returns (uint256) {
  201. uint256 available_pot = SafeMath.sub(legacy_pot, last_withdraw[user]);
  202. if (available_pot == 0 || shares[user] == 0) {
  203. return 0;
  204. }
  205. uint256 p_in_m = SafeMath.div(SafeMath.mul(shares[user], precision_multiplier), total_shares);
  206. uint256 gains = SafeMath.div(SafeMath.mul(p_in_m, available_pot), precision_multiplier);
  207.  
  208. return gains;
  209. }
  210.  
  211. function depositSharesForPlayer (address player, uint256 value, uint256 bought_shares) internal {
  212. require (value > 0, "Invalid amount");
  213. require (bought_shares > 0, "Invalid amount of shares");
  214. // Ensure previous gains are calculated and stored
  215. update(player);
  216.  
  217. // Is this where the user buys shares?
  218. // uint256 share_value = 1000000; // Needs to be calculated somehow unless we follow a different system for this. Maybe each unit gives X shares?
  219. // uint256 bought_shares = SafeMath.div(SafeMath.div(SafeMath.mul(msg.value, precision_multiplier), share_value), precision_multiplier); // This should return the shares based on a share price, but I think we need to adjust this to something else related to the cookies/unit prices
  220.  
  221. pot = SafeMath.add(pot, value);
  222. legacy_pot = SafeMath.add(legacy_pot, value);
  223. total_shares = SafeMath.add(total_shares, bought_shares);
  224. shares[player] = SafeMath.add(shares[player], bought_shares);
  225.  
  226. emit Deposit(player, value, bought_shares, now);
  227. }
  228.  
  229. function removeSharesForPlayer (address player, uint256 sold_shares) private {
  230. require (sold_shares > 0, "Invalid amount of shares");
  231. // Ensure previous gains are calculated and stored
  232. update(player);
  233.  
  234. total_shares = SafeMath.sub(total_shares, sold_shares);
  235. shares[player] = SafeMath.sub(shares[player], sold_shares);
  236.  
  237. emit Remove(player, sold_shares, now);
  238. }
  239.  
  240. function withdrawGains () public {
  241. // Calculate total gains (previously unclaimed + gains since last calculation)
  242. uint256 total_gains = SafeMath.add(unclaimed[msg.sender], calculateGains(msg.sender));
  243.  
  244. // Ensure there are funds to withdraw
  245. assert (total_gains > 0);
  246. assert (pot > total_gains);
  247.  
  248. // Withdraw from dividend pot
  249. pot = SafeMath.sub(pot, total_gains);
  250.  
  251. // Reset user stats to reflect a withdrawal
  252. unclaimed[msg.sender] = 0;
  253. last_withdraw[msg.sender] = legacy_pot;
  254.  
  255. // Transfer funds
  256. msg.sender.transfer(total_gains);
  257.  
  258. emit Withdraw(msg.sender, total_gains, now);
  259. }
  260.  
  261. function getUnitPrice(uint256 unit_id) public view returns (uint256) {
  262. if (unit_price[unit_id] == 0) {
  263. return units[unit_id].starting_price;
  264. }
  265.  
  266. return unit_price[unit_id];
  267. }
  268.  
  269. function getUnitPriceInc(uint256 unit_id) public view returns (uint256) {
  270. return units[unit_id].price_inc;
  271. }
  272.  
  273. function getUnitBuyFee(uint256 unit_id) public view returns (uint256) {
  274. return units[unit_id].buy_fee;
  275. }
  276.  
  277. function getUnitSellFee(uint256 unit_id) public view returns (uint256) {
  278. return units[unit_id].sell_fee;
  279. }
  280.  
  281. function getUnitCookiesPerSecond(uint256 unit_id) public view returns (uint256) {
  282. return units[unit_id].cookies_per_second;
  283. }
  284.  
  285. function getUnitShares(uint256 unit_id) public view returns (uint256) {
  286. return units[unit_id].shares;
  287. }
  288.  
  289. function setUnitPrice(uint256 unit_id, uint256 _unit_price) internal {
  290. unit_price[unit_id] = _unit_price;
  291. }
  292.  
  293. function getUnit(uint256 unit_id) public view returns (Unit memory) {
  294. require (unit_id >= 0, 'Invalid unit_id');
  295. require (unit_id < total_units, 'This unit does not exist');
  296.  
  297. return units[unit_id];
  298. }
  299.  
  300. function addUnit(
  301. string calldata name,
  302. uint256 cookies_per_second,
  303. uint256 starting_price,
  304. uint256 price_inc,
  305. uint256 shares_per_unit,
  306. uint256 buy_fee,
  307. uint256 sell_fee,
  308. bool unlocked
  309. ) external adminOnly returns (Unit memory) {
  310. // Create Unit
  311. units[total_units] = Unit({
  312. name:name,
  313. cookies_per_second:cookies_per_second,
  314. starting_price:starting_price,
  315. price_inc:price_inc,
  316. shares:shares_per_unit,
  317. buy_fee:buy_fee,
  318. sell_fee:sell_fee,
  319. unlocked:unlocked
  320. });
  321.  
  322. // Update total units
  323. total_units = SafeMath.add(total_units, 1);
  324.  
  325. // Return the added unit
  326. return units[total_units];
  327. }
  328.  
  329. function toggleUnit(uint256 unit_id) public adminOnly returns (bool) {
  330. require (unit_id >= 0, 'Invalid unit_id');
  331. require (unit_id < total_units, 'This unit does not exist');
  332.  
  333. units[unit_id].unlocked = !units[unit_id].unlocked;
  334.  
  335. return true;
  336. }
  337.  
  338. // -------------------- Banking System
  339. uint256 private cookie_pot;
  340. mapping (uint256 => uint256) private unit_pot;
  341.  
  342. function getUnitPot (uint256 unit_id) public view returns (uint256) {
  343. return unit_pot[unit_id];
  344. }
  345.  
  346. function getCookiePot () public view returns (uint256) {
  347. return cookie_pot;
  348. }
  349.  
  350. // function getTotalFunds () public view returns (uint256) {
  351. // uint256 total_unit_count = getTotalUnits();
  352.  
  353. // uint256 total_funds = cookie_pot;
  354.  
  355. // for (uint8 i = 0; i < total_unit_count; i++) {
  356. // total_funds = SafeMath.add(total_funds, getUnitPot(i));
  357. // }
  358.  
  359. // return total_funds;
  360. // }
  361.  
  362. function depositToCookiePot (uint256 value) private returns (bool) {
  363. require(value > 0, "Invalid amount");
  364. cookie_pot = SafeMath.add(cookie_pot, value);
  365. return true;
  366. }
  367.  
  368. function depositToUnitPot (uint256 unit_id, uint256 value) private returns (bool) {
  369. require(value > 0, "Invalid amount");
  370. unit_pot[unit_id] = SafeMath.add(unit_pot[unit_id], value);
  371. return true;
  372. }
  373.  
  374. function withdrawFromCookiePot (address payable _to, uint256 amount) external adminOnly returns (bool) {
  375. require (amount > 0, "Invalid amount");
  376. require (amount <= getCookiePot(), "Insufficient funds");
  377.  
  378. cookie_pot = SafeMath.sub(cookie_pot, amount);
  379. _to.transfer(amount);
  380.  
  381. return true;
  382. }
  383.  
  384. function withdrawFromUnitPot (address payable _to, uint256 unit_id, uint256 amount) internal returns (bool) {
  385. require (amount > 0, "Invalid amount");
  386. require (amount <= getUnitPot(unit_id), "Insufficient funds");
  387.  
  388. unit_pot[unit_id] = SafeMath.sub(unit_pot[unit_id], amount);
  389. _to.transfer(amount);
  390.  
  391. return true;
  392. }
  393.  
  394. // function withdraw (uint256 amount) external adminOnly returns (bool) {
  395. // require (amount <= getTotalFunds(), "Insufficient funds"); // Need to change this to frag
  396.  
  397. // msg.sender.transfer(amount);
  398.  
  399. // return true;
  400. // }
  401.  
  402. function getUnitsForPlayer(uint256 unit_id, address player) public view returns (uint256) {
  403. return unit[unit_id][player];
  404. }
  405.  
  406. function getTotalUnitCount(uint256 unit_id) public view returns (uint256) {
  407. return unit_count[unit_id];
  408. }
  409.  
  410. function getProductionStartForPlayer(address player) public view returns (uint256) {
  411. return production_start[player];
  412. }
  413.  
  414. function setTotalUnitCount(uint256 unit_id, uint256 count) internal {
  415. require (count >= 0, 'Invalid count');
  416. unit_count[unit_id] = count;
  417. }
  418.  
  419. function setUnitsForPlayer(uint256 unit_id, address player, uint256 value) internal {
  420. require (value >= 0, "Invalid value");
  421. unit[unit_id][player] = value;
  422.  
  423. assert (unit[unit_id][player] == value);
  424. }
  425.  
  426. function getProducedCookiesForPlayer(address player) public view returns (uint256) {
  427. uint256 production_from = getProductionStartForPlayer(player);
  428. if (production_from == 0) {
  429. return 0;
  430. }
  431. uint256 total_unit_count = getTotalUnits();
  432. uint256 produced_time = SafeMath.sub(now, production_from);
  433. uint256 produced = 0;
  434.  
  435. for (uint8 i = 0; i < total_unit_count; i++) {
  436. if (unit[i][player] > 0) {
  437. uint256 cookies_per_second = getUnitCookiesPerSecond(i);
  438. uint256 produced_cookies = SafeMath.mul(cookies_per_second, produced_time);
  439. produced = SafeMath.add(produced, produced_cookies);
  440. }
  441. }
  442.  
  443. return produced;
  444. }
  445.  
  446. function getCookiesForPlayer(address player) public view returns (uint256) {
  447. uint256 cookies_on_hand = cookies[player];
  448. uint256 produced_cookies = getProducedCookiesForPlayer(player);
  449. // Add produced cookies
  450. uint256 total_cookies = SafeMath.add(cookies_on_hand, produced_cookies);
  451. return total_cookies;
  452. }
  453.  
  454. function setCookiesForPlayer(address player, uint256 value) internal {
  455. require (value >= 0, "Invalid value");
  456. cookies[player] = value;
  457.  
  458. assert (cookies[player] == value);
  459. }
  460.  
  461. // -------------------- Buying System
  462. constructor (address payable _dev2, address payable _dev3) Protected() public {
  463. dev = msg.sender;
  464. dev2 = _dev2;
  465. dev3 = _dev3;
  466. }
  467.  
  468. function calculateBoughtUnits(uint256 unit_id, uint256 value) public view returns (uint256) {
  469. return SafeMath.div(value, getUnitPrice(unit_id));
  470. }
  471.  
  472. function calculateNewUnitPric (uint256 unit_id, uint256 value) public view returns (uint256) {
  473. return SafeMath.add(SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
  474. }
  475.  
  476. function calculateNewPlayerUnits(uint256 unit_id, uint256 value, address player) public view returns (uint256) {
  477. return SafeMath.add(calculateBoughtUnits(unit_id, value), getUnitsForPlayer(unit_id, player));
  478. }
  479.  
  480. function calculateSharesBought(uint256 unit_id, uint256 value) public view returns (uint256) {
  481. return SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitShares(unit_id));
  482. }
  483.  
  484. function calculateTotalFees(uint256 unit_id, uint256 value) public view returns (uint256) {
  485. uint256 dev_fee = SafeMath.percentageOf(value, dfp);
  486. uint256 dev_fee3 = SafeMath.percentageOf(value, dfp3);
  487. uint256 ref_fee = SafeMath.percentageOf(value, rfp);
  488. uint256 for_cookie_and_unit_divs = SafeMath.percentageOf(value, getUnitBuyFee(unit_id));
  489. return SafeMath.add(
  490. SafeMath.add(dev_fee, dev_fee),
  491. SafeMath.add(
  492. SafeMath.add(dev_fee3, ref_fee),
  493. for_cookie_and_unit_divs
  494. ));
  495. }
  496.  
  497. function calculateForUnitPot(uint256 unit_id, uint256 value) public view returns (uint256) {
  498. uint256 total_fees = calculateTotalFees(unit_id, value);
  499. return SafeMath.sub(value, total_fees);
  500. }
  501.  
  502. function handleBuyUnits (address player, uint256 value, uint256 unit_id, uint256 for_shares) internal {
  503. uint256 bought_units = SafeMath.div(value, getUnitPrice(unit_id));
  504. uint256 new_unit_price = SafeMath.add(SafeMath.mul(bought_units, getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
  505. uint256 player_units = SafeMath.add(bought_units, getUnitsForPlayer(unit_id, player));
  506. uint256 shares_bought = SafeMath.mul(bought_units, getUnitShares(unit_id));
  507. uint256 new_unit_count = SafeMath.add(getTotalUnitCount(unit_id), bought_units);
  508.  
  509. depositSharesForPlayer(player, for_shares, shares_bought);
  510. setUnitPrice(unit_id, new_unit_price);
  511. setTotalUnitCount(unit_id, new_unit_count);
  512. setUnitsForPlayer(unit_id, player, player_units);
  513. }
  514.  
  515. /*
  516. * This function should do the following:
  517. * Increase player shares based on how many unit X the user buys
  518. * Distribute dev fees
  519. * Distribute
  520. *
  521. */
  522. function buyUnits (uint256 unit_id, address payable ref) external payable returns (bool) {
  523. // Make all monetary calculations
  524. uint256 dev_fee = SafeMath.percentageOf(msg.value, dfp);
  525. uint256 dev_fee3 = SafeMath.percentageOf(msg.value, dfp3);
  526. uint256 ref_fee = SafeMath.percentageOf(msg.value, rfp);
  527. uint256 for_cookie_and_unit_divs = SafeMath.percentageOf(msg.value, getUnitBuyFee(unit_id));
  528. uint256 total_fees = SafeMath.add(
  529. SafeMath.add(dev_fee, dev_fee),
  530. SafeMath.add(
  531. SafeMath.add(dev_fee3, ref_fee),
  532. for_cookie_and_unit_divs
  533. ));
  534. uint256 for_pots = SafeMath.div(for_cookie_and_unit_divs, 2);
  535. uint256 for_unit_pot = SafeMath.sub(msg.value, total_fees);
  536.  
  537. // Distribute funds
  538. dev.transfer(dev_fee);
  539. dev2.transfer(dev_fee);
  540. dev3.transfer(dev_fee3);
  541. ref.transfer(ref_fee);
  542.  
  543. handleBuyUnits(msg.sender, msg.value, unit_id, for_pots);
  544. depositToCookiePot(for_pots);
  545. depositToUnitPot(unit_id, for_unit_pot);
  546. return true;
  547. }
  548.  
  549. function calculateSellPricePerUnitWithFees(uint256 unit_id) public view returns (uint256) {
  550. return SafeMath.div(getUnitPot(unit_id), getTotalUnitCount(unit_id));
  551. }
  552.  
  553.  
  554. function calculateUnitSellValueWithFees(address payable player, uint256 unit_id) public view returns (uint256) {
  555. uint256 units_count = getUnitsForPlayer(unit_id, player);
  556. uint256 price_per_unit = calculateSellPricePerUnitWithFees(unit_id);
  557. return SafeMath.mul(units_count, price_per_unit);
  558. }
  559.  
  560. function handleSellUnits (address player, uint256 unit_id) internal {
  561. uint256 sold_units = getUnitsForPlayer(unit_id, player);
  562. uint256 sold_shares = SafeMath.mul(sold_units, getUnitShares(unit_id));
  563. uint256 new_unit_price = SafeMath.sub(SafeMath.mul(sold_units, getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
  564. uint256 new_unit_count = SafeMath.sub(getTotalUnitCount(unit_id), sold_units);
  565.  
  566. removeSharesForPlayer(player, sold_shares);
  567. setUnitPrice(unit_id, new_unit_price);
  568. setTotalUnitCount(unit_id, new_unit_count);
  569. setUnitsForPlayer(unit_id, player, 0);
  570. }
  571.  
  572. /*
  573. * This function should do the following:
  574. * It should stop the cookie production where it is
  575. * It should distribute dev fees
  576. * It should update & distribute shares
  577. */
  578. function sellUnits (uint256 unit_id, address payable ref) external returns (bool) {
  579. // Make all monetary calculations
  580. uint256 value = calculateUnitSellValueWithFees(msg.sender, unit_id);
  581. uint256 dev_fee = SafeMath.percentageOf(value, dfp);
  582. uint256 dev_fee3 = SafeMath.percentageOf(value, dfp3);
  583. uint256 ref_fee = SafeMath.percentageOf(value, rfp);
  584. uint256 for_cookie_and_unit_divs = SafeMath.percentageOf(value, getUnitSellFee(unit_id));
  585. uint256 for_cookie_pot = SafeMath.div(for_cookie_and_unit_divs, 2);
  586.  
  587. uint256 total_fees = SafeMath.add(
  588. SafeMath.add(dev_fee, dev_fee),
  589. SafeMath.add(
  590. SafeMath.add(dev_fee3, ref_fee),
  591. for_cookie_and_unit_divs
  592. ));
  593. uint256 value_dist = SafeMath.sub(value, total_fees);
  594.  
  595. // Distribute funds
  596. dev.transfer(dev_fee);
  597. dev2.transfer(dev_fee);
  598. dev3.transfer(dev_fee3);
  599. ref.transfer(ref_fee);
  600.  
  601. handleSellUnits(msg.sender, unit_id);
  602. depositToCookiePot(for_cookie_pot);
  603. withdrawFromUnitPot(msg.sender, unit_id, value_dist);
  604.  
  605. return true;
  606. }
  607.  
  608. function calculateCookieValue (uint256 cookie_count) public view returns (uint256) {
  609. if (cookie_count == 0) {
  610. return 0;
  611. }
  612. uint256 total_cookies_in_existence = getTotalCookiesInExistence();
  613. uint256 available_pot = SafeMath.percentageOf(cookie_pot, 10);
  614. uint256 cookie_p = SafeMath.div(SafeMath.mul(precision_multiplier, cookie_count), total_cookies_in_existence);
  615. uint256 p_value = SafeMath.mul(available_pot, cookie_p);
  616. uint256 value = SafeMath.div(p_value, precision_multiplier);
  617. if (value > cookie_pot) {
  618. return cookie_pot;
  619. }
  620. return value;
  621. }
  622.  
  623. function calculateCookieValueForPlayer() public view returns (uint256) {
  624. return calculateCookieValue(getCookiesForPlayer(msg.sender));
  625. }
  626.  
  627. function sellCookies() external returns (bool) {
  628. uint256 produced_cookies = getCookiesForPlayer(msg.sender);
  629. uint256 cookie_value = calculateCookieValue(produced_cookies);
  630. assert(cookie_value > 0);
  631.  
  632. cookies[msg.sender] = 0;
  633. production_start[msg.sender] = now;
  634. cookie_pot = SafeMath.sub(cookie_pot, cookie_value);
  635. total_sold_cookies = SafeMath.add(total_sold_cookies, produced_cookies);
  636. msg.sender.transfer(cookie_value);
  637.  
  638. return true;
  639. }
  640. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement