@venusprotocol/oracle 1.0.0-dev.1 → 1.0.0

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 (187) 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/@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.json +249 -0
  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/access/AccessControl.sol/AccessControl.dbg.json +4 -0
  8. package/artifacts/@openzeppelin/contracts/access/AccessControl.sol/AccessControl.json +215 -0
  9. package/artifacts/@openzeppelin/contracts/access/IAccessControl.sol/IAccessControl.dbg.json +4 -0
  10. package/artifacts/@openzeppelin/contracts/access/IAccessControl.sol/IAccessControl.json +183 -0
  11. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +4 -0
  12. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json +297 -0
  13. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +4 -0
  14. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.json +194 -0
  15. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +4 -0
  16. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.json +233 -0
  17. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +4 -0
  18. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.json +10 -0
  19. package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.dbg.json +4 -0
  20. package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.json +10 -0
  21. package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.dbg.json +4 -0
  22. package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.json +30 -0
  23. package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.dbg.json +4 -0
  24. package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.json +30 -0
  25. package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.dbg.json +4 -0
  26. package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.json +10 -0
  27. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.dbg.json +4 -0
  28. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.json +10 -0
  29. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +4 -0
  30. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.json +10 -0
  31. package/artifacts/@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.dbg.json +4 -0
  32. package/artifacts/@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.json +115 -0
  33. package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.dbg.json +4 -0
  34. package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.json +76 -0
  35. package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.dbg.json +4 -0
  36. package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.json +24 -0
  37. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.dbg.json +4 -0
  38. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.json +63 -0
  39. package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.dbg.json +4 -0
  40. package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.json +10 -0
  41. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +4 -0
  42. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.json +24 -0
  43. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol/AccessControlManager.dbg.json +4 -0
  44. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol/AccessControlManager.json +369 -0
  45. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.dbg.json +4 -0
  46. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.json +181 -0
  47. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.dbg.json +4 -0
  48. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.json +282 -0
  49. package/artifacts/build-info/0137d0ff3ae413641739f9f6322cc2b3.json +1 -0
  50. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +4 -0
  51. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +617 -0
  52. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +4 -0
  53. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.json +79 -0
  54. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +4 -0
  55. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.json +40 -0
  56. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +4 -0
  57. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.json +30 -0
  58. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +4 -0
  59. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.json +43 -0
  60. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +4 -0
  61. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.json +49 -0
  62. package/artifacts/contracts/interfaces/PublicResolverInterface.sol/PublicResolverInterface.dbg.json +4 -0
  63. package/artifacts/contracts/interfaces/PublicResolverInterface.sol/PublicResolverInterface.json +30 -0
  64. package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +4 -0
  65. package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.json +545 -0
  66. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +4 -0
  67. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.json +446 -0
  68. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +4 -0
  69. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.json +10 -0
  70. package/artifacts/contracts/interfaces/SIDRegistryInterface.sol/SIDRegistryInterface.dbg.json +4 -0
  71. package/artifacts/contracts/interfaces/SIDRegistryInterface.sol/SIDRegistryInterface.json +30 -0
  72. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +4 -0
  73. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.json +246 -0
  74. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +4 -0
  75. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.json +10 -0
  76. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +4 -0
  77. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.json +60 -0
  78. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +4 -0
  79. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.json +10 -0
  80. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +4 -0
  81. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +342 -0
  82. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +4 -0
  83. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +392 -0
  84. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +4 -0
  85. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +462 -0
  86. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +4 -0
  87. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +445 -0
  88. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +4 -0
  89. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +663 -0
  90. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +4 -0
  91. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.json +116 -0
  92. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +4 -0
  93. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.json +144 -0
  94. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +4 -0
  95. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.json +144 -0
  96. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +4 -0
  97. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.json +163 -0
  98. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +4 -0
  99. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.json +163 -0
  100. package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.dbg.json +4 -0
  101. package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.json +364 -0
  102. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +4 -0
  103. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.json +328 -0
  104. package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +4 -0
  105. package/artifacts/contracts/test/MockPyth.sol/MockPyth.json +684 -0
  106. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +4 -0
  107. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +95 -0
  108. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +4 -0
  109. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.json +72 -0
  110. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +4 -0
  111. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.json +306 -0
  112. package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +4 -0
  113. package/artifacts/contracts/test/PancakePairHarness.sol/Math.json +10 -0
  114. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +4 -0
  115. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.json +158 -0
  116. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +4 -0
  117. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.json +10 -0
  118. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +4 -0
  119. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.json +346 -0
  120. package/contracts/ResilientOracle.sol +35 -27
  121. package/contracts/interfaces/PythInterface.sol +4 -1
  122. package/contracts/oracles/BinanceOracle.sol +29 -24
  123. package/contracts/oracles/BoundValidator.sol +41 -44
  124. package/contracts/oracles/ChainlinkOracle.sol +44 -40
  125. package/contracts/oracles/PythOracle.sol +39 -34
  126. package/contracts/oracles/TwapOracle.sol +69 -65
  127. package/contracts/test/BEP20Harness.sol +4 -0
  128. package/contracts/test/MockPyth.sol +9 -2
  129. package/deployments/bscmainnet/BinanceOracle.json +279 -37
  130. package/deployments/bscmainnet/BinanceOracle_Implementation.json +399 -29
  131. package/deployments/bscmainnet/BinanceOracle_Proxy.json +58 -34
  132. package/deployments/bscmainnet/BoundValidator.json +50 -50
  133. package/deployments/bscmainnet/BoundValidator_Implementation.json +47 -55
  134. package/deployments/bscmainnet/BoundValidator_Proxy.json +46 -46
  135. package/deployments/bscmainnet/ChainlinkOracle.json +57 -49
  136. package/deployments/bscmainnet/ChainlinkOracle_Implementation.json +48 -54
  137. package/deployments/bscmainnet/ChainlinkOracle_Proxy.json +53 -39
  138. package/deployments/bscmainnet/DefaultProxyAdmin.json +16 -16
  139. package/deployments/bscmainnet/PythOracle.json +64 -57
  140. package/deployments/bscmainnet/PythOracle_Implementation.json +57 -51
  141. package/deployments/bscmainnet/PythOracle_Proxy.json +54 -53
  142. package/deployments/bscmainnet/ResilientOracle.json +57 -48
  143. package/deployments/bscmainnet/ResilientOracle_Implementation.json +67 -70
  144. package/deployments/bscmainnet/ResilientOracle_Proxy.json +54 -40
  145. package/deployments/bscmainnet/TwapOracle.json +69 -82
  146. package/deployments/bscmainnet/TwapOracle_Implementation.json +85 -98
  147. package/deployments/bscmainnet/TwapOracle_Proxy.json +46 -46
  148. package/deployments/bscmainnet/solcInputs/{8658c669dade0e9ab80874eab3bf3468.json → aae9f1601b873a5a4704ede73ede10cb.json} +37 -19
  149. package/deployments/bsctestnet/BinanceOracle.json +35 -35
  150. package/deployments/bsctestnet/BinanceOracle_Implementation.json +31 -31
  151. package/deployments/bsctestnet/BinanceOracle_Proxy.json +33 -33
  152. package/deployments/bsctestnet/BoundValidator.json +36 -36
  153. package/deployments/bsctestnet/BoundValidator_Implementation.json +44 -52
  154. package/deployments/bsctestnet/BoundValidator_Proxy.json +33 -33
  155. package/deployments/bsctestnet/ChainlinkOracle.json +36 -42
  156. package/deployments/bsctestnet/ChainlinkOracle_Implementation.json +42 -48
  157. package/deployments/bsctestnet/ChainlinkOracle_Proxy.json +33 -33
  158. package/deployments/bsctestnet/DefaultProxyAdmin.json +11 -11
  159. package/deployments/bsctestnet/PythOracle.json +48 -41
  160. package/deployments/bsctestnet/PythOracle_Implementation.json +51 -45
  161. package/deployments/bsctestnet/PythOracle_Proxy.json +39 -38
  162. package/deployments/bsctestnet/ResilientOracle.json +36 -41
  163. package/deployments/bsctestnet/ResilientOracle_Implementation.json +63 -66
  164. package/deployments/bsctestnet/ResilientOracle_Proxy.json +34 -34
  165. package/deployments/bsctestnet/TwapOracle.json +56 -56
  166. package/deployments/bsctestnet/TwapOracle_Implementation.json +81 -81
  167. package/deployments/bsctestnet/TwapOracle_Proxy.json +33 -33
  168. package/deployments/bsctestnet/solcInputs/{f35da22a430a956ee338e720b5898e3d.json → 131f58942628e21632de19506977a910.json} +7 -13
  169. package/deployments/bsctestnet/solcInputs/{4769dd98af512bf3b6b6504ad0c3bd74.json → 144b49e8aec3add79553ba273ea32d00.json} +4 -10
  170. package/deployments/bsctestnet/solcInputs/503f2e4e766f3be923fadf8030a5470f.json +78 -0
  171. package/deployments/bsctestnet/solcInputs/b39d33092f7dd383ae3a042566194132.json +75 -0
  172. package/dist/deploy/1-deploy-oracles.d.ts +5 -0
  173. package/dist/deploy/1-deploy-oracles.d.ts.map +1 -0
  174. package/dist/deploy/1-deploy-oracles.js +186 -0
  175. package/dist/deploy/1-deploy-oracles.js.map +1 -0
  176. package/dist/deploy/2-configure-feeds.d.ts +15 -0
  177. package/dist/deploy/2-configure-feeds.d.ts.map +1 -0
  178. package/dist/deploy/2-configure-feeds.js +300 -0
  179. package/dist/deploy/2-configure-feeds.js.map +1 -0
  180. package/dist/hardhat.config.d.ts +13 -0
  181. package/dist/hardhat.config.d.ts.map +1 -0
  182. package/dist/hardhat.config.js +158 -0
  183. package/dist/hardhat.config.js.map +1 -0
  184. package/package.json +2 -15
  185. package/deploy/1-deploy-oracles.ts +0 -195
  186. package/deploy/2-configure-feeds.ts +0 -324
  187. package/deployments/bscmainnet/solcInputs/68ea814c0d6925581f2bda5deba8ce4a.json +0 -60
@@ -8,12 +8,6 @@ import "../interfaces/OracleInterface.sol";
8
8
  import "../interfaces/VBep20Interface.sol";
9
9
  import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
10
10
 
11
- struct TokenConfig {
12
- bytes32 pythId;
13
- address asset;
14
- uint64 maxStalePeriod;
15
- }
16
-
17
11
  /**
18
12
  * PythOracle contract reads prices from actual Pyth oracle contract which accepts, verifies and stores the
19
13
  * updated prices from external sources
@@ -25,6 +19,12 @@ contract PythOracle is AccessControlledV8, OracleInterface {
25
19
  // To cast int64/int8 types from Pyth to unsigned types
26
20
  using SafeCast for int256;
27
21
 
22
+ struct TokenConfig {
23
+ bytes32 pythId;
24
+ address asset;
25
+ uint64 maxStalePeriod;
26
+ }
27
+
28
28
  /// @notice Exponent scale (decimal precision) of prices
29
29
  uint256 public constant EXP_SCALE = 1e18;
30
30
 
@@ -46,7 +46,7 @@ contract PythOracle is AccessControlledV8, OracleInterface {
46
46
  mapping(address => TokenConfig) public tokenConfigs;
47
47
 
48
48
  /// @notice Emit when setting a new pyth oracle address
49
- event PythOracleSet(address indexed newPythOracle);
49
+ event PythOracleSet(address indexed oldPythOracle, address indexed newPythOracle);
50
50
 
51
51
  /// @notice Emit when a token config is added
52
52
  event TokenConfigAdded(address indexed vToken, bytes32 indexed pythId, uint64 indexed maxStalePeriod);
@@ -73,11 +73,13 @@ contract PythOracle is AccessControlledV8, OracleInterface {
73
73
  * @custom:error Zero length error is thrown if length of the array in parameter is 0
74
74
  */
75
75
  function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external {
76
- _checkAccessAllowed("setTokenConfigs(TokenConfig[])");
77
76
  if (tokenConfigs_.length == 0) revert("length can't be 0");
78
77
  uint256 numTokenConfigs = tokenConfigs_.length;
79
- for (uint256 i; i < numTokenConfigs; ++i) {
78
+ for (uint256 i; i < numTokenConfigs; ) {
80
79
  setTokenConfig(tokenConfigs_[i]);
80
+ unchecked {
81
+ ++i;
82
+ }
81
83
  }
82
84
  }
83
85
 
@@ -91,9 +93,10 @@ contract PythOracle is AccessControlledV8, OracleInterface {
91
93
  function setUnderlyingPythOracle(
92
94
  IPyth underlyingPythOracle_
93
95
  ) external notNullAddress(address(underlyingPythOracle_)) {
94
- _checkAccessAllowed("setUnderlyingPythOracle(IPyth)");
96
+ _checkAccessAllowed("setUnderlyingPythOracle(address)");
97
+ IPyth oldUnderlyingPythOracle = underlyingPythOracle;
95
98
  underlyingPythOracle = underlyingPythOracle_;
96
- emit PythOracleSet(address(underlyingPythOracle_));
99
+ emit PythOracleSet(address(oldUnderlyingPythOracle), address(underlyingPythOracle_));
97
100
  }
98
101
 
99
102
  /**
@@ -101,48 +104,36 @@ contract PythOracle is AccessControlledV8, OracleInterface {
101
104
  * @param underlyingPythOracle_ Address of the Pyth oracle
102
105
  * @param accessControlManager_ Address of the access control manager contract
103
106
  */
104
- function initialize(address underlyingPythOracle_, address accessControlManager_) public initializer {
107
+ function initialize(
108
+ address underlyingPythOracle_,
109
+ address accessControlManager_
110
+ ) external initializer notNullAddress(underlyingPythOracle_) {
105
111
  __AccessControlled_init(accessControlManager_);
106
112
 
107
- if (underlyingPythOracle_ == address(0)) revert("pyth oracle cannot be zero address");
108
113
  underlyingPythOracle = IPyth(underlyingPythOracle_);
109
- emit PythOracleSet(underlyingPythOracle_);
110
- }
111
-
112
- /**
113
- * @notice Set single token config. `maxStalePeriod` cannot be 0 and `vToken` can be a null address
114
- * @param tokenConfig Token config struct
115
- * @custom:access Only Governance
116
- * @custom:error Range error is thrown if max stale period is zero
117
- * @custom:error NotNullAddress error is thrown if asset address is null
118
- */
119
- function setTokenConfig(TokenConfig memory tokenConfig) public notNullAddress(tokenConfig.asset) {
120
- _checkAccessAllowed("setTokenConfig(TokenConfig)");
121
- if (tokenConfig.maxStalePeriod == 0) revert("max stale period cannot be 0");
122
- tokenConfigs[tokenConfig.asset] = tokenConfig;
123
- emit TokenConfigAdded(tokenConfig.asset, tokenConfig.pythId, tokenConfig.maxStalePeriod);
114
+ emit PythOracleSet(address(0), underlyingPythOracle_);
124
115
  }
125
116
 
126
117
  /**
127
118
  * @notice Get price of underlying asset of the input vToken, under the hood this function
128
119
  * get price from Pyth contract, the prices of which are updated externally
129
120
  * @param vToken vToken address
130
- * @return price Underlying price with a precision of 10 decimals
121
+ * @return price Underlying price with a precision of 18 decimals
131
122
  * @custom:error Zero address error thrown if underlyingPythOracle address is null
132
123
  * @custom:error Zero address error thrown if asset address is null
133
124
  * @custom:error Range error thrown if price of Pyth oracle is not greater than zero
134
125
  */
135
- function getUnderlyingPrice(address vToken) public view override returns (uint256) {
136
- if (address(underlyingPythOracle) == address(0)) revert("Pyth oracle is zero address");
137
-
126
+ function getUnderlyingPrice(
127
+ address vToken
128
+ ) external view override notNullAddress(address(underlyingPythOracle)) returns (uint256) {
138
129
  address asset;
139
130
  uint256 decimals;
140
131
 
141
132
  // VBNB token doesn't have `underlying` method
142
- if (address(vToken) == vBnb) {
133
+ if (vToken == vBnb) {
143
134
  asset = BNB_ADDR;
144
135
  decimals = 18;
145
- } else if (address(vToken) == vai) {
136
+ } else if (vToken == vai) {
146
137
  asset = vai;
147
138
  decimals = 18;
148
139
  } else {
@@ -171,4 +162,18 @@ contract PythOracle is AccessControlledV8, OracleInterface {
171
162
  return ((price * EXP_SCALE) / (10 ** int256(-priceInfo.expo).toUint256())) * (10 ** (18 - decimals));
172
163
  }
173
164
  }
165
+
166
+ /**
167
+ * @notice Set single token config. `maxStalePeriod` cannot be 0 and `vToken` can be a null address
168
+ * @param tokenConfig Token config struct
169
+ * @custom:access Only Governance
170
+ * @custom:error Range error is thrown if max stale period is zero
171
+ * @custom:error NotNullAddress error is thrown if asset address is null
172
+ */
173
+ function setTokenConfig(TokenConfig memory tokenConfig) public notNullAddress(tokenConfig.asset) {
174
+ _checkAccessAllowed("setTokenConfig(TokenConfig)");
175
+ if (tokenConfig.maxStalePeriod == 0) revert("max stale period cannot be 0");
176
+ tokenConfigs[tokenConfig.asset] = tokenConfig;
177
+ emit TokenConfigAdded(tokenConfig.asset, tokenConfig.pythId, tokenConfig.maxStalePeriod);
178
+ }
174
179
  }
@@ -7,30 +7,30 @@ import "../interfaces/OracleInterface.sol";
7
7
  import "../interfaces/VBep20Interface.sol";
8
8
  import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
9
9
 
10
- struct Observation {
11
- uint256 timestamp;
12
- uint256 acc;
13
- }
14
-
15
- struct TokenConfig {
16
- /// @notice Asset address, which can't be zero address and can be used for existance check
17
- address asset;
18
- /// @notice Decimals of underlying asset
19
- uint256 baseUnit;
20
- /// @notice The address of Pancake pair
21
- address pancakePool;
22
- /// @notice Whether the token is paired with WBNB
23
- bool isBnbBased;
24
- /// @notice A flag identifies whether the Pancake pair is reversed
25
- /// e.g. XVS-WBNB is not reversed, while WBNB-XVS is.
26
- bool isReversedPool;
27
- /// @notice The minimum window in seconds required between TWAP updates
28
- uint256 anchorPeriod;
29
- }
30
-
31
10
  contract TwapOracle is AccessControlledV8, TwapInterface {
32
11
  using FixedPoint for *;
33
12
 
13
+ struct Observation {
14
+ uint256 timestamp;
15
+ uint256 acc;
16
+ }
17
+
18
+ struct TokenConfig {
19
+ /// @notice Asset address, which can't be zero address and can be used for existance check
20
+ address asset;
21
+ /// @notice Decimals of underlying asset respresented as 1e{decimals}
22
+ uint256 baseUnit;
23
+ /// @notice The address of Pancake pair
24
+ address pancakePool;
25
+ /// @notice Whether the token is paired with WBNB
26
+ bool isBnbBased;
27
+ /// @notice A flag identifies whether the Pancake pair is reversed
28
+ /// e.g. XVS-WBNB is reversed, while WBNB-XVS is not.
29
+ bool isReversedPool;
30
+ /// @notice The minimum window in seconds required between TWAP updates
31
+ uint256 anchorPeriod;
32
+ }
33
+
34
34
  /// @notice WBNB address
35
35
  /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
36
36
  address public immutable WBNB;
@@ -44,8 +44,8 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
44
44
  address public immutable vai;
45
45
 
46
46
  /// @notice the base unit of WBNB and BUSD, which are the paired tokens for all assets
47
- uint256 public constant bnbBaseUnit = 1e18;
48
- uint256 public constant busdBaseUnit = 1e18;
47
+ uint256 public constant BNB_BASE_UNIT = 1e18;
48
+ uint256 public constant BUSD_BASE_UNIT = 1e18;
49
49
 
50
50
  /// @notice Configs by token
51
51
  mapping(address => TokenConfig) public tokenConfigs;
@@ -101,12 +101,39 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
101
101
  * @custom:error Zero length error thrown, if length of the config array is 0
102
102
  */
103
103
  function setTokenConfigs(TokenConfig[] memory configs) external {
104
- _checkAccessAllowed("setTokenConfigs(TokenConfig[])");
105
104
  if (configs.length == 0) revert("length can't be 0");
106
105
  uint256 numTokenConfigs = configs.length;
107
- for (uint256 i; i < numTokenConfigs; ++i) {
106
+ for (uint256 i; i < numTokenConfigs; ) {
108
107
  setTokenConfig(configs[i]);
108
+ unchecked {
109
+ ++i;
110
+ }
111
+ }
112
+ }
113
+
114
+ /**
115
+ * @notice Initializes the owner of the contract
116
+ * @param accessControlManager_ Address of the access control manager contract
117
+ */
118
+ function initialize(address accessControlManager_) external initializer {
119
+ __AccessControlled_init(accessControlManager_);
120
+ }
121
+
122
+ /**
123
+ * @notice Updates the current token/BUSD price from PancakeSwap, with 18 decimals of precision.
124
+ * @return anchorPrice anchor price of the underlying asset of the vToken
125
+ * @custom:error Missing error is thrown if token config does not exist
126
+ */
127
+ function updateTwap(address vToken) external returns (uint256) {
128
+ address asset = _getUnderlyingAsset(vToken);
129
+
130
+ if (tokenConfigs[asset].asset == address(0)) revert("asset not exist");
131
+ // Update & fetch WBNB price first, so we can calculate the price of WBNB paired token
132
+ if (asset != WBNB && tokenConfigs[asset].isBnbBased) {
133
+ if (tokenConfigs[WBNB].asset == address(0)) revert("WBNB not exist");
134
+ _updateTwapInternal(tokenConfigs[WBNB]);
109
135
  }
136
+ return _updateTwapInternal(tokenConfigs[asset]);
110
137
  }
111
138
 
112
139
  /**
@@ -127,14 +154,6 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
127
154
  return (price * (10 ** (18 - IERC20Metadata(asset).decimals())));
128
155
  }
129
156
 
130
- /**
131
- * @notice Initializes the owner of the contract
132
- * @param accessControlManager_ Address of the access control manager contract
133
- */
134
- function initialize(address accessControlManager_) public initializer {
135
- __AccessControlled_init(accessControlManager_);
136
- }
137
-
138
157
  /**
139
158
  * @notice Adds a single token config
140
159
  * @param config token config struct
@@ -153,7 +172,6 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
153
172
  _checkAccessAllowed("setTokenConfig(TokenConfig)");
154
173
 
155
174
  if (config.anchorPeriod == 0) revert("anchor period must be positive");
156
- if (config.baseUnit == 0) revert("base unit must be positive");
157
175
  if (config.baseUnit != 10 ** IERC20Metadata(config.asset).decimals())
158
176
  revert("base unit decimals must be same as asset decimals");
159
177
 
@@ -165,23 +183,6 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
165
183
  emit TokenConfigAdded(config.asset, config.pancakePool, config.anchorPeriod);
166
184
  }
167
185
 
168
- /**
169
- * @notice Updates the current token/BUSD price from PancakeSwap, with 18 decimals of precision.
170
- * @return anchorPrice anchor price of the underlying asset of the vToken
171
- * @custom:error Missing error is thrown if token config does not exist
172
- */
173
- function updateTwap(address vToken) public returns (uint256) {
174
- address asset = _getUnderlyingAsset(vToken);
175
-
176
- if (tokenConfigs[asset].asset == address(0)) revert("asset not exist");
177
- // Update & fetch WBNB price first, so we can calculate the price of WBNB paired token
178
- if (asset != WBNB && tokenConfigs[asset].isBnbBased) {
179
- if (tokenConfigs[WBNB].asset == address(0)) revert("WBNB not exist");
180
- _updateTwapInternal(tokenConfigs[WBNB]);
181
- }
182
- return _updateTwapInternal(tokenConfigs[asset]);
183
- }
184
-
185
186
  /**
186
187
  * @notice Fetches the current token/WBNB and token/BUSD price accumulator from PancakeSwap.
187
188
  * @return cumulative price of target token regardless of pair order
@@ -204,7 +205,7 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
204
205
  * @custom:event Emits AnchorPriceUpdated event on successful update of observation with assset address,
205
206
  * AnchorPrice, old observation timestamp and current timestamp
206
207
  */
207
- function _updateTwapInternal(TokenConfig memory config) internal virtual returns (uint256) {
208
+ function _updateTwapInternal(TokenConfig memory config) private returns (uint256) {
208
209
  // pokeWindowValues already handled reversed pool cases,
209
210
  // priceAverage will always be Token/BNB or Token/BUSD *twap** price.
210
211
  (uint256 nowCumulativePrice, uint256 oldCumulativePrice, uint256 oldTimestamp) = pokeWindowValues(config);
@@ -214,7 +215,10 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
214
215
  // This should be impossible, but better safe than sorry
215
216
  if (block.timestamp < oldTimestamp) revert("now must come after before");
216
217
 
217
- uint256 timeElapsed = block.timestamp - oldTimestamp;
218
+ uint256 timeElapsed;
219
+ unchecked {
220
+ timeElapsed = block.timestamp - oldTimestamp;
221
+ }
218
222
 
219
223
  // Calculate Pancake *twap**
220
224
  FixedPoint.uq112x112 memory priceAverage = FixedPoint.uq112x112(
@@ -224,15 +228,15 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
224
228
  uint256 priceAverageMantissa = priceAverage.decode112with18();
225
229
 
226
230
  // To cancel the decimals in cumulative price, we need to mulitply the average price with
227
- // tokenBaseUnit / (wbnbBaseUnit or busdBaseUnit, which is 1e18)
228
- uint256 pairedTokenBaseUnit = config.isBnbBased ? bnbBaseUnit : busdBaseUnit;
231
+ // tokenBaseUnit / (BNB_BASE_UNIT or BUSD_BASE_UNIT, which is 1e18)
232
+ uint256 pairedTokenBaseUnit = config.isBnbBased ? BNB_BASE_UNIT : BUSD_BASE_UNIT;
229
233
  uint256 anchorPriceMantissa = (priceAverageMantissa * config.baseUnit) / pairedTokenBaseUnit;
230
234
 
231
235
  // if this token is paired with BNB, convert its price to USD
232
236
  if (config.isBnbBased) {
233
237
  uint256 bnbPrice = prices[WBNB];
234
238
  if (bnbPrice == 0) revert("bnb price is invalid");
235
- anchorPriceMantissa = (anchorPriceMantissa * bnbPrice) / bnbBaseUnit;
239
+ anchorPriceMantissa = (anchorPriceMantissa * bnbPrice) / BNB_BASE_UNIT;
236
240
  }
237
241
 
238
242
  if (anchorPriceMantissa == 0) revert("twap price cannot be 0");
@@ -256,18 +260,14 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
256
260
  */
257
261
  function pokeWindowValues(
258
262
  TokenConfig memory config
259
- ) internal returns (uint256, uint256 startCumulativePrice, uint256 startCumulativeTimestamp) {
263
+ ) private returns (uint256, uint256 startCumulativePrice, uint256 startCumulativeTimestamp) {
260
264
  uint256 cumulativePrice = currentCumulativePrice(config);
261
265
  uint256 currentTimestamp = block.timestamp;
262
266
  uint256 windowStartTimestamp = currentTimestamp - config.anchorPeriod;
263
267
  Observation[] memory storedObservations = observations[config.asset];
264
268
 
265
269
  uint256 storedObservationsLength = storedObservations.length;
266
- for (
267
- uint256 windowStartIndex = windowStart[config.asset];
268
- windowStartIndex < storedObservationsLength;
269
- ++windowStartIndex
270
- ) {
270
+ for (uint256 windowStartIndex = windowStart[config.asset]; windowStartIndex < storedObservationsLength; ) {
271
271
  if (
272
272
  (storedObservations[windowStartIndex].timestamp >= windowStartTimestamp) ||
273
273
  (windowStartIndex == storedObservationsLength - 1)
@@ -279,6 +279,10 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
279
279
  } else {
280
280
  delete observations[config.asset][windowStartIndex];
281
281
  }
282
+
283
+ unchecked {
284
+ ++windowStartIndex;
285
+ }
282
286
  }
283
287
 
284
288
  observations[config.asset].push(Observation(currentTimestamp, cumulativePrice));
@@ -297,10 +301,10 @@ contract TwapOracle is AccessControlledV8, TwapInterface {
297
301
  * @param vToken vToken address
298
302
  * @return asset underlying asset address
299
303
  */
300
- function _getUnderlyingAsset(address vToken) internal view returns (address asset) {
301
- if (address(vToken) == vBnb) {
304
+ function _getUnderlyingAsset(address vToken) private view returns (address asset) {
305
+ if (vToken == vBnb) {
302
306
  asset = WBNB;
303
- } else if (address(vToken) == vai) {
307
+ } else if (vToken == vai) {
304
308
  asset = vai;
305
309
  } else {
306
310
  asset = VBep20Interface(vToken).underlying();
@@ -10,6 +10,10 @@ contract BEP20Harness is ERC20 {
10
10
  decimalsInternal = decimals_;
11
11
  }
12
12
 
13
+ function faucet(uint256 amount) external {
14
+ _mint(msg.sender, amount);
15
+ }
16
+
13
17
  function decimals() public view virtual override returns (uint8) {
14
18
  return decimalsInternal;
15
19
  }
@@ -18,8 +18,11 @@ contract MockPyth is AbstractPyth {
18
18
  // simply update price feeds
19
19
  function updatePriceFeedsHarness(PythStructs.PriceFeed[] calldata feeds) external {
20
20
  require(feeds.length > 0, "feeds length must > 0");
21
- for (uint256 i = 0; i < feeds.length; ++i) {
21
+ for (uint256 i = 0; i < feeds.length; ) {
22
22
  priceFeeds[feeds[i].id] = feeds[i];
23
+ unchecked {
24
+ ++i;
25
+ }
23
26
  }
24
27
  }
25
28
 
@@ -41,7 +44,7 @@ contract MockPyth is AbstractPyth {
41
44
  // We set it to 1.
42
45
  uint16 chainId = 1;
43
46
 
44
- for (uint256 i = 0; i < updateData.length; i++) {
47
+ for (uint256 i = 0; i < updateData.length; ) {
45
48
  PythStructs.PriceFeed memory priceFeed = abi.decode(updateData[i], (PythStructs.PriceFeed));
46
49
 
47
50
  bool fresh = false;
@@ -64,6 +67,10 @@ contract MockPyth is AbstractPyth {
64
67
  priceFeed.price.price,
65
68
  priceFeed.price.conf
66
69
  );
70
+
71
+ unchecked {
72
+ i++;
73
+ }
67
74
  }
68
75
 
69
76
  // In the real contract, the input of this function contains multiple batches that each contain multiple prices.