From fc52a39b0033916df264551d5364ed43f1ca79da Mon Sep 17 00:00:00 2001 From: Narayan Prusty Date: Wed, 8 May 2024 16:47:01 +0530 Subject: [PATCH] fix: use sfrxEthFraxOracle oracle --- contracts/interfaces/ISfrxEthFraxOracle.sol | 6 ++++ contracts/oracles/SFrxETHOracle.sol | 32 +++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 contracts/interfaces/ISfrxEthFraxOracle.sol diff --git a/contracts/interfaces/ISfrxEthFraxOracle.sol b/contracts/interfaces/ISfrxEthFraxOracle.sol new file mode 100644 index 00000000..8d16c435 --- /dev/null +++ b/contracts/interfaces/ISfrxEthFraxOracle.sol @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: BSD-3-Clause +pragma solidity 0.8.25; + +interface ISfrxEthFraxOracle { + function getPrices() external view returns (bool _isbadData, uint256 _priceLow, uint256 _priceHigh); +} diff --git a/contracts/oracles/SFrxETHOracle.sol b/contracts/oracles/SFrxETHOracle.sol index e100f4c8..6f188dc7 100644 --- a/contracts/oracles/SFrxETHOracle.sol +++ b/contracts/oracles/SFrxETHOracle.sol @@ -1,8 +1,9 @@ // SPDX-License-Identifier: BSD-3-Clause pragma solidity 0.8.25; -import { ISfrxETH } from "../interfaces/ISfrxETH.sol"; import { CorrelatedTokenOracle } from "./common/CorrelatedTokenOracle.sol"; +import { ISfrxEthFraxOracle } from "../interfaces/ISfrxEthFraxOracle.sol"; +import { ensureNonzeroAddress } from "@venusprotocol/solidity-utilities/contracts/validators.sol"; import { EXP_SCALE } from "@venusprotocol/solidity-utilities/contracts/constants.sol"; /** @@ -11,19 +12,38 @@ import { EXP_SCALE } from "@venusprotocol/solidity-utilities/contracts/constants * @notice This oracle fetches the price of sfrxETH */ contract SFrxETHOracle is CorrelatedTokenOracle { + /// @notice Address of SfrxEthFraxOracle + /// @custom:oz-upgrades-unsafe-allow state-variable-immutable + ISfrxEthFraxOracle public immutable SFRXETH_FRAX_ORACLE; + + /// @notice Thrown if the price data is invalid + error BadPriceData(); + /// @notice Constructor for the implementation contract. /// @custom:oz-upgrades-unsafe-allow constructor constructor( + address sfrxEthFraxOracle, address sfrxETH, - address frxETH, + address frax, address resilientOracle - ) CorrelatedTokenOracle(sfrxETH, frxETH, resilientOracle) {} + ) CorrelatedTokenOracle(sfrxETH, frax, resilientOracle) { + ensureNonzeroAddress(sfrxEthFraxOracle); + SFRXETH_FRAX_ORACLE = ISfrxEthFraxOracle(sfrxEthFraxOracle); + } /** - * @notice Gets the frxETH for 1 sfrxETH - * @return amount Amount of frxETH + * @notice Gets the FRAX for 1 sfrxETH + * @return amount Amount of FRAX */ function _getUnderlyingAmount() internal view override returns (uint256) { - return ISfrxETH(CORRELATED_TOKEN).convertToAssets(EXP_SCALE); + (bool isBadData, uint256 priceLow, uint256 priceHigh) = SFRXETH_FRAX_ORACLE.getPrices(); + + if (isBadData) revert BadPriceData(); + + // calculate average price + uint256 averagePrice = (priceLow + priceHigh) / 2; + + // return (1 / averagePrice) as the average price is in sfraxETH + return EXP_SCALE * 2 / averagePrice; } }