-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriceFeed.sol
128 lines (106 loc) · 4.39 KB
/
PriceFeed.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "../IOjo.sol";
import "../OjoTypes.sol";
/// @title Contract for calling Ojo's oracle contract with chainlink's AggregatorV3Interface implemented.
/// @author Ojo Network (https://docs.ojo.network/)
contract PriceFeed is Initializable, AggregatorV3Interface {
uint8 private priceFeedDecimals;
string private priceFeedDescription;
IOjo public immutable ojo;
uint80 constant DEFAULT_ROUND = 1;
uint256 constant DEFAULT_VERSION = 1;
uint256 internal constant INT256_MAX = uint256(type(int256).max);
error GetRoundDataCanBeOnlyCalledWithLatestRound(uint80 requestedRoundId);
error UnsafeUintToIntConversion(uint256 value);
constructor(address ojo_) {
ojo = IOjo(ojo_);
}
/// @notice Initialize clone of this contract.
/// @dev This function is used in place of a constructor in proxy contracts.
/// @param _priceFeedDecimals Amount of decimals a PriceFeed is denominiated in.
/// @param _priceFeedDescription Description of PriceFeed.
function initialize(uint8 _priceFeedDecimals, string calldata _priceFeedDescription)
external
initializer {
priceFeedDecimals = _priceFeedDecimals;
priceFeedDescription = _priceFeedDescription;
}
/// @notice Amount of decimals price is denominated in.
function decimals() external view returns (uint8) {
return priceFeedDecimals;
}
/// @notice Asset that this proxy is tracking.
/// @dev This should be set as the asset symbol ticker as it used to query the Ojo contract.
function description() external view returns (string memory) {
return priceFeedDescription;
}
/// @notice Version always returns 1.
function version() external view returns (uint256) {
return DEFAULT_VERSION;
}
/// @dev Latest round always returns 1 since this contract does not support rounds.
function latestRound() public pure returns (uint80) {
return DEFAULT_ROUND;
}
/// @notice Fetches price data from Ojo contract from a specified round.
/// @dev Even though rounds are not utilized in this contract getRoundData is implemented for contracts
/// that still rely on it. Function will revert if specified round is not the latest round.
/// @return roundId Round ID of price data, this is always set to 1.
/// @return answer Price in USD of asset this contract is tracking.
/// @return startedAt Timestamp relating to price update.
/// @return updatedAt Timestamp relating to price update.
/// @return answeredInRound Equal to round ID.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
if (_roundId != latestRound()) {
revert GetRoundDataCanBeOnlyCalledWithLatestRound(_roundId);
}
return latestRoundData();
}
/// @notice Fetches latest price data from Ojo contract.
/// @return roundId Round ID of price data, this is always set to 1.
/// @return answer Price in USD of asset this contract is tracking.
/// @return startedAt Timestamp relating to price update.
/// @return updatedAt Timestamp relating to price update.
/// @return answeredInRound Equal to round ID.
function latestRoundData()
public
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
roundId = latestRound();
bytes32 assetName = bytes32(bytes(priceFeedDescription));
OjoTypes.PriceData memory priceData = ojo.getPriceData(assetName);
if (priceData.price > INT256_MAX) {
revert UnsafeUintToIntConversion(priceData.price);
}
// These values are equal after chainlink’s OCR update
startedAt = priceData.resolveTime;
updatedAt = priceData.resolveTime;
// roundId is always equal to answeredInRound
answeredInRound = roundId;
return (
roundId,
int256(priceData.price),
startedAt,
updatedAt,
answeredInRound
);
}
}