@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.
Files changed (78) hide show
  1. package/LICENSE +11 -0
  2. package/README.md +117 -0
  3. package/contracts/ResilientOracle.sol +376 -0
  4. package/contracts/interfaces/FeedRegistryInterface.sol +14 -0
  5. package/contracts/interfaces/OracleInterface.sol +22 -0
  6. package/contracts/interfaces/PublicResolverInterface.sol +7 -0
  7. package/contracts/interfaces/PythInterface.sol +249 -0
  8. package/contracts/interfaces/SIDRegistryInterface.sol +7 -0
  9. package/contracts/interfaces/VBep20Interface.sol +11 -0
  10. package/contracts/libraries/PancakeLibrary.sol +68 -0
  11. package/contracts/oracles/BinanceOracle.sol +125 -0
  12. package/contracts/oracles/BoundValidator.sol +149 -0
  13. package/contracts/oracles/ChainlinkOracle.sol +214 -0
  14. package/contracts/oracles/PythOracle.sol +174 -0
  15. package/contracts/oracles/TwapOracle.sol +309 -0
  16. package/contracts/oracles/mocks/MockBinanceFeedRegistry.sol +29 -0
  17. package/contracts/oracles/mocks/MockBinanceOracle.sol +22 -0
  18. package/contracts/oracles/mocks/MockChainlinkOracle.sol +27 -0
  19. package/contracts/oracles/mocks/MockPythOracle.sol +32 -0
  20. package/contracts/oracles/mocks/MockTwapOracle.sol +31 -0
  21. package/contracts/test/AccessControlManager.sol +6 -0
  22. package/contracts/test/BEP20Harness.sol +16 -0
  23. package/contracts/test/MockPyth.sol +123 -0
  24. package/contracts/test/MockSimpleOracle.sol +41 -0
  25. package/contracts/test/MockV3Aggregator.sol +76 -0
  26. package/contracts/test/PancakePairHarness.sol +90 -0
  27. package/contracts/test/VBEP20Harness.sol +20 -0
  28. package/deploy/1-deploy-oracles.ts +195 -0
  29. package/deploy/2-configure-feeds.ts +324 -0
  30. package/deployments/.gitkeep +0 -0
  31. package/deployments/bscmainnet/.chainId +1 -0
  32. package/deployments/bscmainnet/BinanceOracle.json +329 -0
  33. package/deployments/bscmainnet/BinanceOracle_Implementation.json +259 -0
  34. package/deployments/bscmainnet/BinanceOracle_Proxy.json +227 -0
  35. package/deployments/bscmainnet/BoundValidator.json +621 -0
  36. package/deployments/bscmainnet/BoundValidator_Implementation.json +714 -0
  37. package/deployments/bscmainnet/BoundValidator_Proxy.json +251 -0
  38. package/deployments/bscmainnet/ChainlinkOracle.json +683 -0
  39. package/deployments/bscmainnet/ChainlinkOracle_Implementation.json +826 -0
  40. package/deployments/bscmainnet/ChainlinkOracle_Proxy.json +237 -0
  41. package/deployments/bscmainnet/DefaultProxyAdmin.json +257 -0
  42. package/deployments/bscmainnet/PythOracle.json +681 -0
  43. package/deployments/bscmainnet/PythOracle_Implementation.json +798 -0
  44. package/deployments/bscmainnet/PythOracle_Proxy.json +264 -0
  45. package/deployments/bscmainnet/ResilientOracle.json +832 -0
  46. package/deployments/bscmainnet/ResilientOracle_Implementation.json +1047 -0
  47. package/deployments/bscmainnet/ResilientOracle_Proxy.json +237 -0
  48. package/deployments/bscmainnet/TwapOracle.json +900 -0
  49. package/deployments/bscmainnet/TwapOracle_Implementation.json +1124 -0
  50. package/deployments/bscmainnet/TwapOracle_Proxy.json +251 -0
  51. package/deployments/bscmainnet/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
  52. package/deployments/bscmainnet/solcInputs/68ea814c0d6925581f2bda5deba8ce4a.json +60 -0
  53. package/deployments/bscmainnet/solcInputs/8658c669dade0e9ab80874eab3bf3468.json +117 -0
  54. package/deployments/bsctestnet/.chainId +1 -0
  55. package/deployments/bsctestnet/BinanceOracle.json +571 -0
  56. package/deployments/bsctestnet/BinanceOracle_Implementation.json +629 -0
  57. package/deployments/bsctestnet/BinanceOracle_Proxy.json +251 -0
  58. package/deployments/bsctestnet/BoundValidator.json +621 -0
  59. package/deployments/bsctestnet/BoundValidator_Implementation.json +714 -0
  60. package/deployments/bsctestnet/BoundValidator_Proxy.json +251 -0
  61. package/deployments/bsctestnet/ChainlinkOracle.json +697 -0
  62. package/deployments/bsctestnet/ChainlinkOracle_Implementation.json +826 -0
  63. package/deployments/bsctestnet/ChainlinkOracle_Proxy.json +251 -0
  64. package/deployments/bsctestnet/DefaultProxyAdmin.json +257 -0
  65. package/deployments/bsctestnet/PythOracle.json +681 -0
  66. package/deployments/bsctestnet/PythOracle_Implementation.json +798 -0
  67. package/deployments/bsctestnet/PythOracle_Proxy.json +264 -0
  68. package/deployments/bsctestnet/ResilientOracle.json +846 -0
  69. package/deployments/bsctestnet/ResilientOracle_Implementation.json +1047 -0
  70. package/deployments/bsctestnet/ResilientOracle_Proxy.json +251 -0
  71. package/deployments/bsctestnet/TwapOracle.json +887 -0
  72. package/deployments/bsctestnet/TwapOracle_Implementation.json +1111 -0
  73. package/deployments/bsctestnet/TwapOracle_Proxy.json +251 -0
  74. package/deployments/bsctestnet/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
  75. package/deployments/bsctestnet/solcInputs/4769dd98af512bf3b6b6504ad0c3bd74.json +78 -0
  76. package/deployments/bsctestnet/solcInputs/f35da22a430a956ee338e720b5898e3d.json +126 -0
  77. package/networks/mainnet.json +1 -0
  78. package/package.json +127 -0
package/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2022 Venus Protocol.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Venus Oracle
2
+
3
+ ## Usage
4
+
5
+ ### Prerequisites
6
+
7
+ - NodeJS - 14.x
8
+
9
+ - Solc - v0.8.13 (https://github.com/ethereum/solidity/releases/tag/v0.8.13)
10
+
11
+ ### Installing
12
+
13
+ ```
14
+
15
+ yarn install
16
+
17
+ ```
18
+
19
+ ### Linting and code formatting
20
+
21
+ Linting is done using eslint for typescript and solhint for solidity. Prettier is used to format solidity and typescript files.
22
+
23
+ To check linting and formatting on all files run:
24
+
25
+ ```sh
26
+ $ yarn lint
27
+ ```
28
+
29
+ Linting command can be run with the fix flag to fix eligible errors automatically
30
+
31
+ ```sh
32
+ $ yarn lint:sol --fix
33
+ $ yarn lint:ts --fix
34
+ ```
35
+
36
+ To pretty all files run:
37
+
38
+ ```sh
39
+ $ yarn prettier
40
+ ```
41
+
42
+ ### Run Tests
43
+
44
+ ```
45
+
46
+ npx hardhat test
47
+
48
+ ```
49
+
50
+ - To run fork tests add FORK_MAINNET=true PRIVATE_KEY and QUICK_NODE_KEY in the .env file.
51
+
52
+ ## Releases and Versioning
53
+
54
+ Releases are automatically managed using semantic-release and commit messages.
55
+
56
+ ### Deployment
57
+
58
+ ```
59
+
60
+ npx hardhat deploy
61
+
62
+ ```
63
+
64
+ - This command will execute all the deployment scripts in `./deploy` directory
65
+ - The default network will be `hardhat`
66
+ - Deployment to another network: - Make sure the desired network is configured in `hardhat.config.ts` - Add `PRIVATE_KEY` variable in `.env` file - Execute deploy command by adding `--network <network_name>` in the deploy command above - E.g. `npx hardhat deploy --network bsctestnet`
67
+ - Execution of single or custom set of scripts is possible, if:
68
+ - In the deployment scripts you have added `tags` for example: - `func.tags = ["MockTokens"];`
69
+ - Once this is done, adding `--tags "<tag_name>,<tag_name>..."` to the deployment command will execute only the scripts containing the tags.
70
+
71
+ ### Verify Contracts
72
+
73
+ ```
74
+ npx hardhat etherscan-verify --network <network-name>
75
+ ```
76
+
77
+ ### Hardhat Commands
78
+
79
+ ```
80
+
81
+ npx hardhat accounts
82
+
83
+ npx hardhat compile
84
+
85
+ npx hardhat clean
86
+
87
+ npx hardhat test
88
+
89
+ npx hardhat node
90
+
91
+ npx hardhat help
92
+
93
+ REPORT_GAS=true npx hardhat test
94
+
95
+ npx hardhat coverage
96
+
97
+ TS_NODE_FILES=true npx ts-node scripts/deploy.ts
98
+
99
+ npx eslint '**/*.{js,ts}'
100
+
101
+ npx eslint '**/*.{js,ts}' --fix
102
+
103
+ npx prettier '**/*.{json,sol,md}' --check
104
+
105
+ npx prettier '**/*.{json,sol,md}' --write
106
+
107
+ npx solhint 'contracts/**/*.sol'
108
+
109
+ npx solhint 'contracts/**/*.sol' --fix
110
+
111
+ ```
112
+
113
+ ### Documentation
114
+
115
+ Documentation is autogenerated using [solidity-docgen](https://github.com/OpenZeppelin/solidity-docgen).
116
+
117
+ They can be generated by running `yarn docgen`
@@ -0,0 +1,376 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ // SPDX-FileCopyrightText: 2022 Venus
3
+ pragma solidity 0.8.13;
4
+
5
+ import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
6
+ import "./interfaces/VBep20Interface.sol";
7
+ import "./interfaces/OracleInterface.sol";
8
+ import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
9
+
10
+ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOracleInterface {
11
+ /**
12
+ * @dev Oracle roles:
13
+ * **main**: The most trustworthy price source
14
+ * **pivot**: Price oracle used as a loose sanity checker
15
+ * **fallback**: The backup source when main oracle price is invalidated
16
+ */
17
+ enum OracleRole {
18
+ MAIN,
19
+ PIVOT,
20
+ FALLBACK
21
+ }
22
+
23
+ struct TokenConfig {
24
+ /// @notice asset address
25
+ address asset;
26
+ /// @notice `oracles` stores the oracles based on their role in the following order:
27
+ /// [main, pivot, fallback],
28
+ /// It can be indexed with the corresponding enum OracleRole value
29
+ address[3] oracles;
30
+ /// @notice `enableFlagsForOracles` stores the enabled state
31
+ /// for each oracle in the same order as `oracles`
32
+ bool[3] enableFlagsForOracles;
33
+ }
34
+
35
+ uint256 public constant INVALID_PRICE = 0;
36
+
37
+ /// @notice vBNB address
38
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
39
+ address public immutable vBnb;
40
+
41
+ /// @notice VAI address
42
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
43
+ address public immutable vai;
44
+
45
+ /// @notice Set this as asset address for BNB. This is the underlying for vBNB
46
+ address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
47
+
48
+ BoundValidatorInterface public boundValidator;
49
+
50
+ mapping(address => TokenConfig) private tokenConfigs;
51
+
52
+ event TokenConfigAdded(
53
+ address indexed asset,
54
+ address indexed mainOracle,
55
+ address indexed pivotOracle,
56
+ address fallbackOracle
57
+ );
58
+
59
+ /// Event emitted when an oracle is set
60
+ event OracleSet(address indexed asset, address indexed oracle, uint256 indexed role);
61
+
62
+ /// Event emitted when an oracle is enabled or disabled
63
+ event OracleEnabled(address indexed asset, uint256 indexed role, bool indexed enable);
64
+
65
+ /**
66
+ * @notice Checks whether an address is null or not
67
+ */
68
+ modifier notNullAddress(address someone) {
69
+ if (someone == address(0)) revert("can't be zero address");
70
+ _;
71
+ }
72
+
73
+ /**
74
+ * @notice Checks whether token config exists by checking whether vToken is null address
75
+ * @dev vToken can't be null, so it's suitable to be used to check the validity of the config
76
+ * @param asset asset address
77
+ */
78
+ modifier checkTokenConfigExistence(address asset) {
79
+ if (tokenConfigs[asset].asset == address(0)) revert("token config must exist");
80
+ _;
81
+ }
82
+
83
+ /// @notice Constructor for the implementation contract. Sets immutable variables.
84
+ /// @param vBnbAddress The address of the vBNB
85
+ /// @param vaiAddress The address of the VAI
86
+ /// @custom:oz-upgrades-unsafe-allow constructor
87
+ constructor(address vBnbAddress, address vaiAddress) notNullAddress(vBnbAddress) {
88
+ vBnb = vBnbAddress;
89
+ vai = vaiAddress;
90
+ _disableInitializers();
91
+ }
92
+
93
+ /**
94
+ * @notice Pauses oracle
95
+ * @custom:access Only Governance
96
+ */
97
+ function pause() external {
98
+ _checkAccessAllowed("pause()");
99
+ _pause();
100
+ }
101
+
102
+ /**
103
+ * @notice Unpauses oracle
104
+ * @custom:access Only Governance
105
+ */
106
+ function unpause() external {
107
+ _checkAccessAllowed("unpause()");
108
+ _unpause();
109
+ }
110
+
111
+ /**
112
+ * @notice Batch sets token configs
113
+ * @param tokenConfigs_ Token config array
114
+ * @custom:access Only Governance
115
+ * @custom:error Throws a length error if the lenght of the token configs array is 0
116
+ */
117
+ function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external {
118
+ _checkAccessAllowed("setTokenConfigs(TokenConfig[])");
119
+ if (tokenConfigs_.length == 0) revert("length can't be 0");
120
+ uint256 numTokenConfigs = tokenConfigs_.length;
121
+ for (uint256 i; i < numTokenConfigs; ++i) {
122
+ setTokenConfig(tokenConfigs_[i]);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * @notice Sets oracle for a given vToken and role.
128
+ * @dev Supplied vToken **must** exist and main oracle may not be null
129
+ * @param asset Asset address
130
+ * @param oracle Oracle address
131
+ * @param role Oracle role
132
+ * @custom:access Only Governance
133
+ * @custom:error Null address error if main-role oracle address is null
134
+ * @custom:error NotNullAddress error is thrown if asset address is null
135
+ * @custom:error TokenConfigExistance error is thrown if token config is not set
136
+ * @custom:event Emits OracleSet event with asset address, oracle address and role of the oracle for the asset
137
+ */
138
+ function setOracle(
139
+ address asset,
140
+ address oracle,
141
+ OracleRole role
142
+ ) external notNullAddress(asset) checkTokenConfigExistence(asset) {
143
+ _checkAccessAllowed("setOracle(address,address,OracleRole)");
144
+ if (oracle == address(0) && role == OracleRole.MAIN) revert("can't set zero address to main oracle");
145
+ tokenConfigs[asset].oracles[uint256(role)] = oracle;
146
+ emit OracleSet(asset, oracle, uint256(role));
147
+ }
148
+
149
+ /**
150
+ * @notice Enables/ disables oracle for the input vToken, input vToken **must** exist
151
+ * @param asset Asset address
152
+ * @param role Oracle role
153
+ * @param enable Enabled boolean of the oracle
154
+ * @custom:access Only Governance
155
+ * @custom:error NotNullAddress error is thrown if asset address is null
156
+ * @custom:error TokenConfigExistance error is thrown if token config is not set
157
+ */
158
+ function enableOracle(
159
+ address asset,
160
+ OracleRole role,
161
+ bool enable
162
+ ) external notNullAddress(asset) checkTokenConfigExistence(asset) {
163
+ _checkAccessAllowed("enableOracle(address,OracleRole,bool)");
164
+ tokenConfigs[asset].enableFlagsForOracles[uint256(role)] = enable;
165
+ emit OracleEnabled(asset, uint256(role), enable);
166
+ }
167
+
168
+ /**
169
+ * @notice Updates the pivot oracle price. Currently using TWAP
170
+ * @dev This function should always be called before calling getUnderlyingPrice
171
+ * @param vToken vToken address
172
+ */
173
+ function updatePrice(address vToken) external override {
174
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
175
+ if (pivotOracle != address(0) && pivotOracleEnabled) {
176
+ //if **pivot** oracle is PythOrcle it will revert so we need to catch the revert
177
+ try TwapInterface(pivotOracle).updateTwap(vToken) {} catch {}
178
+ }
179
+ }
180
+
181
+ /**
182
+ * @dev Gets token config by vToken address
183
+ * @param vToken vToken address
184
+ * @return tokenConfig Config for the vToken
185
+ */
186
+ function getTokenConfig(address vToken) external view returns (TokenConfig memory) {
187
+ address asset = _getUnderlyingAsset(vToken);
188
+ return tokenConfigs[asset];
189
+ }
190
+
191
+ /**
192
+ * @notice Gets price of the underlying asset for a given vToken. Validation flow:
193
+ * - Check if the oracle is paused globally
194
+ * - Validate price from main oracle against pivot oracle
195
+ * - Validate price from fallback oracle against pivot oracle or main oracle if the first validation failed
196
+ * In the case that the pivot oracle is not available but main price is available and validation is successful,
197
+ * main oracle price is returned.
198
+ * @param vToken vToken address
199
+ * @return price USD price in scaled decimal places.
200
+ * @custom:error Paused error is thrown when resilent oracle is paused
201
+ * @custom:error Invalid resilient oracle price error is thrown if fetched prices from oracle is invalid
202
+ */
203
+ function getUnderlyingPrice(address vToken) external view override returns (uint256) {
204
+ if (paused()) revert("resilient oracle is paused");
205
+ uint256 pivotPrice = INVALID_PRICE;
206
+
207
+ // Get pivot oracle price, Invalid price if not available or error
208
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
209
+ if (pivotOracleEnabled && pivotOracle != address(0)) {
210
+ try OracleInterface(pivotOracle).getUnderlyingPrice(vToken) returns (uint256 pricePivot) {
211
+ pivotPrice = pricePivot;
212
+ } catch {}
213
+ }
214
+
215
+ // Compare main price and pivot price, return main price and if validation was successful
216
+ // note: In case pivot oracle is not available but main price is available and
217
+ // validation is successful, the main oracle price is returned.
218
+ (uint256 mainPrice, bool validatedPivotMain) = _getMainOraclePrice(
219
+ vToken,
220
+ pivotPrice,
221
+ pivotOracleEnabled && pivotOracle != address(0)
222
+ );
223
+ if (mainPrice != INVALID_PRICE && validatedPivotMain) return mainPrice;
224
+
225
+ // Compare fallback and pivot if main oracle comparision fails with pivot
226
+ // Return fallback price when fallback price is validated successfully with pivot oracle
227
+ (uint256 fallbackPrice, bool validatedPivotFallback) = _getFallbackOraclePrice(vToken, pivotPrice);
228
+ if (fallbackPrice != INVALID_PRICE && validatedPivotFallback) return fallbackPrice;
229
+
230
+ // Lastly compare main price and fallback price
231
+ if (
232
+ mainPrice != INVALID_PRICE &&
233
+ fallbackPrice != INVALID_PRICE &&
234
+ boundValidator.validatePriceWithAnchorPrice(vToken, fallbackPrice, mainPrice)
235
+ ) {
236
+ return mainPrice;
237
+ }
238
+
239
+ revert("invalid resilient oracle price");
240
+ }
241
+
242
+ /**
243
+ * @notice Initializes the contract admin and sets the BoundValidator contract address
244
+ * @param _boundValidator Address of the bound validator contract
245
+ * @param accessControlManager_ Address of the access control manager contract
246
+ */
247
+ function initialize(BoundValidatorInterface _boundValidator, address accessControlManager_) public initializer {
248
+ if (address(_boundValidator) == address(0)) revert("invalid bound validator address");
249
+ boundValidator = _boundValidator;
250
+
251
+ __AccessControlled_init(accessControlManager_);
252
+ __Pausable_init();
253
+ }
254
+
255
+ /**
256
+ * @notice Sets/resets single token configs.
257
+ * @dev main oracle **must not** be a null address
258
+ * @param tokenConfig Token config struct
259
+ * @custom:access Only Governance
260
+ * @custom:error NotNullAddress is thrown if asset address is null
261
+ * @custom:error NotNullAddress is thrown if main-role oracle address for asset is null
262
+ * @custom:event Emits TokenConfigAdded event when vToken config is set successfully by governnace
263
+ */
264
+ function setTokenConfig(
265
+ TokenConfig memory tokenConfig
266
+ ) public notNullAddress(tokenConfig.asset) notNullAddress(tokenConfig.oracles[uint256(OracleRole.MAIN)]) {
267
+ _checkAccessAllowed("setTokenConfig(TokenConfig)");
268
+
269
+ tokenConfigs[tokenConfig.asset] = tokenConfig;
270
+ emit TokenConfigAdded(
271
+ tokenConfig.asset,
272
+ tokenConfig.oracles[uint256(OracleRole.MAIN)],
273
+ tokenConfig.oracles[uint256(OracleRole.PIVOT)],
274
+ tokenConfig.oracles[uint256(OracleRole.FALLBACK)]
275
+ );
276
+ }
277
+
278
+ /**
279
+ * @notice Gets oracle and enabled status by vToken address
280
+ * @param vToken vToken address
281
+ * @param role Oracle role
282
+ * @return oracle Oracle address based on role
283
+ * @return enabled Enabled flag of the oracle based on token config
284
+ */
285
+ function getOracle(address vToken, OracleRole role) public view returns (address oracle, bool enabled) {
286
+ address asset = _getUnderlyingAsset(vToken);
287
+
288
+ oracle = tokenConfigs[asset].oracles[uint256(role)];
289
+ enabled = tokenConfigs[asset].enableFlagsForOracles[uint256(role)];
290
+ }
291
+
292
+ /**
293
+ * @notice Gets underlying asset price for the provided vToken
294
+ * @dev This function won't revert when price is 0, because the fallback oracle may still be
295
+ * able to fetch a correct price
296
+ * @param vToken vToken address
297
+ * @param pivotPrice Pivot oracle price
298
+ * @param pivotEnabled If pivot oracle is not empty and enabled
299
+ * @return price USD price in scaled decimals
300
+ * e.g. vToken decimals is 8 then price is returned as 10**18 * 10**(18-8) = 10**28 decimals
301
+ * @return pivotValidated Boolean representing if the validation of main oracle price
302
+ * and pivot oracle price were successful
303
+ * @custom:error Invalid price error is thrown if main oracle fails to fetch price of underlying asset
304
+ * @custom:error Invalid price error is thrown if main oracle is not enabled or main oracle
305
+ * address is null
306
+ */
307
+ function _getMainOraclePrice(
308
+ address vToken,
309
+ uint256 pivotPrice,
310
+ bool pivotEnabled
311
+ ) internal view returns (uint256, bool) {
312
+ (address mainOracle, bool mainOracleEnabled) = getOracle(vToken, OracleRole.MAIN);
313
+ if (mainOracleEnabled && mainOracle != address(0)) {
314
+ try OracleInterface(mainOracle).getUnderlyingPrice(vToken) returns (uint256 mainOraclePrice) {
315
+ if (!pivotEnabled) {
316
+ return (mainOraclePrice, true);
317
+ }
318
+ if (pivotPrice == INVALID_PRICE) {
319
+ return (mainOraclePrice, false);
320
+ }
321
+ return (
322
+ mainOraclePrice,
323
+ boundValidator.validatePriceWithAnchorPrice(vToken, mainOraclePrice, pivotPrice)
324
+ );
325
+ } catch {
326
+ return (INVALID_PRICE, false);
327
+ }
328
+ }
329
+
330
+ return (INVALID_PRICE, false);
331
+ }
332
+
333
+ /**
334
+ * @dev This function won't revert when the price is 0 because getUnderlyingPrice checks if price is > 0
335
+ * @param vToken vToken address
336
+ * @return price USD price in 18 decimals
337
+ * @return pivotValidated Boolean representing if the validation of fallback oracle price
338
+ * and pivot oracle price were successfull
339
+ * @custom:error Invalid price error is thrown if fallback oracle fails to fetch price of underlying asset
340
+ * @custom:error Invalid price error is thrown if fallback oracle is not enabled or fallback oracle
341
+ * address is null
342
+ */
343
+ function _getFallbackOraclePrice(address vToken, uint256 pivotPrice) internal view returns (uint256, bool) {
344
+ (address fallbackOracle, bool fallbackEnabled) = getOracle(vToken, OracleRole.FALLBACK);
345
+ if (fallbackEnabled && fallbackOracle != address(0)) {
346
+ try OracleInterface(fallbackOracle).getUnderlyingPrice(vToken) returns (uint256 fallbackOraclePrice) {
347
+ if (pivotPrice == INVALID_PRICE) {
348
+ return (fallbackOraclePrice, false);
349
+ }
350
+ return (
351
+ fallbackOraclePrice,
352
+ boundValidator.validatePriceWithAnchorPrice(vToken, fallbackOraclePrice, pivotPrice)
353
+ );
354
+ } catch {
355
+ return (INVALID_PRICE, false);
356
+ }
357
+ }
358
+
359
+ return (INVALID_PRICE, false);
360
+ }
361
+
362
+ /**
363
+ * @dev This function returns the underlying asset of a vToken
364
+ * @param vToken vToken address
365
+ * @return asset underlying asset address
366
+ */
367
+ function _getUnderlyingAsset(address vToken) internal view returns (address asset) {
368
+ if (address(vToken) == vBnb) {
369
+ asset = BNB_ADDR;
370
+ } else if (address(vToken) == vai) {
371
+ asset = vai;
372
+ } else {
373
+ asset = VBep20Interface(vToken).underlying();
374
+ }
375
+ }
376
+ }
@@ -0,0 +1,14 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
+
4
+ interface FeedRegistryInterface {
5
+ function latestRoundDataByName(
6
+ string memory base,
7
+ string memory quote
8
+ )
9
+ external
10
+ view
11
+ returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
12
+
13
+ function decimalsByName(string memory base, string memory quote) external view returns (uint8);
14
+ }
@@ -0,0 +1,22 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
+
4
+ interface OracleInterface {
5
+ function getUnderlyingPrice(address vToken) external view returns (uint256);
6
+ }
7
+
8
+ interface ResilientOracleInterface is OracleInterface {
9
+ function updatePrice(address vToken) external;
10
+ }
11
+
12
+ interface TwapInterface is OracleInterface {
13
+ function updateTwap(address vToken) external returns (uint256);
14
+ }
15
+
16
+ interface BoundValidatorInterface {
17
+ function validatePriceWithAnchorPrice(
18
+ address vToken,
19
+ uint256 reporterPrice,
20
+ uint256 anchorPrice
21
+ ) external view returns (bool);
22
+ }
@@ -0,0 +1,7 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ // SPDX-FileCopyrightText: 2022 Venus
3
+ pragma solidity 0.8.13;
4
+
5
+ interface PublicResolverInterface {
6
+ function addr(bytes32 node) external view returns (address payable);
7
+ }