This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChainlinkAdapterOracle.sol
85 lines (70 loc) · 3.08 KB
/
ChainlinkAdapterOracle.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "./BaseAdapter.sol";
import "../utils/BlueBerryErrors.sol";
import "../interfaces/IBaseOracle.sol";
import "../interfaces/chainlink/IFeedRegistry.sol";
contract ChainlinkAdapterOracle is IBaseOracle, BaseAdapter {
using SafeCast for int256;
// Chainlink denominations
// (source: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/Denominations.sol)
IFeedRegistry public registry;
address public constant USD = address(840);
/// @dev Mapping from original token to remapped token for price querying (e.g. WBTC -> BTC, renBTC -> BTC)
mapping(address => address) public remappedTokens;
event SetRegistry(address registry);
event SetTokenRemapping(
address indexed token,
address indexed remappedToken
);
constructor(IFeedRegistry registry_) {
if (address(registry_) == address(0)) revert ZERO_ADDRESS();
registry = registry_;
}
/// @dev Set chainlink feed registry source
/// @param _registry Chainlink feed registry source
function setFeedRegistry(IFeedRegistry _registry) external onlyOwner {
if (address(_registry) == address(0)) revert ZERO_ADDRESS();
registry = _registry;
emit SetRegistry(address(_registry));
}
/// @dev Set token remapping
/// @param _tokens List of tokens to set remapping
/// @param _remappedTokens List of tokens to set remapping to
/// @notice Token decimals of the original and remapped tokens should be the same
function setTokenRemappings(
address[] calldata _tokens,
address[] calldata _remappedTokens
) external onlyOwner {
if (_remappedTokens.length != _tokens.length)
revert INPUT_ARRAY_MISMATCH();
for (uint256 idx = 0; idx < _tokens.length; idx++) {
if (_remappedTokens[idx] == address(0)) revert ZERO_ADDRESS();
if (_tokens[idx] == address(0)) revert ZERO_ADDRESS();
remappedTokens[_tokens[idx]] = _remappedTokens[idx];
emit SetTokenRemapping(_tokens[idx], _remappedTokens[idx]);
}
}
/**
* @notice Returns the USD based price of given token, price value has 18 decimals
* @param _token Token address to get price of
* @return price USD price of token in 18 decimal
*/
function getPrice(address _token) external view override returns (uint256) {
// remap token if possible
address token = remappedTokens[_token];
if (token == address(0)) token = _token;
uint256 maxDelayTime = maxDelayTimes[token];
if (maxDelayTime == 0) revert NO_MAX_DELAY(_token);
// try to get token-USD price
uint256 decimals = registry.decimals(token, USD);
(, int256 answer, , uint256 updatedAt, ) = registry.latestRoundData(
token,
USD
);
if (updatedAt < block.timestamp - maxDelayTime)
revert PRICE_OUTDATED(_token);
return (answer.toUint256() * 1e18) / 10**decimals;
}
}