@venusprotocol/oracle 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +1 -1
  2. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +1 -1
  3. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +1 -1
  4. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +1 -1
  5. package/artifacts/@openzeppelin/contracts/utils/math/SafeCast.sol/SafeCast.dbg.json +1 -1
  6. package/artifacts/@openzeppelin/contracts/utils/math/SafeMath.sol/SafeMath.dbg.json +1 -1
  7. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +1 -1
  8. package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.dbg.json +1 -1
  9. package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.dbg.json +1 -1
  10. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.dbg.json +1 -1
  11. package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.dbg.json +1 -1
  12. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +1 -1
  13. package/artifacts/build-info/c88e3d40dfcb048309cae1c803479ead.json +1 -0
  14. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +1 -1
  15. package/artifacts/contracts/ResilientOracle.sol/ResilientOracle.json +35 -3
  16. package/artifacts/contracts/interfaces/AggregatorV2V3Interface.sol/AggregatorV2V3Interface.dbg.json +1 -1
  17. package/artifacts/contracts/interfaces/BEP20Interface.sol/BEP20Interface.dbg.json +1 -1
  18. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.dbg.json +4 -0
  19. package/artifacts/contracts/interfaces/FeedRegistryInterface.sol/FeedRegistryInterface.json +79 -0
  20. package/artifacts/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +4 -0
  21. package/artifacts/contracts/interfaces/OracleInterface.sol/{PivotValidatorInterface.json → BoundValidatorInterface.json} +8 -3
  22. package/artifacts/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +1 -1
  23. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +4 -0
  24. package/artifacts/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.json +43 -0
  25. package/artifacts/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +4 -0
  26. package/artifacts/contracts/interfaces/OracleInterface.sol/{PivotOracleInterface.json → TwapInterface.json} +5 -10
  27. package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
  28. package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
  29. package/artifacts/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +1 -1
  30. package/artifacts/contracts/libraries/PancakeLibrary.sol/FixedPoint.dbg.json +1 -1
  31. package/artifacts/contracts/libraries/PancakeLibrary.sol/IPancakePair.dbg.json +1 -1
  32. package/artifacts/contracts/libraries/PancakeLibrary.sol/PancakeOracleLibrary.dbg.json +1 -1
  33. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.dbg.json +4 -0
  34. package/artifacts/contracts/oracles/BinanceOracle.sol/BinanceOracle.json +69 -0
  35. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.dbg.json +1 -1
  36. package/artifacts/contracts/oracles/BoundValidator.sol/BoundValidator.json +13 -6
  37. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.dbg.json +1 -1
  38. package/artifacts/contracts/oracles/ChainlinkOracle.sol/ChainlinkOracle.json +6 -6
  39. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
  40. package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +5 -5
  41. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.dbg.json +1 -1
  42. package/artifacts/contracts/oracles/TwapOracle.sol/TwapOracle.json +23 -23
  43. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.dbg.json +4 -0
  44. package/artifacts/contracts/oracles/mocks/MockBinanceFeedRegistry.sol/MockBinanceFeedRegistry.json +116 -0
  45. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +4 -0
  46. package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.json +144 -0
  47. package/artifacts/contracts/oracles/mocks/MockChainlinkOracle.sol/MockChainlinkOracle.dbg.json +1 -1
  48. package/artifacts/contracts/oracles/mocks/MockPythOracle.sol/MockPythOracle.dbg.json +1 -1
  49. package/artifacts/contracts/oracles/mocks/MockTwapOracle.sol/MockTwapOracle.dbg.json +4 -0
  50. package/artifacts/contracts/oracles/mocks/{MockPivotTwapOracle.sol/MockPivotTwapOracle.json → MockTwapOracle.sol/MockTwapOracle.json} +2 -2
  51. package/artifacts/contracts/test/BEP20Harness.sol/BEP20Harness.dbg.json +1 -1
  52. package/artifacts/contracts/test/MockPyth.sol/MockPyth.dbg.json +1 -1
  53. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.dbg.json +4 -0
  54. package/artifacts/contracts/test/MockSimpleOracle.sol/MockBoundValidator.json +95 -0
  55. package/artifacts/contracts/test/MockSimpleOracle.sol/MockSimpleOracle.dbg.json +1 -1
  56. package/artifacts/contracts/test/MockV3Aggregator.sol/MockV3Aggregator.dbg.json +1 -1
  57. package/artifacts/contracts/test/PancakePairHarness.sol/Math.dbg.json +1 -1
  58. package/artifacts/contracts/test/PancakePairHarness.sol/PancakePairHarness.dbg.json +1 -1
  59. package/artifacts/contracts/test/PancakePairHarness.sol/UQ112x112.dbg.json +1 -1
  60. package/artifacts/contracts/test/VBEP20Harness.sol/VBEP20Harness.dbg.json +1 -1
  61. package/deploy/oracles.d.ts +1 -1
  62. package/deploy/oracles.d.ts.map +1 -1
  63. package/deploy/oracles.js +43 -29
  64. package/deploy/oracles.js.map +1 -1
  65. package/deployments/.gitkeep +0 -0
  66. package/package.json +4 -5
  67. package/artifacts/build-info/5759fed659a82870ff637da81e15ba57.json +0 -1
  68. package/artifacts/build-info/b22018d758b06a50b7c80c494f08be30.json +0 -1
  69. package/artifacts/contracts/interfaces/OracleInterface.sol/PivotOracleInterface.dbg.json +0 -4
  70. package/artifacts/contracts/interfaces/OracleInterface.sol/PivotValidatorInterface.dbg.json +0 -4
  71. package/artifacts/contracts/oracles/PivotPythOracle.sol/PivotPythOracle.dbg.json +0 -4
  72. package/artifacts/contracts/oracles/PivotPythOracle.sol/PivotPythOracle.json +0 -441
  73. package/artifacts/contracts/oracles/PivotTwapOracle.sol/PivotTwapOracle.dbg.json +0 -4
  74. package/artifacts/contracts/oracles/PivotTwapOracle.sol/PivotTwapOracle.json +0 -691
  75. package/artifacts/contracts/oracles/mocks/MockPivotTwapOracle.sol/MockPivotTwapOracle.dbg.json +0 -4
  76. package/artifacts/contracts/test/MockBoundValidator.sol/MockBoundValidator.dbg.json +0 -4
  77. package/artifacts/contracts/test/MockBoundValidator.sol/MockBoundValidator.json +0 -226
  78. package/artifacts/contracts/test/MockSimpleOracle.sol/MockPivotOracle.dbg.json +0 -4
  79. package/artifacts/contracts/test/MockSimpleOracle.sol/MockPivotOracle.json +0 -133
  80. package/contracts/ResilientOracle.sol +0 -220
  81. package/contracts/interfaces/AggregatorV2V3Interface.sol +0 -60
  82. package/contracts/interfaces/BEP20Interface.sol +0 -97
  83. package/contracts/interfaces/OracleInterface.sol +0 -15
  84. package/contracts/interfaces/PythInterface.sol +0 -160
  85. package/contracts/interfaces/VBep20Interface.sol +0 -11
  86. package/contracts/libraries/PancakeLibrary.sol +0 -64
  87. package/contracts/oracles/BoundValidator.sol +0 -96
  88. package/contracts/oracles/ChainlinkOracle.sol +0 -178
  89. package/contracts/oracles/PivotPythOracle.sol +0 -18
  90. package/contracts/oracles/PivotTwapOracle.sol +0 -18
  91. package/contracts/oracles/PythOracle.sol +0 -129
  92. package/contracts/oracles/TwapOracle.sol +0 -234
  93. package/contracts/oracles/mocks/MockChainlinkOracle.sol +0 -26
  94. package/contracts/oracles/mocks/MockPivotTwapOracle.sol +0 -31
  95. package/contracts/oracles/mocks/MockPythOracle.sol +0 -31
  96. package/contracts/test/BEP20Harness.sol +0 -21
  97. package/contracts/test/MockBoundValidator.sol +0 -10
  98. package/contracts/test/MockPyth.sol +0 -78
  99. package/contracts/test/MockSimpleOracle.sol +0 -37
  100. package/contracts/test/MockV3Aggregator.sol +0 -91
  101. package/contracts/test/PancakePairHarness.sol +0 -113
  102. package/contracts/test/VBEP20Harness.sol +0 -22
@@ -1,129 +0,0 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
-
4
- import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5
- import "@openzeppelin/contracts/utils/math/SafeCast.sol";
6
- import "@openzeppelin/contracts/utils/math/SignedMath.sol";
7
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
8
- import "../interfaces/PythInterface.sol";
9
- import "../interfaces/OracleInterface.sol";
10
-
11
- struct TokenConfig {
12
- bytes32 pythId;
13
- address vToken;
14
- uint64 maxStalePeriod;
15
- }
16
-
17
- /**
18
- * PythOracle contract reads prices from actual Pyth oracle contract which accepts/verifies and stores the
19
- * updated prices from external sources
20
- */
21
- contract PythOracle is OwnableUpgradeable, OracleInterface {
22
- using SafeMath for uint256;
23
-
24
- // To calculate 10 ** n(which is a signed type)
25
- using SignedMath for int256;
26
-
27
- // To cast int64/int8 types from Pyth to unsigned types
28
- using SafeCast for int256;
29
-
30
- /// @notice price decimals
31
- uint256 public constant EXP_SCALE = 1e18;
32
-
33
- /// @notice the actual pyth oracle address fetch & store the prices
34
- IPyth public underlyingPythOracle;
35
-
36
- /// @notice emit when setting a new pyth oracle address
37
- event PythOracleSet(address indexed newPythOracle);
38
-
39
- /// @notice emit when token config added
40
- event TokenConfigAdded(
41
- address indexed vToken,
42
- bytes32 indexed pythId,
43
- uint64 indexed maxStalePeriod
44
- );
45
-
46
- /// @notice token configs by vToken address
47
- mapping(address => TokenConfig) public tokenConfigs;
48
-
49
- modifier notNullAddress(address someone) {
50
- require(someone != address(0), "can't be zero address");
51
- _;
52
- }
53
-
54
- function initialize(address underlyingPythOracle_) public initializer {
55
- __Ownable_init();
56
- require(underlyingPythOracle_ != address(0), "pyth oracle cannot be zero address");
57
- underlyingPythOracle = IPyth(underlyingPythOracle_);
58
- emit PythOracleSet(underlyingPythOracle_);
59
- }
60
-
61
- /**
62
- * @notice Batch set token configs
63
- * @param tokenConfigs_ token config array
64
- */
65
- function setTokenConfigs(TokenConfig[] memory tokenConfigs_) external onlyOwner() {
66
- require(tokenConfigs_.length != 0, "length can't be 0");
67
- for (uint256 i = 0; i < tokenConfigs_.length; i++) {
68
- setTokenConfig(tokenConfigs_[i]);
69
- }
70
- }
71
-
72
- /**
73
- * @notice Set single token config, `maxStalePeriod` cannot be 0 and `vToken` can be zero address
74
- * @param tokenConfig token config struct
75
- */
76
- function setTokenConfig(TokenConfig memory tokenConfig) public
77
- onlyOwner()
78
- notNullAddress(tokenConfig.vToken)
79
- {
80
- require(tokenConfig.maxStalePeriod != 0, "max stale period cannot be 0");
81
- tokenConfigs[tokenConfig.vToken] = tokenConfig;
82
- emit TokenConfigAdded(tokenConfig.vToken, tokenConfig.pythId, tokenConfig.maxStalePeriod);
83
- }
84
-
85
- /**
86
- * @notice set the underlying pyth oracle contract address
87
- * @param underlyingPythOracle_ pyth oracle contract address
88
- */
89
- function setUnderlyingPythOracle(IPyth underlyingPythOracle_)
90
- external
91
- onlyOwner()
92
- notNullAddress(address(underlyingPythOracle_))
93
- {
94
- underlyingPythOracle = underlyingPythOracle_;
95
- emit PythOracleSet(address(underlyingPythOracle_));
96
- }
97
-
98
- /**
99
- * @notice Get price of underlying asset of the input vToken, under the hood this function
100
- * get price from Pyth contract, the prices of which are updated externally
101
- * @param vToken vToken address
102
- * @return price in 10 decimals
103
- */
104
- function getUnderlyingPrice(address vToken) public view override returns (uint256) {
105
- require(address(underlyingPythOracle) != address(0), "Pyth oracle is zero address");
106
- TokenConfig storage tokenConfig = tokenConfigs[vToken];
107
- require(tokenConfig.vToken != address(0), "vToken doesn't exist");
108
-
109
- // if the price is expired after it's compared against `maxStalePeriod`, the following call will revert
110
- PythStructs.Price memory priceInfo = underlyingPythOracle.getLatestAvailablePriceWithinDuration(
111
- tokenConfig.pythId,
112
- tokenConfig.maxStalePeriod
113
- );
114
-
115
- uint256 price = int256(priceInfo.price).toUint256();
116
-
117
- require(price > 0, "Pyth oracle price must be positive");
118
-
119
- // the price returned from Pyth is price ** 10^expo, which is the real dollar price of the assets
120
- // we need to multiply it by 1e18 to make the price 18 decimals
121
- if (priceInfo.expo > 0) {
122
- return price.mul(EXP_SCALE).mul(10 ** int256(priceInfo.expo).toUint256());
123
- } else {
124
- return price.mul(EXP_SCALE).div(10 ** int256(-priceInfo.expo).toUint256());
125
- }
126
-
127
- }
128
-
129
- }
@@ -1,234 +0,0 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
- pragma experimental ABIEncoderV2;
4
-
5
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
6
- import "@openzeppelin/contracts/utils/math/SafeMath.sol";
7
- import "../libraries/PancakeLibrary.sol";
8
- import "../interfaces/OracleInterface.sol";
9
-
10
-
11
- struct Observation {
12
- uint256 timestamp;
13
- uint256 acc;
14
- }
15
-
16
- struct TokenConfig {
17
- /// @notice vToken address, which can't be zero address and can be used for existance check
18
- address vToken;
19
- /// @notice Decimals of underlying asset
20
- uint256 baseUnit;
21
- /// @notice The address of pancake pair
22
- address pancakePool;
23
- /// @notice Whether the token is paired with WBNB
24
- bool isBnbBased;
25
- /// @notice A flag identifies whether the pancake pair is reversed
26
- /// e.g. XVS-WBNB is not reversed, while WBNB-XVS is.
27
- bool isReversedPool;
28
- /// @notice TWAP update period in second, which is the minimum time in seconds required to update TWAP window
29
- uint256 anchorPeriod;
30
- }
31
-
32
- contract TwapOracle is OwnableUpgradeable, OracleInterface {
33
- using SafeMath for uint256;
34
- using FixedPoint for *;
35
-
36
- /// @notice vBNB address
37
- address public vBNB;
38
-
39
- /// @notice the base unit of WBNB and BUSD, which are the paired tokens for all assets
40
- uint256 public constant bnbBaseUnit = 1e18;
41
- uint256 public constant busdBaseUnit = 1e18;
42
-
43
- uint256 public constant expScale = 1e18;
44
-
45
- /// @notice Configs by token
46
- mapping(address => TokenConfig) public tokenConfigs;
47
-
48
- /// @notice The current price observation of TWAP. With old and current observations
49
- /// we can calculate the TWAP between this range
50
- mapping(address => Observation) public newObservations;
51
-
52
- /// @notice The old price observation of TWAP
53
- mapping(address => Observation) public oldObservations;
54
-
55
- /// @notice Stored price by token
56
- mapping(address => uint256) public prices;
57
-
58
- /// @notice Emit this event when TWAP window is updated
59
- event TwapWindowUpdated(
60
- address indexed vToken,
61
- uint256 oldTimestamp,
62
- uint256 oldAcc,
63
- uint256 newTimestamp,
64
- uint256 newAcc);
65
-
66
- /// @notice Emit this event when TWAP price is updated
67
- event AnchorPriceUpdated(
68
- address indexed vToken,
69
- uint256 price,
70
- uint256 oldTimestamp,
71
- uint256 newTimestamp
72
- );
73
-
74
- /// @notice Emit this event when new token configs are added
75
- event TokenConfigAdded(
76
- address indexed vToken,
77
- address indexed pancakePool,
78
- uint256 indexed anchorPeriod
79
- );
80
-
81
- modifier notNullAddress(address someone) {
82
- require(someone != address(0), "can't be zero address");
83
- _;
84
- }
85
-
86
- function initialize(address vBNB_) public initializer {
87
- __Ownable_init();
88
- require(vBNB_ != address(0), "vBNB can't be zero address");
89
- vBNB = vBNB_;
90
- }
91
-
92
- /**
93
- * @notice Add multiple token configs at the same time
94
- * @param configs config array
95
- */
96
- function setTokenConfigs(TokenConfig[] memory configs) external onlyOwner() {
97
- require(configs.length > 0, "length can't be 0");
98
- for (uint8 i = 0; i < configs.length; i++) {
99
- setTokenConfig(configs[i]);
100
- }
101
- }
102
-
103
- /**
104
- * @notice Add single token configs
105
- * @param config token config struct
106
- */
107
- function setTokenConfig(TokenConfig memory config) public
108
- onlyOwner()
109
- notNullAddress(config.vToken)
110
- notNullAddress(config.pancakePool)
111
- {
112
- require(config.anchorPeriod > 0, "anchor period must be positive");
113
- require(config.baseUnit > 0, "base unit must be positive");
114
- uint256 cumulativePrice = currentCumulativePrice(config);
115
-
116
- // Initialize observation data
117
- oldObservations[config.vToken].timestamp = block.timestamp;
118
- newObservations[config.vToken].timestamp = block.timestamp;
119
- oldObservations[config.vToken].acc = cumulativePrice;
120
- newObservations[config.vToken].acc = cumulativePrice;
121
- tokenConfigs[config.vToken] = config;
122
- emit TokenConfigAdded(
123
- config.vToken,
124
- config.pancakePool,
125
- config.anchorPeriod
126
- );
127
- }
128
-
129
- /**
130
- * @notice Get the underlying TWAP price of input vToken
131
- * @param vToken vToken address
132
- * @return price in USD, with 18 decimals
133
- */
134
- function getUnderlyingPrice(address vToken) external override view returns (uint256) {
135
- require(tokenConfigs[vToken].vToken != address(0), "vToken not exist");
136
- uint256 price = prices[vToken];
137
-
138
- // if price is 0, it means the price hasn't been updated yet and it's meaningless, revert
139
- require(price > 0, "TWAP price must be positive");
140
- return price;
141
- }
142
-
143
- /**
144
- * @notice Fetches the current token/WBNB and token/BUSD price accumulator from pancakeswap.
145
- * @return cumulative price of target token regardless of pair order
146
- */
147
- function currentCumulativePrice(TokenConfig memory config) public view returns (uint256) {
148
- (uint256 price0, uint256 price1,) = PancakeOracleLibrary.currentCumulativePrices(config.pancakePool);
149
- if (config.isReversedPool) {
150
- return price1;
151
- } else {
152
- return price0;
153
- }
154
- }
155
-
156
- function updateTwap(address vToken) public returns (uint256) {
157
- require(tokenConfigs[vToken].vToken != address(0), "vTokne not exist");
158
- // Update & fetch WBNB price first, so we can calculate the price of WBNB paired token
159
- if (vToken != vBNB && tokenConfigs[vToken].isBnbBased) {
160
- updateTwap(vBNB);
161
- }
162
- return _updateTwapInternal(tokenConfigs[vToken]);
163
- }
164
-
165
- /**
166
- * @notice Fetches the current token/BUSD price from PancakeSwap, with 18 decimals of precision.
167
- * @return price in USD, with 18 decimals
168
- */
169
- function _updateTwapInternal(TokenConfig memory config) internal virtual returns (uint256) {
170
- // pokeWindowValues already handled reversed pool cases,
171
- // priceAverage will always be Token/BNB or Token/BUSD TWAP price.
172
- (uint256 nowCumulativePrice, uint256 oldCumulativePrice, uint256 oldTimestamp) = pokeWindowValues(config);
173
-
174
- // This should be impossible, but better safe than sorry
175
- require(block.timestamp > oldTimestamp, "now must come after before");
176
- uint256 timeElapsed = block.timestamp.sub(oldTimestamp);
177
-
178
- // Calculate Pancakge TWAP
179
- FixedPoint.uq112x112 memory priceAverage = FixedPoint.uq112x112(uint224(
180
- nowCumulativePrice.sub(oldCumulativePrice).div(timeElapsed)
181
- ));
182
- // TWAP price with 1e18 decimal mantissa
183
- uint256 priceAverageMantissa = priceAverage.decode112with18();
184
-
185
- // To cancel the decimals in cumulative price, we need to mulitply the average price with
186
- // tokenBaseUnit / (wbnbBaseUnit or busdBaseUnit, which is 1e18)
187
- uint256 pairedTokenBaseUnit = config.isBnbBased ? bnbBaseUnit : busdBaseUnit;
188
- uint256 anchorPriceMantissa = priceAverageMantissa.mul(config.baseUnit).div(pairedTokenBaseUnit);
189
-
190
- // if this token is paired with BNB, convert its price to USD
191
- if (config.isBnbBased) {
192
- uint256 bnbPrice = prices[vBNB];
193
- require(bnbPrice != 0, "bnb price is invalid");
194
- anchorPriceMantissa = anchorPriceMantissa.mul(bnbPrice).div(bnbBaseUnit);
195
- }
196
-
197
- require(anchorPriceMantissa != 0, "twap price cannot be 0");
198
-
199
- emit AnchorPriceUpdated(config.vToken, anchorPriceMantissa, oldTimestamp, block.timestamp);
200
-
201
- // save anchor price, which is 1e18 decimals
202
- prices[config.vToken] = anchorPriceMantissa;
203
-
204
- return anchorPriceMantissa;
205
- }
206
-
207
- /**
208
- * @notice Update new and old observations of lagging window if period elapsed.
209
- * @return cumulative price & old observation
210
- */
211
- function pokeWindowValues(TokenConfig memory config) internal returns (uint256, uint256, uint256) {
212
- uint256 cumulativePrice = currentCumulativePrice(config);
213
-
214
- Observation memory newObservation = newObservations[config.vToken];
215
-
216
- // Update new and old observations if elapsed time is greater than or equal to anchor period
217
- uint256 timeElapsed = block.timestamp.sub(newObservation.timestamp);
218
- if (timeElapsed >= config.anchorPeriod) {
219
- oldObservations[config.vToken].timestamp = newObservation.timestamp;
220
- oldObservations[config.vToken].acc = newObservation.acc;
221
-
222
- newObservations[config.vToken].timestamp = block.timestamp;
223
- newObservations[config.vToken].acc = cumulativePrice;
224
- emit TwapWindowUpdated(
225
- config.vToken,
226
- newObservation.timestamp,
227
- block.timestamp,
228
- newObservation.acc,
229
- cumulativePrice
230
- );
231
- }
232
- return (cumulativePrice, oldObservations[config.vToken].acc, oldObservations[config.vToken].timestamp);
233
- }
234
- }
@@ -1,26 +0,0 @@
1
- // SPDX-License-Identifier: BSD-3-Clause
2
- pragma solidity ^0.8.10;
3
-
4
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
5
- import "../ChainlinkOracle.sol";
6
- import "../../interfaces/VBep20Interface.sol";
7
-
8
- contract MockChainlinkOracle is OwnableUpgradeable {
9
- mapping(address => uint256) public assetPrices;
10
-
11
- //set price in 6 decimal precision
12
- constructor() {}
13
-
14
- function initialize() public initializer {
15
- __Ownable_init();
16
- }
17
-
18
- function setPrice(address asset, uint256 price) external {
19
- assetPrices[asset] = price;
20
- }
21
-
22
- //https://compound.finance/docs/prices
23
- function getUnderlyingPrice(address vToken) public view returns (uint256) {
24
- return assetPrices[vToken];
25
- }
26
- }
@@ -1,31 +0,0 @@
1
- // SPDX-License-Identifier: BSD-3-Clause
2
- pragma solidity ^0.8.10;
3
-
4
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
5
- import "../PivotTwapOracle.sol";
6
- import "../../interfaces/VBep20Interface.sol";
7
-
8
- contract MockPivotTwapOracle is OwnableUpgradeable {
9
- mapping(address => uint256) public assetPrices;
10
-
11
- /// @notice vBNB address
12
- address public vBNB;
13
-
14
- //set price in 6 decimal precision
15
- constructor() {}
16
-
17
- function initialize(address vBNB_) public initializer {
18
- __Ownable_init();
19
- require(vBNB_ != address(0), "vBNB can't be zero address");
20
- vBNB = vBNB_;
21
- }
22
-
23
- function setPrice(address asset, uint256 price) external {
24
- assetPrices[asset] = price;
25
- }
26
-
27
- //https://compound.finance/docs/prices
28
- function getUnderlyingPrice(address vToken) public view returns (uint256) {
29
- return assetPrices[vToken];
30
- }
31
- }
@@ -1,31 +0,0 @@
1
- // SPDX-License-Identifier: BSD-3-Clause
2
- pragma solidity ^0.8.10;
3
-
4
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
5
- import "../PythOracle.sol";
6
- import "../../interfaces/VBep20Interface.sol";
7
-
8
- contract MockPythOracle is OwnableUpgradeable {
9
- mapping(address => uint256) public assetPrices;
10
-
11
- /// @notice the actual pyth oracle address fetch & store the prices
12
- IPyth public underlyingPythOracle;
13
-
14
- //set price in 6 decimal precision
15
- constructor() {}
16
-
17
- function initialize(address underlyingPythOracle_) public initializer {
18
- __Ownable_init();
19
- require(underlyingPythOracle_ != address(0), "pyth oracle cannot be zero address");
20
- underlyingPythOracle = IPyth(underlyingPythOracle_);
21
- }
22
-
23
- function setPrice(address asset, uint256 price) external {
24
- assetPrices[asset] = price;
25
- }
26
-
27
- //https://compound.finance/docs/prices
28
- function getUnderlyingPrice(address vToken) public view returns (uint256) {
29
- return assetPrices[vToken];
30
- }
31
- }
@@ -1,21 +0,0 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
- pragma experimental ABIEncoderV2;
4
-
5
- import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
6
-
7
- contract BEP20Harness is ERC20 {
8
- uint8 public decimalsInternal = 18;
9
-
10
- constructor(
11
- string memory name_,
12
- string memory symbol_,
13
- uint8 decimals_
14
- ) ERC20(name_, symbol_) {
15
- decimalsInternal = decimals_;
16
- }
17
-
18
- function decimals() public view virtual override returns (uint8) {
19
- return decimalsInternal;
20
- }
21
- }
@@ -1,10 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.0;
3
-
4
- import "../oracles/BoundValidator.sol";
5
-
6
- contract MockBoundValidator is BoundValidator {
7
- function initialize() public initializer {
8
- __Ownable_init();
9
- }
10
- }
@@ -1,78 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.0;
3
-
4
- import "../interfaces/PythInterface.sol";
5
-
6
- contract MockPyth is IPyth {
7
- mapping(bytes32 => PythStructs.PriceFeed) public priceFeeds;
8
-
9
- function queryPriceFeed(bytes32 id) public view returns (PythStructs.PriceFeed memory priceFeed) {
10
- require(priceFeeds[id].id != 0, "no price feed found for the given price id");
11
- return priceFeeds[id];
12
- }
13
-
14
- // simply update price feeds
15
- function updatePriceFeedsHarness(PythStructs.PriceFeed[] calldata feeds) external {
16
- require(feeds.length > 0, "feeds length must > 0");
17
- for (uint i = 0; i < feeds.length; ++i) {
18
- priceFeeds[feeds[i].id] = feeds[i];
19
- }
20
- }
21
-
22
- // a very simple version get price with timestamp expiration check, just for test
23
- function getLatestAvailablePriceWithinDuration(bytes32 id, uint64 duration) external view override
24
- returns (PythStructs.Price memory price)
25
- {
26
- PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
27
-
28
- price.price = priceFeed.price;
29
- price.conf = priceFeed.conf;
30
- price.expo = priceFeed.expo;
31
- uint64 publishTime = priceFeed.publishTime;
32
-
33
- require(_diff(block.timestamp, publishTime) <= duration, "No available price within given duration");
34
-
35
- return price;
36
- }
37
-
38
- function _diff(uint x, uint y) internal pure returns (uint) {
39
- if (x > y) {
40
- return x - y;
41
- } else {
42
- return y - x;
43
- }
44
- }
45
-
46
- // not implemented
47
- function updatePriceFeedsIfNecessary(
48
- bytes[] memory updateData,
49
- bytes32[] memory priceIds,
50
- uint64[] memory publishTimes) external payable
51
- {
52
- updateData;
53
- priceIds;
54
- publishTimes;
55
- }
56
- function getUpdateFee(uint updateDataSize) external pure override returns (uint feeAmount) {
57
- updateDataSize;
58
- return 0;
59
- }
60
- function updatePriceFeeds(bytes[] memory updateData) external payable {
61
- updateData;
62
- }
63
- function getCurrentPrice(bytes32 id) external pure override returns (PythStructs.Price memory price) {
64
- id;
65
- return PythStructs.Price({ price: 0, conf: 0, expo: 0});
66
- }
67
- function getEmaPrice(bytes32 id) external pure override returns (PythStructs.Price memory price) {
68
- id;
69
- return PythStructs.Price({ price: 0, conf: 0, expo: 0});
70
- }
71
- function getLatestAvailablePriceUnsafe(bytes32 id) external pure override
72
- returns (PythStructs.Price memory price, uint64 publishTime)
73
- {
74
- id;
75
- return (PythStructs.Price({ price: 0, conf: 0, expo: 0}), 0);
76
- }
77
-
78
- }
@@ -1,37 +0,0 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
-
4
- import "../interfaces/OracleInterface.sol";
5
-
6
- contract MockSimpleOracle is OracleInterface {
7
- mapping(address => uint256) public prices;
8
-
9
- constructor() {
10
- //
11
- }
12
-
13
- function setPrice(address vToken, uint256 price) public {
14
- prices[vToken] = price;
15
- }
16
-
17
- function getUnderlyingPrice(address vToken) external view returns (uint256) {
18
- return prices[vToken];
19
- }
20
- }
21
-
22
- contract MockPivotOracle is MockSimpleOracle, PivotOracleInterface {
23
- mapping(address => bool) public validateResults;
24
-
25
- constructor() {
26
- //
27
- }
28
-
29
- function setValidateResult(address vToken, bool pass) public {
30
- validateResults[vToken] = pass;
31
- }
32
-
33
- function validatePrice(address vToken, uint256 price) external view returns (bool) {
34
- return validateResults[vToken];
35
- }
36
- }
37
-
@@ -1,91 +0,0 @@
1
- // SPDX-License-Identifier: UNLICENSED
2
- pragma solidity ^0.8.0;
3
-
4
- import "../interfaces/AggregatorV2V3Interface.sol";
5
-
6
- /**
7
- * @title MockV3Aggregator
8
- * @notice Based on the FluxAggregator contract
9
- * @notice Use this contract when you need to test
10
- * other contract's ability to read data from an
11
- * aggregator contract, but how the aggregator got
12
- * its answer is unimportant
13
- */
14
- contract MockV3Aggregator is AggregatorV2V3Interface {
15
- uint256 public constant version = 0;
16
-
17
- uint8 public decimals;
18
- int256 public latestAnswer;
19
- uint256 public latestTimestamp;
20
- uint256 public latestRound;
21
-
22
- mapping(uint256 => int256) public getAnswer;
23
- mapping(uint256 => uint256) public getTimestamp;
24
- mapping(uint256 => uint256) private getStartedAt;
25
-
26
- constructor(uint8 _decimals, int256 _initialAnswer) {
27
- decimals = _decimals;
28
- updateAnswer(_initialAnswer);
29
- }
30
-
31
- function updateAnswer(int256 _answer) public {
32
- latestAnswer = _answer;
33
- latestTimestamp = block.timestamp;
34
- latestRound++;
35
- getAnswer[latestRound] = _answer;
36
- getTimestamp[latestRound] = block.timestamp;
37
- getStartedAt[latestRound] = block.timestamp;
38
- }
39
-
40
- function updateRoundData(
41
- uint80 _roundId,
42
- int256 _answer,
43
- uint256 _timestamp,
44
- uint256 _startedAt
45
- ) public {
46
- latestRound = _roundId;
47
- latestAnswer = _answer;
48
- latestTimestamp = _timestamp;
49
- getAnswer[latestRound] = _answer;
50
- getTimestamp[latestRound] = _timestamp;
51
- getStartedAt[latestRound] = _startedAt;
52
- }
53
-
54
- function getRoundData(uint80 _roundId)
55
- external
56
- view
57
- returns (
58
- uint80 roundId,
59
- int256 answer,
60
- uint256 startedAt,
61
- uint256 updatedAt,
62
- uint80 answeredInRound
63
- )
64
- {
65
- return (_roundId, getAnswer[_roundId], getStartedAt[_roundId], getTimestamp[_roundId], _roundId);
66
- }
67
-
68
- function latestRoundData()
69
- external
70
- view
71
- returns (
72
- uint80 roundId,
73
- int256 answer,
74
- uint256 startedAt,
75
- uint256 updatedAt,
76
- uint80 answeredInRound
77
- )
78
- {
79
- return (
80
- uint80(latestRound),
81
- getAnswer[latestRound],
82
- getStartedAt[latestRound],
83
- getTimestamp[latestRound],
84
- uint80(latestRound)
85
- );
86
- }
87
-
88
- function description() external pure returns (string memory) {
89
- return "v0.6/tests/MockV3Aggregator.sol";
90
- }
91
- }