@venusprotocol/oracle 1.2.3 → 1.3.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.
- package/README.md +106 -0
- package/artifacts/build-info/442613ba0e31d357bb61ad07d3e9ccb5.json +1 -0
- package/artifacts/build-info/e79aa95060bbee82bc36063535db4019.json +1 -0
- package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.dbg.json +4 -0
- package/artifacts/contracts/interfaces/PythInterface.sol/AbstractPyth.json +545 -0
- package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/IPyth.json +130 -15
- package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.dbg.json +1 -1
- package/artifacts/contracts/interfaces/PythInterface.sol/PythStructs.json +2 -2
- package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/PythOracle.sol/PythOracle.json +2 -2
- package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.dbg.json +1 -1
- package/artifacts/contracts/oracles/mocks/MockBinanceOracle.sol/MockBinanceOracle.json +22 -3
- 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/test/MockPyth.sol/MockPyth.dbg.json +1 -1
- package/artifacts/contracts/test/MockPyth.sol/MockPyth.json +305 -212
- package/contracts/interfaces/PythInterface.sol +147 -75
- package/contracts/oracles/PythOracle.sol +1 -1
- package/contracts/oracles/mocks/MockBinanceOracle.sol +5 -1
- package/contracts/test/MockPyth.sol +90 -55
- package/deploy/oracles.ts +104 -0
- package/deployments/localhost/.chainId +1 -0
- package/deployments/localhost/BinanceOracle.json +393 -0
- package/deployments/localhost/BinanceOracle_Implementation.json +305 -0
- package/deployments/localhost/BinanceOracle_Proxy.json +241 -0
- package/deployments/localhost/BoundValidator.json +457 -0
- package/deployments/localhost/BoundValidator_Implementation.json +428 -0
- package/deployments/localhost/BoundValidator_Proxy.json +237 -0
- package/deployments/localhost/ChainlinkOracle.json +370 -0
- package/deployments/localhost/ChainlinkOracle_Implementation.json +273 -0
- package/deployments/localhost/ChainlinkOracle_Proxy.json +237 -0
- package/deployments/localhost/DefaultProxyAdmin.json +257 -0
- package/deployments/localhost/PivotTwapOracle.json +393 -0
- package/deployments/localhost/PivotTwapOracle_Implementation.json +304 -0
- package/deployments/localhost/PivotTwapOracle_Proxy.json +241 -0
- package/deployments/localhost/PythOracle.json +393 -0
- package/deployments/localhost/PythOracle_Implementation.json +309 -0
- package/deployments/localhost/PythOracle_Proxy.json +241 -0
- package/deployments/localhost/ResilientOracle.json +700 -0
- package/deployments/localhost/ResilientOracle_Implementation.json +746 -0
- package/deployments/localhost/ResilientOracle_Proxy.json +241 -0
- package/deployments/localhost/solcInputs/0e89febeebc7444140de8e67c9067d2c.json +78 -0
- package/deployments/localhost/solcInputs/468ec7a31e2db8855c82fb8141193271.json +54 -0
- package/deployments/localhost/solcInputs/50629170e50dc9fec79dac585e95ac05.json +138 -0
- package/package.json +2 -2
- package/deploy/oracles.d.ts +0 -4
- package/deploy/oracles.d.ts.map +0 -1
- package/deploy/oracles.js +0 -90
- package/deploy/oracles.js.map +0 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
pragma solidity ^0.8.0;
|
|
3
3
|
|
|
4
4
|
contract PythStructs {
|
|
5
|
+
// A price with a degree of uncertainty, represented as a price +- a confidence interval.
|
|
5
6
|
//
|
|
6
7
|
// The confidence interval roughly corresponds to the standard error of a normal distribution.
|
|
7
8
|
// Both the price and confidence are stored in a fixed-point numeric representation,
|
|
@@ -16,75 +17,44 @@ contract PythStructs {
|
|
|
16
17
|
uint64 conf;
|
|
17
18
|
// Price exponent
|
|
18
19
|
int32 expo;
|
|
20
|
+
// Unix timestamp describing when the price was published
|
|
21
|
+
uint256 publishTime;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
// PriceFeed represents a current aggregate price from pyth publisher feeds.
|
|
22
25
|
struct PriceFeed {
|
|
23
26
|
// The price ID.
|
|
24
27
|
bytes32 id;
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
// Confidence interval around the price.
|
|
30
|
-
uint64 conf;
|
|
31
|
-
// Price exponent.
|
|
32
|
-
int32 expo;
|
|
33
|
-
// Status of price.
|
|
34
|
-
PriceStatus status;
|
|
35
|
-
// Maximum number of allowed publishers that can contribute to a price.
|
|
36
|
-
uint32 maxNumPublishers;
|
|
37
|
-
// Number of publishers that made up current aggregate.
|
|
38
|
-
uint32 numPublishers;
|
|
39
|
-
// Exponentially moving average price.
|
|
40
|
-
int64 emaPrice;
|
|
41
|
-
// Exponentially moving average confidence interval.
|
|
42
|
-
uint64 emaConf;
|
|
43
|
-
// Unix timestamp describing when the price was published
|
|
44
|
-
uint64 publishTime;
|
|
45
|
-
// Price of previous price with TRADING status
|
|
46
|
-
int64 prevPrice;
|
|
47
|
-
// Confidence interval of previous price with TRADING status
|
|
48
|
-
uint64 prevConf;
|
|
49
|
-
// Unix timestamp describing when the previous price with TRADING status was published
|
|
50
|
-
uint64 prevPublishTime;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/* PriceStatus represents the availability status of a price feed.
|
|
54
|
-
UNKNOWN: The price feed is not currently updating for an unknown reason.
|
|
55
|
-
TRADING: The price feed is updating as expected.
|
|
56
|
-
HALTED: The price feed is not currently updating because trading in the product has been halted.
|
|
57
|
-
AUCTION: The price feed is not currently updating because an auction is setting the price.
|
|
58
|
-
*/
|
|
59
|
-
enum PriceStatus {
|
|
60
|
-
UNKNOWN,
|
|
61
|
-
TRADING,
|
|
62
|
-
HALTED,
|
|
63
|
-
AUCTION
|
|
28
|
+
// Latest available price
|
|
29
|
+
Price price;
|
|
30
|
+
// Latest available exponentially-weighted moving average price
|
|
31
|
+
Price emaPrice;
|
|
64
32
|
}
|
|
65
33
|
}
|
|
66
34
|
|
|
67
35
|
/// @title Consume prices from the Pyth Network (https://pyth.network/).
|
|
36
|
+
/// @dev Please refer to the guidance at https://docs.pyth.network/consumers/best-practices
|
|
37
|
+
/// for how to consume prices safely.
|
|
68
38
|
/// @author Pyth Data Association
|
|
69
39
|
interface IPyth {
|
|
70
40
|
/// @dev Emitted when an update for price feed with `id` is processed successfully.
|
|
71
41
|
/// @param id The Pyth Price Feed ID.
|
|
72
42
|
/// @param fresh True if the price update is more recent and stored.
|
|
73
43
|
/// @param chainId ID of the source chain that the batch price update containing this price.
|
|
74
|
-
/// This value comes from Wormhole,
|
|
75
|
-
///
|
|
44
|
+
/// This value comes from Wormhole, and you can find the corresponding chains
|
|
45
|
+
/// at https://docs.wormholenetwork.com/wormhole/contracts.
|
|
76
46
|
/// @param sequenceNumber Sequence number of the batch price update containing this price.
|
|
77
47
|
/// @param lastPublishTime Publish time of the previously stored price.
|
|
78
48
|
/// @param publishTime Publish time of the given price update.
|
|
79
|
-
/// @param price
|
|
80
|
-
/// @param conf
|
|
49
|
+
/// @param price Price of the given price update.
|
|
50
|
+
/// @param conf Confidence interval of the given price update.
|
|
81
51
|
event PriceFeedUpdate(
|
|
82
52
|
bytes32 indexed id,
|
|
83
53
|
bool indexed fresh,
|
|
84
54
|
uint16 chainId,
|
|
85
55
|
uint64 sequenceNumber,
|
|
86
|
-
|
|
87
|
-
|
|
56
|
+
uint256 lastPublishTime,
|
|
57
|
+
uint256 publishTime,
|
|
88
58
|
int64 price,
|
|
89
59
|
uint64 conf
|
|
90
60
|
);
|
|
@@ -102,44 +72,58 @@ interface IPyth {
|
|
|
102
72
|
/// @param fee Amount of paid fee for updating the prices.
|
|
103
73
|
event UpdatePriceFeeds(address indexed sender, uint256 batchCount, uint256 fee);
|
|
104
74
|
|
|
105
|
-
/// @notice Returns the
|
|
106
|
-
|
|
107
|
-
|
|
75
|
+
/// @notice Returns the period (in seconds) that a price feed is considered valid since its publish time
|
|
76
|
+
function getValidTimePeriod() external view returns (uint256 validTimePeriod);
|
|
77
|
+
|
|
78
|
+
/// @notice Returns the price and confidence interval.
|
|
79
|
+
/// @dev Reverts if the price has not been updated within the last `getValidTimePeriod()` seconds.
|
|
80
|
+
/// @param id The Pyth Price Feed ID of which to fetch the price and confidence interval.
|
|
108
81
|
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
109
|
-
function
|
|
82
|
+
function getPrice(bytes32 id) external view returns (PythStructs.Price memory price);
|
|
110
83
|
|
|
111
|
-
/// @notice Returns the
|
|
112
|
-
/// @dev Reverts if the
|
|
113
|
-
/// @param id The Pyth Price Feed ID of which to fetch the
|
|
84
|
+
/// @notice Returns the exponentially-weighted moving average price and confidence interval.
|
|
85
|
+
/// @dev Reverts if the EMA price is not available.
|
|
86
|
+
/// @param id The Pyth Price Feed ID of which to fetch the EMA price and confidence interval.
|
|
114
87
|
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
115
88
|
function getEmaPrice(bytes32 id) external view returns (PythStructs.Price memory price);
|
|
116
89
|
|
|
117
|
-
/// @notice Returns the
|
|
118
|
-
/// @dev This function returns the
|
|
119
|
-
///
|
|
120
|
-
///
|
|
90
|
+
/// @notice Returns the price of a price feed without any sanity checks.
|
|
91
|
+
/// @dev This function returns the most recent price update in this contract without any recency checks.
|
|
92
|
+
/// This function is unsafe as the returned price update may be arbitrarily far in the past.
|
|
93
|
+
///
|
|
94
|
+
/// Users of this function should check the `publishTime` in the price to ensure that the returned price is
|
|
95
|
+
/// sufficiently recent for their application. If you are considering using this function, it may be
|
|
96
|
+
/// safer / easier to use either `getPrice` or `getPriceNoOlderThan`.
|
|
97
|
+
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
98
|
+
function getPriceUnsafe(bytes32 id) external view returns (PythStructs.Price memory price);
|
|
99
|
+
|
|
100
|
+
/// @notice Returns the price that is no older than `age` seconds of the current time.
|
|
101
|
+
/// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in
|
|
102
|
+
/// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
|
|
103
|
+
/// recently.
|
|
104
|
+
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
105
|
+
function getPriceNoOlderThan(bytes32 id, uint256 age) external view returns (PythStructs.Price memory price);
|
|
106
|
+
|
|
107
|
+
/// @notice Returns the exponentially-weighted moving average price of a price feed without any sanity checks.
|
|
108
|
+
/// @dev This function returns the same price as `getEmaPrice` in the case where the price is available.
|
|
109
|
+
/// However, if the price is not recent this function returns the latest available price.
|
|
121
110
|
///
|
|
122
111
|
/// The returned price can be from arbitrarily far in the past; this function makes no guarantees that
|
|
123
112
|
/// the returned price is recent or useful for any particular application.
|
|
124
113
|
///
|
|
125
|
-
/// Users of this function should check the
|
|
114
|
+
/// Users of this function should check the `publishTime` in the price to ensure that the returned price is
|
|
126
115
|
/// sufficiently recent for their application. If you are considering using this function, it may be
|
|
127
|
-
/// safer / easier to use either `
|
|
116
|
+
/// safer / easier to use either `getEmaPrice` or `getEmaPriceNoOlderThan`.
|
|
128
117
|
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
129
|
-
|
|
130
|
-
function getLatestAvailablePriceUnsafe(bytes32 id)
|
|
131
|
-
external
|
|
132
|
-
view
|
|
133
|
-
returns (PythStructs.Price memory price, uint64 publishTime);
|
|
118
|
+
function getEmaPriceUnsafe(bytes32 id) external view returns (PythStructs.Price memory price);
|
|
134
119
|
|
|
135
|
-
/// @notice Returns the
|
|
136
|
-
///
|
|
120
|
+
/// @notice Returns the exponentially-weighted moving average price that is no older than `age` seconds
|
|
121
|
+
/// of the current time.
|
|
122
|
+
/// @dev This function is a sanity-checked version of `getEmaPriceUnsafe` which is useful in
|
|
137
123
|
/// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
|
|
138
124
|
/// recently.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
view
|
|
142
|
-
returns (PythStructs.Price memory price);
|
|
125
|
+
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
|
|
126
|
+
function getEmaPriceNoOlderThan(bytes32 id, uint256 age) external view returns (PythStructs.Price memory price);
|
|
143
127
|
|
|
144
128
|
/// @notice Update price feeds with given update messages.
|
|
145
129
|
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
|
|
@@ -148,12 +132,12 @@ interface IPyth {
|
|
|
148
132
|
/// The call will succeed even if the update is not the most recent.
|
|
149
133
|
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
|
|
150
134
|
/// @param updateData Array of price update data.
|
|
151
|
-
function updatePriceFeeds(bytes[]
|
|
135
|
+
function updatePriceFeeds(bytes[] calldata updateData) external payable;
|
|
152
136
|
|
|
153
137
|
/// @notice Wrapper around updatePriceFeeds that rejects fast if a price update is not necessary. A price update is
|
|
154
138
|
/// necessary if the current on-chain publishTime is older than the given publishTime. It relies solely on the
|
|
155
|
-
/// given `publishTimes` for the price feeds and
|
|
156
|
-
///
|
|
139
|
+
/// given `publishTimes` for the price feeds and does not read the actual price
|
|
140
|
+
/// update publish time within `updateData`.
|
|
157
141
|
///
|
|
158
142
|
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
|
|
159
143
|
/// `getUpdateFee` with the length of the `updateData` array.
|
|
@@ -168,9 +152,9 @@ interface IPyth {
|
|
|
168
152
|
/// @param priceIds Array of price ids.
|
|
169
153
|
/// @param publishTimes Array of publishTimes. `publishTimes[i]` corresponds to known `publishTime` of `priceIds[i]`
|
|
170
154
|
function updatePriceFeedsIfNecessary(
|
|
171
|
-
bytes[]
|
|
172
|
-
bytes32[]
|
|
173
|
-
uint64[]
|
|
155
|
+
bytes[] calldata updateData,
|
|
156
|
+
bytes32[] calldata priceIds,
|
|
157
|
+
uint64[] calldata publishTimes
|
|
174
158
|
) external payable;
|
|
175
159
|
|
|
176
160
|
/// @notice Returns the required fee to update an array of price updates.
|
|
@@ -178,3 +162,91 @@ interface IPyth {
|
|
|
178
162
|
/// @return feeAmount The required fee in Wei.
|
|
179
163
|
function getUpdateFee(uint256 updateDataSize) external view returns (uint256 feeAmount);
|
|
180
164
|
}
|
|
165
|
+
|
|
166
|
+
abstract contract AbstractPyth is IPyth {
|
|
167
|
+
/// @notice Returns the price feed with given id.
|
|
168
|
+
/// @dev Reverts if the price does not exist.
|
|
169
|
+
/// @param id The Pyth Price Feed ID of which to fetch the PriceFeed.
|
|
170
|
+
function queryPriceFeed(bytes32 id) public view virtual returns (PythStructs.PriceFeed memory priceFeed);
|
|
171
|
+
|
|
172
|
+
/// @notice Returns true if a price feed with the given id exists.
|
|
173
|
+
/// @param id The Pyth Price Feed ID of which to check its existence.
|
|
174
|
+
function priceFeedExists(bytes32 id) public view virtual returns (bool exists);
|
|
175
|
+
|
|
176
|
+
function getValidTimePeriod() public view virtual override returns (uint256 validTimePeriod);
|
|
177
|
+
|
|
178
|
+
function getPrice(bytes32 id) external view override returns (PythStructs.Price memory price) {
|
|
179
|
+
return getPriceNoOlderThan(id, getValidTimePeriod());
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function getEmaPrice(bytes32 id) external view override returns (PythStructs.Price memory price) {
|
|
183
|
+
return getEmaPriceNoOlderThan(id, getValidTimePeriod());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function getPriceUnsafe(bytes32 id) public view override returns (PythStructs.Price memory price) {
|
|
187
|
+
PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
|
|
188
|
+
return priceFeed.price;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getPriceNoOlderThan(bytes32 id, uint256 age)
|
|
192
|
+
public
|
|
193
|
+
view
|
|
194
|
+
override
|
|
195
|
+
returns (PythStructs.Price memory price)
|
|
196
|
+
{
|
|
197
|
+
price = getPriceUnsafe(id);
|
|
198
|
+
|
|
199
|
+
require(diff(block.timestamp, price.publishTime) <= age, "no price available which is recent enough");
|
|
200
|
+
|
|
201
|
+
return price;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function getEmaPriceUnsafe(bytes32 id) public view override returns (PythStructs.Price memory price) {
|
|
205
|
+
PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
|
|
206
|
+
return priceFeed.emaPrice;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function getEmaPriceNoOlderThan(bytes32 id, uint256 age)
|
|
210
|
+
public
|
|
211
|
+
view
|
|
212
|
+
override
|
|
213
|
+
returns (PythStructs.Price memory price)
|
|
214
|
+
{
|
|
215
|
+
price = getEmaPriceUnsafe(id);
|
|
216
|
+
|
|
217
|
+
require(diff(block.timestamp, price.publishTime) <= age, "no ema price available which is recent enough");
|
|
218
|
+
|
|
219
|
+
return price;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function diff(uint256 x, uint256 y) internal pure returns (uint256) {
|
|
223
|
+
if (x > y) {
|
|
224
|
+
return x - y;
|
|
225
|
+
} else {
|
|
226
|
+
return y - x;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Access modifier is overridden to public to be able to call it locally.
|
|
231
|
+
function updatePriceFeeds(bytes[] calldata updateData) public payable virtual override;
|
|
232
|
+
|
|
233
|
+
function updatePriceFeedsIfNecessary(
|
|
234
|
+
bytes[] calldata updateData,
|
|
235
|
+
bytes32[] calldata priceIds,
|
|
236
|
+
uint64[] calldata publishTimes
|
|
237
|
+
) external payable override {
|
|
238
|
+
require(priceIds.length == publishTimes.length, "priceIds and publishTimes arrays should have same length");
|
|
239
|
+
|
|
240
|
+
bool updateNeeded = false;
|
|
241
|
+
for (uint256 i = 0; i < priceIds.length; i++) {
|
|
242
|
+
if (!priceFeedExists(priceIds[i]) || queryPriceFeed(priceIds[i]).price.publishTime < publishTimes[i]) {
|
|
243
|
+
updateNeeded = true;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
require(updateNeeded, "no prices in the submitted batch have fresh prices, so this update will have no effect");
|
|
249
|
+
|
|
250
|
+
updatePriceFeeds(updateData);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -104,7 +104,7 @@ contract PythOracle is OwnableUpgradeable, OracleInterface {
|
|
|
104
104
|
require(tokenConfig.asset != address(0), "asset doesn't exist");
|
|
105
105
|
|
|
106
106
|
// if the price is expired after it's compared against `maxStalePeriod`, the following call will revert
|
|
107
|
-
PythStructs.Price memory priceInfo = underlyingPythOracle.
|
|
107
|
+
PythStructs.Price memory priceInfo = underlyingPythOracle.getPriceNoOlderThan(
|
|
108
108
|
tokenConfig.pythId,
|
|
109
109
|
tokenConfig.maxStalePeriod
|
|
110
110
|
);
|
|
@@ -3,14 +3,18 @@ pragma solidity ^0.8.10;
|
|
|
3
3
|
|
|
4
4
|
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
|
|
5
5
|
import "../../interfaces/VBep20Interface.sol";
|
|
6
|
+
import "../../interfaces/FeedRegistryInterface.sol";
|
|
6
7
|
|
|
7
8
|
contract MockBinanceOracle is OwnableUpgradeable {
|
|
8
9
|
mapping(address => uint256) public assetPrices;
|
|
9
10
|
|
|
11
|
+
FeedRegistryInterface public feedRegistry;
|
|
12
|
+
|
|
10
13
|
constructor() {}
|
|
11
14
|
|
|
12
|
-
function initialize() public initializer {
|
|
15
|
+
function initialize(FeedRegistryInterface feed) public initializer {
|
|
13
16
|
__Ownable_init();
|
|
17
|
+
feedRegistry = feed;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
function setPrice(address asset, uint256 price) external {
|
|
@@ -3,10 +3,19 @@ pragma solidity ^0.8.0;
|
|
|
3
3
|
|
|
4
4
|
import "../interfaces/PythInterface.sol";
|
|
5
5
|
|
|
6
|
-
contract MockPyth is
|
|
7
|
-
mapping(bytes32 => PythStructs.PriceFeed)
|
|
6
|
+
contract MockPyth is AbstractPyth {
|
|
7
|
+
mapping(bytes32 => PythStructs.PriceFeed) priceFeeds;
|
|
8
|
+
uint64 sequenceNumber;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
uint256 singleUpdateFeeInWei;
|
|
11
|
+
uint256 validTimePeriod;
|
|
12
|
+
|
|
13
|
+
constructor(uint256 _validTimePeriod, uint256 _singleUpdateFeeInWei) {
|
|
14
|
+
singleUpdateFeeInWei = _singleUpdateFeeInWei;
|
|
15
|
+
validTimePeriod = _validTimePeriod;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function queryPriceFeed(bytes32 id) public view override returns (PythStructs.PriceFeed memory priceFeed) {
|
|
10
19
|
require(priceFeeds[id].id != 0, "no price feed found for the given price id");
|
|
11
20
|
return priceFeeds[id];
|
|
12
21
|
}
|
|
@@ -19,70 +28,96 @@ contract MockPyth is IPyth {
|
|
|
19
28
|
}
|
|
20
29
|
}
|
|
21
30
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
view
|
|
26
|
-
override
|
|
27
|
-
returns (PythStructs.Price memory price)
|
|
28
|
-
{
|
|
29
|
-
PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
|
|
31
|
+
function priceFeedExists(bytes32 id) public view override returns (bool) {
|
|
32
|
+
return (priceFeeds[id].id != 0);
|
|
33
|
+
}
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
uint64 publishTime = priceFeed.publishTime;
|
|
35
|
+
function getValidTimePeriod() public view override returns (uint256) {
|
|
36
|
+
return validTimePeriod;
|
|
37
|
+
}
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
// Takes an array of encoded price feeds and stores them.
|
|
40
|
+
// You can create this data either by calling createPriceFeedData or
|
|
41
|
+
// by using web3.js or ethers abi utilities.
|
|
42
|
+
function updatePriceFeeds(bytes[] calldata updateData) public payable override {
|
|
43
|
+
uint256 requiredFee = getUpdateFee(updateData.length);
|
|
44
|
+
require(msg.value >= requiredFee, "Insufficient paid fee amount");
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
if (msg.value > requiredFee) {
|
|
47
|
+
(bool success, ) = payable(msg.sender).call{ value: msg.value - requiredFee }("");
|
|
48
|
+
require(success, "failed to transfer update fee");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
uint256 freshPrices = 0;
|
|
52
|
+
|
|
53
|
+
// Chain ID is id of the source chain that the price update comes from. Since it is just a mock contract
|
|
54
|
+
// We set it to 1.
|
|
55
|
+
uint16 chainId = 1;
|
|
40
56
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
for (uint256 i = 0; i < updateData.length; i++) {
|
|
58
|
+
PythStructs.PriceFeed memory priceFeed = abi.decode(updateData[i], (PythStructs.PriceFeed));
|
|
59
|
+
|
|
60
|
+
bool fresh = false;
|
|
61
|
+
uint256 lastPublishTime = priceFeeds[priceFeed.id].price.publishTime;
|
|
62
|
+
|
|
63
|
+
if (lastPublishTime < priceFeed.price.publishTime) {
|
|
64
|
+
// Price information is more recent than the existing price information.
|
|
65
|
+
fresh = true;
|
|
66
|
+
priceFeeds[priceFeed.id] = priceFeed;
|
|
67
|
+
freshPrices += 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
emit PriceFeedUpdate(
|
|
71
|
+
priceFeed.id,
|
|
72
|
+
fresh,
|
|
73
|
+
chainId,
|
|
74
|
+
sequenceNumber,
|
|
75
|
+
priceFeed.price.publishTime,
|
|
76
|
+
lastPublishTime,
|
|
77
|
+
priceFeed.price.price,
|
|
78
|
+
priceFeed.price.conf
|
|
79
|
+
);
|
|
46
80
|
}
|
|
47
|
-
}
|
|
48
81
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
priceIds;
|
|
57
|
-
publishTimes;
|
|
58
|
-
}
|
|
82
|
+
// In the real contract, the input of this function contains multiple batches that each contain multiple prices.
|
|
83
|
+
// This event is emitted when a batch is processed. In this mock contract we consider
|
|
84
|
+
// there is only one batch of prices.
|
|
85
|
+
// Each batch has (chainId, sequenceNumber) as it's unique identifier. Here chainId
|
|
86
|
+
// is set to 1 and an increasing sequence number is used.
|
|
87
|
+
emit BatchPriceFeedUpdate(chainId, sequenceNumber, updateData.length, freshPrices);
|
|
88
|
+
sequenceNumber += 1;
|
|
59
89
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return 0;
|
|
90
|
+
// There is only 1 batch of prices
|
|
91
|
+
emit UpdatePriceFeeds(msg.sender, 1, requiredFee);
|
|
63
92
|
}
|
|
64
93
|
|
|
65
|
-
function
|
|
66
|
-
|
|
94
|
+
function getUpdateFee(uint256 updateDataSize) public view override returns (uint256 feeAmount) {
|
|
95
|
+
return singleUpdateFeeInWei * updateDataSize;
|
|
67
96
|
}
|
|
68
97
|
|
|
69
|
-
function
|
|
70
|
-
id
|
|
71
|
-
|
|
72
|
-
|
|
98
|
+
function createPriceFeedUpdateData(
|
|
99
|
+
bytes32 id,
|
|
100
|
+
int64 price,
|
|
101
|
+
uint64 conf,
|
|
102
|
+
int32 expo,
|
|
103
|
+
int64 emaPrice,
|
|
104
|
+
uint64 emaConf,
|
|
105
|
+
uint64 publishTime
|
|
106
|
+
) public pure returns (bytes memory priceFeedData) {
|
|
107
|
+
PythStructs.PriceFeed memory priceFeed;
|
|
73
108
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
109
|
+
priceFeed.id = id;
|
|
110
|
+
|
|
111
|
+
priceFeed.price.price = price;
|
|
112
|
+
priceFeed.price.conf = conf;
|
|
113
|
+
priceFeed.price.expo = expo;
|
|
114
|
+
priceFeed.price.publishTime = publishTime;
|
|
115
|
+
|
|
116
|
+
priceFeed.emaPrice.price = emaPrice;
|
|
117
|
+
priceFeed.emaPrice.conf = emaConf;
|
|
118
|
+
priceFeed.emaPrice.expo = expo;
|
|
119
|
+
priceFeed.emaPrice.publishTime = publishTime;
|
|
78
120
|
|
|
79
|
-
|
|
80
|
-
external
|
|
81
|
-
pure
|
|
82
|
-
override
|
|
83
|
-
returns (PythStructs.Price memory price, uint64 publishTime)
|
|
84
|
-
{
|
|
85
|
-
id;
|
|
86
|
-
return (PythStructs.Price({ price: 0, conf: 0, expo: 0 }), 0);
|
|
121
|
+
priceFeedData = abi.encode(priceFeed);
|
|
87
122
|
}
|
|
88
123
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// npx hardhat deploy --network bsctestnet
|
|
2
|
+
import networks from "@venusprotocol/venus-protocol/networks/mainnet.json";
|
|
3
|
+
import hre from "hardhat";
|
|
4
|
+
import { DeployFunction } from "hardhat-deploy/dist/types";
|
|
5
|
+
import { HardhatRuntimeEnvironment } from "hardhat/types";
|
|
6
|
+
|
|
7
|
+
const func: DeployFunction = async function ({ getNamedAccounts, deployments, network }: HardhatRuntimeEnvironment) {
|
|
8
|
+
const { deploy } = deployments;
|
|
9
|
+
const { deployer } = await getNamedAccounts();
|
|
10
|
+
|
|
11
|
+
await deploy("BoundValidator", {
|
|
12
|
+
from: deployer,
|
|
13
|
+
log: true,
|
|
14
|
+
deterministicDeployment: false,
|
|
15
|
+
proxy: {
|
|
16
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
17
|
+
execute: {
|
|
18
|
+
methodName: "initialize",
|
|
19
|
+
args: [],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const boundValidator = await hre.ethers.getContract("BoundValidator");
|
|
25
|
+
|
|
26
|
+
await deploy("ResilientOracle", {
|
|
27
|
+
from: deployer,
|
|
28
|
+
log: true,
|
|
29
|
+
deterministicDeployment: false,
|
|
30
|
+
proxy: {
|
|
31
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
32
|
+
execute: {
|
|
33
|
+
methodName: "initialize",
|
|
34
|
+
args: [boundValidator.address],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await deploy("ChainlinkOracle", {
|
|
40
|
+
contract: network.live ? "ChainlinkOracle" : "MockChainlinkOracle",
|
|
41
|
+
from: deployer,
|
|
42
|
+
log: true,
|
|
43
|
+
deterministicDeployment: false,
|
|
44
|
+
proxy: {
|
|
45
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
46
|
+
execute: {
|
|
47
|
+
methodName: "initialize",
|
|
48
|
+
args: [],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const vBNBAddress = networks.Contracts.vBNB;
|
|
54
|
+
|
|
55
|
+
await deploy("PivotTwapOracle", {
|
|
56
|
+
contract: network.live ? "PivotTwapOracle" : "MockTwapOracle",
|
|
57
|
+
from: deployer,
|
|
58
|
+
log: true,
|
|
59
|
+
deterministicDeployment: false,
|
|
60
|
+
proxy: {
|
|
61
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
62
|
+
execute: {
|
|
63
|
+
methodName: "initialize",
|
|
64
|
+
args: [vBNBAddress],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// @todo: just testnet address, will be replaced to mainnet version in the future
|
|
70
|
+
const actualPythOracleAddress = "0xd7308b14BF4008e7C7196eC35610B1427C5702EA";
|
|
71
|
+
|
|
72
|
+
await deploy("PythOracle", {
|
|
73
|
+
contract: network.live ? "PythOracle" : "MockPythOracle",
|
|
74
|
+
from: deployer,
|
|
75
|
+
log: true,
|
|
76
|
+
deterministicDeployment: false,
|
|
77
|
+
proxy: {
|
|
78
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
79
|
+
execute: {
|
|
80
|
+
methodName: "initialize",
|
|
81
|
+
args: [actualPythOracleAddress],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// @todo: just testnet address, will be replaced to mainnet version in the future
|
|
87
|
+
const actualBinanceFeedRegistryAddress = "0x999DD49FeFdC043fDAC4FE12Bb1e4bb31cB4c47B";
|
|
88
|
+
|
|
89
|
+
await deploy("BinanceOracle", {
|
|
90
|
+
contract: network.live ? "BinanceOracle" : "MockBinanceOracle",
|
|
91
|
+
from: deployer,
|
|
92
|
+
log: true,
|
|
93
|
+
deterministicDeployment: false,
|
|
94
|
+
proxy: {
|
|
95
|
+
proxyContract: "OptimizedTransparentProxy",
|
|
96
|
+
execute: {
|
|
97
|
+
methodName: "initialize",
|
|
98
|
+
args: [actualBinanceFeedRegistryAddress],
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export default func;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
31337
|