all files / plugins/assets/ EURFiatCollateral.sol

100% Statements 7/7
66.67% Branches 4/6
100% Functions 2/2
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                      42× 42× 42× 42×                                 111×     107×     107×     105×   105× 105×       105×      
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.17;
 
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "../../libraries/Fixed.sol";
import "./FiatCollateral.sol";
 
/**
 * @title EURFiatCollateral
 * @notice Collateral plugin for a EUR fiatcoin collateral, like EURT
 * Expected: {tok} == {ref}, {ref} is pegged to {target} or defaults, {target} != {UoA}
 */
contract EURFiatCollateral is FiatCollateral {
    using FixLib for uint192;
    using OracleLib for AggregatorV3Interface;
 
    AggregatorV3Interface public immutable targetUnitChainlinkFeed; // {UoA/target}
    uint48 public immutable targetUnitOracleTimeout; // {s}
 
    /// @param config.chainlinkFeed Feed units:{UoA/ref}
    /// @param targetUnitChainlinkFeed_ Feed units: {UoA/target}
    /// @param targetUnitOracleTimeout_ {s} oracle timeout to use for targetUnitChainlinkFeed
    constructor(
        CollateralConfig memory config,
        AggregatorV3Interface targetUnitChainlinkFeed_,
        uint48 targetUnitOracleTimeout_
    ) FiatCollateral(config) {
        Erequire(address(targetUnitChainlinkFeed_) != address(0), "missing targetUnit feed");
        Erequire(targetUnitOracleTimeout_ > 0, "targetUnitOracleTimeout zero");
        targetUnitChainlinkFeed = targetUnitChainlinkFeed_;
        targetUnitOracleTimeout = targetUnitOracleTimeout_;
    }
 
    /// Can revert, used by other contract functions in order to catch errors
    /// @return low {UoA/tok} The low price estimate
    /// @return high {UoA/tok} The high price estimate
    /// @return pegPrice {UoA/ref}
    function tryPrice()
        external
        view
        override
        returns (
            uint192 low,
            uint192 high,
            uint192 pegPrice
        )
    {
        uint192 pricePerRef = chainlinkFeed.price(oracleTimeout); // {UoA/ref}
 
        // {UoA/target}
        uint192 pricePerTarget = targetUnitChainlinkFeed.price(targetUnitOracleTimeout);
 
        // div-by-zero later
        if (pricePerTarget == 0) {
            return (0, FIX_MAX, 0);
        }
 
        uint192 err = pricePerRef.mul(oracleError, CEIL);
 
        low = pricePerRef - err;
        high = pricePerRef + err;
        // assert(low <= high); obviously true just by inspection
 
        // {target/ref} = {UoA/ref} / {UoA/target}
        pegPrice = pricePerRef.div(pricePerTarget);
    }
}