// 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 NonFiatCollateral
* @notice Collateral plugin for a nonfiat collateral that requires default checks, such as WBTC.
* Expected: {tok} == {ref}, {ref} is pegged to {target} or defaulting, {target} != {UoA}
*/
contract NonFiatCollateral 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: {target/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 {target/ref}
function tryPrice()
external
view
override
returns (
uint192 low,
uint192 high,
uint192 pegPrice
)
{
pegPrice = chainlinkFeed.price(oracleTimeout); // {target/ref}
// Assumption: {ref/tok} = 1; inherit from `AppreciatingFiatCollateral` if need appreciation
// {UoA/tok} = {UoA/target} * {target/ref} * {ref/tok} (1)
uint192 p = targetUnitChainlinkFeed.price(targetUnitOracleTimeout).mul(pegPrice);
// this oracleError is already the combined total oracle error
uint192 err = p.mul(oracleError, CEIL);
low = p - err;
high = p + err;
// assert(low <= high); obviously true just by inspection
}
}
|