@venusprotocol/oracle 1.0.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +117 -0
- package/contracts/ResilientOracle.sol +376 -0
- package/contracts/interfaces/FeedRegistryInterface.sol +14 -0
- package/contracts/interfaces/OracleInterface.sol +22 -0
- package/contracts/interfaces/PublicResolverInterface.sol +7 -0
- package/contracts/interfaces/PythInterface.sol +249 -0
- package/contracts/interfaces/SIDRegistryInterface.sol +7 -0
- package/contracts/interfaces/VBep20Interface.sol +11 -0
- package/contracts/libraries/PancakeLibrary.sol +68 -0
- package/contracts/oracles/BinanceOracle.sol +125 -0
- package/contracts/oracles/BoundValidator.sol +149 -0
- package/contracts/oracles/ChainlinkOracle.sol +214 -0
- package/contracts/oracles/PythOracle.sol +174 -0
- package/contracts/oracles/TwapOracle.sol +309 -0
- package/contracts/oracles/mocks/MockBinanceFeedRegistry.sol +29 -0
- package/contracts/oracles/mocks/MockBinanceOracle.sol +22 -0
- package/contracts/oracles/mocks/MockChainlinkOracle.sol +27 -0
- package/contracts/oracles/mocks/MockPythOracle.sol +32 -0
- package/contracts/oracles/mocks/MockTwapOracle.sol +31 -0
- package/contracts/test/AccessControlManager.sol +6 -0
- package/contracts/test/BEP20Harness.sol +16 -0
- package/contracts/test/MockPyth.sol +123 -0
- package/contracts/test/MockSimpleOracle.sol +41 -0
- package/contracts/test/MockV3Aggregator.sol +76 -0
- package/contracts/test/PancakePairHarness.sol +90 -0
- package/contracts/test/VBEP20Harness.sol +20 -0
- package/deploy/1-deploy-oracles.ts +195 -0
- package/deploy/2-configure-feeds.ts +324 -0
- package/deployments/.gitkeep +0 -0
- package/deployments/bscmainnet/.chainId +1 -0
- package/deployments/bscmainnet/BinanceOracle.json +329 -0
- package/deployments/bscmainnet/BinanceOracle_Implementation.json +259 -0
- package/deployments/bscmainnet/BinanceOracle_Proxy.json +227 -0
- package/deployments/bscmainnet/BoundValidator.json +621 -0
- package/deployments/bscmainnet/BoundValidator_Implementation.json +714 -0
- package/deployments/bscmainnet/BoundValidator_Proxy.json +251 -0
- package/deployments/bscmainnet/ChainlinkOracle.json +683 -0
- package/deployments/bscmainnet/ChainlinkOracle_Implementation.json +826 -0
- package/deployments/bscmainnet/ChainlinkOracle_Proxy.json +237 -0
- package/deployments/bscmainnet/DefaultProxyAdmin.json +257 -0
- package/deployments/bscmainnet/PythOracle.json +681 -0
- package/deployments/bscmainnet/PythOracle_Implementation.json +798 -0
- package/deployments/bscmainnet/PythOracle_Proxy.json +264 -0
- package/deployments/bscmainnet/ResilientOracle.json +832 -0
- package/deployments/bscmainnet/ResilientOracle_Implementation.json +1047 -0
- package/deployments/bscmainnet/ResilientOracle_Proxy.json +237 -0
- package/deployments/bscmainnet/TwapOracle.json +900 -0
- package/deployments/bscmainnet/TwapOracle_Implementation.json +1124 -0
- package/deployments/bscmainnet/TwapOracle_Proxy.json +251 -0
- package/deployments/bscmainnet/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
- package/deployments/bscmainnet/solcInputs/68ea814c0d6925581f2bda5deba8ce4a.json +60 -0
- package/deployments/bscmainnet/solcInputs/8658c669dade0e9ab80874eab3bf3468.json +117 -0
- package/deployments/bsctestnet/.chainId +1 -0
- package/deployments/bsctestnet/BinanceOracle.json +571 -0
- package/deployments/bsctestnet/BinanceOracle_Implementation.json +629 -0
- package/deployments/bsctestnet/BinanceOracle_Proxy.json +251 -0
- package/deployments/bsctestnet/BoundValidator.json +621 -0
- package/deployments/bsctestnet/BoundValidator_Implementation.json +714 -0
- package/deployments/bsctestnet/BoundValidator_Proxy.json +251 -0
- package/deployments/bsctestnet/ChainlinkOracle.json +697 -0
- package/deployments/bsctestnet/ChainlinkOracle_Implementation.json +826 -0
- package/deployments/bsctestnet/ChainlinkOracle_Proxy.json +251 -0
- package/deployments/bsctestnet/DefaultProxyAdmin.json +257 -0
- package/deployments/bsctestnet/PythOracle.json +681 -0
- package/deployments/bsctestnet/PythOracle_Implementation.json +798 -0
- package/deployments/bsctestnet/PythOracle_Proxy.json +264 -0
- package/deployments/bsctestnet/ResilientOracle.json +846 -0
- package/deployments/bsctestnet/ResilientOracle_Implementation.json +1047 -0
- package/deployments/bsctestnet/ResilientOracle_Proxy.json +251 -0
- package/deployments/bsctestnet/TwapOracle.json +887 -0
- package/deployments/bsctestnet/TwapOracle_Implementation.json +1111 -0
- package/deployments/bsctestnet/TwapOracle_Proxy.json +251 -0
- package/deployments/bsctestnet/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
- package/deployments/bsctestnet/solcInputs/4769dd98af512bf3b6b6504ad0c3bd74.json +78 -0
- package/deployments/bsctestnet/solcInputs/f35da22a430a956ee338e720b5898e3d.json +126 -0
- package/networks/mainnet.json +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
pragma solidity 0.8.13;
|
|
3
|
+
|
|
4
|
+
import "../interfaces/VBep20Interface.sol";
|
|
5
|
+
import "../interfaces/OracleInterface.sol";
|
|
6
|
+
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol";
|
|
7
|
+
import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
|
|
8
|
+
|
|
9
|
+
struct TokenConfig {
|
|
10
|
+
/// @notice Underlying token address, which can't be a null address and can be used to check if a token is supported
|
|
11
|
+
/// @notice 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB for BNB
|
|
12
|
+
address asset;
|
|
13
|
+
/// @notice Chainlink feed address
|
|
14
|
+
address feed;
|
|
15
|
+
/// @notice Price expiration period of this asset
|
|
16
|
+
uint256 maxStalePeriod;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
contract ChainlinkOracle is AccessControlledV8, OracleInterface {
|
|
20
|
+
/// @notice vBNB address
|
|
21
|
+
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
|
|
22
|
+
address public immutable vBnb;
|
|
23
|
+
|
|
24
|
+
/// @notice VAI address
|
|
25
|
+
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
|
|
26
|
+
address public immutable vai;
|
|
27
|
+
|
|
28
|
+
/// @notice Set this as asset address for BNB. This is the underlying address for vBNB
|
|
29
|
+
address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
|
|
30
|
+
|
|
31
|
+
/// @notice Manually set an override price, useful under extenuating conditions such as price feed failure
|
|
32
|
+
mapping(address => uint256) public prices;
|
|
33
|
+
|
|
34
|
+
/// @notice Token config by assets
|
|
35
|
+
mapping(address => TokenConfig) public tokenConfigs;
|
|
36
|
+
|
|
37
|
+
/// @notice Emit when a price is manually set
|
|
38
|
+
event PricePosted(
|
|
39
|
+
address indexed asset,
|
|
40
|
+
uint256 previousPriceMantissa,
|
|
41
|
+
uint256 requestedPriceMantissa,
|
|
42
|
+
uint256 newPriceMantissa
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/// @notice Emit when a token config is added
|
|
46
|
+
event TokenConfigAdded(address indexed asset, address feed, uint256 maxStalePeriod);
|
|
47
|
+
|
|
48
|
+
modifier notNullAddress(address someone) {
|
|
49
|
+
if (someone == address(0)) revert("can't be zero address");
|
|
50
|
+
_;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// @notice Constructor for the implementation contract. Sets immutable variables.
|
|
54
|
+
/// @param vBnbAddress The address of the vBNB
|
|
55
|
+
/// @param vaiAddress The address of the VAI
|
|
56
|
+
/// @custom:oz-upgrades-unsafe-allow constructor
|
|
57
|
+
constructor(address vBnbAddress, address vaiAddress) notNullAddress(vBnbAddress) notNullAddress(vaiAddress) {
|
|
58
|
+
vBnb = vBnbAddress;
|
|
59
|
+
vai = vaiAddress;
|
|
60
|
+
_disableInitializers();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @notice Set the forced prices of the underlying token of input vToken
|
|
65
|
+
* @param vToken vToken address
|
|
66
|
+
* @param underlyingPriceMantissa price in 18 decimals
|
|
67
|
+
* @custom:access Only Governance
|
|
68
|
+
* @custom:error NotNullAddress thrown if address of vToken is null
|
|
69
|
+
* @custom:event Emits PricePosted event on succesfully setup of underlying price
|
|
70
|
+
*/
|
|
71
|
+
function setUnderlyingPrice(
|
|
72
|
+
VBep20Interface vToken,
|
|
73
|
+
uint256 underlyingPriceMantissa
|
|
74
|
+
) external notNullAddress(address(vToken)) {
|
|
75
|
+
_checkAccessAllowed("setUnderlyingPrice(VBep20Interface,uint256)");
|
|
76
|
+
|
|
77
|
+
address asset = address(vToken) == vBnb ? BNB_ADDR : address(vToken.underlying());
|
|
78
|
+
emit PricePosted(asset, prices[asset], underlyingPriceMantissa, underlyingPriceMantissa);
|
|
79
|
+
prices[asset] = underlyingPriceMantissa;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @notice Manually set the price of a given asset
|
|
84
|
+
* @param asset Asset address
|
|
85
|
+
* @param price Underlying price in 18 decimals
|
|
86
|
+
* @custom:access Only Governance
|
|
87
|
+
* @custom:event Emits PricePosted event on succesfully setup of underlying price
|
|
88
|
+
*/
|
|
89
|
+
function setDirectPrice(address asset, uint256 price) external notNullAddress(asset) {
|
|
90
|
+
_checkAccessAllowed("setDirectPrice(address,uint256)");
|
|
91
|
+
|
|
92
|
+
prices[asset] = price;
|
|
93
|
+
emit PricePosted(asset, prices[asset], price, price);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @notice Add multiple token configs at the same time
|
|
98
|
+
* @param tokenConfigs_ config array
|
|
99
|
+
* @custom:access Only Governance
|
|
100
|
+
* @custom:error Zero length error thrown, if length of the array in parameter is 0
|
|
101
|
+
*/
|
|
102
|
+
function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external {
|
|
103
|
+
_checkAccessAllowed("setTokenConfigs(TokenConfig[])");
|
|
104
|
+
if (tokenConfigs_.length == 0) revert("length can't be 0");
|
|
105
|
+
uint256 numTokenConfigs = tokenConfigs_.length;
|
|
106
|
+
for (uint256 i; i < numTokenConfigs; ++i) {
|
|
107
|
+
setTokenConfig(tokenConfigs_[i]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @notice Initializes the owner of the contract
|
|
113
|
+
* @param accessControlManager_ Address of the access control manager contract
|
|
114
|
+
*/
|
|
115
|
+
function initialize(address accessControlManager_) public initializer {
|
|
116
|
+
__AccessControlled_init(accessControlManager_);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @notice Add single token config. vToken & feed cannot be null addresses and maxStalePeriod must be positive
|
|
121
|
+
* @param tokenConfig Token config struct
|
|
122
|
+
* @custom:access Only Governance
|
|
123
|
+
* @custom:error NotNullAddress error is thrown if asset address is null
|
|
124
|
+
* @custom:error NotNullAddress error is thrown if token feed address is null
|
|
125
|
+
* @custom:error Range error is thrown if maxStale period of token is not greater than zero
|
|
126
|
+
* @custom:event Emits TokenConfigAdded event on succesfully setting of the token config
|
|
127
|
+
*/
|
|
128
|
+
function setTokenConfig(
|
|
129
|
+
TokenConfig memory tokenConfig
|
|
130
|
+
) public notNullAddress(tokenConfig.asset) notNullAddress(tokenConfig.feed) {
|
|
131
|
+
_checkAccessAllowed("setTokenConfig(TokenConfig)");
|
|
132
|
+
|
|
133
|
+
if (tokenConfig.maxStalePeriod == 0) revert("stale period can't be zero");
|
|
134
|
+
tokenConfigs[tokenConfig.asset] = tokenConfig;
|
|
135
|
+
emit TokenConfigAdded(tokenConfig.asset, tokenConfig.feed, tokenConfig.maxStalePeriod);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @notice Gets the Chainlink price for the underlying asset of a given vToken, revert when vToken is a null address
|
|
140
|
+
* @param vToken vToken address
|
|
141
|
+
* @return price Underlying price in USD
|
|
142
|
+
*/
|
|
143
|
+
function getUnderlyingPrice(address vToken) public view override returns (uint256) {
|
|
144
|
+
return _getUnderlyingPriceInternal(VBep20Interface(vToken));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @notice Gets the Chainlink price for the underlying asset of a given vToken
|
|
149
|
+
* or the manually set price if it's been set
|
|
150
|
+
* @dev The decimals of the underlying token are considered to ensure the returned price
|
|
151
|
+
* has 18 decimals of precision
|
|
152
|
+
* @param vToken vToken address
|
|
153
|
+
* @return price Underlying price in USD
|
|
154
|
+
*/
|
|
155
|
+
function _getUnderlyingPriceInternal(VBep20Interface vToken) internal view returns (uint256 price) {
|
|
156
|
+
address token;
|
|
157
|
+
uint256 decimals;
|
|
158
|
+
|
|
159
|
+
// vBNB token doesn't have `underlying` method
|
|
160
|
+
if (address(vToken) == vBnb) {
|
|
161
|
+
token = BNB_ADDR;
|
|
162
|
+
decimals = 18;
|
|
163
|
+
} else if (address(vToken) == vai) {
|
|
164
|
+
token = vai;
|
|
165
|
+
decimals = 18;
|
|
166
|
+
} else {
|
|
167
|
+
token = vToken.underlying();
|
|
168
|
+
decimals = VBep20Interface(token).decimals();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
uint256 tokenPrice = prices[token];
|
|
172
|
+
if (tokenPrice != 0) {
|
|
173
|
+
price = tokenPrice;
|
|
174
|
+
} else {
|
|
175
|
+
price = _getChainlinkPrice(token);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
uint256 decimalDelta = uint256(18) - uint256(decimals);
|
|
179
|
+
return price * (10 ** decimalDelta);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @notice Get the Chainlink price for the underlying asset of a given vToken, revert if token config doesn't exit
|
|
184
|
+
* @dev The precision of the price feed is used to ensure the returned price has 18 decimals of precision
|
|
185
|
+
* @param asset Underlying asset address
|
|
186
|
+
* @return price Underlying price in USD, with 18 decimals of precision
|
|
187
|
+
* @custom:error NotNullAddress error is thrown if the asset address is null
|
|
188
|
+
* @custom:error Price error is thrown if the Chainlink price of asset is not greater than zero
|
|
189
|
+
* @custom:error Timing error is thrown if current timestamp is less than the last updatedAt timestamp
|
|
190
|
+
* @custom:error Timing error is thrown if time difference between current time and last updated time
|
|
191
|
+
* is greater than maxStalePeriod
|
|
192
|
+
*/
|
|
193
|
+
function _getChainlinkPrice(
|
|
194
|
+
address asset
|
|
195
|
+
) internal view notNullAddress(tokenConfigs[asset].asset) returns (uint256) {
|
|
196
|
+
TokenConfig memory tokenConfig = tokenConfigs[asset];
|
|
197
|
+
AggregatorV2V3Interface feed = AggregatorV2V3Interface(tokenConfig.feed);
|
|
198
|
+
|
|
199
|
+
// note: maxStalePeriod cannot be 0
|
|
200
|
+
uint256 maxStalePeriod = tokenConfig.maxStalePeriod;
|
|
201
|
+
|
|
202
|
+
// Chainlink USD-denominated feeds store answers at 8 decimals, mostly
|
|
203
|
+
uint256 decimalDelta = uint256(18) - feed.decimals();
|
|
204
|
+
|
|
205
|
+
(, int256 answer, , uint256 updatedAt, ) = feed.latestRoundData();
|
|
206
|
+
if (answer == 0) revert("chainlink price must be positive");
|
|
207
|
+
if (block.timestamp < updatedAt) revert("updatedAt exceeds block time");
|
|
208
|
+
|
|
209
|
+
uint256 deltaTime = block.timestamp - updatedAt;
|
|
210
|
+
if (deltaTime > maxStalePeriod) revert("chainlink price expired");
|
|
211
|
+
|
|
212
|
+
return uint256(answer) * (10 ** decimalDelta);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
pragma solidity 0.8.13;
|
|
3
|
+
|
|
4
|
+
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
|
|
5
|
+
import "@openzeppelin/contracts/utils/math/SignedMath.sol";
|
|
6
|
+
import "../interfaces/PythInterface.sol";
|
|
7
|
+
import "../interfaces/OracleInterface.sol";
|
|
8
|
+
import "../interfaces/VBep20Interface.sol";
|
|
9
|
+
import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
|
|
10
|
+
|
|
11
|
+
struct TokenConfig {
|
|
12
|
+
bytes32 pythId;
|
|
13
|
+
address asset;
|
|
14
|
+
uint64 maxStalePeriod;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* PythOracle contract reads prices from actual Pyth oracle contract which accepts, verifies and stores the
|
|
19
|
+
* updated prices from external sources
|
|
20
|
+
*/
|
|
21
|
+
contract PythOracle is AccessControlledV8, OracleInterface {
|
|
22
|
+
// To calculate 10 ** n(which is a signed type)
|
|
23
|
+
using SignedMath for int256;
|
|
24
|
+
|
|
25
|
+
// To cast int64/int8 types from Pyth to unsigned types
|
|
26
|
+
using SafeCast for int256;
|
|
27
|
+
|
|
28
|
+
/// @notice Exponent scale (decimal precision) of prices
|
|
29
|
+
uint256 public constant EXP_SCALE = 1e18;
|
|
30
|
+
|
|
31
|
+
/// @notice vBNB address
|
|
32
|
+
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
|
|
33
|
+
address public immutable vBnb;
|
|
34
|
+
|
|
35
|
+
/// @notice VAI address
|
|
36
|
+
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
|
|
37
|
+
address public immutable vai;
|
|
38
|
+
|
|
39
|
+
/// @notice Set this as asset address for BNB. This is the underlying for vBNB
|
|
40
|
+
address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
|
|
41
|
+
|
|
42
|
+
/// @notice The actual pyth oracle address fetch & store the prices
|
|
43
|
+
IPyth public underlyingPythOracle;
|
|
44
|
+
|
|
45
|
+
/// @notice Token configs by asset address
|
|
46
|
+
mapping(address => TokenConfig) public tokenConfigs;
|
|
47
|
+
|
|
48
|
+
/// @notice Emit when setting a new pyth oracle address
|
|
49
|
+
event PythOracleSet(address indexed newPythOracle);
|
|
50
|
+
|
|
51
|
+
/// @notice Emit when a token config is added
|
|
52
|
+
event TokenConfigAdded(address indexed vToken, bytes32 indexed pythId, uint64 indexed maxStalePeriod);
|
|
53
|
+
|
|
54
|
+
modifier notNullAddress(address someone) {
|
|
55
|
+
if (someone == address(0)) revert("can't be zero address");
|
|
56
|
+
_;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// @notice Constructor for the implementation contract. Sets immutable variables.
|
|
60
|
+
/// @param vBnbAddress The address of the vBNB
|
|
61
|
+
/// @param vaiAddress The address of the VAI
|
|
62
|
+
/// @custom:oz-upgrades-unsafe-allow constructor
|
|
63
|
+
constructor(address vBnbAddress, address vaiAddress) notNullAddress(vBnbAddress) notNullAddress(vaiAddress) {
|
|
64
|
+
vBnb = vBnbAddress;
|
|
65
|
+
vai = vaiAddress;
|
|
66
|
+
_disableInitializers();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @notice Batch set token configs
|
|
71
|
+
* @param tokenConfigs_ Token config array
|
|
72
|
+
* @custom:access Only Governance
|
|
73
|
+
* @custom:error Zero length error is thrown if length of the array in parameter is 0
|
|
74
|
+
*/
|
|
75
|
+
function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external {
|
|
76
|
+
_checkAccessAllowed("setTokenConfigs(TokenConfig[])");
|
|
77
|
+
if (tokenConfigs_.length == 0) revert("length can't be 0");
|
|
78
|
+
uint256 numTokenConfigs = tokenConfigs_.length;
|
|
79
|
+
for (uint256 i; i < numTokenConfigs; ++i) {
|
|
80
|
+
setTokenConfig(tokenConfigs_[i]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @notice Set the underlying Pyth oracle contract address
|
|
86
|
+
* @param underlyingPythOracle_ Pyth oracle contract address
|
|
87
|
+
* @custom:access Only Governance
|
|
88
|
+
* @custom:error NotNullAddress error thrown if underlyingPythOracle_ address is zero
|
|
89
|
+
* @custom:event Emits PythOracleSet event with address of Pyth oracle.
|
|
90
|
+
*/
|
|
91
|
+
function setUnderlyingPythOracle(
|
|
92
|
+
IPyth underlyingPythOracle_
|
|
93
|
+
) external notNullAddress(address(underlyingPythOracle_)) {
|
|
94
|
+
_checkAccessAllowed("setUnderlyingPythOracle(IPyth)");
|
|
95
|
+
underlyingPythOracle = underlyingPythOracle_;
|
|
96
|
+
emit PythOracleSet(address(underlyingPythOracle_));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @notice Initializes the owner of the contract and sets required contracts
|
|
101
|
+
* @param underlyingPythOracle_ Address of the Pyth oracle
|
|
102
|
+
* @param accessControlManager_ Address of the access control manager contract
|
|
103
|
+
*/
|
|
104
|
+
function initialize(address underlyingPythOracle_, address accessControlManager_) public initializer {
|
|
105
|
+
__AccessControlled_init(accessControlManager_);
|
|
106
|
+
|
|
107
|
+
if (underlyingPythOracle_ == address(0)) revert("pyth oracle cannot be zero address");
|
|
108
|
+
underlyingPythOracle = IPyth(underlyingPythOracle_);
|
|
109
|
+
emit PythOracleSet(underlyingPythOracle_);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @notice Set single token config. `maxStalePeriod` cannot be 0 and `vToken` can be a null address
|
|
114
|
+
* @param tokenConfig Token config struct
|
|
115
|
+
* @custom:access Only Governance
|
|
116
|
+
* @custom:error Range error is thrown if max stale period is zero
|
|
117
|
+
* @custom:error NotNullAddress error is thrown if asset address is null
|
|
118
|
+
*/
|
|
119
|
+
function setTokenConfig(TokenConfig memory tokenConfig) public notNullAddress(tokenConfig.asset) {
|
|
120
|
+
_checkAccessAllowed("setTokenConfig(TokenConfig)");
|
|
121
|
+
if (tokenConfig.maxStalePeriod == 0) revert("max stale period cannot be 0");
|
|
122
|
+
tokenConfigs[tokenConfig.asset] = tokenConfig;
|
|
123
|
+
emit TokenConfigAdded(tokenConfig.asset, tokenConfig.pythId, tokenConfig.maxStalePeriod);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @notice Get price of underlying asset of the input vToken, under the hood this function
|
|
128
|
+
* get price from Pyth contract, the prices of which are updated externally
|
|
129
|
+
* @param vToken vToken address
|
|
130
|
+
* @return price Underlying price with a precision of 10 decimals
|
|
131
|
+
* @custom:error Zero address error thrown if underlyingPythOracle address is null
|
|
132
|
+
* @custom:error Zero address error thrown if asset address is null
|
|
133
|
+
* @custom:error Range error thrown if price of Pyth oracle is not greater than zero
|
|
134
|
+
*/
|
|
135
|
+
function getUnderlyingPrice(address vToken) public view override returns (uint256) {
|
|
136
|
+
if (address(underlyingPythOracle) == address(0)) revert("Pyth oracle is zero address");
|
|
137
|
+
|
|
138
|
+
address asset;
|
|
139
|
+
uint256 decimals;
|
|
140
|
+
|
|
141
|
+
// VBNB token doesn't have `underlying` method
|
|
142
|
+
if (address(vToken) == vBnb) {
|
|
143
|
+
asset = BNB_ADDR;
|
|
144
|
+
decimals = 18;
|
|
145
|
+
} else if (address(vToken) == vai) {
|
|
146
|
+
asset = vai;
|
|
147
|
+
decimals = 18;
|
|
148
|
+
} else {
|
|
149
|
+
asset = VBep20Interface(vToken).underlying();
|
|
150
|
+
decimals = VBep20Interface(asset).decimals();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
TokenConfig storage tokenConfig = tokenConfigs[asset];
|
|
154
|
+
if (tokenConfig.asset == address(0)) revert("asset doesn't exist");
|
|
155
|
+
|
|
156
|
+
// if the price is expired after it's compared against `maxStalePeriod`, the following call will revert
|
|
157
|
+
PythStructs.Price memory priceInfo = underlyingPythOracle.getPriceNoOlderThan(
|
|
158
|
+
tokenConfig.pythId,
|
|
159
|
+
tokenConfig.maxStalePeriod
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
uint256 price = int256(priceInfo.price).toUint256();
|
|
163
|
+
|
|
164
|
+
if (price == 0) revert("invalid pyth oracle price");
|
|
165
|
+
|
|
166
|
+
// the price returned from Pyth is price ** 10^expo, which is the real dollar price of the assets
|
|
167
|
+
// we need to multiply it by 1e18 to make the price 18 decimals
|
|
168
|
+
if (priceInfo.expo > 0) {
|
|
169
|
+
return price * EXP_SCALE * (10 ** int256(priceInfo.expo).toUint256()) * (10 ** (18 - decimals));
|
|
170
|
+
} else {
|
|
171
|
+
return ((price * EXP_SCALE) / (10 ** int256(-priceInfo.expo).toUint256())) * (10 ** (18 - decimals));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|