AbstractPyth.sol 3.6 KB

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