Skip to content

Commit

Permalink
request changes 2 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
C11online committed Jul 28, 2021
1 parent 5ddc128 commit 68602a6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions contracts/oracles/WstEthOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ contract WstEthOracle is IOracleUsd, Auth {

uint immutable stEthDecimals;

address public immutable wstETH;

address public immutable addressWETH;

uint constant public MAX_SAFE_PRICE_DIFF = 500;
Expand All @@ -34,8 +36,11 @@ contract WstEthOracle is IOracleUsd, Auth {
constructor(address _vaultParameters, address _oracleRegistry, address _wstETH, address _stETHPriceFeed) Auth(_vaultParameters) {
require(_vaultParameters != address(0) && _oracleRegistry != address(0) && _wstETH != address(0) && _stETHPriceFeed != address(0), "Unit Protocol: ZERO_ADDRESS");
oracleRegistry = IOracleRegistry(_oracleRegistry);
addressWETH = IOracleRegistry(_oracleRegistry).WETH();
address _addressWETH = IOracleRegistry(_oracleRegistry).WETH();
require(_addressWETH != address(0), "Unit Protocol: ZERO_ADDRESS");
addressWETH = _addressWETH;
stEthPriceFeed = _stETHPriceFeed;
wstETH = _wstETH;
address stEthToken = IWstEthToken(_wstETH).stETH();
require(stEthToken != address(0), "Unit Protocol: ZERO_ADDRESS");
stEthDecimals = ERC20Like(stEthToken).decimals();
Expand All @@ -51,28 +56,28 @@ contract WstEthOracle is IOracleUsd, Auth {
return stEthDecimals;
}

function _percentage_diff(uint nv, uint ov) private view returns (uint) {
function _percentage_diff(uint nv, uint ov) private pure returns (uint) {
if (nv > ov) {
return ( nv - ov ) * 10000 / ov;
} else {
return ( ov - nv ) * 10000 / ov;
}
}

function has_changed_unsafely(uint256 pool_price, uint256 oracle_price) private view returns (bool) {
function has_changed_unsafely(uint256 pool_price, uint256 oracle_price) private pure returns (bool) {
return _percentage_diff(pool_price, oracle_price) > MAX_SAFE_PRICE_DIFF;
}

// returns Q112-encoded value
function assetToUsd(address bearing, uint amount) public override view returns (uint) {
require(bearing == wstETH, "Unit Protocol: BEARING_IS_NOT_WSTETH");
if (amount == 0) return 0;
uint _qtyStEthByWstEth = IWstEthToken(bearing).getStETHByWstETH(amount);
(uint _poolPriceStEth, bool _isSafePrice, uint _oraclePriceStEth) = IStEthPriceFeed(stEthPriceFeed).full_price_info();
_isSafePrice = _poolPriceStEth <= 10**18 && !has_changed_unsafely(_poolPriceStEth, _oraclePriceStEth);
require(_isSafePrice == true, "Unit Protocol: STETH_PRICE_IS_NOT_SAFE");
uint _decimals = getDecimalsStEth();
uint underlyingAmount = _qtyStEthByWstEth.mul(_poolPriceStEth).div(10**_decimals);
require(addressWETH != address(0), "Unit Protocol: ZERO_ADDRESS");
IOracleUsd _oracleForUnderlying = IOracleUsd(oracleRegistry.oracleByAsset(addressWETH));
require(address(_oracleForUnderlying) != address(0), "Unit Protocol: ORACLE_NOT_FOUND");
return _oracleForUnderlying.assetToUsd(addressWETH, underlyingAmount);
Expand Down

0 comments on commit 68602a6

Please sign in to comment.