@venusprotocol/oracle 1.4.1 → 1.5.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 (118) hide show
  1. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorInterface.sol/AggregatorInterface.dbg.json +4 -0
  2. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorInterface.sol/AggregatorInterface.json +138 -0
  3. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.dbg.json +4 -0
  4. package/artifacts/{contracts → @chainlink/contracts/src/v0.8}/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.json +2 -2
  5. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol/AggregatorV3Interface.dbg.json +4 -0
  6. package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol/AggregatorV3Interface.json +122 -0
  7. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +1 -1
  8. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json +2 -2
  9. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +1 -1
  10. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +1 -1
  11. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +1 -1
  12. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.dbg.json +1 -1
  13. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.json +2 -2
  14. package/artifacts/@openzeppelin/contracts/utils/math/SafeMath.sol/SafeMath.dbg.json +1 -1
  15. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +1 -1
  16. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.json +2 -2
  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/AddressUpgradeable.sol/AddressUpgradeable.json +2 -2
  22. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +1 -1
  23. package/artifacts/build-info/4f6308532d612c9477fab05b03e221e5.json +1 -0
  24. package/artifacts/build-info/57da5d39a9f98618d0dcf1f6d4ff6b0d.json +1 -0
  25. package/artifacts/contracts/PriceOracle.sol/PriceOracle.dbg.json +1 -1
  26. package/artifacts/contracts/PriceOracle.sol/PriceOracle.json +0 -13
  27. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +1 -1
  28. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +33 -9
  29. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +1 -1
  30. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +1 -1
  31. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +1 -1
  32. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +1 -1
  33. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +1 -1
  34. package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +1 -1
  35. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
  36. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
  37. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.json +2 -2
  38. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +1 -1
  39. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.json +4 -17
  40. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +1 -1
  41. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.json +2 -2
  42. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +1 -1
  43. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +1 -1
  44. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.json +2 -2
  45. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +1 -1
  46. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +26 -2
  47. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +1 -1
  48. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +39 -2
  49. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +1 -1
  50. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +39 -2
  51. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
  52. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +39 -2
  53. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +1 -1
  54. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +53 -30
  55. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +1 -1
  56. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.json +2 -2
  57. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +1 -1
  58. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.json +2 -2
  59. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +1 -1
  60. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.json +2 -2
  61. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +1 -1
  62. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.json +2 -2
  63. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +1 -1
  64. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.json +2 -2
  65. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +1 -1
  66. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.json +2 -2
  67. package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +1 -1
  68. package/artifacts/contracts/test/MockPyth.sol/MockPyth.json +2 -2
  69. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +1 -1
  70. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +2 -2
  71. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +1 -1
  72. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.json +2 -2
  73. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +1 -1
  74. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.json +3 -3
  75. package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +1 -1
  76. package/artifacts/contracts/test/PancakePairHarness.sol/Math.json +2 -2
  77. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +1 -1
  78. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.json +2 -2
  79. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +1 -1
  80. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.json +2 -2
  81. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +1 -1
  82. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.json +2 -2
  83. package/contracts/PriceOracle.sol +4 -4
  84. package/contracts/ResilientOracle.sol +140 -92
  85. package/contracts/interfaces/FeedRegistryInterface.sol +7 -10
  86. package/contracts/interfaces/OracleInterface.sol +2 -3
  87. package/contracts/interfaces/PythInterface.sol +10 -13
  88. package/contracts/interfaces/VBep20Interface.sol +4 -4
  89. package/contracts/libraries/PancakeLibrary.sol +7 -20
  90. package/contracts/oracles/BinanceOracle.sol +37 -6
  91. package/contracts/oracles/BoundValidator.sol +33 -11
  92. package/contracts/oracles/ChainlinkOracle.sol +78 -44
  93. package/contracts/oracles/PythOracle.sol +51 -17
  94. package/contracts/oracles/TwapOracle.sol +113 -67
  95. package/contracts/oracles/mocks/MockBinanceFeedRegistry.sol +7 -10
  96. package/contracts/oracles/mocks/MockBinanceOracle.sol +1 -1
  97. package/contracts/oracles/mocks/MockChainlinkOracle.sol +1 -2
  98. package/contracts/oracles/mocks/MockPythOracle.sol +1 -1
  99. package/contracts/oracles/mocks/MockTwapOracle.sol +1 -1
  100. package/contracts/test/BEP20Harness.sol +3 -8
  101. package/contracts/test/MockPyth.sol +2 -2
  102. package/contracts/test/MockSimpleOracle.sol +2 -2
  103. package/contracts/test/MockV3Aggregator.sol +9 -24
  104. package/contracts/test/PancakePairHarness.sol +7 -20
  105. package/contracts/test/VBEP20Harness.sol +2 -3
  106. package/deploy/1-deploy-oracles.d.ts.map +1 -1
  107. package/deploy/1-deploy-oracles.js +11 -2
  108. package/deploy/1-deploy-oracles.js.map +1 -1
  109. package/package.json +10 -5
  110. package/artifacts/build-info/3965bb4348b0cd9f8e1c6653863ba2c7.json +0 -1
  111. package/artifacts/build-info/74e7c40ef8c9d7273f9e994ec1da9646.json +0 -1
  112. package/artifacts/contracts/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.dbg.json +0 -4
  113. package/artifacts/contracts/interfaces/BEP20Interface.sol/BEP20Interface.dbg.json +0 -4
  114. package/artifacts/contracts/interfaces/BEP20Interface.sol/BEP20Interface.json +0 -246
  115. package/artifacts/hardhat/console.sol/console.dbg.json +0 -4
  116. package/artifacts/hardhat/console.sol/console.json +0 -10
  117. package/contracts/interfaces/AggregatorV2V3Interface.sol +0 -60
  118. package/contracts/interfaces/BEP20Interface.sol +0 -97
@@ -1,6 +1,6 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
- pragma experimental ABIEncoderV2;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ // SPDX-FileCopyrightText: 2022 Venus
3
+ pragma solidity 0.8.13;
4
4
 
5
5
  import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
6
6
  import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
@@ -10,13 +10,20 @@ import "./interfaces/OracleInterface.sol";
10
10
  contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOracleInterface {
11
11
  uint256 public constant INVALID_PRICE = 0;
12
12
 
13
+ /// @notice vBNB address
14
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
15
+ address public immutable vBnb;
16
+
17
+ /// @notice Set this as asset address for BNB. This is the underlying for vBNB
18
+ address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
19
+
13
20
  BoundValidatorInterface public boundValidator;
14
21
 
15
22
  /**
16
23
  * @dev oracle role, we have 3 roles at the moment
17
- * MAIN: The most trustworthy price source
18
- * PIVOT: Not so trustworthy price source, used as a loose sanity checker
19
- * FALLBACK: The backup source when main oracle price is invalidated
24
+ * **main**: The most trustworthy price source
25
+ * **pivot**: Not so trustworthy price source, used as a loose sanity checker
26
+ * **fallback**: The backup source when main oracle price is invalidated
20
27
  */
21
28
  enum OracleRole {
22
29
  MAIN,
@@ -37,7 +44,6 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
37
44
 
38
45
  mapping(address => TokenConfig) private tokenConfigs;
39
46
 
40
- event GlobalEnable(bool indexed isEnable);
41
47
  event TokenConfigAdded(
42
48
  address indexed asset,
43
49
  address indexed mainOracle,
@@ -62,8 +68,20 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
62
68
  _;
63
69
  }
64
70
 
71
+ /// @notice Constructor for the implementation contract. Sets immutable variables.
72
+ /// @param vBnbAddress The address of the VBNB
73
+ /// @custom:oz-upgrades-unsafe-allow constructor
74
+ constructor(address vBnbAddress) notNullAddress(vBnbAddress) {
75
+ vBnb = vBnbAddress;
76
+ _disableInitializers();
77
+ }
78
+
79
+ /**
80
+ * @notice Initializes the contract admin and sets the BoundValidator contract address
81
+ * @param _boundValidator Address of the bound validator contract
82
+ */
65
83
  function initialize(BoundValidatorInterface _boundValidator) public initializer {
66
- require(address(_boundValidator) != address(0), "invaliud bound validator address");
84
+ require(address(_boundValidator) != address(0), "invalid bound validator address");
67
85
  boundValidator = _boundValidator;
68
86
 
69
87
  __Ownable_init();
@@ -71,14 +89,14 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
71
89
  }
72
90
 
73
91
  /**
74
- * @notice Pause protocol
92
+ * @notice Pause oracle
75
93
  */
76
94
  function pause() external onlyOwner {
77
95
  _pause();
78
96
  }
79
97
 
80
98
  /**
81
- * @notice Unpause protocol
99
+ * @notice Unpause oracle
82
100
  */
83
101
  function unpause() external onlyOwner {
84
102
  _unpause();
@@ -87,9 +105,10 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
87
105
  /**
88
106
  * @dev Get token config by vToken address
89
107
  * @param vToken vtoken address
108
+ * @return tokenConfig config of the vToken
90
109
  */
91
110
  function getTokenConfig(address vToken) external view returns (TokenConfig memory) {
92
- address asset = VBep20Interface(vToken).underlying();
111
+ address asset = address(vToken) == vBnb ? BNB_ADDR : VBep20Interface(vToken).underlying();
93
112
  return tokenConfigs[asset];
94
113
  }
95
114
 
@@ -97,9 +116,11 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
97
116
  * @notice Get oracle & enabling status by vToken address
98
117
  * @param vToken vtoken address
99
118
  * @param role oracle role
119
+ * @return oracle oracle address based on role
120
+ * @return enabled enabled flag of the oracle based on token config
100
121
  */
101
122
  function getOracle(address vToken, OracleRole role) public view returns (address oracle, bool enabled) {
102
- address asset = VBep20Interface(vToken).underlying();
123
+ address asset = address(vToken) == vBnb ? BNB_ADDR : VBep20Interface(vToken).underlying();
103
124
  oracle = tokenConfigs[asset].oracles[uint256(role)];
104
125
  enabled = tokenConfigs[asset].enableFlagsForOracles[uint256(role)];
105
126
  }
@@ -107,24 +128,28 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
107
128
  /**
108
129
  * @notice Batch set token configs
109
130
  * @param tokenConfigs_ token config array
131
+ * @custom:access Only Governance
132
+ * @custom:error Length error if the lenght of the array is 0, passed in parameter
110
133
  */
111
134
  function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external onlyOwner {
112
135
  require(tokenConfigs_.length != 0, "length can't be 0");
113
- for (uint256 i = 0; i < tokenConfigs_.length; i++) {
136
+ uint256 numTokenConfigs = tokenConfigs_.length;
137
+ for (uint256 i; i < numTokenConfigs; ++i) {
114
138
  setTokenConfig(tokenConfigs_[i]);
115
139
  }
116
140
  }
117
141
 
118
142
  /**
119
- * @notice Set single token configs, vToken MUST HAVE NOT be added before, and main oracle MUST NOT be zero address
143
+ * @notice Set/reset single token configs and main oracle **must not** be zero address
120
144
  * @param tokenConfig token config struct
145
+ * @custom:access Only Governance
146
+ * @custom:error NotNullAddress thrown if asset address is zero
147
+ * @custom:error NotNullAddress thrown if main-role oracle address for asset is zero
148
+ * @custom:event Emits TokenConfigAdded event when vToken config are set by governnace
121
149
  */
122
- function setTokenConfig(TokenConfig memory tokenConfig)
123
- public
124
- onlyOwner
125
- notNullAddress(tokenConfig.asset)
126
- notNullAddress(tokenConfig.oracles[uint256(OracleRole.MAIN)])
127
- {
150
+ function setTokenConfig(
151
+ TokenConfig memory tokenConfig
152
+ ) public onlyOwner notNullAddress(tokenConfig.asset) notNullAddress(tokenConfig.oracles[uint256(OracleRole.MAIN)]) {
128
153
  tokenConfigs[tokenConfig.asset] = tokenConfig;
129
154
  emit TokenConfigAdded(
130
155
  tokenConfig.asset,
@@ -135,10 +160,15 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
135
160
  }
136
161
 
137
162
  /**
138
- * @notice Set oracle of any type for the input vToken, input vToken MUST exist
163
+ * @notice Set oracle of any type for the input vToken, input vToken **must** exist
139
164
  * @param asset asset address
140
165
  * @param oracle oracle address
141
166
  * @param role oracle role
167
+ * @custom:access Only Governance
168
+ * @custom:error Zero address error if main-role oracle password is set to zero
169
+ * @custom:error NotNullAddress error thrown if asset address is zero
170
+ * @custom:error TokenConfigExistance error thrown if token config is not set
171
+ * @custom:event Emits OracleSet event with asset address, oracle address and role of the oracle for the asset
142
172
  */
143
173
  function setOracle(
144
174
  address asset,
@@ -151,10 +181,13 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
151
181
  }
152
182
 
153
183
  /**
154
- * @notice Enable/disable oracle for the input vToken, input vToken MUST exist
184
+ * @notice Enable/disable oracle for the input vToken, input vToken **must** exist
155
185
  * @param asset asset address
156
186
  * @param role oracle role
157
187
  * @param enable expected status
188
+ * @custom:access Only Governance
189
+ * @custom:error NotNullAddress error thrown if asset address is zero
190
+ * @custom:error TokenConfigExistance error thrown if token config is not set
158
191
  */
159
192
  function enableOracle(
160
193
  address asset,
@@ -166,14 +199,15 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
166
199
  }
167
200
 
168
201
  /**
169
- * @notice Currently it calls the updateTwap
202
+ * @notice Update the pivot oracle price. Currently using **twap**
203
+ * @dev This function should be called every time before calling getUnderlyingPrice
170
204
  * @param vToken vToken address
171
205
  */
172
206
  function updatePrice(address vToken) external override {
173
207
  (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
174
208
  if (pivotOracle != address(0) && pivotOracleEnabled) {
175
- //if PIVOT oracle is PythOrcle it will revert so we need to catch the revert
176
- try TwapInterface(pivotOracle).updateTwap(vToken) returns (uint256 _price) {} catch {}
209
+ //if **pivot** oracle is PythOrcle it will revert so we need to catch the revert
210
+ try TwapInterface(pivotOracle).updateTwap(vToken) {} catch {}
177
211
  }
178
212
  }
179
213
 
@@ -183,103 +217,117 @@ contract ResilientOracle is OwnableUpgradeable, PausableUpgradeable, ResilientOr
183
217
  * - check price from main oracle against pivot oracle
184
218
  * - check price from fallback oracle against pivot oracle or main oracle if fails
185
219
  * @param vToken vToken address
186
- * @return price USD price in 18 decimals
220
+ * @return price USD price in scaled decimal places, In case pivot oracle is not available
221
+ * but main price is available and validation is successful, main oracle price is returned.
222
+ * @custom:error paused error thrown when resilent oracle is paused
223
+ * @custom:error Invalid resilient oracle price error thrown if fetched prices from oracle is invalid
187
224
  */
188
225
  function getUnderlyingPrice(address vToken) external view override returns (uint256) {
189
226
  require(!paused(), "resilient oracle is paused");
227
+ uint256 pivotPrice = INVALID_PRICE;
228
+
229
+ // Get pivot oracle price, Invalid price if not available or error
230
+ (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
231
+ if (pivotOracleEnabled && pivotOracle != address(0)) {
232
+ try OracleInterface(pivotOracle).getUnderlyingPrice(vToken) returns (uint256 pricePivot) {
233
+ pivotPrice = pricePivot;
234
+ } catch {}
235
+ }
190
236
 
191
- uint256 price = _getMainOraclePrice(vToken);
192
- if (price == INVALID_PRICE) {
193
- price = _getFallbackOraclePrice(vToken);
237
+ // Compare main price and pivot price, return main price and if validation was successful
238
+ // note: In case pivot oracle is not available but main price is available and
239
+ // validation is successful, the main oracle price is returned.
240
+ (uint256 mainPrice, bool validatedPivotMain) = _getMainOraclePrice(
241
+ vToken,
242
+ pivotPrice,
243
+ pivotOracleEnabled && pivotOracle != address(0)
244
+ );
245
+ if (mainPrice != INVALID_PRICE && validatedPivotMain) return mainPrice;
246
+
247
+ // Compare fallback and pivot if main oracle comparision fails with pivot
248
+ // Return fallback price when fallback price is validated successfully with pivot oracle
249
+ (uint256 fallbackPrice, bool validatedPivotFallback) = _getFallbackOraclePrice(vToken, pivotPrice);
250
+ if (fallbackPrice != INVALID_PRICE && validatedPivotFallback) return fallbackPrice;
251
+
252
+ // Lastly compare main price and fallback price
253
+ if (
254
+ mainPrice != INVALID_PRICE &&
255
+ fallbackPrice != INVALID_PRICE &&
256
+ boundValidator.validatePriceWithAnchorPrice(vToken, fallbackPrice, mainPrice)
257
+ ) {
258
+ return mainPrice;
194
259
  }
195
260
 
196
- require(price != INVALID_PRICE, "invalid resilient oracle price");
197
- return price;
261
+ revert("invalid resilient oracle price");
198
262
  }
199
263
 
200
264
  /**
201
- * @notice This function won't revert when price is 0, because the fallback oracle may come to play later
265
+ * @notice Get asset underlying vToken asset price
266
+ * @dev This function won't revert when price is 0, because the fallback oracle may still be
267
+ * able to fetch a correct price
202
268
  * @param vToken vToken address
203
- * @return price USD price in 18 decimals
269
+ * @param pivotPrice pivot oracle price
270
+ * @param pivotEnabled if pivot oracle is not empty and enabled
271
+ * @return price USD price in scaled decimals
272
+ * e.g. vToken decimals is 8 then price is returned as 10**18 * 10**(18-8) = 10**28 decimals
273
+ * @return pivotValidated Boolean representing if the validation of main oracle price
274
+ * and pivot oracle price was successful
275
+ * @custom:error Invalid price error thrown if main oracle fails to fetch price of underlying asset
276
+ * @custom:error Invalid price error thrown if main oracle is not enabled or main oracle
277
+ * address is zero
204
278
  */
205
- function _getMainOraclePrice(address vToken) internal view returns (uint256) {
206
- uint256 price = INVALID_PRICE;
207
-
279
+ function _getMainOraclePrice(
280
+ address vToken,
281
+ uint256 pivotPrice,
282
+ bool pivotEnabled
283
+ ) internal view returns (uint256, bool) {
208
284
  (address mainOracle, bool mainOracleEnabled) = getOracle(vToken, OracleRole.MAIN);
209
285
  if (mainOracleEnabled && mainOracle != address(0)) {
210
286
  try OracleInterface(mainOracle).getUnderlyingPrice(vToken) returns (uint256 mainOraclePrice) {
211
- price = mainOraclePrice;
212
-
213
- (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
214
-
215
- if (pivotOracleEnabled && pivotOracle != address(0)) {
216
- try OracleInterface(pivotOracle).getUnderlyingPrice(vToken) returns (uint256 pivotPrice) {
217
- if (pivotPrice != INVALID_PRICE) {
218
- bool isPriceValid = boundValidator.validatePriceWithAnchorPrice(vToken, price, pivotPrice);
219
- if (!isPriceValid) {
220
- return INVALID_PRICE;
221
- }
222
- }
223
- } catch {}
287
+ if (!pivotEnabled) {
288
+ return (mainOraclePrice, true);
224
289
  }
225
- } catch {}
290
+ if (pivotPrice == INVALID_PRICE) {
291
+ return (mainOraclePrice, false);
292
+ }
293
+ return (
294
+ mainOraclePrice,
295
+ boundValidator.validatePriceWithAnchorPrice(vToken, mainOraclePrice, pivotPrice)
296
+ );
297
+ } catch {
298
+ return (INVALID_PRICE, false);
299
+ }
226
300
  }
227
301
 
228
- return price;
302
+ return (INVALID_PRICE, false);
229
303
  }
230
304
 
231
305
  /**
232
306
  * @notice This function won't revert when price is 0, because the getUnderlyingPrice checks if pirce is > 0
233
307
  * @param vToken vToken address
234
308
  * @return price USD price in 18 decimals
309
+ * @return pivotValidated Boolean representing if the validation of fallback oracle price
310
+ * and pivot oracle price was successfull
311
+ * @custom:error Invalid price error thrown if fallback oracle fails to fetch price of underlying asset
312
+ * @custom:error Invalid price error thrown if fallback oracle is not enabled or fallback oracle
313
+ * address is zero
235
314
  */
236
- function _getFallbackOraclePrice(address vToken) internal view returns (uint256) {
237
- uint256 price = INVALID_PRICE;
238
- bool compareWithMain = false;
239
-
315
+ function _getFallbackOraclePrice(address vToken, uint256 pivotPrice) internal view returns (uint256, bool) {
240
316
  (address fallbackOracle, bool fallbackEnabled) = getOracle(vToken, OracleRole.FALLBACK);
241
317
  if (fallbackEnabled && fallbackOracle != address(0)) {
242
318
  try OracleInterface(fallbackOracle).getUnderlyingPrice(vToken) returns (uint256 fallbackOraclePrice) {
243
- price = fallbackOraclePrice;
244
-
245
- (address pivotOracle, bool pivotOracleEnabled) = getOracle(vToken, OracleRole.PIVOT);
246
- if (pivotOracleEnabled && pivotOracle != address(0)) {
247
- try OracleInterface(pivotOracle).getUnderlyingPrice(vToken) returns (uint256 pivotPrice) {
248
- if (pivotPrice != INVALID_PRICE) {
249
- bool isPriceValid = boundValidator.validatePriceWithAnchorPrice(vToken, price, pivotPrice);
250
- if (!isPriceValid) {
251
- compareWithMain = true;
252
- }
253
- } else {
254
- compareWithMain = true;
255
- }
256
- } catch {
257
- compareWithMain = true;
258
- }
259
- } else {
260
- compareWithMain = true;
261
- }
262
- } catch {}
263
- }
264
-
265
- if (compareWithMain) {
266
- (address mainOracle, bool mainOracleEnabled) = getOracle(vToken, OracleRole.MAIN);
267
- if (mainOracleEnabled && mainOracle != address(0)) {
268
- try OracleInterface(mainOracle).getUnderlyingPrice(vToken) returns (uint256 mainOraclePrice) {
269
- bool isPriceValid = boundValidator.validatePriceWithAnchorPrice(vToken, price, mainOraclePrice);
270
- if (!isPriceValid) {
271
- return INVALID_PRICE;
272
- } else {
273
- price = mainOraclePrice;
274
- }
275
- } catch {
276
- price = INVALID_PRICE;
319
+ if (pivotPrice == INVALID_PRICE) {
320
+ return (fallbackOraclePrice, false);
277
321
  }
278
- } else {
279
- price = INVALID_PRICE;
322
+ return (
323
+ fallbackOraclePrice,
324
+ boundValidator.validatePriceWithAnchorPrice(vToken, fallbackOraclePrice, pivotPrice)
325
+ );
326
+ } catch {
327
+ return (INVALID_PRICE, false);
280
328
  }
281
329
  }
282
330
 
283
- return price;
331
+ return (INVALID_PRICE, false);
284
332
  }
285
333
  }
@@ -1,17 +1,14 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.10;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
3
 
4
4
  interface FeedRegistryInterface {
5
- function latestRoundDataByName(string memory base, string memory quote)
5
+ function latestRoundDataByName(
6
+ string memory base,
7
+ string memory quote
8
+ )
6
9
  external
7
10
  view
8
- returns (
9
- uint80 roundId,
10
- int256 answer,
11
- uint256 startedAt,
12
- uint256 updatedAt,
13
- uint80 answeredInRound
14
- );
11
+ returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
15
12
 
16
13
  function decimalsByName(string memory base, string memory quote) external view returns (uint8);
17
14
  }
@@ -1,6 +1,5 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
- pragma experimental ABIEncoderV2;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
4
3
 
5
4
  interface OracleInterface {
6
5
  function getUnderlyingPrice(address vToken) external view returns (uint256);
@@ -1,5 +1,6 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.0;
2
+ // SPDX-FileCopyrightText: 2021 Pyth Data Foundation
3
+ pragma solidity 0.8.13;
3
4
 
4
5
  contract PythStructs {
5
6
  // A price with a degree of uncertainty, represented as a price +- a confidence interval.
@@ -188,12 +189,10 @@ abstract contract AbstractPyth is IPyth {
188
189
  return priceFeed.price;
189
190
  }
190
191
 
191
- function getPriceNoOlderThan(bytes32 id, uint256 age)
192
- public
193
- view
194
- override
195
- returns (PythStructs.Price memory price)
196
- {
192
+ function getPriceNoOlderThan(
193
+ bytes32 id,
194
+ uint256 age
195
+ ) public view override returns (PythStructs.Price memory price) {
197
196
  price = getPriceUnsafe(id);
198
197
 
199
198
  require(diff(block.timestamp, price.publishTime) <= age, "no price available which is recent enough");
@@ -206,12 +205,10 @@ abstract contract AbstractPyth is IPyth {
206
205
  return priceFeed.emaPrice;
207
206
  }
208
207
 
209
- function getEmaPriceNoOlderThan(bytes32 id, uint256 age)
210
- public
211
- view
212
- override
213
- returns (PythStructs.Price memory price)
214
- {
208
+ function getEmaPriceNoOlderThan(
209
+ bytes32 id,
210
+ uint256 age
211
+ ) public view override returns (PythStructs.Price memory price) {
215
212
  price = getEmaPriceUnsafe(id);
216
213
 
217
214
  require(diff(block.timestamp, price.publishTime) <= age, "no ema price available which is recent enough");
@@ -1,9 +1,9 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
3
 
4
- import "./BEP20Interface.sol";
4
+ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
5
5
 
6
- interface VBep20Interface is BEP20Interface {
6
+ interface VBep20Interface is IERC20Metadata {
7
7
  /**
8
8
  * @notice Underlying asset for this VToken
9
9
  */
@@ -1,5 +1,5 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
3
 
4
4
  library FixedPoint {
5
5
  // range: [0, 2**112 - 1]
@@ -32,19 +32,13 @@ library PancakeOracleLibrary {
32
32
 
33
33
  // helper function that returns the current block timestamp within the range of uint32, i.e. [0, 2**32 - 1]
34
34
  function currentBlockTimestamp() internal view returns (uint32) {
35
- return uint32(block.timestamp % 2**32);
35
+ return uint32(block.timestamp % 2 ** 32);
36
36
  }
37
37
 
38
38
  // produces the cumulative price using counterfactuals to save gas and avoid a call to sync.
39
- function currentCumulativePrices(address pair)
40
- internal
41
- view
42
- returns (
43
- uint256 price0Cumulative,
44
- uint256 price1Cumulative,
45
- uint32 blockTimestamp
46
- )
47
- {
39
+ function currentCumulativePrices(
40
+ address pair
41
+ ) internal view returns (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) {
48
42
  blockTimestamp = currentBlockTimestamp();
49
43
  price0Cumulative = IPancakePair(pair).price0CumulativeLast();
50
44
  price1Cumulative = IPancakePair(pair).price1CumulativeLast();
@@ -66,14 +60,7 @@ library PancakeOracleLibrary {
66
60
  }
67
61
 
68
62
  interface IPancakePair {
69
- function getReserves()
70
- external
71
- view
72
- returns (
73
- uint112 reserve0,
74
- uint112 reserve1,
75
- uint32 blockTimestampLast
76
- );
63
+ function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
77
64
 
78
65
  function price0CumulativeLast() external view returns (uint256);
79
66
 
@@ -1,22 +1,53 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.10;
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
3
 
4
4
  import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
5
+ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
5
6
  import "../interfaces/FeedRegistryInterface.sol";
6
7
  import "../interfaces/VBep20Interface.sol";
7
- import "../interfaces/BEP20Interface.sol";
8
8
 
9
9
  contract BinanceOracle is Initializable {
10
10
  FeedRegistryInterface public feedRegistry;
11
11
 
12
+ /// @notice vBNB address
13
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
14
+ address public immutable vBnb;
15
+
16
+ /// @notice Constructor for the implementation contract. Sets immutable variables.
17
+ /// @param vBnbAddress The address of the VBNB
18
+ /// @custom:oz-upgrades-unsafe-allow constructor
19
+ constructor(address vBnbAddress) {
20
+ require(vBnbAddress != address(0), "can't be zero address");
21
+ vBnb = vBnbAddress;
22
+ _disableInitializers();
23
+ }
24
+
25
+ /**
26
+ * @notice Sets the contracts required to fetch price
27
+ * @param feed Address of binance oracle feed registry.
28
+ */
12
29
  function initialize(FeedRegistryInterface feed) public initializer {
13
30
  feedRegistry = feed;
14
31
  }
15
32
 
33
+ /**
34
+ * @notice Gets the price of vToken from binance oracle
35
+ * @param vToken Address of the vToken
36
+ * @return price in USD
37
+ */
16
38
  function getUnderlyingPrice(VBep20Interface vToken) public view returns (uint256) {
17
- BEP20Interface underlyingToken = BEP20Interface(vToken.underlying());
39
+ string memory symbol;
40
+ uint256 decimals;
18
41
 
19
- string memory symbol = underlyingToken.symbol();
42
+ // VBNB token doesn't have `underlying` method
43
+ if (address(vToken) == vBnb) {
44
+ symbol = "BNB";
45
+ decimals = 18;
46
+ } else {
47
+ IERC20Metadata underlyingToken = IERC20Metadata(vToken.underlying());
48
+ symbol = underlyingToken.symbol();
49
+ decimals = underlyingToken.decimals();
50
+ }
20
51
 
21
52
  if (keccak256(bytes(symbol)) == keccak256(bytes("WBNB"))) {
22
53
  symbol = "BNB";
@@ -25,6 +56,6 @@ contract BinanceOracle is Initializable {
25
56
  (, int256 answer, , , ) = feedRegistry.latestRoundDataByName(symbol, "USD");
26
57
 
27
58
  uint256 decimalDelta = feedRegistry.decimalsByName(symbol, "USD");
28
- return (uint256(answer) * (10**(18 - decimalDelta))) * (10**(18 - underlyingToken.decimals()));
59
+ return (uint256(answer) * (10 ** (18 - decimalDelta))) * (10 ** (18 - decimals));
29
60
  }
30
61
  }