daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest May 16th, 2018 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.4.21;
  2.  
  3. /*
  4. * ====================================*
  5. *
  6. *  Halo3D COIN
  7. *
  8. * ====================================*
  9. * -> What?
  10. * The original autonomous pyramid, improved:
  11. * [x] More stable than ever, having withstood severe testnet abuse and attack attempts from our community!.
  12. * [x] Audited, tested, and approved by known community security specialists.
  13. * [X] New functionality; you can now perform partial sell orders. If you succumb to weak hands, you don't have to dump all of your bags!
  14. * [x] New functionality; you can now transfer tokens between wallets. Trading is now possible from within the contract!
  15. * [x] New Feature: PoS Masternodes! The first implementation of Ethereum Staking in the world! Vitalik is mad.
  16. * [x] Masternodes: Holding 100 H3D Tokens allow you to generate a Masternode link, Masternode links are used as unique entry points to the contract!
  17. * [x] Masternodes: All players who enter the contract through your Masternode have 33% of their 20% dividends fee rerouted from the master-node, to the node-master!
  18. *
  19. * [x] REVOLUTIONARY 0% TRANSFER FEES, NOW YOU CAN SEND HarjCoin tokens to all your family, no charge
  20. * [x] GENEROUS 2% FEE ON EACH BUY AND SELL GO TO CHARITY https://giveth.io/
  21. *     https://etherscan.io/address/0x5ADF43DD006c6C36506e2b2DFA352E60002d22Dc
  22. *
  23. * -> Who worked on this project?
  24. * Trusted community from Crypto
  25. *
  26. * https://h3d.pw/  <- CHECK OUR WEBSITE! :)
  27. * https://discord.gg/w6HamAS <- CHECK OUR DISCORD! O_O
  28. */
  29.  
  30.  
  31. /**
  32.  * Definition of contract accepting Halo3D tokens
  33.  * Games, casinos, anything can reuse this contract to support Halo3D tokens
  34.  */
  35. contract AcceptsHalo3D {
  36.     Halo3D public tokenContract;
  37.  
  38.     function AcceptsHalo3D(address _tokenContract) public {
  39.         tokenContract = Halo3D(_tokenContract);
  40.     }
  41.  
  42.     modifier onlyTokenContract {
  43.         require(msg.sender == address(tokenContract));
  44.         _;
  45.     }
  46.  
  47.     /**
  48.     * @dev Standard ERC677 function that will handle incoming token transfers.
  49.     *
  50.     * @param _from  Token sender address.
  51.     * @param _value Amount of tokens.
  52.     * @param _data  Transaction metadata.
  53.     */
  54.     function tokenFallback(address _from, uint256 _value, bytes _data) external returns (bool);
  55. }
  56.  
  57.  
  58. contract Halo3D {
  59.     /*=================================
  60.     =            MODIFIERS            =
  61.     =================================*/
  62.     // only people with tokens
  63.     modifier onlyBagholders() {
  64.         require(myTokens() > 0);
  65.         _;
  66.     }
  67.  
  68.     // only people with profits
  69.     modifier onlyStronghands() {
  70.         require(myDividends(true) > 0);
  71.         _;
  72.     }
  73.  
  74.     modifier notContract() {
  75.       require (msg.sender == tx.origin);
  76.       _;
  77.     }
  78.  
  79.     // administrators can:
  80.     // -> change the name of the contract
  81.     // -> change the name of the token
  82.     // -> change the PoS difficulty (How many tokens it costs to hold a masternode, in case it gets crazy high later)
  83.     // they CANNOT:
  84.     // -> take funds
  85.     // -> disable withdrawals
  86.     // -> kill the contract
  87.     // -> change the price of tokens
  88.     modifier onlyAdministrator(){
  89.         address _customerAddress = msg.sender;
  90.         require(administrators[_customerAddress]);
  91.         _;
  92.     }
  93.  
  94.  
  95.     // ensures that the first tokens in the contract will be equally distributed
  96.     // meaning, no divine dump will be ever possible
  97.     // result: healthy longevity.
  98.     modifier antiEarlyWhale(uint256 _amountOfEthereum){
  99.         address _customerAddress = msg.sender;
  100.  
  101.         // are we still in the vulnerable phase?
  102.         // if so, enact anti early whale protocol
  103.         if( onlyAmbassadors && ((totalEthereumBalance() - _amountOfEthereum) <= ambassadorQuota_ )){
  104.             require(
  105.                 // is the customer in the ambassador list?
  106.                 ambassadors_[_customerAddress] == true &&
  107.  
  108.                 // does the customer purchase exceed the max ambassador quota?
  109.                 (ambassadorAccumulatedQuota_[_customerAddress] + _amountOfEthereum) <= ambassadorMaxPurchase_
  110.  
  111.             );
  112.  
  113.             // updated the accumulated quota
  114.             ambassadorAccumulatedQuota_[_customerAddress] = SafeMath.add(ambassadorAccumulatedQuota_[_customerAddress], _amountOfEthereum);
  115.  
  116.             // execute
  117.             _;
  118.         } else {
  119.             // in case the ether count drops low, the ambassador phase won't reinitiate
  120.             onlyAmbassadors = false;
  121.             _;
  122.         }
  123.  
  124.     }
  125.  
  126.     /*==============================
  127.     =            EVENTS            =
  128.     ==============================*/
  129.     event onTokenPurchase(
  130.         address indexed customerAddress,
  131.         uint256 incomingEthereum,
  132.         uint256 tokensMinted,
  133.         address indexed referredBy
  134.     );
  135.  
  136.     event onTokenSell(
  137.         address indexed customerAddress,
  138.         uint256 tokensBurned,
  139.         uint256 ethereumEarned
  140.     );
  141.  
  142.     event onReinvestment(
  143.         address indexed customerAddress,
  144.         uint256 ethereumReinvested,
  145.         uint256 tokensMinted
  146.     );
  147.  
  148.     event onWithdraw(
  149.         address indexed customerAddress,
  150.         uint256 ethereumWithdrawn
  151.     );
  152.  
  153.     // ERC20
  154.     event Transfer(
  155.         address indexed from,
  156.         address indexed to,
  157.         uint256 tokens
  158.     );
  159.  
  160.  
  161.     /*=====================================
  162.     =            CONFIGURABLES            =
  163.     =====================================*/
  164.     string public name = "Halo3D";
  165.     string public symbol = "H3D";
  166.     uint8 constant public decimals = 18;
  167.     uint8 constant internal dividendFee_ = 20; // 20% dividend fee on each buy and sell
  168.     uint8 constant internal charityFee_ = 2; // 2% charity fee on each buy and sell
  169.     uint256 constant internal tokenPriceInitial_ = 0.0000001 ether;
  170.     uint256 constant internal tokenPriceIncremental_ = 0.00000001 ether;
  171.     uint256 constant internal magnitude = 2**64;
  172.  
  173.     // Address to send the charity  ! :)
  174.     //  https://giveth.io/
  175.     // https://etherscan.io/address/0x5ADF43DD006c6C36506e2b2DFA352E60002d22Dc
  176.     address constant public giveEthCharityAddress = 0x5ADF43DD006c6C36506e2b2DFA352E60002d22Dc;
  177.     uint256 public totalEthCharityRecieved; // total ETH charity recieved from this contract
  178.     uint256 public totalEthCharityCollected; // total ETH charity collected in this contract
  179.  
  180.     // proof of stake (defaults at 100 tokens)
  181.     uint256 public stakingRequirement = 100e18;
  182.  
  183.     // ambassador program
  184.     mapping(address => bool) internal ambassadors_;
  185.     uint256 constant internal ambassadorMaxPurchase_ = 0.3 ether;
  186.     uint256 constant internal ambassadorQuota_ = 6 ether;
  187.  
  188.  
  189.  
  190.    /*================================
  191.     =            DATASETS            =
  192.     ================================*/
  193.     // amount of shares for each address (scaled number)
  194.     mapping(address => uint256) internal tokenBalanceLedger_;
  195.     mapping(address => uint256) internal referralBalance_;
  196.     mapping(address => int256) internal payoutsTo_;
  197.     mapping(address => uint256) internal ambassadorAccumulatedQuota_;
  198.     uint256 internal tokenSupply_ = 0;
  199.     uint256 internal profitPerShare_;
  200.  
  201.     // administrator list (see above on what they can do)
  202.     mapping(address => bool) public administrators;
  203.  
  204.     // when this is set to true, only ambassadors can purchase tokens (this prevents a whale premine, it ensures a fairly distributed upper pyramid)
  205.     bool public onlyAmbassadors = false;
  206.  
  207.     // Special Halo3D Platform control from scam game contracts on Halo3D platform
  208.     mapping(address => bool) public canAcceptTokens_; // contracts, which can accept Halo3D tokens
  209.  
  210.  
  211.  
  212.     /*=======================================
  213.     =            PUBLIC FUNCTIONS            =
  214.     =======================================*/
  215.     /*
  216.     * -- APPLICATION ENTRY POINTS --
  217.     */
  218.     function Halo3D()
  219.         public
  220.     {
  221.         // add administrators here
  222.         administrators[0xca35b7d915458ef540ade6068dfe2f44e8fa733c] = true;
  223.  
  224.         // add the ambassadors here.
  225.         // mantso - lead solidity dev & lead web dev.
  226.         ambassadors_[0xca35b7d915458ef540ade6068dfe2f44e8fa733c] = true;
  227.     }
  228.  
  229.  
  230.     /**
  231.      * Converts all incoming ethereum to tokens for the caller, and passes down the referral addy (if any)
  232.      */
  233.     function buy(address _referredBy)
  234.         public
  235.         payable
  236.         returns(uint256)
  237.     {
  238.         purchaseInternal(msg.value, _referredBy);
  239.     }
  240.  
  241.     /**
  242.      * Fallback function to handle ethereum that was send straight to the contract
  243.      * Unfortunately we cannot use a referral address this way.
  244.      */
  245.     function()
  246.         payable
  247.         public
  248.     {
  249.         purchaseInternal(msg.value, 0x0);
  250.     }
  251.  
  252.     /**
  253.      * Sends charity money to the  https://giveth.io/
  254.      * Their charity address is here https://etherscan.io/address/0x5ADF43DD006c6C36506e2b2DFA352E60002d22Dc
  255.      */
  256.     function payCharity() payable public {
  257.       uint256 ethToPay = SafeMath.sub(totalEthCharityCollected, totalEthCharityRecieved);
  258.       require(ethToPay > 1);
  259.       totalEthCharityRecieved = SafeMath.add(totalEthCharityRecieved, ethToPay);
  260.       if(!giveEthCharityAddress.call.value(ethToPay).gas(400000)()) {
  261.          totalEthCharityRecieved = SafeMath.sub(totalEthCharityRecieved, ethToPay);
  262.       }
  263.     }
  264.  
  265.     /**
  266.      * Converts all of caller's dividends to tokens.
  267.      */
  268.     function reinvest()
  269.         onlyStronghands()
  270.         public
  271.     {
  272.         // fetch dividends
  273.         uint256 _dividends = myDividends(false); // retrieve ref. bonus later in the code
  274.  
  275.         // pay out the dividends virtually
  276.         address _customerAddress = msg.sender;
  277.         payoutsTo_[_customerAddress] +=  (int256) (_dividends * magnitude);
  278.  
  279.         // retrieve ref. bonus
  280.         _dividends += referralBalance_[_customerAddress];
  281.         referralBalance_[_customerAddress] = 0;
  282.  
  283.         // dispatch a buy order with the virtualized "withdrawn dividends"
  284.         uint256 _tokens = purchaseTokens(_dividends, 0x0);
  285.  
  286.         // fire event
  287.         onReinvestment(_customerAddress, _dividends, _tokens);
  288.     }
  289.  
  290.     /**
  291.      * Alias of sell() and withdraw().
  292.      */
  293.     function exit()
  294.         public
  295.     {
  296.         // get token count for caller & sell them all
  297.         address _customerAddress = msg.sender;
  298.         uint256 _tokens = tokenBalanceLedger_[_customerAddress];
  299.         if(_tokens > 0) sell(_tokens);
  300.  
  301.         // lambo delivery service
  302.         withdraw();
  303.     }
  304.  
  305.     /**
  306.      * Withdraws all of the callers earnings.
  307.      */
  308.     function withdraw()
  309.         onlyStronghands()
  310.         public
  311.     {
  312.         // setup data
  313.         address _customerAddress = msg.sender;
  314.         uint256 _dividends = myDividends(false); // get ref. bonus later in the code
  315.  
  316.         // update dividend tracker
  317.         payoutsTo_[_customerAddress] +=  (int256) (_dividends * magnitude);
  318.  
  319.         // add ref. bonus
  320.         _dividends += referralBalance_[_customerAddress];
  321.         referralBalance_[_customerAddress] = 0;
  322.  
  323.         // lambo delivery service
  324.         _customerAddress.transfer(_dividends);
  325.  
  326.         // fire event
  327.         onWithdraw(_customerAddress, _dividends);
  328.     }
  329.  
  330.     /**
  331.      * Liquifies tokens to ethereum.
  332.      */
  333.     function sell(uint256 _amountOfTokens)
  334.         onlyBagholders()
  335.         public
  336.     {
  337.         // setup data
  338.         address _customerAddress = msg.sender;
  339.         // russian hackers BTFO
  340.         require(_amountOfTokens <= tokenBalanceLedger_[_customerAddress]);
  341.         uint256 _tokens = _amountOfTokens;
  342.         uint256 _ethereum = tokensToEthereum_(_tokens);
  343.  
  344.         uint256 _dividends = SafeMath.div(SafeMath.mul(_ethereum, dividendFee_), 100);
  345.         uint256 _charityPayout = SafeMath.div(SafeMath.mul(_ethereum, charityFee_), 100);
  346.  
  347.         // Take out dividends and then _charityPayout
  348.         uint256 _taxedEthereum =  SafeMath.sub(SafeMath.sub(_ethereum, _dividends), _charityPayout);
  349.  
  350.         // Add ethereum to send to charity
  351.         totalEthCharityCollected = SafeMath.add(totalEthCharityCollected, _charityPayout);
  352.  
  353.         // burn the sold tokens
  354.         tokenSupply_ = SafeMath.sub(tokenSupply_, _tokens);
  355.         tokenBalanceLedger_[_customerAddress] = SafeMath.sub(tokenBalanceLedger_[_customerAddress], _tokens);
  356.  
  357.         // update dividends tracker
  358.         int256 _updatedPayouts = (int256) (profitPerShare_ * _tokens + (_taxedEthereum * magnitude));
  359.         payoutsTo_[_customerAddress] -= _updatedPayouts;
  360.  
  361.         // dividing by zero is a bad idea
  362.         if (tokenSupply_ > 0) {
  363.             // update the amount of dividends per token
  364.             profitPerShare_ = SafeMath.add(profitPerShare_, (_dividends * magnitude) / tokenSupply_);
  365.         }
  366.  
  367.         // fire event
  368.         onTokenSell(_customerAddress, _tokens, _taxedEthereum);
  369.     }
  370.  
  371.  
  372.     /**
  373.      * Transfer tokens from the caller to a new holder.
  374.      * REMEMBER THIS IS 0% TRANSFER FEE
  375.      */
  376.     function transfer(address _toAddress, uint256 _amountOfTokens)
  377.         onlyBagholders()
  378.         public
  379.         returns(bool)
  380.     {
  381.         // setup
  382.         address _customerAddress = msg.sender;
  383.  
  384.         // make sure we have the requested tokens
  385.         // also disables transfers until ambassador phase is over
  386.         // ( we dont want whale premines )
  387.         require(!onlyAmbassadors && _amountOfTokens <= tokenBalanceLedger_[_customerAddress]);
  388.  
  389.         // withdraw all outstanding dividends first
  390.         if(myDividends(true) > 0) withdraw();
  391.  
  392.         // exchange tokens
  393.         tokenBalanceLedger_[_customerAddress] = SafeMath.sub(tokenBalanceLedger_[_customerAddress], _amountOfTokens);
  394.         tokenBalanceLedger_[_toAddress] = SafeMath.add(tokenBalanceLedger_[_toAddress], _amountOfTokens);
  395.  
  396.         // update dividend trackers
  397.         payoutsTo_[_customerAddress] -= (int256) (profitPerShare_ * _amountOfTokens);
  398.         payoutsTo_[_toAddress] += (int256) (profitPerShare_ * _amountOfTokens);
  399.  
  400.  
  401.         // fire event
  402.         Transfer(_customerAddress, _toAddress, _amountOfTokens);
  403.  
  404.         // ERC20
  405.         return true;
  406.     }
  407.  
  408.     /**
  409.     * Transfer token to a specified address and forward the data to recipient
  410.     * ERC-677 standard
  411.     * https://github.com/ethereum/EIPs/issues/677
  412.     * @param _to    Receiver address.
  413.     * @param _value Amount of tokens that will be transferred.
  414.     * @param _data  Transaction metadata.
  415.     */
  416.     function transferAndCall(address _to, uint256 _value, bytes _data) external returns (bool) {
  417.       require(_to != address(0));
  418.       require(canAcceptTokens_[_to] == true); // security check that contract approved by Halo3D platform
  419.       require(transfer(_to, _value)); // do a normal token transfer to the contract
  420.  
  421.       if (isContract(_to)) {
  422.         AcceptsHalo3D receiver = AcceptsHalo3D(_to);
  423.         require(receiver.tokenFallback(msg.sender, _value, _data));
  424.       }
  425.  
  426.       return true;
  427.     }
  428.  
  429.     /**
  430.      * Additional check that the game address we are sending tokens to is a contract
  431.      * assemble the given address bytecode. If bytecode exists then the _addr is a contract.
  432.      */
  433.      function isContract(address _addr) private constant returns (bool is_contract) {
  434.        // retrieve the size of the code on target address, this needs assembly
  435.        uint length;
  436.        assembly { length := extcodesize(_addr) }
  437.        return length > 0;
  438.      }
  439.  
  440.     /*----------  ADMINISTRATOR ONLY FUNCTIONS  ----------*/
  441.     /**
  442.      * In case the amassador quota is not met, the administrator can manually disable the ambassador phase.
  443.      */
  444.     function disableInitialStage()
  445.         onlyAdministrator()
  446.         public
  447.     {
  448.         onlyAmbassadors = false;
  449.     }
  450.  
  451.     /**
  452.      * In case one of us dies, we need to replace ourselves.
  453.      */
  454.     function setAdministrator(address _identifier, bool _status)
  455.         onlyAdministrator()
  456.         public
  457.     {
  458.         administrators[_identifier] = _status;
  459.     }
  460.  
  461.     /**
  462.      * Precautionary measures in case we need to adjust the masternode rate.
  463.      */
  464.     function setStakingRequirement(uint256 _amountOfTokens)
  465.         onlyAdministrator()
  466.         public
  467.     {
  468.         stakingRequirement = _amountOfTokens;
  469.     }
  470.  
  471.     /**
  472.      * Add or remove game contract, which can accept Halo3D tokens
  473.      */
  474.     function setCanAcceptTokens(address _address, bool _value)
  475.       onlyAdministrator()
  476.       public
  477.     {
  478.       canAcceptTokens_[_address] = _value;
  479.     }
  480.  
  481.     /**
  482.      * If we want to rebrand, we can.
  483.      */
  484.     function setName(string _name)
  485.         onlyAdministrator()
  486.         public
  487.     {
  488.         name = _name;
  489.     }
  490.  
  491.     /**
  492.      * If we want to rebrand, we can.
  493.      */
  494.     function setSymbol(string _symbol)
  495.         onlyAdministrator()
  496.         public
  497.     {
  498.         symbol = _symbol;
  499.     }
  500.  
  501.  
  502.     /*----------  HELPERS AND CALCULATORS  ----------*/
  503.     /**
  504.      * Method to view the current Ethereum stored in the contract
  505.      * Example: totalEthereumBalance()
  506.      */
  507.     function totalEthereumBalance()
  508.         public
  509.         view
  510.         returns(uint)
  511.     {
  512.         return this.balance;
  513.     }
  514.  
  515.     /**
  516.      * Retrieve the total token supply.
  517.      */
  518.     function totalSupply()
  519.         public
  520.         view
  521.         returns(uint256)
  522.     {
  523.         return tokenSupply_;
  524.     }
  525.  
  526.     /**
  527.      * Retrieve the tokens owned by the caller.
  528.      */
  529.     function myTokens()
  530.         public
  531.         view
  532.         returns(uint256)
  533.     {
  534.         address _customerAddress = msg.sender;
  535.         return balanceOf(_customerAddress);
  536.     }
  537.  
  538.     /**
  539.      * Retrieve the dividends owned by the caller.
  540.      * If `_includeReferralBonus` is to to 1/true, the referral bonus will be included in the calculations.
  541.      * The reason for this, is that in the frontend, we will want to get the total divs (global + ref)
  542.      * But in the internal calculations, we want them separate.
  543.      */
  544.     function myDividends(bool _includeReferralBonus)
  545.         public
  546.         view
  547.         returns(uint256)
  548.     {
  549.         address _customerAddress = msg.sender;
  550.         return _includeReferralBonus ? dividendsOf(_customerAddress) + referralBalance_[_customerAddress] : dividendsOf(_customerAddress) ;
  551.     }
  552.  
  553.     /**
  554.      * Retrieve the token balance of any single address.
  555.      */
  556.     function balanceOf(address _customerAddress)
  557.         view
  558.         public
  559.         returns(uint256)
  560.     {
  561.         return tokenBalanceLedger_[_customerAddress];
  562.     }
  563.  
  564.     /**
  565.      * Retrieve the dividend balance of any single address.
  566.      */
  567.     function dividendsOf(address _customerAddress)
  568.         view
  569.         public
  570.         returns(uint256)
  571.     {
  572.         return (uint256) ((int256)(profitPerShare_ * tokenBalanceLedger_[_customerAddress]) - payoutsTo_[_customerAddress]) / magnitude;
  573.     }
  574.  
  575.     /**
  576.      * Return the buy price of 1 individual token.
  577.      */
  578.     function sellPrice()
  579.         public
  580.         view
  581.         returns(uint256)
  582.     {
  583.         // our calculation relies on the token supply, so we need supply. Doh.
  584.         if(tokenSupply_ == 0){
  585.             return tokenPriceInitial_ - tokenPriceIncremental_;
  586.         } else {
  587.             uint256 _ethereum = tokensToEthereum_(1e18);
  588.             uint256 _dividends = SafeMath.div(SafeMath.mul(_ethereum, dividendFee_), 100);
  589.             uint256 _charityPayout = SafeMath.div(SafeMath.mul(_ethereum, charityFee_), 100);
  590.             uint256 _taxedEthereum = SafeMath.sub(SafeMath.sub(_ethereum, _dividends), _charityPayout);
  591.             return _taxedEthereum;
  592.         }
  593.     }
  594.  
  595.     /**
  596.      * Return the sell price of 1 individual token.
  597.      */
  598.     function buyPrice()
  599.         public
  600.         view
  601.         returns(uint256)
  602.     {
  603.         // our calculation relies on the token supply, so we need supply. Doh.
  604.         if(tokenSupply_ == 0){
  605.             return tokenPriceInitial_ + tokenPriceIncremental_;
  606.         } else {
  607.             uint256 _ethereum = tokensToEthereum_(1e18);
  608.             uint256 _dividends = SafeMath.div(SafeMath.mul(_ethereum, dividendFee_), 100);
  609.             uint256 _charityPayout = SafeMath.div(SafeMath.mul(_ethereum, charityFee_), 100);
  610.             uint256 _taxedEthereum =  SafeMath.add(SafeMath.add(_ethereum, _dividends), _charityPayout);
  611.             return _taxedEthereum;
  612.         }
  613.     }
  614.  
  615.     /**
  616.      * Function for the frontend to dynamically retrieve the price scaling of buy orders.
  617.      */
  618.     function calculateTokensReceived(uint256 _ethereumToSpend)
  619.         public
  620.         view
  621.         returns(uint256)
  622.     {
  623.         uint256 _dividends = SafeMath.div(SafeMath.mul(_ethereumToSpend, dividendFee_), 100);
  624.         uint256 _charityPayout = SafeMath.div(SafeMath.mul(_ethereumToSpend, charityFee_), 100);
  625.         uint256 _taxedEthereum = SafeMath.sub(SafeMath.sub(_ethereumToSpend, _dividends), _charityPayout);
  626.         uint256 _amountOfTokens = ethereumToTokens_(_taxedEthereum);
  627.         return _amountOfTokens;
  628.     }
  629.  
  630.     /**
  631.      * Function for the frontend to dynamically retrieve the price scaling of sell orders.
  632.      */
  633.     function calculateEthereumReceived(uint256 _tokensToSell)
  634.         public
  635.         view
  636.         returns(uint256)
  637.     {
  638.         require(_tokensToSell <= tokenSupply_);
  639.         uint256 _ethereum = tokensToEthereum_(_tokensToSell);
  640.         uint256 _dividends = SafeMath.div(SafeMath.mul(_ethereum, dividendFee_), 100);
  641.         uint256 _charityPayout = SafeMath.div(SafeMath.mul(_ethereum, charityFee_), 100);
  642.         uint256 _taxedEthereum = SafeMath.sub(SafeMath.sub(_ethereum, _dividends), _charityPayout);
  643.         return _taxedEthereum;
  644.     }
  645.  
  646.     /**
  647.      * Function for the frontend to show ether waiting to be send to charity in contract
  648.      */
  649.     function etherToSendCharity()
  650.         public
  651.         view
  652.         returns(uint256) {
  653.         return SafeMath.sub(totalEthCharityCollected, totalEthCharityRecieved);
  654.     }
  655.  
  656.  
  657.     /*==========================================
  658.     =            INTERNAL FUNCTIONS            =
  659.     ==========================================*/
  660.  
  661.     // Make sure we will send back excess if user sends more then 5 ether before 100 ETH in contract
  662.     function purchaseInternal(uint256 _incomingEthereum, address _referredBy)
  663.       notContract()// no contracts allowed
  664.       internal
  665.       returns(uint256) {
  666.  
  667.       uint256 purchaseEthereum = _incomingEthereum;
  668.       uint256 excess;
  669.       if(purchaseEthereum > 5 ether) { // check if the transaction is over 5 ether
  670.           if (address(this).balance <= 100 ether) { // if so check the contract is less then 100 ether
  671.               purchaseEthereum = 5 ether;
  672.               excess = SafeMath.sub(_incomingEthereum, purchaseEthereum);
  673.           }
  674.       }
  675.  
  676.       purchaseTokens(purchaseEthereum, _referredBy);
  677.  
  678.       if (excess > 0) {
  679.         msg.sender.transfer(excess);
  680.       }
  681.     }
  682.  
  683.  
  684.     function purchaseTokens(uint256 _incomingEthereum, address _referredBy)
  685.         antiEarlyWhale(_incomingEthereum)
  686.         internal
  687.         returns(uint256)
  688.     {
  689.         // data setup
  690.         uint256 _undividedDividends = SafeMath.div(SafeMath.mul(_incomingEthereum, dividendFee_), 100);
  691.         uint256 _referralBonus = SafeMath.div(_undividedDividends, 3);
  692.         uint256 _charityPayout = SafeMath.div(SafeMath.mul(_incomingEthereum, charityFee_), 100);
  693.         uint256 _dividends = SafeMath.sub(_undividedDividends, _referralBonus);
  694.         uint256 _taxedEthereum = SafeMath.sub(SafeMath.sub(_incomingEthereum, _undividedDividends), _charityPayout);
  695.  
  696.         totalEthCharityCollected = SafeMath.add(totalEthCharityCollected, _charityPayout);
  697.  
  698.         uint256 _amountOfTokens = ethereumToTokens_(_taxedEthereum);
  699.         uint256 _fee = _dividends * magnitude;
  700.  
  701.         // no point in continuing execution if OP is a poorfag russian hacker
  702.         // prevents overflow in the case that the pyramid somehow magically starts being used by everyone in the world
  703.         // (or hackers)
  704.         // and yes we know that the safemath function automatically rules out the "greater then" equasion.
  705.         require(_amountOfTokens > 0 && (SafeMath.add(_amountOfTokens,tokenSupply_) > tokenSupply_));
  706.  
  707.         // is the user referred by a masternode?
  708.         if(
  709.             // is this a referred purchase?
  710.             _referredBy != 0x0000000000000000000000000000000000000000 &&
  711.  
  712.             // no cheating!
  713.             _referredBy != msg.sender &&
  714.  
  715.             // does the referrer have at least X whole tokens?
  716.             // i.e is the referrer a godly chad masternode
  717.             tokenBalanceLedger_[_referredBy] >= stakingRequirement
  718.         ){
  719.             // wealth redistribution
  720.             referralBalance_[_referredBy] = SafeMath.add(referralBalance_[_referredBy], _referralBonus);
  721.         } else {
  722.             // no ref purchase
  723.             // add the referral bonus back to the global dividends cake
  724.             _dividends = SafeMath.add(_dividends, _referralBonus);
  725.             _fee = _dividends * magnitude;
  726.         }
  727.  
  728.         // we can't give people infinite ethereum
  729.         if(tokenSupply_ > 0){
  730.  
  731.             // add tokens to the pool
  732.             tokenSupply_ = SafeMath.add(tokenSupply_, _amountOfTokens);
  733.  
  734.             // take the amount of dividends gained through this transaction, and allocates them evenly to each shareholder
  735.             profitPerShare_ += (_dividends * magnitude / (tokenSupply_));
  736.  
  737.             // calculate the amount of tokens the customer receives over his purchase
  738.             _fee = _fee - (_fee-(_amountOfTokens * (_dividends * magnitude / (tokenSupply_))));
  739.  
  740.         } else {
  741.             // add tokens to the pool
  742.             tokenSupply_ = _amountOfTokens;
  743.         }
  744.  
  745.         // update circulating supply & the ledger address for the customer
  746.         tokenBalanceLedger_[msg.sender] = SafeMath.add(tokenBalanceLedger_[msg.sender], _amountOfTokens);
  747.  
  748.         // Tells the contract that the buyer doesn't deserve dividends for the tokens before they owned them;
  749.         //really i know you think you do but you don't
  750.         int256 _updatedPayouts = (int256) ((profitPerShare_ * _amountOfTokens) - _fee);
  751.         payoutsTo_[msg.sender] += _updatedPayouts;
  752.  
  753.         // fire event
  754.         onTokenPurchase(msg.sender, _incomingEthereum, _amountOfTokens, _referredBy);
  755.  
  756.         return _amountOfTokens;
  757.     }
  758.  
  759.     /**
  760.      * Calculate Token price based on an amount of incoming ethereum
  761.      * It's an algorithm, hopefully we gave you the whitepaper with it in scientific notation;
  762.      * Some conversions occurred to prevent decimal errors or underflows / overflows in solidity code.
  763.      */
  764.     function ethereumToTokens_(uint256 _ethereum)
  765.         internal
  766.         view
  767.         returns(uint256)
  768.     {
  769.         uint256 _tokenPriceInitial = tokenPriceInitial_ * 1e18;
  770.         uint256 _tokensReceived =
  771.          (
  772.             (
  773.                 // underflow attempts BTFO
  774.                 SafeMath.sub(
  775.                     (sqrt
  776.                         (
  777.                             (_tokenPriceInitial**2)
  778.                             +
  779.                             (2*(tokenPriceIncremental_ * 1e18)*(_ethereum * 1e18))
  780.                             +
  781.                             (((tokenPriceIncremental_)**2)*(tokenSupply_**2))
  782.                             +
  783.                             (2*(tokenPriceIncremental_)*_tokenPriceInitial*tokenSupply_)
  784.                         )
  785.                     ), _tokenPriceInitial
  786.                 )
  787.             )/(tokenPriceIncremental_)
  788.         )-(tokenSupply_)
  789.         ;
  790.  
  791.         return _tokensReceived;
  792.     }
  793.  
  794.     /**
  795.      * Calculate token sell value.
  796.      * It's an algorithm, hopefully we gave you the whitepaper with it in scientific notation;
  797.      * Some conversions occurred to prevent decimal errors or underflows / overflows in solidity code.
  798.      */
  799.      function tokensToEthereum_(uint256 _tokens)
  800.         internal
  801.         view
  802.         returns(uint256)
  803.     {
  804.  
  805.         uint256 tokens_ = (_tokens + 1e18);
  806.         uint256 _tokenSupply = (tokenSupply_ + 1e18);
  807.         uint256 _etherReceived =
  808.         (
  809.             // underflow attempts BTFO
  810.             SafeMath.sub(
  811.                 (
  812.                     (
  813.                         (
  814.                             tokenPriceInitial_ +(tokenPriceIncremental_ * (_tokenSupply/1e18))
  815.                         )-tokenPriceIncremental_
  816.                     )*(tokens_ - 1e18)
  817.                 ),(tokenPriceIncremental_*((tokens_**2-tokens_)/1e18))/2
  818.             )
  819.         /1e18);
  820.         return _etherReceived;
  821.     }
  822.  
  823.  
  824.     //This is where all your gas goes, sorry
  825.     //Not sorry, you probably only paid 1 gwei
  826.     function sqrt(uint x) internal pure returns (uint y) {
  827.         uint z = (x + 1) / 2;
  828.         y = x;
  829.         while (z < y) {
  830.             y = z;
  831.             z = (x / z + z) / 2;
  832.         }
  833.     }
  834. }
  835.  
  836. /**
  837.  * @title SafeMath
  838.  * @dev Math operations with safety checks that throw on error
  839.  */
  840. library SafeMath {
  841.  
  842.     /**
  843.     * @dev Multiplies two numbers, throws on overflow.
  844.     */
  845.     function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  846.         if (a == 0) {
  847.             return 0;
  848.         }
  849.         uint256 c = a * b;
  850.         assert(c / a == b);
  851.         return c;
  852.     }
  853.  
  854.     /**
  855.     * @dev Integer division of two numbers, truncating the quotient.
  856.     */
  857.     function div(uint256 a, uint256 b) internal pure returns (uint256) {
  858.         // assert(b > 0); // Solidity automatically throws when dividing by 0
  859.         uint256 c = a / b;
  860.         // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  861.         return c;
  862.     }
  863.  
  864.     /**
  865.     * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  866.     */
  867.     function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  868.         assert(b <= a);
  869.         return a - b;
  870.     }
  871.  
  872.     /**
  873.     * @dev Adds two numbers, throws on overflow.
  874.     */
  875.     function add(uint256 a, uint256 b) internal pure returns (uint256) {
  876.         uint256 c = a + b;
  877.         assert(c >= a);
  878.         return c;
  879.     }
  880. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top