-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChainlinkOracle.sol
179 lines (155 loc) · 6.65 KB
/
ChainlinkOracle.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.17;
import "./PriceOracle.sol";
import "../MErc20.sol";
import "../EIP20Interface.sol";
import "../SafeMath.sol";
import "./AggregatorV3Interface.sol";
/// @notice contract that stores all chainlink oracle addresses for each respective underlying asset
contract ChainlinkOracle is PriceOracle {
/// @dev redundant as used with solidity 0.8.0,
/// but used to not make code changes around math
using SafeMath for uint256;
/// @notice Administrator for this contract
address public admin;
/// @notice this is deprecated and will not be used
bytes32 public nativeToken;
/// @notice overridden prices for assets, not used if unset
mapping (address => uint256) internal prices;
/// @notice chainlink feeds for assets, maps the hash of a
/// token symbol to the corresponding chainlink feed
mapping (bytes32 => AggregatorV3Interface) internal feeds;
/// @notice emitted when a new price override by admin is posted
event PricePosted(
address asset,
uint256 previousPriceMantissa,
uint256 requestedPriceMantissa,
uint256 newPriceMantissa
);
/// @notice emitted when a new admin is set
event NewAdmin(address oldAdmin, address newAdmin);
/// @notice emitted when a new feed is set
event FeedSet(address feed, string symbol);
/// @param _nativeToken The native token symbol, unused in this deployment so it can be anything
constructor(string memory _nativeToken) {
admin = msg.sender;
nativeToken = keccak256(abi.encodePacked(_nativeToken));
}
/// @notice Admin only modifier
modifier onlyAdmin() {
require(msg.sender == admin, "only admin may call");
_;
}
/// @notice Get the underlying price of a listed mToken asset
/// @param mToken The mToken to get the underlying price of
/// @return The underlying asset price mantissa scaled by 1e18
function getUnderlyingPrice(
MToken mToken
) public view override returns (uint256) {
string memory symbol = mToken.symbol();
if (keccak256(abi.encodePacked(symbol)) == nativeToken) { /// @dev this branch should never get called as native tokens are not supported on this deployment
return getChainlinkPrice(getFeed(symbol));
} else {
return getPrice(mToken);
}
}
/// @notice Get the underlying price of a token
/// @param mToken The mToken to get the underlying price of
/// @return price The underlying asset price mantissa scaled by 1e18
/// @dev if the admin sets the price override, this function will
/// return that instead of the chainlink price
function getPrice(MToken mToken) internal view returns (uint256 price) {
EIP20Interface token = EIP20Interface(
MErc20(address(mToken)).underlying()
);
if (prices[address(token)] != 0) {
price = prices[address(token)];
} else {
price = getChainlinkPrice(getFeed(token.symbol()));
}
uint256 decimalDelta = uint256(18).sub(uint256(token.decimals()));
// Ensure that we don't multiply the result by 0
if (decimalDelta > 0) {
return price.mul(10 ** decimalDelta);
} else {
return price;
}
}
/// @notice Get the price of a token from Chainlink
/// @param feed The Chainlink feed to get the price of
/// @return The price of the asset from Chainlink scaled by 1e18
function getChainlinkPrice(
AggregatorV3Interface feed
) internal view returns (uint256) {
(, int256 answer, , uint256 updatedAt, ) = AggregatorV3Interface(feed)
.latestRoundData();
require(answer > 0, "Chainlink price cannot be lower than 0");
require(updatedAt != 0, "Round is in incompleted state");
// Chainlink USD-denominated feeds store answers at 8 decimals
uint256 decimalDelta = uint256(18).sub(feed.decimals());
// Ensure that we don't multiply the result by 0
if (decimalDelta > 0) {
return uint256(answer).mul(10 ** decimalDelta);
} else {
return uint256(answer);
}
}
/// @notice Set the price of an asset overriding the value returned from Chainlink
/// @param mToken The mToken to set the price of
/// @param underlyingPriceMantissa The price scaled by mantissa of the asset
function setUnderlyingPrice(
MToken mToken,
uint256 underlyingPriceMantissa
) external onlyAdmin {
address asset = address(MErc20(address(mToken)).underlying());
emit PricePosted(
asset,
prices[asset],
underlyingPriceMantissa,
underlyingPriceMantissa
);
prices[asset] = underlyingPriceMantissa;
}
/// @notice Set the price of an asset overriding the value returned from Chainlink
/// @param asset The asset to set the price of
/// @param price The price scaled by 1e18 of the asset
function setDirectPrice(address asset, uint256 price) external onlyAdmin {
emit PricePosted(asset, prices[asset], price, price);
prices[asset] = price;
}
/// @notice Set the chainlink feed for a given token symbol
/// @param symbol The symbol of the mToken's underlying token to set the feed for
/// if the underlying token has symbol of MKR, the symbol would be "MKR"
/// @param feed The address of the chainlink feed
function setFeed(string calldata symbol, address feed) external onlyAdmin {
require(
feed != address(0) && feed != address(this),
"invalid feed address"
);
emit FeedSet(feed, symbol);
feeds[keccak256(abi.encodePacked(symbol))] = AggregatorV3Interface(
feed
);
}
/// @notice Get the chainlink feed for a given token symbol
/// @param symbol The symbol of the mToken's underlying token to get the feed for
/// @return The address of the chainlink feed
function getFeed(
string memory symbol
) public view returns (AggregatorV3Interface) {
return feeds[keccak256(abi.encodePacked(symbol))];
}
/// @notice Get the price of an asset from the override config
/// @param asset The asset to get the price of
/// @return The price of the asset scaled by 1e18
function assetPrices(address asset) external view returns (uint256) {
return prices[asset];
}
/// @notice Set the admin address
/// @param newAdmin The new admin address
function setAdmin(address newAdmin) external onlyAdmin {
address oldAdmin = admin;
admin = newAdmin;
emit NewAdmin(oldAdmin, newAdmin);
}
}