Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. pragma solidity ^0.5.8;
  2.  
  3. import { SafeMath } from './SafeMath.sol';
  4.  
  5. contract BuyUnits {
  6. mapping (uint256 => mapping (address => uint256)) public units;
  7. mapping (uint256 => uint256) public unit_price;
  8. mapping (address => uint256) public shares;
  9. mapping (uint256 => uint256) public unit_pot;
  10.  
  11. uint256 public cookie_pot = 0;
  12. uint256 public shares_pot = 0;
  13. uint256 public dfp = 2;
  14. uint256 public dfp3 = 1;
  15. uint256 public rfp = 5;
  16.  
  17. address payable public d1;
  18. address payable public d2;
  19. address payable public d3;
  20.  
  21. constructor (address payable dd1, address payable dd2) public {
  22. d1 = msg.sender;
  23. d2 = dd1;
  24. d3 = dd2;
  25. }
  26.  
  27. function getUnitPrice(uint256 unit_id) public view returns (uint256) {
  28. if (unit_price[unit_id] != 0) {
  29. return unit_price[unit_id];
  30. }
  31. return 100000000;
  32. }
  33.  
  34. function getUnitPriceInc(uint256 unit_id) public pure returns (uint256) {
  35. return 3000000;
  36. }
  37.  
  38. function getUnitShares(uint256 unit_id) public pure returns (uint256) {
  39. return 2000000;
  40. }
  41.  
  42. function getUnitBuyFee(uint256 unit_id) public pure returns (uint256) {
  43. return 20000000;
  44. }
  45.  
  46. function getUnitsForPlayer(uint256 unit_id, address player) public view returns (uint256) {
  47. return units[unit_id][player];
  48. }
  49.  
  50. function setUnitPrice(uint256 unit_id, uint256 new_unit_price) public {
  51. unit_price[unit_id] = new_unit_price;
  52. }
  53.  
  54. function setUnitsForPlayer(uint256 unit_id, address player, uint256 player_units) public {
  55. units[unit_id][player] = player_units;
  56. }
  57.  
  58. function depositSharesForPlayer(address player, uint256 for_shares, uint256 bought_shares) public {
  59. shares[player] = SafeMath.add(shares[player], bought_shares);
  60. shares_pot = SafeMath.add(shares_pot, for_shares);
  61. }
  62.  
  63. function depositToCookiePot(uint256 for_pot) public {
  64. cookie_pot = SafeMath.add(cookie_pot, for_pot);
  65. }
  66.  
  67. function depositToUnitPot(uint256 unit_id, uint256 for_pot) public {
  68. unit_pot[unit_id] = SafeMath.add(unit_pot[unit_id], for_pot);
  69. }
  70.  
  71. function calculateBoughtUnits(uint256 unit_id, uint256 value) public view returns (uint256) {
  72. return SafeMath.div(value, getUnitPrice(unit_id));
  73. }
  74.  
  75. function calculateNewUnitPric (uint256 unit_id, uint256 value) public view returns (uint256) {
  76. return SafeMath.add(SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
  77. }
  78.  
  79. function calculateNewPlayerUnits(uint256 unit_id, uint256 value, address player) public view returns (uint256) {
  80. return SafeMath.add(calculateBoughtUnits(unit_id, value), getUnitsForPlayer(unit_id, player));
  81. }
  82.  
  83. function calculateSharesBought(uint256 unit_id, uint256 value) public view returns (uint256) {
  84. return SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitShares(unit_id));
  85. }
  86.  
  87. function percentageOf(uint256 total, uint256 percent) public pure returns (uint256) {
  88. return SafeMath.percentageOf(total, percent);
  89. }
  90.  
  91. function handleBuyUnits (address player, uint256 value, uint256 unit_id, uint256 for_shares) internal {
  92. uint256 bought_units = SafeMath.div(value, getUnitPrice(unit_id));
  93. uint256 new_unit_price = SafeMath.add(SafeMath.mul(bought_units, getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
  94. uint256 player_units = SafeMath.add(bought_units, getUnitsForPlayer(unit_id, player));
  95. uint256 shares_bought = SafeMath.mul(bought_units, getUnitShares(unit_id));
  96.  
  97. // Increase unit price
  98. setUnitPrice(unit_id, new_unit_price);
  99. // Distribute unit(s)
  100. setUnitsForPlayer(unit_id, player, player_units);
  101. depositSharesForPlayer(player, for_shares, shares_bought);
  102. }
  103.  
  104. /*
  105. * This function should do the following:
  106. * Increase player shares based on how many unit X the user buys
  107. * Distribute dev fees
  108. * Distribute
  109. *
  110. */
  111. function buyUnits (uint256 unit_id, address payable ref) external payable returns (bool) {
  112. // Make all monetary calculations
  113. uint256 dev_fee = SafeMath.percentageOf(msg.value, dfp); // 2
  114. uint256 dev_fee3 = SafeMath.percentageOf(msg.value, dfp3); // 1
  115. uint256 ref_fee = SafeMath.percentageOf(msg.value, rfp); // 5
  116. uint256 for_cookie_and_unit_divs = SafeMath.percentageOf(msg.value, getUnitBuyFee(unit_id)); // 20
  117. uint256 total_fees = SafeMath.add(
  118. SafeMath.add(dev_fee, dev_fee),
  119. SafeMath.add(
  120. SafeMath.add(dev_fee3, ref_fee),
  121. for_cookie_and_unit_divs
  122. )); // ((2 + 2) + ((1 + 5) + 20)) = 30
  123. uint256 for_pots = SafeMath.div(for_cookie_and_unit_divs, 2); // 10
  124.  
  125. // Distribute funds
  126. // d1.transfer(dev_fee);
  127. // d2.transfer(dev_fee);
  128. // d3.transfer(dev_fee3);
  129. // ref.transfer(ref_fee);
  130.  
  131. handleBuyUnits(msg.sender, msg.value, unit_id, for_pots); // xx, 100, 0, 10
  132. depositToCookiePot(for_pots);
  133. depositToUnitPot(unit_id, SafeMath.sub(msg.value, total_fees));
  134. return true;
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement