@venusprotocol/oracle 1.6.12-dev.7 → 1.6.13

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 (89) 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/{5e3358a24918f424864f98433dd157fb.json → fc01a49cbd804e304740a46d676189d0.json} +1 -1
  26. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +1 -1
  27. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +4 -36
  28. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +1 -1
  29. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.json +10 -10
  30. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +1 -1
  31. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.json +1 -1
  32. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +1 -1
  33. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.json +2 -2
  34. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +1 -1
  35. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.json +0 -19
  36. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +1 -1
  37. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.json +3 -3
  38. package/artifacts/contracts/interfaces/PublicResolverInterface.sol/PublicResolverInterface.dbg.json +1 -1
  39. package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +1 -1
  40. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
  41. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
  42. package/artifacts/contracts/interfaces/SIDRegistryInterface.sol/SIDRegistryInterface.dbg.json +1 -1
  43. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +1 -1
  44. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +1 -1
  45. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +1 -1
  46. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +1 -1
  47. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +1 -1
  48. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +32 -45
  49. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +1 -1
  50. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +54 -4
  51. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +1 -1
  52. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +60 -5
  53. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
  54. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +43 -6
  55. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +1 -1
  56. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +41 -18
  57. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +1 -1
  58. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.json +16 -16
  59. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +1 -1
  60. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +1 -1
  61. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.json +2 -2
  62. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +1 -1
  63. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.json +2 -2
  64. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +1 -1
  65. package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.dbg.json +1 -1
  66. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +1 -1
  67. package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +1 -1
  68. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +1 -1
  69. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +3 -32
  70. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +1 -1
  71. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.json +2 -21
  72. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +1 -1
  73. package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +1 -1
  74. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +1 -1
  75. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +1 -1
  76. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +1 -1
  77. package/contracts/ResilientOracle.sol +70 -100
  78. package/contracts/interfaces/FeedRegistryInterface.sol +4 -4
  79. package/contracts/interfaces/OracleInterface.sol +3 -5
  80. package/contracts/oracles/BinanceOracle.sol +58 -53
  81. package/contracts/oracles/BoundValidator.sol +51 -22
  82. package/contracts/oracles/ChainlinkOracle.sol +68 -28
  83. package/contracts/oracles/PythOracle.sol +48 -28
  84. package/contracts/oracles/TwapOracle.sol +57 -39
  85. package/contracts/oracles/mocks/MockBinanceFeedRegistry.sol +6 -6
  86. package/contracts/test/MockSimpleOracle.sol +2 -14
  87. package/dist/deploy/1-deploy-oracles.js +6 -6
  88. package/dist/deploy/1-deploy-oracles.js.map +1 -1
  89. package/package.json +2 -2
@@ -8,7 +8,6 @@ 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";
12
11
 
13
12
  /**
14
13
  * @title BinanceOracle
@@ -18,37 +17,28 @@ import "../interfaces/OracleInterface.sol";
18
17
  contract BinanceOracle is AccessControlledV8, OracleInterface {
19
18
  address public sidRegistryAddress;
20
19
 
21
- /// @notice Set this as asset address for BNB. This is the underlying address for vBNB
22
- address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
23
-
24
- /// @notice Quote address for USD
20
+ /// @notice vBNB address
25
21
  /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
26
- address public immutable USD_ADDR;
22
+ address public immutable vBnb;
27
23
 
28
- /// @notice Address of WBNB contract
24
+ /// @notice VAI address
29
25
  /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
30
- address public immutable WBNB;
26
+ address public immutable vai;
31
27
 
32
28
  /// @notice Max stale period configuration for assets
33
29
  mapping(string => uint256) public maxStalePeriod;
34
30
 
35
31
  event MaxStalePeriodAdded(string indexed asset, uint256 maxStalePeriod);
36
32
 
37
- /**
38
- * @notice Checks whether an address is null or not
39
- */
40
- modifier notNullAddress(address someone) {
41
- if (someone == address(0)) revert("can't be zero address");
42
- _;
43
- }
44
-
45
33
  /// @notice Constructor for the implementation contract. Sets immutable variables.
46
- /// @param wBnbAddress The address of the WBNB
47
- /// @param usdAddress The address of the USD symbol
34
+ /// @param vBnbAddress The address of the vBNB
35
+ /// @param vaiAddress The address of the VAI
48
36
  /// @custom:oz-upgrades-unsafe-allow constructor
49
- constructor(address wBnbAddress, address usdAddress) notNullAddress(wBnbAddress) notNullAddress(usdAddress) {
50
- WBNB = wBnbAddress;
51
- USD_ADDR = usdAddress;
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;
52
42
  _disableInitializers();
53
43
  }
54
44
 
@@ -71,54 +61,44 @@ contract BinanceOracle is AccessControlledV8, OracleInterface {
71
61
  * @param _sidRegistryAddress Address of SID registry
72
62
  * @param _accessControlManager Address of the access control manager contract
73
63
  */
74
- function initialize(
75
- address _sidRegistryAddress,
76
- address _accessControlManager
77
- ) external initializer notNullAddress(_sidRegistryAddress) {
64
+ function initialize(address _sidRegistryAddress, address _accessControlManager) external initializer {
78
65
  sidRegistryAddress = _sidRegistryAddress;
79
66
  __AccessControlled_init(_accessControlManager);
80
67
  }
81
68
 
82
69
  /**
83
- * @notice Uses Space ID to fetch the feed registry address
84
- * @return feedRegistryAddress Address of binance oracle feed registry.
85
- */
86
- function getFeedRegistryAddress() public view returns (address) {
87
- bytes32 nodeHash = 0x94fe3821e0768eb35012484db4df61890f9a6ca5bfa984ef8ff717e73139faff;
88
-
89
- SIDRegistryInterface sidRegistry = SIDRegistryInterface(sidRegistryAddress);
90
- address publicResolverAddress = sidRegistry.resolver(nodeHash);
91
- PublicResolverInterface publicResolver = PublicResolverInterface(publicResolverAddress);
92
-
93
- return publicResolver.addr(nodeHash);
94
- }
95
-
96
- /**
97
- * @notice Gets the price of a asset from the binance oracle
98
- * @param asset Address of the asset
70
+ * @notice Gets the price of a vToken from the binance oracle
71
+ * @param vToken Address of the vToken
99
72
  * @return Price in USD
100
73
  */
101
- function getPrice(address asset) public view returns (uint256) {
74
+ function getUnderlyingPrice(address vToken) external view override returns (uint256) {
102
75
  string memory symbol;
103
76
  uint256 decimals;
104
77
 
105
- if (asset == BNB_ADDR) {
106
- symbol = "WBNB";
78
+ // VBNB token doesn't have `underlying` method
79
+ if (vToken == vBnb) {
80
+ symbol = "BNB";
81
+ decimals = 18;
82
+ } else if (vToken == vai) {
83
+ symbol = "VAI";
107
84
  decimals = 18;
108
- asset = WBNB;
109
85
  } else {
110
- IERC20Metadata token = IERC20Metadata(asset);
111
- symbol = token.symbol();
112
- decimals = token.decimals();
86
+ IERC20Metadata underlyingToken = IERC20Metadata(VBep20Interface(vToken).underlying());
87
+ symbol = underlyingToken.symbol();
88
+ decimals = underlyingToken.decimals();
113
89
  }
114
90
 
115
- return _getPrice(asset, symbol, decimals);
116
- }
91
+ if (compare(symbol, "WBNB")) {
92
+ symbol = "BNB";
93
+ }
94
+
95
+ if (compare(symbol, "wBETH")) {
96
+ symbol = "WBETH";
97
+ }
117
98
 
118
- function _getPrice(address asset, string memory symbol, uint256 decimals) internal view returns (uint256) {
119
99
  FeedRegistryInterface feedRegistry = FeedRegistryInterface(getFeedRegistryAddress());
120
100
 
121
- (, int256 answer, , uint256 updatedAt, ) = feedRegistry.latestRoundData(asset, USD_ADDR);
101
+ (, int256 answer, , uint256 updatedAt, ) = feedRegistry.latestRoundDataByName(symbol, "USD");
122
102
  if (answer <= 0) revert("invalid binance oracle price");
123
103
  if (block.timestamp < updatedAt) revert("updatedAt exceeds block time");
124
104
 
@@ -126,9 +106,34 @@ contract BinanceOracle is AccessControlledV8, OracleInterface {
126
106
  unchecked {
127
107
  deltaTime = block.timestamp - updatedAt;
128
108
  }
109
+
129
110
  if (deltaTime > maxStalePeriod[symbol]) revert("binance oracle price expired");
130
111
 
131
- uint256 decimalDelta = feedRegistry.decimals(asset, USD_ADDR);
112
+ uint256 decimalDelta = feedRegistry.decimalsByName(symbol, "USD");
132
113
  return (uint256(answer) * (10 ** (18 - decimalDelta))) * (10 ** (18 - decimals));
133
114
  }
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
+ }
134
139
  }
@@ -27,12 +27,29 @@ 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
+
30
41
  /// @notice Emit this event when new validation configs are added
31
42
  event ValidateConfigAdded(address indexed asset, uint256 indexed upperBound, uint256 indexed lowerBound);
32
43
 
33
44
  /// @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
34
47
  /// @custom:oz-upgrades-unsafe-allow constructor
35
- 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;
36
53
  _disableInitializers();
37
54
  }
38
55
 
@@ -62,6 +79,25 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
62
79
  __AccessControlled_init(accessControlManager_);
63
80
  }
64
81
 
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
+
65
101
  /**
66
102
  * @notice Add a single validation config
67
103
  * @param config Validation config struct
@@ -81,23 +117,6 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
81
117
  emit ValidateConfigAdded(config.asset, config.upperBoundRatio, config.lowerBoundRatio);
82
118
  }
83
119
 
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
-
101
120
  /**
102
121
  * @notice Test whether the reported price is within the valid bounds
103
122
  * @param asset Asset address
@@ -115,8 +134,18 @@ contract BoundValidator is AccessControlledV8, BoundValidatorInterface {
115
134
  return false;
116
135
  }
117
136
 
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;
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
+ }
122
151
  }
@@ -23,6 +23,14 @@ contract ChainlinkOracle is AccessControlledV8, OracleInterface {
23
23
  uint256 maxStalePeriod;
24
24
  }
25
25
 
26
+ /// @notice vBNB address
27
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
28
+ address public immutable vBnb;
29
+
30
+ /// @notice VAI address
31
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
32
+ address public immutable vai;
33
+
26
34
  /// @notice Set this as asset address for BNB. This is the underlying address for vBNB
27
35
  address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
28
36
 
@@ -43,18 +51,42 @@ contract ChainlinkOracle is AccessControlledV8, OracleInterface {
43
51
  _;
44
52
  }
45
53
 
46
- /// @notice Constructor for the implementation contract.
54
+ /// @notice Constructor for the implementation contract. Sets immutable variables.
55
+ /// @param vBnbAddress The address of the vBNB
56
+ /// @param vaiAddress The address of the VAI
47
57
  /// @custom:oz-upgrades-unsafe-allow constructor
48
- constructor() {
58
+ constructor(address vBnbAddress, address vaiAddress) notNullAddress(vBnbAddress) notNullAddress(vaiAddress) {
59
+ vBnb = vBnbAddress;
60
+ vai = vaiAddress;
49
61
  _disableInitializers();
50
62
  }
51
63
 
64
+ /**
65
+ * @notice Set the forced prices of the underlying token of input vToken
66
+ * @param vToken vToken address
67
+ * @param underlyingPriceMantissa price in 18 decimals
68
+ * @custom:access Only Governance
69
+ * @custom:error NotNullAddress thrown if address of vToken is null
70
+ * @custom:event Emits PricePosted event on succesfully setup of underlying price
71
+ */
72
+ function setUnderlyingPrice(
73
+ VBep20Interface vToken,
74
+ uint256 underlyingPriceMantissa
75
+ ) external notNullAddress(address(vToken)) {
76
+ _checkAccessAllowed("setUnderlyingPrice(address,uint256)");
77
+
78
+ address asset = address(vToken) == vBnb ? BNB_ADDR : address(vToken.underlying());
79
+ uint256 previousPriceMantissa = prices[asset];
80
+ prices[asset] = underlyingPriceMantissa;
81
+ emit PricePosted(asset, previousPriceMantissa, prices[asset]);
82
+ }
83
+
52
84
  /**
53
85
  * @notice Manually set the price of a given asset
54
86
  * @param asset Asset address
55
- * @param price Asset price in 18 decimals
87
+ * @param price Underlying price in 18 decimals
56
88
  * @custom:access Only Governance
57
- * @custom:event Emits PricePosted event on succesfully setup of asset price
89
+ * @custom:event Emits PricePosted event on succesfully setup of underlying price
58
90
  */
59
91
  function setDirectPrice(address asset, uint256 price) external notNullAddress(asset) {
60
92
  _checkAccessAllowed("setDirectPrice(address,uint256)");
@@ -90,7 +122,16 @@ contract ChainlinkOracle is AccessControlledV8, OracleInterface {
90
122
  }
91
123
 
92
124
  /**
93
- * @notice Add single token config. asset & feed cannot be null addresses and maxStalePeriod must be positive
125
+ * @notice Gets the Chainlink price for the underlying asset of a given vToken, revert when vToken is a null address
126
+ * @param vToken vToken address
127
+ * @return price Underlying price in USD
128
+ */
129
+ function getUnderlyingPrice(address vToken) external view override returns (uint256) {
130
+ return _getUnderlyingPriceInternal(VBep20Interface(vToken));
131
+ }
132
+
133
+ /**
134
+ * @notice Add single token config. vToken & feed cannot be null addresses and maxStalePeriod must be positive
94
135
  * @param tokenConfig Token config struct
95
136
  * @custom:access Only Governance
96
137
  * @custom:error NotNullAddress error is thrown if asset address is null
@@ -109,46 +150,45 @@ contract ChainlinkOracle is AccessControlledV8, OracleInterface {
109
150
  }
110
151
 
111
152
  /**
112
- * @notice Gets the price of a asset from the chainlink oracle
113
- * @param asset Address of the asset
114
- * @return Price in USD from Chainlink or a manually set price for the asset
153
+ * @notice Gets the Chainlink price for the underlying asset of a given vToken
154
+ * or the manually set price if it's been set
155
+ * @dev The decimals of the underlying token are considered to ensure the returned price
156
+ * has 18 decimals of precision
157
+ * @param vToken vToken address
158
+ * @return price Underlying price in USD
115
159
  */
116
- function getPrice(address asset) public view returns (uint256) {
160
+ function _getUnderlyingPriceInternal(VBep20Interface vToken) private view returns (uint256 price) {
161
+ address token;
117
162
  uint256 decimals;
118
163
 
119
- if (asset == BNB_ADDR) {
164
+ // vBNB token doesn't have `underlying` method
165
+ if (address(vToken) == vBnb) {
166
+ token = BNB_ADDR;
167
+ decimals = 18;
168
+ } else if (address(vToken) == vai) {
169
+ token = vai;
120
170
  decimals = 18;
121
171
  } else {
122
- IERC20Metadata token = IERC20Metadata(asset);
123
- decimals = token.decimals();
172
+ token = vToken.underlying();
173
+ decimals = VBep20Interface(token).decimals();
124
174
  }
125
175
 
126
- return _getPriceInternal(asset, decimals);
127
- }
128
-
129
- /**
130
- * @notice Gets the Chainlink price for a given asset
131
- * @param asset address of the asset
132
- * @param decimals decimals of the asset
133
- * @return price Asset price in USD or a manually set price of the asset
134
- */
135
- function _getPriceInternal(address asset, uint256 decimals) internal view returns (uint256 price) {
136
- uint256 tokenPrice = prices[asset];
176
+ uint256 tokenPrice = prices[token];
137
177
  if (tokenPrice != 0) {
138
178
  price = tokenPrice;
139
179
  } else {
140
- price = _getChainlinkPrice(asset);
180
+ price = _getChainlinkPrice(token);
141
181
  }
142
182
 
143
- uint256 decimalDelta = 18 - decimals;
183
+ uint256 decimalDelta = 18 - uint256(decimals);
144
184
  return price * (10 ** decimalDelta);
145
185
  }
146
186
 
147
187
  /**
148
- * @notice Get the Chainlink price for an asset, revert if token config doesn't exist
188
+ * @notice Get the Chainlink price for the underlying asset of a given vToken, revert if token config doesn't exist
149
189
  * @dev The precision of the price feed is used to ensure the returned price has 18 decimals of precision
150
- * @param asset Address of the asset
151
- * @return price Price in USD, with 18 decimals of precision
190
+ * @param asset Underlying asset address
191
+ * @return price Underlying price in USD, with 18 decimals of precision
152
192
  * @custom:error NotNullAddress error is thrown if the asset address is null
153
193
  * @custom:error Price error is thrown if the Chainlink price of asset is not greater than zero
154
194
  * @custom:error Timing error is thrown if current timestamp is less than the last updatedAt timestamp
@@ -30,6 +30,14 @@ contract PythOracle is AccessControlledV8, OracleInterface {
30
30
  /// @notice Exponent scale (decimal precision) of prices
31
31
  uint256 public constant EXP_SCALE = 1e18;
32
32
 
33
+ /// @notice vBNB address
34
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
35
+ address public immutable vBnb;
36
+
37
+ /// @notice VAI address
38
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
39
+ address public immutable vai;
40
+
33
41
  /// @notice Set this as asset address for BNB. This is the underlying for vBNB
34
42
  address public constant BNB_ADDR = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
35
43
 
@@ -43,16 +51,20 @@ contract PythOracle is AccessControlledV8, OracleInterface {
43
51
  event PythOracleSet(address indexed oldPythOracle, address indexed newPythOracle);
44
52
 
45
53
  /// @notice Emit when a token config is added
46
- event TokenConfigAdded(address indexed asset, bytes32 indexed pythId, uint64 indexed maxStalePeriod);
54
+ event TokenConfigAdded(address indexed vToken, bytes32 indexed pythId, uint64 indexed maxStalePeriod);
47
55
 
48
56
  modifier notNullAddress(address someone) {
49
57
  if (someone == address(0)) revert("can't be zero address");
50
58
  _;
51
59
  }
52
60
 
53
- /// @notice Constructor for the implementation contract.
61
+ /// @notice Constructor for the implementation contract. Sets immutable variables.
62
+ /// @param vBnbAddress The address of the vBNB
63
+ /// @param vaiAddress The address of the VAI
54
64
  /// @custom:oz-upgrades-unsafe-allow constructor
55
- constructor() {
65
+ constructor(address vBnbAddress, address vaiAddress) notNullAddress(vBnbAddress) notNullAddress(vaiAddress) {
66
+ vBnb = vBnbAddress;
67
+ vai = vaiAddress;
56
68
  _disableInitializers();
57
69
  }
58
70
 
@@ -105,38 +117,32 @@ contract PythOracle is AccessControlledV8, OracleInterface {
105
117
  }
106
118
 
107
119
  /**
108
- * @notice Set single token config. `maxStalePeriod` cannot be 0 and `asset` cannot be a null address
109
- * @param tokenConfig Token config struct
110
- * @custom:access Only Governance
111
- * @custom:error Range error is thrown if max stale period is zero
112
- * @custom:error NotNullAddress error is thrown if asset address is null
113
- */
114
- function setTokenConfig(TokenConfig memory tokenConfig) public notNullAddress(tokenConfig.asset) {
115
- _checkAccessAllowed("setTokenConfig(TokenConfig)");
116
- if (tokenConfig.maxStalePeriod == 0) revert("max stale period cannot be 0");
117
- tokenConfigs[tokenConfig.asset] = tokenConfig;
118
- emit TokenConfigAdded(tokenConfig.asset, tokenConfig.pythId, tokenConfig.maxStalePeriod);
119
- }
120
-
121
- /**
122
- * @notice Gets the price of a asset from the pyth oracle
123
- * @param asset Address of the asset
124
- * @return Price in USD
120
+ * @notice Get price of underlying asset of the input vToken, under the hood this function
121
+ * get price from Pyth contract, the prices of which are updated externally
122
+ * @param vToken vToken address
123
+ * @return price Underlying price with a precision of 18 decimals
124
+ * @custom:error Zero address error thrown if underlyingPythOracle address is null
125
+ * @custom:error Zero address error thrown if asset address is null
126
+ * @custom:error Range error thrown if price of Pyth oracle is not greater than zero
125
127
  */
126
- function getPrice(address asset) public view returns (uint256) {
128
+ function getUnderlyingPrice(
129
+ address vToken
130
+ ) external view override notNullAddress(address(underlyingPythOracle)) returns (uint256) {
131
+ address asset;
127
132
  uint256 decimals;
128
133
 
129
- if (asset == BNB_ADDR) {
134
+ // VBNB token doesn't have `underlying` method
135
+ if (vToken == vBnb) {
136
+ asset = BNB_ADDR;
137
+ decimals = 18;
138
+ } else if (vToken == vai) {
139
+ asset = vai;
130
140
  decimals = 18;
131
141
  } else {
132
- IERC20Metadata token = IERC20Metadata(asset);
133
- decimals = token.decimals();
142
+ asset = VBep20Interface(vToken).underlying();
143
+ decimals = VBep20Interface(asset).decimals();
134
144
  }
135
145
 
136
- return _getPriceInternal(asset, decimals);
137
- }
138
-
139
- function _getPriceInternal(address asset, uint256 decimals) internal view returns (uint256) {
140
146
  TokenConfig storage tokenConfig = tokenConfigs[asset];
141
147
  if (tokenConfig.asset == address(0)) revert("asset doesn't exist");
142
148
 
@@ -158,4 +164,18 @@ contract PythOracle is AccessControlledV8, OracleInterface {
158
164
  return ((price * EXP_SCALE) / (10 ** int256(-priceInfo.expo).toUint256())) * (10 ** (18 - decimals));
159
165
  }
160
166
  }
167
+
168
+ /**
169
+ * @notice Set single token config. `maxStalePeriod` cannot be 0 and `vToken` can be a null address
170
+ * @param tokenConfig Token config struct
171
+ * @custom:access Only Governance
172
+ * @custom:error Range error is thrown if max stale period is zero
173
+ * @custom:error NotNullAddress error is thrown if asset address is null
174
+ */
175
+ function setTokenConfig(TokenConfig memory tokenConfig) public notNullAddress(tokenConfig.asset) {
176
+ _checkAccessAllowed("setTokenConfig(TokenConfig)");
177
+ if (tokenConfig.maxStalePeriod == 0) revert("max stale period cannot be 0");
178
+ tokenConfigs[tokenConfig.asset] = tokenConfig;
179
+ emit TokenConfigAdded(tokenConfig.asset, tokenConfig.pythId, tokenConfig.maxStalePeriod);
180
+ }
161
181
  }