@venusprotocol/oracle 1.6.12 → 1.7.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 (85) hide show
  1. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorInterface.sol/AggregatorInterface.dbg.json +1 -1
  2. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.dbg.json +1 -1
  3. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol/AggregatorV3Interface.dbg.json +1 -1
  4. package/artifacts/@openzeppelin/contracts/access/AccessControl.sol/AccessControl.dbg.json +1 -1
  5. package/artifacts/@openzeppelin/contracts/access/IAccessControl.sol/IAccessControl.dbg.json +1 -1
  6. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +1 -1
  7. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +1 -1
  8. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +1 -1
  9. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +1 -1
  10. package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.dbg.json +1 -1
  11. package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.dbg.json +1 -1
  12. package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.dbg.json +1 -1
  13. package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.dbg.json +1 -1
  14. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.dbg.json +1 -1
  15. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +1 -1
  16. package/artifacts/@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.dbg.json +1 -1
  17. package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.dbg.json +1 -1
  18. package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.dbg.json +1 -1
  19. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.dbg.json +1 -1
  20. package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.dbg.json +1 -1
  21. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +1 -1
  22. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol/AccessControlManager.dbg.json +1 -1
  23. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.dbg.json +1 -1
  24. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.dbg.json +1 -1
  25. package/artifacts/build-info/{fc01a49cbd804e304740a46d676189d0.json → e534ed0a2d3545720de49e52284e044b.json} +1 -1
  26. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +1 -1
  27. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +36 -4
  28. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +1 -1
  29. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +1 -1
  30. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.json +1 -1
  31. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +1 -1
  32. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.json +2 -2
  33. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +1 -1
  34. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.json +19 -0
  35. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +1 -1
  36. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.json +3 -3
  37. package/artifacts/contracts/interfaces/PublicResolverInterface.sol/PublicResolverInterface.dbg.json +1 -1
  38. package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +1 -1
  39. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
  40. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
  41. package/artifacts/contracts/interfaces/SIDRegistryInterface.sol/SIDRegistryInterface.dbg.json +1 -1
  42. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +1 -1
  43. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +1 -1
  44. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +1 -1
  45. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +1 -1
  46. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +1 -1
  47. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +66 -34
  48. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +1 -1
  49. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +4 -54
  50. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +1 -1
  51. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +5 -60
  52. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
  53. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +6 -43
  54. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +1 -1
  55. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +18 -41
  56. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +1 -1
  57. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +1 -1
  58. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +1 -1
  59. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.json +2 -2
  60. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +1 -1
  61. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.json +2 -2
  62. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +1 -1
  63. package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.dbg.json +1 -1
  64. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +1 -1
  65. package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +1 -1
  66. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +1 -1
  67. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +32 -3
  68. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +1 -1
  69. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.json +21 -2
  70. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +1 -1
  71. package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +1 -1
  72. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +1 -1
  73. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +1 -1
  74. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +1 -1
  75. package/contracts/ResilientOracle.sol +100 -70
  76. package/contracts/interfaces/OracleInterface.sol +5 -3
  77. package/contracts/oracles/BinanceOracle.sol +63 -57
  78. package/contracts/oracles/BoundValidator.sol +22 -51
  79. package/contracts/oracles/ChainlinkOracle.sol +28 -68
  80. package/contracts/oracles/PythOracle.sol +28 -48
  81. package/contracts/oracles/TwapOracle.sol +39 -57
  82. package/contracts/test/MockSimpleOracle.sol +14 -2
  83. package/dist/deploy/1-deploy-oracles.js +5 -5
  84. package/dist/deploy/1-deploy-oracles.js.map +1 -1
  85. package/package.json +5 -5
@@ -20,9 +20,9 @@ import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlle
20
20
  * the protocol from oracle attacks. Currently it includes integrations with Chainlink, Pyth, Binance Oracle
21
21
  * and TWAP (Time-Weighted Average Price) oracles. TWAP uses PancakeSwap as the on-chain price source.
22
22
  *
23
- * For every market (vToken) we configure the main, pivot and fallback oracles. The main oracle oracle is the
24
- * most trustworthy price source, the pivot oracle is used as a loose sanity checker and the fallback oracle
25
- * is used as a backup price source.
23
+ * For every market (vToken) we configure the main, pivot and fallback oracles. The oracles are configured per
24
+ * vToken's underlying asset address. The main oracle oracle is the most trustworthy price source, the pivot
25
+ * oracle is used as a loose sanity checker and the fallback oracle is used as a backup price source.
26
26
  *
27
27
  * To validate prices returned from two oracles, we use an upper and lower bound ratio that is set for every
28
28
  * market. The upper bound ratio represents the deviation between reported price (the price that’s being
@@ -109,8 +109,8 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
109
109
  }
110
110
 
111
111
  /**
112
- * @notice Checks whether token config exists by checking whether vToken is null address
113
- * @dev vToken can't be null, so it's suitable to be used to check the validity of the config
112
+ * @notice Checks whether token config exists by checking whether asset is null address
113
+ * @dev address can't be null, so it's suitable to be used to check the validity of the config
114
114
  * @param asset asset address
115
115
  */
116
116
  modifier checkTokenConfigExistence(address asset) {
@@ -180,8 +180,8 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
180
180
  }
181
181
 
182
182
  /**
183
- * @notice Sets oracle for a given vToken and role.
184
- * @dev Supplied vToken **must** exist and main oracle may not be null
183
+ * @notice Sets oracle for a given asset and role.
184
+ * @dev Supplied asset **must** exist and main oracle may not be null
185
185
  * @param asset Asset address
186
186
  * @param oracle Oracle address
187
187
  * @param role Oracle role
@@ -203,7 +203,8 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
203
203
  }
204
204
 
205
205
  /**
206
- * @notice Enables/ disables oracle for the input vToken, input vToken **must** exist
206
+ * @notice Enables/ disables oracle for the input asset. Token config for the input asset **must** exist
207
+ * @dev Configuration for the asset **must** already exist and the asset cannot be 0 address
207
208
  * @param asset Asset address
208
209
  * @param role Oracle role
209
210
  * @param enable Enabled boolean of the oracle
@@ -227,20 +228,33 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
227
228
  * @param vToken vToken address
228
229
  */
229
230
  function updatePrice(address vToken) external override {
230
- (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
231
+ address asset = _getUnderlyingAsset(vToken);
232
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
231
233
  if (pivotOracle != address(0) && pivotOracleEnabled) {
232
234
  //if pivot oracle is not TwapOracle it will revert so we need to catch the revert
233
- try TwapInterface(pivotOracle).updateTwap(vToken) {} catch {}
235
+ try TwapInterface(pivotOracle).updateTwap(asset) {} catch {}
234
236
  }
235
237
  }
236
238
 
237
239
  /**
238
- * @dev Gets token config by vToken address
239
- * @param vToken vToken address
240
- * @return tokenConfig Config for the vToken
240
+ * @notice Updates the pivot oracle price. Currently using TWAP
241
+ * @dev This function should always be called before calling getPrice
242
+ * @param asset asset address
241
243
  */
242
- function getTokenConfig(address vToken) external view returns (TokenConfig memory) {
243
- address asset = _getUnderlyingAsset(vToken);
244
+ function updateAssetPrice(address asset) external {
245
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
246
+ if (pivotOracle != address(0) && pivotOracleEnabled) {
247
+ //if pivot oracle is not TwapOracle it will revert so we need to catch the revert
248
+ try TwapInterface(pivotOracle).updateTwap(asset) {} catch {}
249
+ }
250
+ }
251
+
252
+ /**
253
+ * @dev Gets token config by asset address
254
+ * @param asset asset address
255
+ * @return tokenConfig Config for the asset
256
+ */
257
+ function getTokenConfig(address asset) external view returns (TokenConfig memory) {
244
258
  return tokenConfigs[asset];
245
259
  }
246
260
 
@@ -259,41 +273,21 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
259
273
  */
260
274
  function getUnderlyingPrice(address vToken) external view override returns (uint256) {
261
275
  if (paused()) revert("resilient oracle is paused");
262
- uint256 pivotPrice = INVALID_PRICE;
263
276
 
264
- // Get pivot oracle price, Invalid price if not available or error
265
- (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
266
- if (pivotOracleEnabled && pivotOracle != address(0)) {
267
- try OracleInterface(pivotOracle).getUnderlyingPrice(vToken) returns (uint256 pricePivot) {
268
- pivotPrice = pricePivot;
269
- } catch {}
270
- }
271
-
272
- // Compare main price and pivot price, return main price and if validation was successful
273
- // note: In case pivot oracle is not available but main price is available and
274
- // validation is successful, the main oracle price is returned.
275
- (uint256 mainPrice, bool validatedPivotMain) = _getMainOraclePrice(
276
- vToken,
277
- pivotPrice,
278
- pivotOracleEnabled && pivotOracle != address(0)
279
- );
280
- if (mainPrice != INVALID_PRICE && validatedPivotMain) return mainPrice;
281
-
282
- // Compare fallback and pivot if main oracle comparision fails with pivot
283
- // Return fallback price when fallback price is validated successfully with pivot oracle
284
- (uint256 fallbackPrice, bool validatedPivotFallback) = _getFallbackOraclePrice(vToken, pivotPrice);
285
- if (fallbackPrice != INVALID_PRICE && validatedPivotFallback) return fallbackPrice;
286
-
287
- // Lastly compare main price and fallback price
288
- if (
289
- mainPrice != INVALID_PRICE &&
290
- fallbackPrice != INVALID_PRICE &&
291
- boundValidator.validatePriceWithAnchorPrice(vToken, mainPrice, fallbackPrice)
292
- ) {
293
- return mainPrice;
294
- }
277
+ address asset = _getUnderlyingAsset(vToken);
278
+ return _getPrice(asset);
279
+ }
295
280
 
296
- revert("invalid resilient oracle price");
281
+ /**
282
+ * @notice Gets price of the asset
283
+ * @param asset asset address
284
+ * @return price USD price in scaled decimal places.
285
+ * @custom:error Paused error is thrown when resilent oracle is paused
286
+ * @custom:error Invalid resilient oracle price error is thrown if fetched prices from oracle is invalid
287
+ */
288
+ function getPrice(address asset) external view override returns (uint256) {
289
+ if (paused()) revert("resilient oracle is paused");
290
+ return _getPrice(asset);
297
291
  }
298
292
 
299
293
  /**
@@ -303,7 +297,7 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
303
297
  * @custom:access Only Governance
304
298
  * @custom:error NotNullAddress is thrown if asset address is null
305
299
  * @custom:error NotNullAddress is thrown if main-role oracle address for asset is null
306
- * @custom:event Emits TokenConfigAdded event when vToken config is set successfully by governnace
300
+ * @custom:event Emits TokenConfigAdded event when the asset config is set successfully by the authorized account
307
301
  */
308
302
  function setTokenConfig(
309
303
  TokenConfig memory tokenConfig
@@ -320,42 +314,78 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
320
314
  }
321
315
 
322
316
  /**
323
- * @notice Gets oracle and enabled status by vToken address
324
- * @param vToken vToken address
317
+ * @notice Gets oracle and enabled status by asset address
318
+ * @param asset asset address
325
319
  * @param role Oracle role
326
320
  * @return oracle Oracle address based on role
327
321
  * @return enabled Enabled flag of the oracle based on token config
328
322
  */
329
- function getOracle(address vToken, OracleRole role) public view returns (address oracle, bool enabled) {
330
- address asset = _getUnderlyingAsset(vToken);
331
-
323
+ function getOracle(address asset, OracleRole role) public view returns (address oracle, bool enabled) {
332
324
  oracle = tokenConfigs[asset].oracles[uint256(role)];
333
325
  enabled = tokenConfigs[asset].enableFlagsForOracles[uint256(role)];
334
326
  }
335
327
 
328
+ function _getPrice(address asset) internal view returns (uint256) {
329
+ uint256 pivotPrice = INVALID_PRICE;
330
+
331
+ // Get pivot oracle price, Invalid price if not available or error
332
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
333
+ if (pivotOracleEnabled && pivotOracle != address(0)) {
334
+ try OracleInterface(pivotOracle).getPrice(asset) returns (uint256 pricePivot) {
335
+ pivotPrice = pricePivot;
336
+ } catch {}
337
+ }
338
+
339
+ // Compare main price and pivot price, return main price and if validation was successful
340
+ // note: In case pivot oracle is not available but main price is available and
341
+ // validation is successful, the main oracle price is returned.
342
+ (uint256 mainPrice, bool validatedPivotMain) = _getMainOraclePrice(
343
+ asset,
344
+ pivotPrice,
345
+ pivotOracleEnabled && pivotOracle != address(0)
346
+ );
347
+ if (mainPrice != INVALID_PRICE && validatedPivotMain) return mainPrice;
348
+
349
+ // Compare fallback and pivot if main oracle comparision fails with pivot
350
+ // Return fallback price when fallback price is validated successfully with pivot oracle
351
+ (uint256 fallbackPrice, bool validatedPivotFallback) = _getFallbackOraclePrice(asset, pivotPrice);
352
+ if (fallbackPrice != INVALID_PRICE && validatedPivotFallback) return fallbackPrice;
353
+
354
+ // Lastly compare main price and fallback price
355
+ if (
356
+ mainPrice != INVALID_PRICE &&
357
+ fallbackPrice != INVALID_PRICE &&
358
+ boundValidator.validatePriceWithAnchorPrice(asset, mainPrice, fallbackPrice)
359
+ ) {
360
+ return mainPrice;
361
+ }
362
+
363
+ revert("invalid resilient oracle price");
364
+ }
365
+
336
366
  /**
337
- * @notice Gets underlying asset price for the provided vToken
367
+ * @notice Gets a price for the provided asset
338
368
  * @dev This function won't revert when price is 0, because the fallback oracle may still be
339
369
  * able to fetch a correct price
340
- * @param vToken vToken address
370
+ * @param asset asset address
341
371
  * @param pivotPrice Pivot oracle price
342
372
  * @param pivotEnabled If pivot oracle is not empty and enabled
343
373
  * @return price USD price in scaled decimals
344
- * e.g. vToken decimals is 8 then price is returned as 10**18 * 10**(18-8) = 10**28 decimals
374
+ * e.g. asset decimals is 8 then price is returned as 10**18 * 10**(18-8) = 10**28 decimals
345
375
  * @return pivotValidated Boolean representing if the validation of main oracle price
346
376
  * and pivot oracle price were successful
347
- * @custom:error Invalid price error is thrown if main oracle fails to fetch price of underlying asset
377
+ * @custom:error Invalid price error is thrown if main oracle fails to fetch price of the asset
348
378
  * @custom:error Invalid price error is thrown if main oracle is not enabled or main oracle
349
379
  * address is null
350
380
  */
351
381
  function _getMainOraclePrice(
352
- address vToken,
382
+ address asset,
353
383
  uint256 pivotPrice,
354
384
  bool pivotEnabled
355
- ) private view returns (uint256, bool) {
356
- (address mainOracle, bool mainOracleEnabled) = getOracle(vToken, OracleRole.MAIN);
385
+ ) internal view returns (uint256, bool) {
386
+ (address mainOracle, bool mainOracleEnabled) = getOracle(asset, OracleRole.MAIN);
357
387
  if (mainOracleEnabled && mainOracle != address(0)) {
358
- try OracleInterface(mainOracle).getUnderlyingPrice(vToken) returns (uint256 mainOraclePrice) {
388
+ try OracleInterface(mainOracle).getPrice(asset) returns (uint256 mainOraclePrice) {
359
389
  if (!pivotEnabled) {
360
390
  return (mainOraclePrice, true);
361
391
  }
@@ -364,7 +394,7 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
364
394
  }
365
395
  return (
366
396
  mainOraclePrice,
367
- boundValidator.validatePriceWithAnchorPrice(vToken, mainOraclePrice, pivotPrice)
397
+ boundValidator.validatePriceWithAnchorPrice(asset, mainOraclePrice, pivotPrice)
368
398
  );
369
399
  } catch {
370
400
  return (INVALID_PRICE, false);
@@ -375,25 +405,25 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
375
405
  }
376
406
 
377
407
  /**
378
- * @dev This function won't revert when the price is 0 because getUnderlyingPrice checks if price is > 0
379
- * @param vToken vToken address
408
+ * @dev This function won't revert when the price is 0 because getPrice checks if price is > 0
409
+ * @param asset asset address
380
410
  * @return price USD price in 18 decimals
381
411
  * @return pivotValidated Boolean representing if the validation of fallback oracle price
382
412
  * and pivot oracle price were successfull
383
- * @custom:error Invalid price error is thrown if fallback oracle fails to fetch price of underlying asset
413
+ * @custom:error Invalid price error is thrown if fallback oracle fails to fetch price of the asset
384
414
  * @custom:error Invalid price error is thrown if fallback oracle is not enabled or fallback oracle
385
415
  * address is null
386
416
  */
387
- function _getFallbackOraclePrice(address vToken, uint256 pivotPrice) private view returns (uint256, bool) {
388
- (address fallbackOracle, bool fallbackEnabled) = getOracle(vToken, OracleRole.FALLBACK);
417
+ function _getFallbackOraclePrice(address asset, uint256 pivotPrice) private view returns (uint256, bool) {
418
+ (address fallbackOracle, bool fallbackEnabled) = getOracle(asset, OracleRole.FALLBACK);
389
419
  if (fallbackEnabled && fallbackOracle != address(0)) {
390
- try OracleInterface(fallbackOracle).getUnderlyingPrice(vToken) returns (uint256 fallbackOraclePrice) {
420
+ try OracleInterface(fallbackOracle).getPrice(asset) returns (uint256 fallbackOraclePrice) {
391
421
  if (pivotPrice == INVALID_PRICE) {
392
422
  return (fallbackOraclePrice, false);
393
423
  }
394
424
  return (
395
425
  fallbackOraclePrice,
396
- boundValidator.validatePriceWithAnchorPrice(vToken, fallbackOraclePrice, pivotPrice)
426
+ boundValidator.validatePriceWithAnchorPrice(asset, fallbackOraclePrice, pivotPrice)
397
427
  );
398
428
  } catch {
399
429
  return (INVALID_PRICE, false);
@@ -2,20 +2,22 @@
2
2
  pragma solidity 0.8.13;
3
3
 
4
4
  interface OracleInterface {
5
- function getUnderlyingPrice(address vToken) external view returns (uint256);
5
+ function getPrice(address asset) external view returns (uint256);
6
6
  }
7
7
 
8
8
  interface ResilientOracleInterface is OracleInterface {
9
9
  function updatePrice(address vToken) external;
10
+
11
+ function getUnderlyingPrice(address vToken) external view returns (uint256);
10
12
  }
11
13
 
12
14
  interface TwapInterface is OracleInterface {
13
- function updateTwap(address vToken) external returns (uint256);
15
+ function updateTwap(address asset) external returns (uint256);
14
16
  }
15
17
 
16
18
  interface BoundValidatorInterface {
17
19
  function validatePriceWithAnchorPrice(
18
- address vToken,
20
+ address asset,
19
21
  uint256 reporterPrice,
20
22
  uint256 anchorPrice
21
23
  ) external view returns (bool);
@@ -8,6 +8,7 @@ import "../interfaces/FeedRegistryInterface.sol";
8
8
  import "../interfaces/PublicResolverInterface.sol";
9
9
  import "../interfaces/OracleInterface.sol";
10
10
  import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
11
+ import "../interfaces/OracleInterface.sol";
11
12
 
12
13
  /**
13
14
  * @title BinanceOracle
@@ -17,28 +18,30 @@ import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlle
17
18
  contract BinanceOracle is AccessControlledV8, OracleInterface {
18
19
  address public sidRegistryAddress;
19
20
 
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;
21
+ /// @notice Set this as asset address for BNB. This is the underlying address for vBNB
22
+ address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
27
23
 
28
24
  /// @notice Max stale period configuration for assets
29
25
  mapping(string => uint256) public maxStalePeriod;
30
26
 
27
+ /// @notice Override symbols to be compatible with Binance feed registry
28
+ mapping(string => string) public symbols;
29
+
31
30
  event MaxStalePeriodAdded(string indexed asset, uint256 maxStalePeriod);
32
31
 
33
- /// @notice Constructor for the implementation contract. Sets immutable variables.
34
- /// @param vBnbAddress The address of the vBNB
35
- /// @param vaiAddress The address of the VAI
32
+ event SymbolOverridden(string symbol, string indexed overriddenSymbol);
33
+
34
+ /**
35
+ * @notice Checks whether an address is null or not
36
+ */
37
+ modifier notNullAddress(address someone) {
38
+ if (someone == address(0)) revert("can't be zero address");
39
+ _;
40
+ }
41
+
42
+ /// @notice Constructor for the implementation contract.
36
43
  /// @custom:oz-upgrades-unsafe-allow constructor
37
- constructor(address vBnbAddress, address vaiAddress) {
38
- if (vBnbAddress == address(0)) revert("vBNB can't be zero address");
39
- if (vaiAddress == address(0)) revert("VAI can't be zero address");
40
- vBnb = vBnbAddress;
41
- vai = vaiAddress;
44
+ constructor() {
42
45
  _disableInitializers();
43
46
  }
44
47
 
@@ -56,46 +59,74 @@ contract BinanceOracle is AccessControlledV8, OracleInterface {
56
59
  emit MaxStalePeriodAdded(symbol, _maxStalePeriod);
57
60
  }
58
61
 
62
+ /**
63
+ * @notice Used to override a symbol when fetching price
64
+ * @param symbol The symbol to override
65
+ * @param overrideSymbol The symbol after override
66
+ */
67
+ function setSymbolOverride(string calldata symbol, string calldata overrideSymbol) external {
68
+ _checkAccessAllowed("setSymbolOverride(string,string)");
69
+ if (bytes(symbol).length == 0) revert("symbol cannot be empty");
70
+
71
+ symbols[symbol] = overrideSymbol;
72
+ emit SymbolOverridden(symbol, overrideSymbol);
73
+ }
74
+
59
75
  /**
60
76
  * @notice Sets the contracts required to fetch prices
61
77
  * @param _sidRegistryAddress Address of SID registry
62
78
  * @param _accessControlManager Address of the access control manager contract
63
79
  */
64
- function initialize(address _sidRegistryAddress, address _accessControlManager) external initializer {
80
+ function initialize(
81
+ address _sidRegistryAddress,
82
+ address _accessControlManager
83
+ ) external initializer notNullAddress(_sidRegistryAddress) {
65
84
  sidRegistryAddress = _sidRegistryAddress;
66
85
  __AccessControlled_init(_accessControlManager);
67
86
  }
68
87
 
69
88
  /**
70
- * @notice Gets the price of a vToken from the binance oracle
71
- * @param vToken Address of the vToken
89
+ * @notice Uses Space ID to fetch the feed registry address
90
+ * @return feedRegistryAddress Address of binance oracle feed registry.
91
+ */
92
+ function getFeedRegistryAddress() public view returns (address) {
93
+ bytes32 nodeHash = 0x94fe3821e0768eb35012484db4df61890f9a6ca5bfa984ef8ff717e73139faff;
94
+
95
+ SIDRegistryInterface sidRegistry = SIDRegistryInterface(sidRegistryAddress);
96
+ address publicResolverAddress = sidRegistry.resolver(nodeHash);
97
+ PublicResolverInterface publicResolver = PublicResolverInterface(publicResolverAddress);
98
+
99
+ return publicResolver.addr(nodeHash);
100
+ }
101
+
102
+ /**
103
+ * @notice Gets the price of a asset from the binance oracle
104
+ * @param asset Address of the asset
72
105
  * @return Price in USD
73
106
  */
74
- function getUnderlyingPrice(address vToken) external view override returns (uint256) {
107
+ function getPrice(address asset) public view returns (uint256) {
75
108
  string memory symbol;
76
109
  uint256 decimals;
77
110
 
78
- // VBNB token doesn't have `underlying` method
79
- if (vToken == vBnb) {
111
+ if (asset == BNB_ADDR) {
80
112
  symbol = "BNB";
81
113
  decimals = 18;
82
- } else if (vToken == vai) {
83
- symbol = "VAI";
84
- decimals = 18;
85
114
  } else {
86
- IERC20Metadata underlyingToken = IERC20Metadata(VBep20Interface(vToken).underlying());
87
- symbol = underlyingToken.symbol();
88
- decimals = underlyingToken.decimals();
115
+ IERC20Metadata token = IERC20Metadata(asset);
116
+ symbol = token.symbol();
117
+ decimals = token.decimals();
89
118
  }
90
119
 
91
- if (compare(symbol, "WBNB")) {
92
- symbol = "BNB";
93
- }
120
+ string storage overrideSymbol = symbols[symbol];
94
121
 
95
- if (compare(symbol, "wBETH")) {
96
- symbol = "WBETH";
122
+ if (bytes(overrideSymbol).length != 0) {
123
+ symbol = overrideSymbol;
97
124
  }
98
125
 
126
+ return _getPrice(symbol, decimals);
127
+ }
128
+
129
+ function _getPrice(string memory symbol, uint256 decimals) internal view returns (uint256) {
99
130
  FeedRegistryInterface feedRegistry = FeedRegistryInterface(getFeedRegistryAddress());
100
131
 
101
132
  (, int256 answer, , uint256 updatedAt, ) = feedRegistry.latestRoundDataByName(symbol, "USD");
@@ -106,34 +137,9 @@ contract BinanceOracle is AccessControlledV8, OracleInterface {
106
137
  unchecked {
107
138
  deltaTime = block.timestamp - updatedAt;
108
139
  }
109
-
110
140
  if (deltaTime > maxStalePeriod[symbol]) revert("binance oracle price expired");
111
141
 
112
142
  uint256 decimalDelta = feedRegistry.decimalsByName(symbol, "USD");
113
143
  return (uint256(answer) * (10 ** (18 - decimalDelta))) * (10 ** (18 - decimals));
114
144
  }
115
-
116
- /**
117
- * @notice Uses Space ID to fetch the feed registry address
118
- * @return feedRegistryAddress Address of binance oracle feed registry.
119
- */
120
- function getFeedRegistryAddress() public view returns (address) {
121
- bytes32 nodeHash = 0x94fe3821e0768eb35012484db4df61890f9a6ca5bfa984ef8ff717e73139faff;
122
-
123
- SIDRegistryInterface sidRegistry = SIDRegistryInterface(sidRegistryAddress);
124
- address publicResolverAddress = sidRegistry.resolver(nodeHash);
125
- PublicResolverInterface publicResolver = PublicResolverInterface(publicResolverAddress);
126
-
127
- return publicResolver.addr(nodeHash);
128
- }
129
-
130
- /**
131
- * @notice Used to compare if two strings are equal or not
132
- * @param str1 The first string
133
- * @param str2 The second string
134
- * @return equal Returns true if both are equal or else false.
135
- */
136
- function compare(string memory str1, string memory str2) private pure returns (bool) {
137
- return keccak256(bytes(str1)) == keccak256(bytes(str2));
138
- }
139
145
  }
@@ -27,29 +27,12 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
27
27
  /// @notice validation configs by asset
28
28
  mapping(address => ValidateConfig) public validateConfigs;
29
29
 
30
- /// @notice vBNB address
31
- /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
32
- address public immutable vBnb;
33
-
34
- /// @notice VAI address
35
- /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
36
- address public immutable vai;
37
-
38
- /// @notice Set this as asset address for BNB. This is the underlying for vBNB
39
- address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
40
-
41
30
  /// @notice Emit this event when new validation configs are added
42
31
  event ValidateConfigAdded(address indexed asset, uint256 indexed upperBound, uint256 indexed lowerBound);
43
32
 
44
33
  /// @notice Constructor for the implementation contract. Sets immutable variables.
45
- /// @param vBnbAddress The address of the vBNB
46
- /// @param vaiAddress The address of the VAI
47
34
  /// @custom:oz-upgrades-unsafe-allow constructor
48
- constructor(address vBnbAddress, address vaiAddress) {
49
- if (vBnbAddress == address(0)) revert("vBNB can't be zero address");
50
- if (vaiAddress == address(0)) revert("VAI can't be zero address");
51
- vBnb = vBnbAddress;
52
- vai = vaiAddress;
35
+ constructor() {
53
36
  _disableInitializers();
54
37
  }
55
38
 
@@ -79,25 +62,6 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
79
62
  __AccessControlled_init(accessControlManager_);
80
63
  }
81
64
 
82
- /**
83
- * @notice Test reported asset price against anchor price
84
- * @param vToken vToken address
85
- * @param reportedPrice The price to be tested
86
- * @custom:error Missing error thrown if asset config is not set
87
- * @custom:error Price error thrown if anchor price is not valid
88
- */
89
- function validatePriceWithAnchorPrice(
90
- address vToken,
91
- uint256 reportedPrice,
92
- uint256 anchorPrice
93
- ) external view override returns (bool) {
94
- address asset = _getUnderlyingAsset(vToken);
95
-
96
- if (validateConfigs[asset].upperBoundRatio == 0) revert("validation config not exist");
97
- if (anchorPrice == 0) revert("anchor price is not valid");
98
- return _isWithinAnchor(asset, reportedPrice, anchorPrice);
99
- }
100
-
101
65
  /**
102
66
  * @notice Add a single validation config
103
67
  * @param config Validation config struct
@@ -117,6 +81,23 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
117
81
  emit ValidateConfigAdded(config.asset, config.upperBoundRatio, config.lowerBoundRatio);
118
82
  }
119
83
 
84
+ /**
85
+ * @notice Test reported asset price against anchor price
86
+ * @param asset asset address
87
+ * @param reportedPrice The price to be tested
88
+ * @custom:error Missing error thrown if asset config is not set
89
+ * @custom:error Price error thrown if anchor price is not valid
90
+ */
91
+ function validatePriceWithAnchorPrice(
92
+ address asset,
93
+ uint256 reportedPrice,
94
+ uint256 anchorPrice
95
+ ) public view virtual override returns (bool) {
96
+ if (validateConfigs[asset].upperBoundRatio == 0) revert("validation config not exist");
97
+ if (anchorPrice == 0) revert("anchor price is not valid");
98
+ return _isWithinAnchor(asset, reportedPrice, anchorPrice);
99
+ }
100
+
120
101
  /**
121
102
  * @notice Test whether the reported price is within the valid bounds
122
103
  * @param asset Asset address
@@ -134,18 +115,8 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
134
115
  return false;
135
116
  }
136
117
 
137
- /**
138
- * @dev This function returns the underlying asset of a vToken
139
- * @param vToken vToken address
140
- * @return asset underlying asset address
141
- */
142
- function _getUnderlyingAsset(address vToken) private view returns (address asset) {
143
- if (address(vToken) == vBnb) {
144
- asset = BNB_ADDR;
145
- } else if (address(vToken) == vai) {
146
- asset = vai;
147
- } else {
148
- asset = VBep20Interface(vToken).underlying();
149
- }
150
- }
118
+ // BoundValidator is to get inherited, so it's a good practice to add some storage gaps like
119
+ // OpenZepplin proposed in their contracts: https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
120
+ // solhint-disable-next-line
121
+ uint256[49] private __gap;
151
122
  }