// Contract: Memefund
// Target function: function transferFrom(address from, address to, uint256 value) public returns (bool)
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

library SafeMaths {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        uint256 c = a / b;

        return c;
    }

}

contract Memefund {

    using SafeMaths for uint256;

    address public rebaseOracle;       // Used for authentication
    address public owner;              // Used for authentication
    address public newOwner;

    uint8 public decimals;
    uint256 public totalSupply;
    string public name;
    string public symbol;

    uint256 private constant MAX_UINT256 = ~uint256(0);   // (2^256) - 1
    uint256 private constant MAXSUPPLY = ~uint128(0);  // (2^128) - 1

    uint256 private totalAtoms;
    uint256 private atomsPerMolecule;

    mapping (address => uint256) private atomBalances;
    mapping (address => mapping (address => uint256)) private allowedMolecules;

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
    event LogRebase(uint256 _totalSupply);
    event LogNewRebaseOracle(address _rebaseOracle);
    event OwnershipTransferred(address indexed _from, address indexed _to);

    constructor() public
    {
        decimals = 9;                               // decimals  
        totalSupply = 100000000*10**9;                // initialSupply
        name = "Memefund";                         // Set the name for display purposes
        symbol = "MFUND";                            // Set the symbol for display purposes

        owner = msg.sender;
        totalAtoms = MAX_UINT256 - (MAX_UINT256 % totalSupply);     // totalAtoms is a multiple of totalSupply so that atomsPerMolecule is an integer.
        atomBalances[msg.sender] = totalAtoms;
        atomsPerMolecule = totalAtoms.div(totalSupply);

        emit Transfer(address(0), msg.sender, totalSupply);
    }

    /**
     * @param newRebaseOracle The address of the new oracle for rebasement (used for authentication).
     */
    function setRebaseOracle(address newRebaseOracle) external {
        require(msg.sender == owner, "Can only be executed by owner.");
        rebaseOracle = newRebaseOracle;

        emit LogNewRebaseOracle(rebaseOracle);
    }

    /**
     * @dev Propose a new owner.
     * @param _newOwner The address of the new owner.
     */
    function transferOwnership(address _newOwner) public
    {
        require(msg.sender == owner, "Can only be executed by owner.");
        require(_newOwner != address(0), "0x00 address not allowed.");
        newOwner = _newOwner;
    }

    /**
     * @dev Accept new owner.
     */
    function acceptOwnership() public
    {
        require(msg.sender == newOwner, "Sender not authorized.");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
        newOwner = address(0);
    }

    /**
     * @dev Notifies Benchmark contract about a new rebase cycle.
     * @param supplyDelta The number of new molecule tokens to add into or remove from circulation.
     * @param increaseSupply Whether to increase or decrease the total supply.
     * @return The total number of molecules after the supply adjustment.
     */
    function rebase(uint256 supplyDelta, bool increaseSupply) external returns (uint256) {
        require(msg.sender == rebaseOracle, "Can only be executed by rebaseOracle.");
        
        if (supplyDelta == 0) {
            emit LogRebase(totalSupply);
            return totalSupply;
        }

        if (increaseSupply == true) {
            totalSupply = totalSupply.add(supplyDelta);
        } else {
            totalSupply = totalSupply.sub(supplyDelta);
        }

        if (totalSupply > MAXSUPPLY) {
            totalSupply = MAXSUPPLY;
        }

        atomsPerMolecule = totalAtoms.div(totalSupply);

        emit LogRebase(totalSupply);
        return totalSupply;
    }

    /**
     * @param who The address to query.
     * @return The balance of the specified address.
     */
    function balanceOf(address who) public view returns (uint256) {
        return atomBalances[who].div(atomsPerMolecule);
    }

    /**
     * @dev Transfer tokens to a specified address.
     * @param to The address to transfer to.
     * @param value The amount to be transferred.
     * @return True on success, false otherwise.
     */
    function transfer(address to, uint256 value) public returns (bool) {
        require(to != address(0),"Invalid address.");
        require(to != address(this),"Molecules contract can't receive MARK.");

        uint256 atomValue = value.mul(atomsPerMolecule);

        atomBalances[msg.sender] = atomBalances[msg.sender].sub(atomValue);
        atomBalances[to] = atomBalances[to].add(atomValue);

        emit Transfer(msg.sender, to, value);
        return true;
    }

    /**
     * @dev Function to check the amount of tokens that an owner has allowed to a spender.
     * @param owner_ The address which owns the funds.
     * @param spender The address which will spend the funds.
     * @return The number of tokens still available for the spender.
     */
    function allowance(address owner_, address spender) public view returns (uint256) {
        return allowedMolecules[owner_][spender];
    }