@venusprotocol/oracle 1.1.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
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
+ }
@@ -0,0 +1,31 @@
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
+ }
@@ -0,0 +1,31 @@
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
+ }
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,78 @@
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
+ }
@@ -0,0 +1,37 @@
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
+
@@ -0,0 +1,91 @@
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
+ }
@@ -0,0 +1,113 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.0;
3
+
4
+ // a library for performing various math operations
5
+
6
+ library Math {
7
+ function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
8
+ z = x < y ? x : y;
9
+ }
10
+
11
+ // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
12
+ function sqrt(uint256 y) internal pure returns (uint256 z) {
13
+ if (y > 3) {
14
+ z = y;
15
+ uint256 x = y / 2 + 1;
16
+ while (x < z) {
17
+ z = x;
18
+ x = (y / x + x) / 2;
19
+ }
20
+ } else if (y != 0) {
21
+ z = 1;
22
+ }
23
+ }
24
+ }
25
+
26
+
27
+ // range: [0, 2**112 - 1]
28
+ // resolution: 1 / 2**112
29
+
30
+ library UQ112x112 {
31
+ //solhint-disable-next-line state-visibility
32
+ uint224 constant Q112 = 2**112;
33
+
34
+ // encode a uint112 as a UQ112x112
35
+ function encode(uint112 y) internal pure returns (uint224 z) {
36
+ z = uint224(y) * Q112; // never overflows
37
+ }
38
+
39
+ // divide a UQ112x112 by a uint112, returning a UQ112x112
40
+ function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
41
+ z = x / uint224(y);
42
+ }
43
+ }
44
+
45
+
46
+ contract PancakePairHarness {
47
+ using UQ112x112 for uint224;
48
+
49
+ address public token0;
50
+ address public token1;
51
+
52
+ uint112 private reserve0; // uses single storage slot, accessible via getReserves
53
+ uint112 private reserve1; // uses single storage slot, accessible via getReserves
54
+ uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves
55
+
56
+ uint256 public price0CumulativeLast;
57
+ uint256 public price1CumulativeLast;
58
+ uint256 public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event
59
+
60
+ function currentBlockTimestamp() external view returns (uint32) {
61
+ return uint32(block.timestamp % 2 ** 32);
62
+ }
63
+
64
+ function getReserves()
65
+ public
66
+ view
67
+ returns (
68
+ uint112 _reserve0,
69
+ uint112 _reserve1,
70
+ uint32 _blockTimestampLast
71
+ )
72
+ {
73
+ _reserve0 = reserve0;
74
+ _reserve1 = reserve1;
75
+ _blockTimestampLast = blockTimestampLast;
76
+ }
77
+
78
+
79
+ // called once by the factory at time of deployment
80
+ function initialize(address _token0, address _token1) external {
81
+ token0 = _token0;
82
+ token1 = _token1;
83
+ }
84
+
85
+ // update reserves and, on the first call per block, price accumulators
86
+ function update(
87
+ uint256 balance0,
88
+ uint256 balance1,
89
+ uint112 _reserve0,
90
+ uint112 _reserve1
91
+ ) external {
92
+ require(
93
+ balance0 <= type(uint112).max && balance1 <= type(uint112).max,
94
+ "PancakeV2: OVERFLOW"
95
+ );
96
+ uint32 blockTimestamp = uint32(block.timestamp % 2**32);
97
+ unchecked {
98
+ uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
99
+ if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
100
+ // * never overflows, and + overflow is desired
101
+ price0CumulativeLast +=
102
+ uint256(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) *
103
+ timeElapsed;
104
+ price1CumulativeLast +=
105
+ uint256(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) *
106
+ timeElapsed;
107
+ }
108
+ }
109
+ reserve0 = uint112(balance0);
110
+ reserve1 = uint112(balance1);
111
+ blockTimestampLast = blockTimestamp;
112
+ }
113
+ }
@@ -0,0 +1,22 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.0;
3
+ pragma experimental ABIEncoderV2;
4
+
5
+ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6
+ import "./BEP20Harness.sol";
7
+
8
+ contract VBEP20Harness is BEP20Harness {
9
+ /**
10
+ * @notice Underlying asset for this VToken
11
+ */
12
+ address public underlying;
13
+
14
+ constructor(
15
+ string memory name_,
16
+ string memory symbol_,
17
+ uint8 decimals,
18
+ address underlying_
19
+ ) BEP20Harness(name_, symbol_, decimals) {
20
+ underlying = underlying_;
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@venusprotocol/oracle",
3
3
  "description": "Venus Protocol Price Oracle",
4
- "version": "1.1.3",
4
+ "version": "1.2.0",
5
5
  "author": "",
6
6
  "files": [
7
7
  "artifacts",
8
8
  "networks",
9
- "deploy"
9
+ "deploy",
10
+ "contracts"
10
11
  ],
11
12
  "scripts": {
12
13
  "clean": "shx rm -rf ./artifacts ./cache ./coverage ./src/types ./coverage.json",
@@ -22,7 +23,7 @@
22
23
  "prettier:check": "prettier --check --config ./.prettierrc.yaml \"**/*.{js,json,md,sol,ts,yaml,yml}\"",
23
24
  "test": "npm run compile && hardhat test",
24
25
  "typechain": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat typechain",
25
- "build": "rm -rf dist && tsc --declaration && hardhat compile && cp -r ./artifacts ./dist && cp -r networks dist/ && cp ./{package.json,yarn.lock} dist/",
26
+ "build": "rm -rf dist && tsc --declaration && hardhat compile && cp -r ./{package.json,yarn.lock,artifacts,networks,contracts} dist/",
26
27
  "publish": "yarn build && cd dist && yarn npm publish"
27
28
  },
28
29
  "dependencies": {