This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAbstractPyth.sol
124 lines (104 loc) · 3.61 KB
/
AbstractPyth.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "./PythStructs.sol";
import "./IPyth.sol";
import "./PythErrors.sol";
abstract contract AbstractPyth is IPyth {
/// @notice Returns the price feed with given id.
/// @dev Reverts if the price does not exist.
/// @param id The Pyth Price Feed ID of which to fetch the PriceFeed.
function queryPriceFeed(
bytes32 id
) public view virtual returns (PythStructs.PriceFeed memory priceFeed);
/// @notice Returns true if a price feed with the given id exists.
/// @param id The Pyth Price Feed ID of which to check its existence.
function priceFeedExists(
bytes32 id
) public view virtual returns (bool exists);
function getValidTimePeriod()
public
view
virtual
override
returns (uint validTimePeriod);
function getPrice(
bytes32 id
) external view virtual override returns (PythStructs.Price memory price) {
return getPriceNoOlderThan(id, getValidTimePeriod());
}
function getEmaPrice(
bytes32 id
) external view virtual override returns (PythStructs.Price memory price) {
return getEmaPriceNoOlderThan(id, getValidTimePeriod());
}
function getPriceUnsafe(
bytes32 id
) public view virtual override returns (PythStructs.Price memory price) {
PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
return priceFeed.price;
}
function getPriceNoOlderThan(
bytes32 id,
uint age
) public view virtual override returns (PythStructs.Price memory price) {
price = getPriceUnsafe(id);
if (diff(block.timestamp, price.publishTime) > age)
revert PythErrors.StalePrice();
return price;
}
function getEmaPriceUnsafe(
bytes32 id
) public view virtual override returns (PythStructs.Price memory price) {
PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
return priceFeed.emaPrice;
}
function getEmaPriceNoOlderThan(
bytes32 id,
uint age
) public view virtual override returns (PythStructs.Price memory price) {
price = getEmaPriceUnsafe(id);
if (diff(block.timestamp, price.publishTime) > age)
revert PythErrors.StalePrice();
return price;
}
function diff(uint x, uint y) internal pure returns (uint) {
if (x > y) {
return x - y;
} else {
return y - x;
}
}
// Access modifier is overridden to public to be able to call it locally.
function updatePriceFeeds(
bytes[] calldata updateData
) public payable virtual override;
function updatePriceFeedsIfNecessary(
bytes[] calldata updateData,
bytes32[] calldata priceIds,
uint64[] calldata publishTimes
) external payable virtual override {
if (priceIds.length != publishTimes.length)
revert PythErrors.InvalidArgument();
for (uint i = 0; i < priceIds.length; i++) {
if (
!priceFeedExists(priceIds[i]) ||
queryPriceFeed(priceIds[i]).price.publishTime < publishTimes[i]
) {
updatePriceFeeds(updateData);
return;
}
}
revert PythErrors.NoFreshUpdate();
}
function parsePriceFeedUpdates(
bytes[] calldata updateData,
bytes32[] calldata priceIds,
uint64 minPublishTime,
uint64 maxPublishTime
)
external
payable
virtual
override
returns (PythStructs.PriceFeed[] memory priceFeeds);
}