|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity >=0.8.0; |
| 3 | + |
| 4 | +import "openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol"; |
| 5 | + |
| 6 | +import "../../external/redstone/IRedstoneOracle.sol"; |
| 7 | +import "../BasePriceOracle.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @title RedstoneAdapterPriceOracle |
| 11 | + * @notice Returns prices from Redstone. |
| 12 | + * @dev Implements `BasePriceOracle`. |
| 13 | + * @author Veliko Minkov <[email protected]> (https://github.com/vminkov) |
| 14 | + */ |
| 15 | +contract RedstoneAdapterPriceOracleWeETH is BasePriceOracle { |
| 16 | + /** |
| 17 | + * @notice The Redstone oracle contract |
| 18 | + */ |
| 19 | + IRedstoneOracle public REDSTONE_ORACLE; |
| 20 | + |
| 21 | + /** |
| 22 | + * @dev Constructor to set admin, wtoken address and native token USD price feed address |
| 23 | + * @param redstoneOracle The Redstone oracle contract address |
| 24 | + */ |
| 25 | + constructor(address redstoneOracle) { |
| 26 | + REDSTONE_ORACLE = IRedstoneOracle(redstoneOracle); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @notice Internal function returning the price in of `underlying`. |
| 31 | + * @dev will return a price denominated in the native token |
| 32 | + */ |
| 33 | + function _price(address underlying) internal view returns (uint256) { |
| 34 | + // special case for wrsETH |
| 35 | + // if input is wrsETH, we need to get the price of rsETH |
| 36 | + if (underlying == 0x04C0599Ae5A44757c0af6F9eC3b93da8976c150A) { |
| 37 | + underlying = 0x028227c4dd1e5419d11Bb6fa6e661920c519D4F5; |
| 38 | + } |
| 39 | + uint256 priceInUsd = REDSTONE_ORACLE.priceOf(underlying); |
| 40 | + uint256 priceOfNativeInUsd = REDSTONE_ORACLE.priceOfETH(); |
| 41 | + return (priceInUsd * 1e18) / priceOfNativeInUsd; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @notice Returns the price in of `underlying` either in the |
| 46 | + * native token (implements `BasePriceOracle`). |
| 47 | + */ |
| 48 | + function price(address underlying) external view override returns (uint256) { |
| 49 | + return _price(underlying); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @notice Returns the price in WNATIVE of the token underlying `cToken`. |
| 54 | + * @dev Implements the `BasePriceOracle` interface for Ionic pools (and Compound v2). |
| 55 | + * @return Price in WNATIVE of the token underlying `cToken`, scaled by `10 ** (36 - underlyingDecimals)`. |
| 56 | + */ |
| 57 | + function getUnderlyingPrice(ICErc20 cToken) external view override returns (uint256) { |
| 58 | + // Get underlying token address |
| 59 | + address underlying = cToken.underlying(); |
| 60 | + |
| 61 | + uint256 oraclePrice = _price(underlying); |
| 62 | + |
| 63 | + uint256 underlyingDecimals = uint256(ERC20Upgradeable(underlying).decimals()); |
| 64 | + return |
| 65 | + underlyingDecimals <= 18 |
| 66 | + ? uint256(oraclePrice) * (10 ** (18 - underlyingDecimals)) |
| 67 | + : uint256(oraclePrice) / (10 ** (underlyingDecimals - 18)); |
| 68 | + } |
| 69 | +} |
0 commit comments