Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.5.8;
- import { SafeMath } from './SafeMath.sol';
- contract BuyUnits {
- mapping (uint256 => mapping (address => uint256)) public units;
- mapping (uint256 => uint256) public unit_price;
- mapping (address => uint256) public shares;
- mapping (uint256 => uint256) public unit_pot;
- uint256 public cookie_pot = 0;
- uint256 public shares_pot = 0;
- uint256 public dfp = 2;
- uint256 public dfp3 = 1;
- uint256 public rfp = 5;
- address payable public d1;
- address payable public d2;
- address payable public d3;
- constructor (address payable dd1, address payable dd2) public {
- d1 = msg.sender;
- d2 = dd1;
- d3 = dd2;
- }
- function getUnitPrice(uint256 unit_id) public view returns (uint256) {
- if (unit_price[unit_id] != 0) {
- return unit_price[unit_id];
- }
- return 100000000;
- }
- function getUnitPriceInc(uint256 unit_id) public pure returns (uint256) {
- return 3000000;
- }
- function getUnitShares(uint256 unit_id) public pure returns (uint256) {
- return 2000000;
- }
- function getUnitBuyFee(uint256 unit_id) public pure returns (uint256) {
- return 20000000;
- }
- function getUnitsForPlayer(uint256 unit_id, address player) public view returns (uint256) {
- return units[unit_id][player];
- }
- function setUnitPrice(uint256 unit_id, uint256 new_unit_price) public {
- unit_price[unit_id] = new_unit_price;
- }
- function setUnitsForPlayer(uint256 unit_id, address player, uint256 player_units) public {
- units[unit_id][player] = player_units;
- }
- function depositSharesForPlayer(address player, uint256 for_shares, uint256 bought_shares) public {
- shares[player] = SafeMath.add(shares[player], bought_shares);
- shares_pot = SafeMath.add(shares_pot, for_shares);
- }
- function depositToCookiePot(uint256 for_pot) public {
- cookie_pot = SafeMath.add(cookie_pot, for_pot);
- }
- function depositToUnitPot(uint256 unit_id, uint256 for_pot) public {
- unit_pot[unit_id] = SafeMath.add(unit_pot[unit_id], for_pot);
- }
- function calculateBoughtUnits(uint256 unit_id, uint256 value) public view returns (uint256) {
- return SafeMath.div(value, getUnitPrice(unit_id));
- }
- function calculateNewUnitPric (uint256 unit_id, uint256 value) public view returns (uint256) {
- return SafeMath.add(SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
- }
- function calculateNewPlayerUnits(uint256 unit_id, uint256 value, address player) public view returns (uint256) {
- return SafeMath.add(calculateBoughtUnits(unit_id, value), getUnitsForPlayer(unit_id, player));
- }
- function calculateSharesBought(uint256 unit_id, uint256 value) public view returns (uint256) {
- return SafeMath.mul(calculateBoughtUnits(unit_id, value), getUnitShares(unit_id));
- }
- function percentageOf(uint256 total, uint256 percent) public pure returns (uint256) {
- return SafeMath.percentageOf(total, percent);
- }
- function handleBuyUnits (address player, uint256 value, uint256 unit_id, uint256 for_shares) internal {
- uint256 bought_units = SafeMath.div(value, getUnitPrice(unit_id));
- uint256 new_unit_price = SafeMath.add(SafeMath.mul(bought_units, getUnitPriceInc(unit_id)), getUnitPrice(unit_id));
- uint256 player_units = SafeMath.add(bought_units, getUnitsForPlayer(unit_id, player));
- uint256 shares_bought = SafeMath.mul(bought_units, getUnitShares(unit_id));
- // Increase unit price
- setUnitPrice(unit_id, new_unit_price);
- // Distribute unit(s)
- setUnitsForPlayer(unit_id, player, player_units);
- depositSharesForPlayer(player, for_shares, shares_bought);
- }
- /*
- * This function should do the following:
- * Increase player shares based on how many unit X the user buys
- * Distribute dev fees
- * Distribute
- *
- */
- function buyUnits (uint256 unit_id, address payable ref) external payable returns (bool) {
- // Make all monetary calculations
- uint256 dev_fee = SafeMath.percentageOf(msg.value, dfp); // 2
- uint256 dev_fee3 = SafeMath.percentageOf(msg.value, dfp3); // 1
- uint256 ref_fee = SafeMath.percentageOf(msg.value, rfp); // 5
- uint256 for_cookie_and_unit_divs = SafeMath.percentageOf(msg.value, getUnitBuyFee(unit_id)); // 20
- uint256 total_fees = SafeMath.add(
- SafeMath.add(dev_fee, dev_fee),
- SafeMath.add(
- SafeMath.add(dev_fee3, ref_fee),
- for_cookie_and_unit_divs
- )); // ((2 + 2) + ((1 + 5) + 20)) = 30
- uint256 for_pots = SafeMath.div(for_cookie_and_unit_divs, 2); // 10
- // Distribute funds
- // d1.transfer(dev_fee);
- // d2.transfer(dev_fee);
- // d3.transfer(dev_fee3);
- // ref.transfer(ref_fee);
- handleBuyUnits(msg.sender, msg.value, unit_id, for_pots); // xx, 100, 0, 10
- depositToCookiePot(for_pots);
- depositToUnitPot(unit_id, SafeMath.sub(msg.value, total_fees));
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement