contract PreDeriveum is ERC20, ERC20Detailed, SafeGuard {
    uint256 public constant INITIAL_SUPPLY = 10000000000;
    DStore public tokenDB;

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC20Detailed("Pre-Deriveum", "PDER", 0) {
        tokenDB = new DStore(INITIAL_SUPPLY);
        require(tokenDB.setModule(address(this), true));
        require(tokenDB.move(address(this), msg.sender, INITIAL_SUPPLY));
        require(tokenDB.transferRoot(msg.sender));
        emit Transfer(address(0), msg.sender, INITIAL_SUPPLY);
    }

    /**
    * @dev Total number of tokens in existence
    */
    function totalSupply() public view returns (uint256) {
        return tokenDB.getTotalSupply();
    }

    /**
    * @dev Gets the balance of the specified address.
    * @param owner The address to query the balance of.
    * @return An uint256 representing the amount owned by the passed address.
    */
    function balanceOf(address owner) public view returns (uint256) {
        return tokenDB.getBalance(owner);
    }

    /**
     * @dev Function to check the amount of tokens that an owner allowed to a spender.
     * @param owner address The address which owns the funds.
     * @param spender address The address which will spend the funds.
     * @return A uint256 specifying the amount of tokens still available for the spender.
     */
    function allowance(address owner, address spender) public view returns (uint256) {
        return tokenDB.getAllowance(owner, spender);
    }

    /**
     * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
     * Beware that changing an allowance with this method brings the risk that someone may use both the old
     * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
     * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     * @param spender The address which will spend the funds.
     * @param value The amount of tokens to be spent.
     */
    function approve(address spender, uint256 value) public returns (bool) {
        require(spender != address(0));

        require(tokenDB.setApprove(msg.sender, spender, value));
        emit Approval(msg.sender, spender, value);
        return true;
    }

    /**
     * @dev Transfer tokens from one address to another
     * @param from address The address which you want to send tokens from
     * @param to address The address which you want to transfer to
     * @param value uint256 the amount of tokens to be transferred
     */
    function transferFrom(address from, address to, uint256 value) public returns (bool) {
        uint256 allow = tokenDB.getAllowance(from, msg.sender);
        allow = allow.sub(value);
        require(tokenDB.setApprove(from, msg.sender, allow));
        _transfer(from, to, value);
        return true;
    }

    /**
     * @dev Increase the amount of tokens that an owner allowed to a spender.
     * approve should be called when allowed_[_spender] == 0. To increment
     * allowed value is better to use this function to avoid 2 calls (and wait until
     * the first transaction is mined)
     * From MonolithDAO Token.sol
     * @param spender The address which will spend the funds.
     * @param addedValue The amount of tokens to increase the allowance by.
     */
    function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
        require(spender != address(0));

        uint256 allow = tokenDB.getAllowance(msg.sender, spender);
        allow = allow.add(addedValue);
        require(tokenDB.setApprove(msg.sender, spender, allow));
        emit Approval(msg.sender, spender, allow);
        return true;
    }

    /**
     * @dev Decrease the amount of tokens that an owner allowed to a spender.
     * approve should be called when allowed_[_spender] == 0. To decrement
     * allowed value is better to use this function to avoid 2 calls (and wait until
     * the first transaction is mined)
     * From MonolithDAO Token.sol
     * @param spender The address which will spend the funds.
     * @param subtractedValue The amount of tokens to decrease the allowance by.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
        require(spender != address(0));

        uint256 allow = tokenDB.getAllowance(msg.sender, spender);
        allow = allow.sub(subtractedValue);
        require(tokenDB.setApprove(msg.sender, spender, allow));
        emit Approval(msg.sender, spender, allow);
        return true;
    }

    /**
     * @dev Transfer token for a specified addresses
     * @param from The address to transfer from.
     * @param to The address to transfer to.
     * @param value The amount to be transferred.
     */
    function _transfer(address from, address to, uint256 value) internal {
        require(to != address(0));

        require(tokenDB.move(from, to, value));
        emit Transfer(from, to, value);
    }
}
/**
 * @title PreDeriveum
 * @dev The Deriveum pre token.
 *
 */
function balanceOf(address owner) public view returns (uint256) {
    return tokenDB.getBalance(owner);
}

/**
 * @dev Gets the balance of the specified address.
 * @param owner The address to query the balance of.
 * @return An uint256 representing the amount owned by the passed address.
 */