@venusprotocol/oracle 1.6.10 → 1.6.11-dev.11
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.
- package/LICENSE +11 -0
- package/README.md +140 -0
- package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorInterface.sol/AggregatorInterface.dbg.json +1 -1
- package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.dbg.json +1 -1
- package/artifacts/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol/AggregatorV3Interface.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/access/AccessControl.sol/AccessControl.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/access/IAccessControl.sol/IAccessControl.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json +2 -2
- package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.json +2 -2
- package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.json +2 -2
- package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.json +2 -2
- package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.json +2 -2
- package/artifacts/@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.dbg.json +1 -1
- package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.json +2 -2
- package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +1 -1
- package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol/AccessControlManager.dbg.json +4 -0
- package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol/AccessControlManager.json +369 -0
- package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.dbg.json +4 -0
- package/artifacts/{contracts/Governance/AccessControlled.sol/AccessControlled.json → @venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.json} +3 -3
- package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.dbg.json +4 -0
- package/artifacts/{contracts/Governance/IAccessControlManager.sol/IAccessControlManager.json → @venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.json} +2 -2
- package/artifacts/build-info/2f9bd32d7afaff51cf1627ed4903b445.json +1 -0
- package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +1 -1
- package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +8 -8
- package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PublicResolverInterface.sol/PublicResolverInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.json +2 -2
- package/artifacts/contracts/interfaces/SIDRegistryInterface.sol/SIDRegistryInterface.dbg.json +1 -1
- package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +1 -1
- package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +1 -1
- package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.json +2 -2
- package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +1 -1
- package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +1 -1
- package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.json +2 -2
- package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +221 -3
- package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +1 -1
- package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +5 -5
- package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +7 -13
- package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +11 -5
- package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +24 -37
- package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.json +2 -2
- package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.json +3 -22
- package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.json +2 -2
- package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.json +2 -2
- package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.json +2 -2
- package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.dbg.json +4 -0
- package/artifacts/contracts/test/AccessControlManager.sol/AccessControlManagerScenario.json +364 -0
- package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +1 -1
- package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.json +15 -2
- package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +1 -1
- package/artifacts/contracts/test/MockPyth.sol/MockPyth.json +2 -2
- package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +1 -1
- package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +2 -2
- package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +1 -1
- package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.json +2 -2
- package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +1 -1
- package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.json +2 -2
- package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +1 -1
- package/artifacts/contracts/test/PancakePairHarness.sol/Math.json +2 -2
- package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +1 -1
- package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.json +2 -2
- package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +1 -1
- package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.json +2 -2
- package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +1 -1
- package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.json +15 -2
- package/contracts/ResilientOracle.sol +45 -37
- package/contracts/interfaces/PythInterface.sol +4 -1
- package/contracts/oracles/BinanceOracle.sol +65 -24
- package/contracts/oracles/BoundValidator.sol +51 -54
- package/contracts/oracles/ChainlinkOracle.sol +54 -54
- package/contracts/oracles/PythOracle.sol +48 -47
- package/contracts/oracles/TwapOracle.sol +85 -83
- package/contracts/oracles/mocks/MockBinanceFeedRegistry.sol +1 -1
- package/contracts/oracles/mocks/MockBinanceOracle.sol +1 -7
- package/contracts/oracles/mocks/MockPythOracle.sol +1 -1
- package/contracts/oracles/mocks/MockTwapOracle.sol +1 -1
- package/contracts/test/AccessControlManager.sol +6 -0
- package/contracts/test/BEP20Harness.sol +4 -0
- package/contracts/test/MockPyth.sol +9 -2
- package/deployments/bscmainnet/.chainId +1 -0
- package/deployments/bscmainnet/BinanceOracle.json +571 -0
- package/deployments/bscmainnet/BinanceOracle_Implementation.json +629 -0
- package/deployments/bscmainnet/BinanceOracle_Proxy.json +251 -0
- package/deployments/bscmainnet/BoundValidator.json +621 -0
- package/deployments/bscmainnet/BoundValidator_Implementation.json +706 -0
- package/deployments/bscmainnet/BoundValidator_Proxy.json +251 -0
- package/deployments/bscmainnet/ChainlinkOracle.json +691 -0
- package/deployments/bscmainnet/ChainlinkOracle_Implementation.json +820 -0
- package/deployments/bscmainnet/ChainlinkOracle_Proxy.json +251 -0
- package/deployments/bscmainnet/DefaultProxyAdmin.json +257 -0
- package/deployments/bscmainnet/PythOracle.json +688 -0
- package/deployments/bscmainnet/PythOracle_Implementation.json +804 -0
- package/deployments/bscmainnet/PythOracle_Proxy.json +265 -0
- package/deployments/bscmainnet/ResilientOracle.json +841 -0
- package/deployments/bscmainnet/ResilientOracle_Implementation.json +1044 -0
- package/deployments/bscmainnet/ResilientOracle_Proxy.json +251 -0
- package/deployments/bscmainnet/TwapOracle.json +887 -0
- package/deployments/bscmainnet/TwapOracle_Implementation.json +1111 -0
- package/deployments/bscmainnet/TwapOracle_Proxy.json +251 -0
- package/deployments/bscmainnet/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
- package/deployments/bscmainnet/solcInputs/aae9f1601b873a5a4704ede73ede10cb.json +135 -0
- package/deployments/bsctestnet/BinanceOracle.json +273 -31
- package/deployments/bsctestnet/BinanceOracle_Implementation.json +398 -28
- package/deployments/bsctestnet/BinanceOracle_Proxy.json +52 -28
- package/deployments/bsctestnet/BoundValidator.json +40 -40
- package/deployments/bsctestnet/BoundValidator_Implementation.json +43 -51
- package/deployments/bsctestnet/BoundValidator_Proxy.json +35 -35
- package/deployments/bsctestnet/ChainlinkOracle.json +57 -49
- package/deployments/bsctestnet/ChainlinkOracle_Implementation.json +46 -52
- package/deployments/bsctestnet/ChainlinkOracle_Proxy.json +50 -36
- package/deployments/bsctestnet/DefaultProxyAdmin.json +18 -18
- package/deployments/bsctestnet/PythOracle.json +52 -45
- package/deployments/bsctestnet/PythOracle_Implementation.json +53 -47
- package/deployments/bsctestnet/PythOracle_Proxy.json +41 -40
- package/deployments/bsctestnet/ResilientOracle.json +51 -42
- package/deployments/bsctestnet/ResilientOracle_Implementation.json +65 -68
- package/deployments/bsctestnet/ResilientOracle_Proxy.json +48 -34
- package/deployments/bsctestnet/TwapOracle.json +70 -83
- package/deployments/bsctestnet/TwapOracle_Implementation.json +84 -97
- package/deployments/bsctestnet/TwapOracle_Proxy.json +46 -46
- package/deployments/bsctestnet/solcInputs/131f58942628e21632de19506977a910.json +120 -0
- package/deployments/bsctestnet/solcInputs/144b49e8aec3add79553ba273ea32d00.json +72 -0
- package/deployments/bsctestnet/solcInputs/503f2e4e766f3be923fadf8030a5470f.json +78 -0
- package/deployments/bsctestnet/solcInputs/b39d33092f7dd383ae3a042566194132.json +75 -0
- package/dist/deploy/1-deploy-oracles.d.ts +5 -0
- package/dist/deploy/1-deploy-oracles.d.ts.map +1 -0
- package/{deploy → dist/deploy}/1-deploy-oracles.js +66 -24
- package/dist/deploy/1-deploy-oracles.js.map +1 -0
- package/dist/deploy/2-configure-feeds.d.ts +15 -0
- package/dist/deploy/2-configure-feeds.d.ts.map +1 -0
- package/{deploy → dist/deploy}/2-configure-feeds.js +139 -16
- package/dist/deploy/2-configure-feeds.js.map +1 -0
- package/dist/hardhat.config.d.ts +13 -0
- package/dist/hardhat.config.d.ts.map +1 -0
- package/dist/hardhat.config.js +158 -0
- package/dist/hardhat.config.js.map +1 -0
- package/package.json +26 -19
- package/artifacts/@openzeppelin/contracts/utils/math/SafeMath.sol/SafeMath.dbg.json +0 -4
- package/artifacts/@openzeppelin/contracts/utils/math/SafeMath.sol/SafeMath.json +0 -10
- package/artifacts/build-info/a47a69a8f108d3e7113faf89771988ff.json +0 -1
- package/artifacts/build-info/cc300b55b814df48229058e829c7c3c6.json +0 -1
- package/artifacts/build-info/de79b2637546f6f7d38c8f655549d9e2.json +0 -1
- package/artifacts/contracts/Governance/AccessControlManager.sol/AccessControlManager.dbg.json +0 -4
- package/artifacts/contracts/Governance/AccessControlManager.sol/AccessControlManager.json +0 -369
- package/artifacts/contracts/Governance/AccessControlled.sol/AccessControlled.dbg.json +0 -4
- package/artifacts/contracts/Governance/IAccessControlManager.sol/IAccessControlManager.dbg.json +0 -4
- package/artifacts/contracts/PriceOracle.sol/PriceOracle.dbg.json +0 -4
- package/artifacts/contracts/PriceOracle.sol/PriceOracle.json +0 -43
- package/contracts/Governance/AccessControlManager.sol +0 -99
- package/contracts/Governance/AccessControlled.sol +0 -75
- package/contracts/Governance/IAccessControlManager.sol +0 -22
- package/contracts/PriceOracle.sol +0 -16
- package/deploy/1-deploy-oracles.d.ts +0 -2
- package/deploy/1-deploy-oracles.d.ts.map +0 -1
- package/deploy/1-deploy-oracles.js.map +0 -1
- package/deploy/2-configure-feeds.d.ts +0 -2
- package/deploy/2-configure-feeds.d.ts.map +0 -1
- package/deploy/2-configure-feeds.js.map +0 -1
- package/deployments/bsctestnet/AccessControlManager.json +0 -563
- package/deployments/bsctestnet/solcInputs/30f2cb3e89e183b8669f05bddf9426d9.json +0 -183
|
@@ -5,31 +5,31 @@ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
|
|
5
5
|
import "../libraries/PancakeLibrary.sol";
|
|
6
6
|
import "../interfaces/OracleInterface.sol";
|
|
7
7
|
import "../interfaces/VBep20Interface.sol";
|
|
8
|
-
import "
|
|
8
|
+
import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
uint256 acc;
|
|
13
|
-
}
|
|
10
|
+
contract TwapOracle is AccessControlledV8, TwapInterface {
|
|
11
|
+
using FixedPoint for *;
|
|
14
12
|
|
|
15
|
-
struct
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
}
|
|
13
|
+
struct Observation {
|
|
14
|
+
uint256 timestamp;
|
|
15
|
+
uint256 acc;
|
|
16
|
+
}
|
|
30
17
|
|
|
31
|
-
|
|
32
|
-
|
|
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 represented 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
33
|
|
|
34
34
|
/// @notice WBNB address
|
|
35
35
|
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
|
|
@@ -44,10 +44,8 @@ contract TwapOracle is AccessControlled, 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
|
|
48
|
-
uint256 public constant
|
|
49
|
-
|
|
50
|
-
uint256 public constant expScale = 1e18;
|
|
47
|
+
uint256 public constant BNB_BASE_UNIT = 1e18;
|
|
48
|
+
uint256 public constant BUSD_BASE_UNIT = 1e18;
|
|
51
49
|
|
|
52
50
|
/// @notice Configs by token
|
|
53
51
|
mapping(address => TokenConfig) public tokenConfigs;
|
|
@@ -77,7 +75,7 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
77
75
|
event TokenConfigAdded(address indexed asset, address indexed pancakePool, uint256 indexed anchorPeriod);
|
|
78
76
|
|
|
79
77
|
modifier notNullAddress(address someone) {
|
|
80
|
-
|
|
78
|
+
if (someone == address(0)) revert("can't be zero address");
|
|
81
79
|
_;
|
|
82
80
|
}
|
|
83
81
|
|
|
@@ -102,13 +100,40 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
102
100
|
* @param configs Config array
|
|
103
101
|
* @custom:error Zero length error thrown, if length of the config array is 0
|
|
104
102
|
*/
|
|
105
|
-
function setTokenConfigs(TokenConfig[] memory configs) external
|
|
106
|
-
|
|
107
|
-
require(configs.length > 0, "length can't be 0");
|
|
103
|
+
function setTokenConfigs(TokenConfig[] memory configs) external {
|
|
104
|
+
if (configs.length == 0) revert("length can't be 0");
|
|
108
105
|
uint256 numTokenConfigs = configs.length;
|
|
109
|
-
for (uint256 i; i < numTokenConfigs;
|
|
106
|
+
for (uint256 i; i < numTokenConfigs; ) {
|
|
110
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]);
|
|
111
135
|
}
|
|
136
|
+
return _updateTwapInternal(tokenConfigs[asset]);
|
|
112
137
|
}
|
|
113
138
|
|
|
114
139
|
/**
|
|
@@ -121,23 +146,14 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
121
146
|
function getUnderlyingPrice(address vToken) external view override returns (uint256) {
|
|
122
147
|
address asset = _getUnderlyingAsset(vToken);
|
|
123
148
|
|
|
124
|
-
|
|
149
|
+
if (tokenConfigs[asset].asset == address(0)) revert("asset not exist");
|
|
125
150
|
uint256 price = prices[asset];
|
|
126
151
|
|
|
127
152
|
// if price is 0, it means the price hasn't been updated yet and it's meaningless, revert
|
|
128
|
-
|
|
153
|
+
if (price == 0) revert("TWAP price must be positive");
|
|
129
154
|
return (price * (10 ** (18 - IERC20Metadata(asset).decimals())));
|
|
130
155
|
}
|
|
131
156
|
|
|
132
|
-
/**
|
|
133
|
-
* @notice Initializes the owner of the contract
|
|
134
|
-
* @param accessControlManager_ Address of the access control manager contract
|
|
135
|
-
*/
|
|
136
|
-
function initialize(address accessControlManager_) public initializer {
|
|
137
|
-
__Ownable2Step_init();
|
|
138
|
-
__AccessControlled_init_unchained(accessControlManager_);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
157
|
/**
|
|
142
158
|
* @notice Adds a single token config
|
|
143
159
|
* @param config token config struct
|
|
@@ -155,12 +171,9 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
155
171
|
) public notNullAddress(config.asset) notNullAddress(config.pancakePool) {
|
|
156
172
|
_checkAccessAllowed("setTokenConfig(TokenConfig)");
|
|
157
173
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
config.baseUnit == 10 ** IERC20Metadata(config.asset).decimals(),
|
|
162
|
-
"base unit decimals must be same as asset decimals"
|
|
163
|
-
);
|
|
174
|
+
if (config.anchorPeriod == 0) revert("anchor period must be positive");
|
|
175
|
+
if (config.baseUnit != 10 ** IERC20Metadata(config.asset).decimals())
|
|
176
|
+
revert("base unit decimals must be same as asset decimals");
|
|
164
177
|
|
|
165
178
|
uint256 cumulativePrice = currentCumulativePrice(config);
|
|
166
179
|
|
|
@@ -170,23 +183,6 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
170
183
|
emit TokenConfigAdded(config.asset, config.pancakePool, config.anchorPeriod);
|
|
171
184
|
}
|
|
172
185
|
|
|
173
|
-
/**
|
|
174
|
-
* @notice Updates the current token/BUSD price from PancakeSwap, with 18 decimals of precision.
|
|
175
|
-
* @return anchorPrice anchor price of the underlying asset of the vToken
|
|
176
|
-
* @custom:error Missing error is thrown if token config does not exist
|
|
177
|
-
*/
|
|
178
|
-
function updateTwap(address vToken) public returns (uint256) {
|
|
179
|
-
address asset = _getUnderlyingAsset(vToken);
|
|
180
|
-
|
|
181
|
-
require(tokenConfigs[asset].asset != address(0), "asset not exist");
|
|
182
|
-
// Update & fetch WBNB price first, so we can calculate the price of WBNB paired token
|
|
183
|
-
if (asset != WBNB && tokenConfigs[asset].isBnbBased) {
|
|
184
|
-
require(tokenConfigs[WBNB].asset != address(0), "WBNB not exist");
|
|
185
|
-
_updateTwapInternal(tokenConfigs[WBNB]);
|
|
186
|
-
}
|
|
187
|
-
return _updateTwapInternal(tokenConfigs[asset]);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
186
|
/**
|
|
191
187
|
* @notice Fetches the current token/WBNB and token/BUSD price accumulator from PancakeSwap.
|
|
192
188
|
* @return cumulative price of target token regardless of pair order
|
|
@@ -209,16 +205,22 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
209
205
|
* @custom:event Emits AnchorPriceUpdated event on successful update of observation with assset address,
|
|
210
206
|
* AnchorPrice, old observation timestamp and current timestamp
|
|
211
207
|
*/
|
|
212
|
-
function _updateTwapInternal(TokenConfig memory config)
|
|
208
|
+
function _updateTwapInternal(TokenConfig memory config) private returns (uint256) {
|
|
213
209
|
// pokeWindowValues already handled reversed pool cases,
|
|
214
210
|
// priceAverage will always be Token/BNB or Token/BUSD *twap** price.
|
|
215
211
|
(uint256 nowCumulativePrice, uint256 oldCumulativePrice, uint256 oldTimestamp) = pokeWindowValues(config);
|
|
216
212
|
|
|
213
|
+
if (block.timestamp == oldTimestamp) return prices[config.asset];
|
|
214
|
+
|
|
217
215
|
// This should be impossible, but better safe than sorry
|
|
218
|
-
|
|
219
|
-
|
|
216
|
+
if (block.timestamp < oldTimestamp) revert("now must come after before");
|
|
217
|
+
|
|
218
|
+
uint256 timeElapsed;
|
|
219
|
+
unchecked {
|
|
220
|
+
timeElapsed = block.timestamp - oldTimestamp;
|
|
221
|
+
}
|
|
220
222
|
|
|
221
|
-
// Calculate
|
|
223
|
+
// Calculate Pancake *twap**
|
|
222
224
|
FixedPoint.uq112x112 memory priceAverage = FixedPoint.uq112x112(
|
|
223
225
|
uint224((nowCumulativePrice - oldCumulativePrice) / timeElapsed)
|
|
224
226
|
);
|
|
@@ -226,18 +228,18 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
226
228
|
uint256 priceAverageMantissa = priceAverage.decode112with18();
|
|
227
229
|
|
|
228
230
|
// To cancel the decimals in cumulative price, we need to mulitply the average price with
|
|
229
|
-
// tokenBaseUnit / (
|
|
230
|
-
uint256 pairedTokenBaseUnit = config.isBnbBased ?
|
|
231
|
+
// tokenBaseUnit / (BNB_BASE_UNIT or BUSD_BASE_UNIT, which is 1e18)
|
|
232
|
+
uint256 pairedTokenBaseUnit = config.isBnbBased ? BNB_BASE_UNIT : BUSD_BASE_UNIT;
|
|
231
233
|
uint256 anchorPriceMantissa = (priceAverageMantissa * config.baseUnit) / pairedTokenBaseUnit;
|
|
232
234
|
|
|
233
235
|
// if this token is paired with BNB, convert its price to USD
|
|
234
236
|
if (config.isBnbBased) {
|
|
235
237
|
uint256 bnbPrice = prices[WBNB];
|
|
236
|
-
|
|
237
|
-
anchorPriceMantissa = (anchorPriceMantissa * bnbPrice) /
|
|
238
|
+
if (bnbPrice == 0) revert("bnb price is invalid");
|
|
239
|
+
anchorPriceMantissa = (anchorPriceMantissa * bnbPrice) / BNB_BASE_UNIT;
|
|
238
240
|
}
|
|
239
241
|
|
|
240
|
-
|
|
242
|
+
if (anchorPriceMantissa == 0) revert("twap price cannot be 0");
|
|
241
243
|
|
|
242
244
|
emit AnchorPriceUpdated(config.asset, anchorPriceMantissa, oldTimestamp, block.timestamp);
|
|
243
245
|
|
|
@@ -258,18 +260,14 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
258
260
|
*/
|
|
259
261
|
function pokeWindowValues(
|
|
260
262
|
TokenConfig memory config
|
|
261
|
-
)
|
|
263
|
+
) private returns (uint256, uint256 startCumulativePrice, uint256 startCumulativeTimestamp) {
|
|
262
264
|
uint256 cumulativePrice = currentCumulativePrice(config);
|
|
263
265
|
uint256 currentTimestamp = block.timestamp;
|
|
264
266
|
uint256 windowStartTimestamp = currentTimestamp - config.anchorPeriod;
|
|
265
267
|
Observation[] memory storedObservations = observations[config.asset];
|
|
266
268
|
|
|
267
269
|
uint256 storedObservationsLength = storedObservations.length;
|
|
268
|
-
for (
|
|
269
|
-
uint256 windowStartIndex = windowStart[config.asset];
|
|
270
|
-
windowStartIndex < storedObservationsLength;
|
|
271
|
-
++windowStartIndex
|
|
272
|
-
) {
|
|
270
|
+
for (uint256 windowStartIndex = windowStart[config.asset]; windowStartIndex < storedObservationsLength; ) {
|
|
273
271
|
if (
|
|
274
272
|
(storedObservations[windowStartIndex].timestamp >= windowStartTimestamp) ||
|
|
275
273
|
(windowStartIndex == storedObservationsLength - 1)
|
|
@@ -281,6 +279,10 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
281
279
|
} else {
|
|
282
280
|
delete observations[config.asset][windowStartIndex];
|
|
283
281
|
}
|
|
282
|
+
|
|
283
|
+
unchecked {
|
|
284
|
+
++windowStartIndex;
|
|
285
|
+
}
|
|
284
286
|
}
|
|
285
287
|
|
|
286
288
|
observations[config.asset].push(Observation(currentTimestamp, cumulativePrice));
|
|
@@ -299,10 +301,10 @@ contract TwapOracle is AccessControlled, TwapInterface {
|
|
|
299
301
|
* @param vToken vToken address
|
|
300
302
|
* @return asset underlying asset address
|
|
301
303
|
*/
|
|
302
|
-
function _getUnderlyingAsset(address vToken)
|
|
303
|
-
if (
|
|
304
|
+
function _getUnderlyingAsset(address vToken) private view returns (address asset) {
|
|
305
|
+
if (vToken == vBnb) {
|
|
304
306
|
asset = WBNB;
|
|
305
|
-
} else if (
|
|
307
|
+
} else if (vToken == vai) {
|
|
306
308
|
asset = vai;
|
|
307
309
|
} else {
|
|
308
310
|
asset = VBep20Interface(vToken).underlying();
|
|
@@ -20,7 +20,7 @@ contract MockBinanceFeedRegistry is FeedRegistryInterface {
|
|
|
20
20
|
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
|
|
21
21
|
{
|
|
22
22
|
quote;
|
|
23
|
-
return (0, int256(assetPrices[base]), 0,
|
|
23
|
+
return (0, int256(assetPrices[base]), 0, block.timestamp - 10, 0);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function decimalsByName(string memory base, string memory quote) external view override returns (uint8) {
|
|
@@ -3,23 +3,17 @@ pragma solidity 0.8.13;
|
|
|
3
3
|
|
|
4
4
|
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
|
|
5
5
|
import "../../interfaces/VBep20Interface.sol";
|
|
6
|
-
import "../../interfaces/FeedRegistryInterface.sol";
|
|
7
6
|
|
|
8
7
|
contract MockBinanceOracle is OwnableUpgradeable {
|
|
9
8
|
mapping(address => uint256) public assetPrices;
|
|
10
9
|
|
|
11
|
-
FeedRegistryInterface public feedRegistry;
|
|
12
|
-
|
|
13
10
|
constructor() {}
|
|
14
11
|
|
|
15
12
|
function setPrice(address asset, uint256 price) external {
|
|
16
13
|
assetPrices[asset] = price;
|
|
17
14
|
}
|
|
18
15
|
|
|
19
|
-
function initialize(
|
|
20
|
-
__Ownable_init();
|
|
21
|
-
feedRegistry = feed;
|
|
22
|
-
}
|
|
16
|
+
function initialize() public initializer {}
|
|
23
17
|
|
|
24
18
|
function getUnderlyingPrice(address vToken) public view returns (uint256) {
|
|
25
19
|
address token = VBep20Interface(vToken).underlying();
|
|
@@ -20,7 +20,7 @@ contract MockPythOracle is OwnableUpgradeable {
|
|
|
20
20
|
|
|
21
21
|
function initialize(address underlyingPythOracle_) public initializer {
|
|
22
22
|
__Ownable_init();
|
|
23
|
-
|
|
23
|
+
if (underlyingPythOracle_ == address(0)) revert("pyth oracle cannot be zero address");
|
|
24
24
|
underlyingPythOracle = IPyth(underlyingPythOracle_);
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -19,7 +19,7 @@ contract MockTwapOracle is OwnableUpgradeable {
|
|
|
19
19
|
|
|
20
20
|
function initialize(address vBNB_) public initializer {
|
|
21
21
|
__Ownable_init();
|
|
22
|
-
|
|
22
|
+
if (vBNB_ == address(0)) revert("vBNB can't be zero address");
|
|
23
23
|
vBNB = vBNB_;
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
pragma solidity 0.8.13;
|
|
3
|
+
import "@openzeppelin/contracts/access/AccessControl.sol";
|
|
4
|
+
import "@venusprotocol/governance-contracts/contracts/Governance/AccessControlManager.sol";
|
|
5
|
+
|
|
6
|
+
contract AccessControlManagerScenario is AccessControlManager {}
|
|
@@ -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;
|
|
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;
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
56
|