This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOTOracleWrapper.sol
66 lines (55 loc) · 1.89 KB
/
HOTOracleWrapper.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
import {IOracleWrapper} from "../interfaces/IOracleWrapper.sol";
import {IHOTOracle} from
"@valantis-hot/contracts/interfaces/IHOTOracle.sol";
import {FullMath} from "@v3-lib-0.8/contracts/FullMath.sol";
contract HOTOracleWrapper is IOracleWrapper {
// #region immutable public variable.
IHOTOracle public immutable oracle;
uint8 public immutable decimals0;
uint8 public immutable decimals1;
// #endregion immutable public variable.
constructor(address oracle_, uint8 decimals0_, uint8 decimals1_) {
if (oracle_ == address(0)) {
revert AddressZero();
}
if (decimals0_ == 0) {
revert DecimalsToken0Zero();
}
if (decimals1_ == 0) {
revert DecimalsToken1Zero();
}
oracle = IHOTOracle(oracle_);
decimals0 = decimals0_;
decimals1 = decimals1_;
}
function getPrice0() public view returns (uint256 price0) {
uint256 priceX96 = oracle.getSqrtOraclePriceX96();
if (priceX96 <= type(uint128).max) {
price0 = FullMath.mulDiv(
priceX96 * priceX96, 10 ** decimals0, 2 ** 192
);
} else {
price0 = FullMath.mulDiv(
FullMath.mulDiv(priceX96, priceX96, 1 << 64),
10 ** decimals0,
1 << 128
);
}
}
function getPrice1() public view returns (uint256 price1) {
uint256 priceX96 = oracle.getSqrtOraclePriceX96();
if (priceX96 <= type(uint128).max) {
price1 = FullMath.mulDiv(
2 ** 192, 10 ** decimals1, priceX96 * priceX96
);
} else {
price1 = FullMath.mulDiv(
1 << 128,
10 ** decimals1,
FullMath.mulDiv(priceX96, priceX96, 1 << 64)
);
}
}
}