AbstractPyth.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /// @notice This function is deprecated and is only kept for backward compatibility.
  19. function getValidTimePeriod()
  20. public
  21. view
  22. virtual
  23. returns (uint validTimePeriod);
  24. /// @notice This function is deprecated and is only kept for backward compatibility.
  25. function getPrice(
  26. bytes32 id
  27. ) external view virtual returns (PythStructs.Price memory price) {
  28. return getPriceNoOlderThan(id, getValidTimePeriod());
  29. }
  30. /// @notice This function is deprecated and is only kept for backward compatibility.
  31. function getEmaPrice(
  32. bytes32 id
  33. ) external view virtual returns (PythStructs.Price memory price) {
  34. return getEmaPriceNoOlderThan(id, getValidTimePeriod());
  35. }
  36. function getPriceUnsafe(
  37. bytes32 id
  38. ) public view virtual override returns (PythStructs.Price memory price) {
  39. PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
  40. return priceFeed.price;
  41. }
  42. function getPriceNoOlderThan(
  43. bytes32 id,
  44. uint age
  45. ) public view virtual override returns (PythStructs.Price memory price) {
  46. price = getPriceUnsafe(id);
  47. if (diff(block.timestamp, price.publishTime) > age)
  48. revert PythErrors.StalePrice();
  49. return price;
  50. }
  51. function getEmaPriceUnsafe(
  52. bytes32 id
  53. ) public view virtual override returns (PythStructs.Price memory price) {
  54. PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id);
  55. return priceFeed.emaPrice;
  56. }
  57. function getEmaPriceNoOlderThan(
  58. bytes32 id,
  59. uint age
  60. ) public view virtual override returns (PythStructs.Price memory price) {
  61. price = getEmaPriceUnsafe(id);
  62. if (diff(block.timestamp, price.publishTime) > age)
  63. revert PythErrors.StalePrice();
  64. return price;
  65. }
  66. function diff(uint x, uint y) internal pure returns (uint) {
  67. if (x > y) {
  68. return x - y;
  69. } else {
  70. return y - x;
  71. }
  72. }
  73. // Access modifier is overridden to public to be able to call it locally.
  74. function updatePriceFeeds(
  75. bytes[] calldata updateData
  76. ) public payable virtual override;
  77. function updatePriceFeedsIfNecessary(
  78. bytes[] calldata updateData,
  79. bytes32[] calldata priceIds,
  80. uint64[] calldata publishTimes
  81. ) external payable virtual override {
  82. if (priceIds.length != publishTimes.length)
  83. revert PythErrors.InvalidArgument();
  84. for (uint i = 0; i < priceIds.length; i++) {
  85. if (
  86. !priceFeedExists(priceIds[i]) ||
  87. queryPriceFeed(priceIds[i]).price.publishTime < publishTimes[i]
  88. ) {
  89. updatePriceFeeds(updateData);
  90. return;
  91. }
  92. }
  93. revert PythErrors.NoFreshUpdate();
  94. }
  95. function parsePriceFeedUpdates(
  96. bytes[] calldata updateData,
  97. bytes32[] calldata priceIds,
  98. uint64 minPublishTime,
  99. uint64 maxPublishTime
  100. )
  101. external
  102. payable
  103. virtual
  104. override
  105. returns (PythStructs.PriceFeed[] memory priceFeeds);
  106. function parsePriceFeedUpdatesUnique(
  107. bytes[] calldata updateData,
  108. bytes32[] calldata priceIds,
  109. uint64 minPublishTime,
  110. uint64 maxPublishTime
  111. )
  112. external
  113. payable
  114. virtual
  115. override
  116. returns (PythStructs.PriceFeed[] memory priceFeeds);
  117. }