Oracle prices are stored within the pair contracts. To integrate the current oracle price into a smart contract, simply call getOraclePrice0()
and getOraclePrice1()
functions. These functions return the oracle price of the pair at the current time as a U(112,112)-fixed-point number and the timestamp of the latest block that the reserves of the pair contract have been updated.
Copy pragma solidity >=0.7.0 <0.9.0;
import './interfaces/INostraSwapPair.sol';
import './interfaces/IERC20.sol';
contract ETHUSDCOracle {
address public ETHUSDCPair;
address public WETH;
address public USDC;
constructor(address _ETHUSDCPair, address _WETH, address _USDC) {
address token0 = INostraSwapPair(_ETHUSDCPair).token0();
address token1 = INostraSwapPair(_ETHUSDCPair).token1();
require((token0 == _WETH && token1 == _USDC) || (token0 == _USDC && token1 == _WETH), "Invalid Addresses");
ETHUSDCPair = _ETHUSDCPair;
WETH = _WETH;
USDC = _USDC;
}
function ETHAbove3000() external view returns (string memory) {
uint256 ETHPrice;
uint8 decimalWETH = IERC20(WETH).decimals();
uint8 decimalUSDC = IERC20(USDC).decimals();
if (INostraSwapPair(ETHUSDCPair).token0() == WETH) {
(ETHPrice, ) = INostraSwapPair(ETHUSDCPair).getOraclePrice0();
} else {
(ETHPrice, ) = INostraSwapPair(ETHUSDCPair).getOraclePrice1();
}
ETHPrice = ETHPrice * (10 ** decimalWETH) / (10 ** decimalUSDC)
if (ETHPrice > encodeFP(3000)) {
return "BullBullBull";
} else {
return "BearBearBear";
}
}
function encodeFP(uint input) internal pure returns (uint224 retVal) {
require(input < 2 ** 112, "Input too large!");
return uint224(input * (2 ** 112));
}
}