contract MakerDAOPriceFeed is Ownable, EthPriceFeedI {
    using SafeMath for uint256;
    
    uint256 public constant RATE_THRESHOLD_PERCENTAGE = 10;
    uint256 public constant MAKERDAO_FEED_MULTIPLIER = 10**36;

    ReadableI public makerDAOMedianizer;

    uint256 private weiPerUnitRate;

    uint256 private lastTimeUpdated; 
    
    event RateUpdated(uint256 _newRate, uint256 _timeUpdated);

    modifier isValidRate(uint256 _weiPerUnitRate) {
        require(validRate(_weiPerUnitRate));
        _;
    }

    constructor(ReadableI _makerDAOMedianizer) public {
        require(_makerDAOMedianizer != address(0));
        makerDAOMedianizer = _makerDAOMedianizer;

        weiPerUnitRate = convertToRate(_makerDAOMedianizer.read());
        lastTimeUpdated = now;
    }
    
    /// @dev Receives rate from outside oracle
    /// @param _weiPerUnitRate calculated off chain and received in the contract
    function updateRate(uint256 _weiPerUnitRate) 
        external 
        onlyOwner
        isValidRate(_weiPerUnitRate)
    {
        weiPerUnitRate = _weiPerUnitRate;

        lastTimeUpdated = now; 

        emit RateUpdated(_weiPerUnitRate, now);
    }

    function getUnit()
        external
        view 
        returns(string)
    {
        return "USD";
    }

    /// @dev View function to see the rate stored in the contract.
    function getRate() 
        public 
        view 
        returns(uint256)
    {
        return weiPerUnitRate; 
    }

    /// @dev View function to see that last time that the rate was updated. 
    function getLastTimeUpdated()
        public
        view
        returns(uint256)
    {
        return lastTimeUpdated;
    }

    /// @dev Checks that a rate is valid.
    /// @param _weiPerUnitRate The rate to check
    /// @return True iff the rate is valid
    function validRate(uint256 _weiPerUnitRate) public view returns(bool) {
        if (_weiPerUnitRate == 0) return false;

        (bytes32 value, bool valid) = makerDAOMedianizer.peek();

        // If the value from the medianizer is not valid, use the current rate as reference
        uint256 currentRate = valid ? convertToRate(value) : weiPerUnitRate;

        // Get the difference
        uint256 diff = _weiPerUnitRate < currentRate ?  currentRate.sub(_weiPerUnitRate) : _weiPerUnitRate.sub(currentRate);

        return diff <= currentRate.mul(RATE_THRESHOLD_PERCENTAGE).div(100);
    }

    /// @dev Transforms a bytes32 value taken from MakerDAO's Medianizer contract into wei per usd rate
    /// @param _fromMedianizer Value taken from MakerDAO's Medianizer contract
    /// @return The wei per usd rate
    function convertToRate(bytes32 _fromMedianizer) internal pure returns(uint256) {
        uint256 value = uint256(_fromMedianizer);
        return MAKERDAO_FEED_MULTIPLIER.div(value);
    }
}
function convertToRate(bytes32 _fromMedianizer) internal pure returns(uint256) {
    uint256 value = uint256(_fromMedianizer);
    return MAKERDAO_FEED_MULTIPLIER.div(value);
}

/// @dev Transforms a bytes32 value taken from MakerDAO's Medianizer contract into wei per usd rate
/// @param _fromMedianizer Value taken from MakerDAO's Medianizer contract
/// @return The wei per usd rate