This repository was archived by the owner on May 6, 2025. It is now read-only.
forked from brunoenten/pyth-sdk-solidity
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractPyth.sol
More file actions
136 lines (116 loc) · 4.05 KB
/
AbstractPyth.sol
File metadata and controls
136 lines (116 loc) · 4.05 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "./PythStructs.sol";
import "./IPyth.sol";
import "./PythErrors.sol";
/**
* @notice **Deprecated** – this codebase will be removed on **1 August 2025**.
*
* @dev Switch to the maintained package:
* `npm install @pythnetwork/pyth-sdk-solidity`
*
* Migration guide:
* https://docs.pyth.network/price-feeds/use-real-time-data/evm
*
* @custom:deprecated Repository scheduled for deletion on 1 August 2025.
* Use `@pythnetwork/pyth-sdk-solidity` instead.
*/
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);
}