pragma solidity 0.4.25;

/*
* https://www.metadollar.org
*
* project start 13/12/16 at 20.00
* do not send your eth before the project starts
* 
*/



contract METADOLLAR {

    struct UserRecord {
        address referrer;
        uint tokens;
        uint gained_funds;
        uint ref_funds;
        // this field can be negative
        int funds_correction;
    }

    using SafeMath for uint;
    using SafeMathInt for int;
    using Fee for Fee.fee;
    using ToAddress for bytes;

    // ERC20
    string constant public name = "METADOLLAR DYNAMIC FUND";
    string constant public symbol = "MDF";
    uint8 constant public decimals = 18;

    // Fees
    Fee.fee private fee_purchase = Fee.fee(1, 10); // 10%
    Fee.fee private fee_selling  = Fee.fee(1, 20); // 6%
    Fee.fee private fee_transfer = Fee.fee(1, 100); // 1%
    Fee.fee private fee_referral = Fee.fee(33, 100); // 33%

    // Minimal amount of tokens to be an participant of referral program
    uint constant private minimal_stake = 10e18;

    // Factor for converting eth <-> tokens with required precision of calculations
    uint constant private precision_factor = 1e18;

    // Pricing policy
    //  - if user buy 1 token, price will be increased by "price_offset" value
    //  - if user sell 1 token, price will be decreased by "price_offset" value
    // For details see methods "fundsToTokens" and "tokensToFunds"
    uint private price = 1e29; // 100 Gwei * precision_factor
    uint constant private price_offset = 1e28; // 10 Gwei * precision_factor

    // Total amount of tokens
    uint private total_supply = 0;

    // Total profit shared between token's holders. It's not reflect exactly sum of funds because this parameter
    // can be modified to keep the real user's dividends when total supply is changed
    // For details see method "dividendsOf" and using "funds_correction" in the code
    uint private shared_profit = 0;

    // Map of the users data
    mapping(address => UserRecord) private user_data;

    // ==== Modifiers ==== //

    modifier onlyValidTokenAmount(uint tokens) {
        require(tokens > 0, "Amount of tokens must be greater than zero");
        require(tokens <= user_data[msg.sender].tokens, "You have not enough tokens");
        _;
    }

    // ==== Public API ==== //

    // ---- Write methods ---- //

    function () public payable {
        buy(msg.data.toAddr());
    }

    /*
    * @dev Buy tokens from incoming funds
    */
    function buy(address referrer) public payable {

        // apply fee
        (uint fee_funds, uint taxed_funds) = fee_purchase.split(msg.value);
        require(fee_funds != 0, "Incoming funds is too small");

        // update user's referrer
        //  - you cannot be a referrer for yourself
        //  - user and his referrer will be together all the life
        UserRecord storage user = user_data[msg.sender];
        if (referrer != 0x0 && referrer != msg.sender && user.referrer == 0x0) {
            user.referrer = referrer;
        }

        // apply referral bonus
        if (user.referrer != 0x0) {
            fee_funds = rewardReferrer(msg.sender, user.referrer, fee_funds, msg.value);
            require(fee_funds != 0, "Incoming funds is too small");
        }

        // calculate amount of tokens and change price
        (uint tokens, uint _price) = fundsToTokens(taxed_funds);
        require(tokens != 0, "Incoming funds is too small");
        price = _price;

        // mint tokens and increase shared profit
        mintTokens(msg.sender, tokens);
        shared_profit = shared_profit.add(fee_funds);

        emit Purchase(msg.sender, msg.value, tokens, price / precision_factor, now);
    }

    /*
    * @dev Sell given amount of tokens and get funds
    */
    function sell(uint tokens) public onlyValidTokenAmount(tokens) {

        // calculate amount of funds and change price
        (uint funds, uint _price) = tokensToFunds(tokens);
        require(funds != 0, "Insufficient tokens to do that");
        price = _price;

        // apply fee
        (uint fee_funds, uint taxed_funds) = fee_selling.split(funds);
        require(fee_funds != 0, "Insufficient tokens to do that");

        // burn tokens and add funds to user's dividends
        burnTokens(msg.sender, tokens);
        UserRecord storage user = user_data[msg.sender];
        user.gained_funds = user.gained_funds.add(taxed_funds);

        // increase shared profit
        shared_profit = shared_profit.add(fee_funds);

        emit Selling(msg.sender, tokens, funds, price / precision_factor, now);
    }

    /*
    * @dev Transfer given amount of tokens from sender to another user
    * ERC20
    */
    function transfer(address to_addr, uint tokens) public onlyValidTokenAmount(tokens) returns (bool success) {

        require(to_addr != msg.sender, "You cannot transfer tokens to yourself");

        // apply fee
        (uint fee_tokens, uint taxed_tokens) = fee_transfer.split(tokens);
        require(fee_tokens != 0, "Insufficient tokens to do that");

        // calculate amount of funds and change price
        (uint funds, uint _price) = tokensToFunds(fee_tokens);
        require(funds != 0, "Insufficient tokens to do that");
        price = _price;

        // burn and mint tokens excluding fee
        burnTokens(msg.sender, tokens);
        mintTokens(to_addr, taxed_tokens);

        // increase shared profit
        shared_profit = shared_profit.add(funds);

        emit Transfer(msg.sender, to_addr, tokens);
        return true;
    }

    /*
    * @dev Reinvest all dividends
    */
    function reinvest() public {

        // get all dividends
        uint funds = dividendsOf(msg.sender);
        require(funds > 0, "You have no dividends");

        // make correction, dividents will be 0 after that
        UserRecord storage user = user_data[msg.sender];
        user.funds_correction = user.funds_correction.add(int(funds));

        // apply fee
        (uint fee_funds, uint taxed_funds) = fee_purchase.split(funds);
        require(fee_funds != 0, "Insufficient dividends to do that");

        // apply referral bonus
        if (user.referrer != 0x0) {
            fee_funds = rewardReferrer(msg.sender, user.referrer, fee_funds, funds);
            require(fee_funds != 0, "Insufficient dividends to do that");
        }

        // calculate amount of tokens and change price
        (uint tokens, uint _price) = fundsToTokens(taxed_funds);
        require(tokens != 0, "Insufficient dividends to do that");
        price = _price;

        // mint tokens and increase shared profit
        mintTokens(msg.sender, tokens);
        shared_profit = shared_profit.add(fee_funds);

        emit Reinvestment(msg.sender, funds, tokens, price / precision_factor, now);
    }

    /*
    * @dev Withdraw all dividends
    */
    function withdraw() public {

        // get all dividends
        uint funds = dividendsOf(msg.sender);
        require(funds > 0, "You have no dividends");

        // make correction, dividents will be 0 after that
        UserRecord storage user = user_data[msg.sender];
        user.funds_correction = user.funds_correction.add(int(funds));

        // send funds
        msg.sender.transfer(funds);

        emit Withdrawal(msg.sender, funds, now);
    }

    /*
    * @dev Sell all tokens and withraw dividends
    */
    function exit() public {

        // sell all tokens
        uint tokens = user_data[msg.sender].tokens;
        if (tokens > 0) {
            sell(tokens);
        }

        withdraw();
    }

    /*
    * @dev CAUTION! This method distributes all incoming funds between token's holders and gives you nothing
    * It will be used by another contracts/addresses from our ecosystem in future
    * But if you want to donate, you're welcome :)
    */
    function donate() public payable {
        shared_profit = shared_profit.add(msg.value);
        emit Donation(msg.sender, msg.value, now);
    }

    // ---- Read methods ---- //

    /*
    * @dev Total amount of tokens
    * ERC20
    */
    function totalSupply() public view returns (uint) {
        return total_supply;
    }

    /*
    * @dev Amount of user's tokens
    * ERC20
    */
    function balanceOf(address addr) public view returns (uint) {
        return user_data[addr].tokens;
    }

    /*
    * @dev Amount of user's dividends
    */
    function dividendsOf(address addr) public view returns (uint) {

        UserRecord memory user = user_data[addr];

        // gained funds from selling tokens + bonus funds from referrals
        // int because "user.funds_correction" can be negative
        int d = int(user.gained_funds.add(user.ref_funds));
        require(d >= 0);

        // avoid zero divizion
        if (total_supply > 0) {
            // profit is proportional to stake
            d = d.add(int(shared_profit.mul(user.tokens) / total_supply));
        }

        // correction
        // d -= user.funds_correction
        if (user.funds_correction > 0) {
            d = d.sub(user.funds_correction);
        }
        else if (user.funds_correction < 0) {
            d = d.add(-user.funds_correction);
        }

        // just in case
        require(d >= 0);

        // total sum must be positive uint
        return uint(d);
    }

    /*
    * @dev Amount of tokens can be gained from given amount of funds
    */
    function expectedTokens(uint funds, bool apply_fee) public view returns (uint) {
        if (funds == 0) {
            return 0;
        }
        if (apply_fee) {
            (,uint _funds) = fee_purchase.split(funds);
            funds = _funds;
        }
        (uint tokens,) = fundsToTokens(funds);
        return tokens;
    }

    /*
    * @dev Amount of funds can be gained from given amount of tokens
    */
    function expectedFunds(uint tokens, bool apply_fee) public view returns (uint) {
        // empty tokens in total OR no tokens was sold
        if (tokens == 0 || total_supply == 0) {
            return 0;
        }
        // more tokens than were mined in total, just exclude unnecessary tokens from calculating
        else if (tokens > total_supply) {
            tokens = total_supply;
        }
        (uint funds,) = tokensToFunds(tokens);
        if (apply_fee) {
            (,uint _funds) = fee_selling.split(funds);
            funds = _funds;
        }
        return funds;
    }

    /*
    * @dev Purchase price of next 1 token
    */
    function buyPrice() public view returns (uint) {
        return price / precision_factor;
    }

    /*
    * @dev Selling price of next 1 token
    */
    function sellPrice() public view returns (uint) {
        return price.sub(price_offset) / precision_factor;
    }

    // ==== Private API ==== //

    /*
    * @dev Mint given amount of tokens to given user
    */
    function mintTokens(address addr, uint tokens) internal {

        UserRecord storage user = user_data[addr];

        bool not_first_minting = total_supply > 0;

        // make correction to keep dividends the rest of the users
        if (not_first_minting) {
            shared_profit = shared_profit.mul(total_supply.add(tokens)) / total_supply;
        }

        // add tokens
        total_supply = total_supply.add(tokens);
        user.tokens = user.tokens.add(tokens);

        // make correction to keep dividends of user
        if (not_first_minting) {
            user.funds_correction = user.funds_correction.add(int(tokens.mul(shared_profit) / total_supply));
        }
    }

    /*
    * @dev Burn given amout of tokens from given user
    */
    function burnTokens(address addr, uint tokens) internal {

        UserRecord storage user = user_data[addr];

        // keep current dividents of user if last tokens will be burned
        uint dividends_from_tokens = 0;
        if (total_supply == tokens) {
            dividends_from_tokens = shared_profit.mul(user.tokens) / total_supply;
        }

        // make correction to keep dividends the rest of the users
        shared_profit = shared_profit.mul(total_supply.sub(tokens)) / total_supply;

        // sub tokens
        total_supply = total_supply.sub(tokens);
        user.tokens = user.tokens.sub(tokens);

        // make correction to keep dividends of the user
        // if burned not last tokens
        if (total_supply > 0) {
            user.funds_correction = user.funds_correction.sub(int(tokens.mul(shared_profit) / total_supply));
        }
        // if burned last tokens
        else if (dividends_from_tokens != 0) {
            user.funds_correction = user.funds_correction.sub(int(dividends_from_tokens));
        }
    }

    /*
     * @dev Rewards the referrer from given amount of funds
     */
    function rewardReferrer(address addr, address referrer_addr, uint funds, uint full_funds) internal returns (uint funds_after_reward) {
        UserRecord storage referrer = user_data[referrer_addr];
        if (referrer.tokens >= minimal_stake) {
            (uint reward_funds, uint taxed_funds) = fee_referral.split(funds);
            referrer.ref_funds = referrer.ref_funds.add(reward_funds);
            emit ReferralReward(addr, referrer_addr, full_funds, reward_funds, now);
            return taxed_funds;
        }
        else {
            return funds;
        }
    }

    /*
    * @dev Calculate tokens from funds
    *
    * Given:
    *   a[1] = price
    *   d = price_offset
    *   sum(n) = funds
    * Here is used arithmetic progression's equation transformed to a quadratic equation:
    *   a * n^2 + b * n + c = 0
    * Where:
    *   a = d
    *   b = 2 * a[1] - d
    *   c = -2 * sum(n)
    * Solve it and first root is what we need - amount of tokens
    * So:
    *   tokens = n
    *   price = a[n+1]
    *
    * For details see method below
    */
    function fundsToTokens(uint funds) internal view returns (uint tokens, uint _price) {
        uint b = price.mul(2).sub(price_offset);
        uint D = b.mul(b).add(price_offset.mul(8).mul(funds).mul(precision_factor));
        uint n = D.sqrt().sub(b).mul(precision_factor) / price_offset.mul(2);
        uint anp1 = price.add(price_offset.mul(n) / precision_factor);
        return (n, anp1);
    }

    /*
    * @dev Calculate funds from tokens
    *
    * Given:
    *   a[1] = sell_price
    *   d = price_offset
    *   n = tokens
    * Here is used arithmetic progression's equation (-d because of d must be negative to reduce price):
    *   a[n] = a[1] - d * (n - 1)
    *   sum(n) = (a[1] + a[n]) * n / 2
    * So:
    *   funds = sum(n)
    *   price = a[n]
    *
    * For details see method above
    */
    function tokensToFunds(uint tokens) internal view returns (uint funds, uint _price) {
        uint sell_price = price.sub(price_offset);
        uint an = sell_price.add(price_offset).sub(price_offset.mul(tokens) / precision_factor);
        uint sn = sell_price.add(an).mul(tokens) / precision_factor.mul(2);
        return (sn / precision_factor, an);
    }

    // ==== Events ==== //

    event Purchase(address indexed addr, uint funds, uint tokens, uint price, uint time);
    event Selling(address indexed addr, uint tokens, uint funds, uint price, uint time);
    event Reinvestment(address indexed addr, uint funds, uint tokens, uint price, uint time);
    event Withdrawal(address indexed addr, uint funds, uint time);
    event Donation(address indexed addr, uint funds, uint time);
    event ReferralReward(address indexed referral_addr, address indexed referrer_addr, uint funds, uint reward_funds, uint time);

    //ERC20
    event Transfer(address indexed from_addr, address indexed to_addr, uint tokens);

}

library SafeMath {

    /**
    * @dev Multiplies two numbers
    */
    function mul(uint a, uint b) internal pure returns (uint) {
        if (a == 0) {
            return 0;
        }
        uint c = a * b;
        require(c / a == b, "mul failed");
        return c;
    }

    /**
    * @dev Subtracts two numbers
    */
    function sub(uint a, uint b) internal pure returns (uint) {
        require(b <= a, "sub failed");
        return a - b;
    }

    /**
    * @dev Adds two numbers
    */
    function add(uint a, uint b) internal pure returns (uint) {
        uint c = a + b;
        require(c >= a, "add failed");
        return c;
    }

    /**
     * @dev Gives square root from number
     */
    function sqrt(uint x) internal pure returns (uint y) {
        uint z = add(x, 1) / 2;
        y = x;
        while (z < y) {
            y = z;
            z = add(x / z, z) / 2;
        }
    }
}

library SafeMathInt {

    /**
    * @dev Subtracts two numbers
    */
    function sub(int a, int b) internal pure returns (int) {
        int c = a - b;
        require(c <= a, "sub failed");
        return c;
    }

    /**
    * @dev Adds two numbers
    */
    function add(int a, int b) internal pure returns (int) {
        int c = a + b;
        require(c >= a, "add failed");
        return c;
    }
}

library Fee {

    using SafeMath for uint;

    struct fee {
        uint num;
        uint den;
    }

    /*
    * @dev Splits given value to two parts: tax itself and taxed value
    */
    function split(fee memory f, uint value) internal pure returns (uint tax, uint taxed_value) {
        if (value == 0) {
            return (0, 0);
        }
        tax = value.mul(f.num) / f.den;
        taxed_value = value.sub(tax);
    }

    /*
    * @dev Returns only tax part
    */
    function get_tax(fee memory f, uint value) internal pure returns (uint tax) {
        if (value == 0) {
            return 0;
        }
        tax = value.mul(f.num) / f.den;
    }
}

library ToAddress {

    /*
    * @dev Transforms bytes to address
    */
    function toAddr(bytes source) internal pure returns (address addr) {
        assembly {
            addr := mload(add(source, 0x14))
        }
        return addr;
    }
}