/
EthXSpotOracle.sol
51 lines (40 loc) · 1.83 KB
/
EthXSpotOracle.sol
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
import { SpotOracle } from "src/oracles/spot/SpotOracle.sol";
import { IChainlink } from "src/interfaces/IChainlink.sol";
import { WadRayMath } from "src/libraries/math/WadRayMath.sol";
interface IRedstonePriceFeed {
function latestAnswer() external view returns (int256 answer);
}
uint8 constant REDSTONE_DECIMALS = 8;
uint8 constant CHAINLINK_DECIMALS = 8;
contract EthXSpotOracle is SpotOracle {
using WadRayMath for uint256;
IRedstonePriceFeed immutable REDSTONE_ETHX_PRICE_FEED;
IChainlink immutable USD_PER_ETH_CHAINLINK;
constructor(
uint256 _ltv,
address _reserveOracle,
address _redstoneEthXPriceFeed,
address _usdPerEthChainlink
)
SpotOracle(_ltv, _reserveOracle)
{
REDSTONE_ETHX_PRICE_FEED = IRedstonePriceFeed(_redstoneEthXPriceFeed);
USD_PER_ETH_CHAINLINK = IChainlink(_usdPerEthChainlink);
}
// @notice Gets the price of ETHx in ETH.
// @dev redstone oracle returns dollar value per ETHx with 6 decimals.
// This needs to be converted to [wad] and to ETH denomination.
// @return ethPerEthX price of ETHx in ETH [wad]
function getPrice() public view override returns (uint256 ethPerEthX) {
// get price from the protocol feed
// usd per ETHx
uint256 usdPerEthX = uint256(REDSTONE_ETHX_PRICE_FEED.latestAnswer()).scaleUpToWad(REDSTONE_DECIMALS); //
// usd per ETH
(, int256 _usdPerEth,,,) = USD_PER_ETH_CHAINLINK.latestRoundData(); // price of stETH denominated in ETH
uint256 usdPerEth = uint256(_usdPerEth).scaleUpToWad(CHAINLINK_DECIMALS); // price of stETH denominated in ETH
// (USD per ETHx) / (USD per ETH) = (USD per ETHx) * (ETH per USD) = ETH per ETHx
ethPerEthX = usdPerEthX.wadDivDown(usdPerEth);
}
}