IPyth.sol 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: Apache-2.0
  2. pragma solidity ^0.8.0;
  3. import "./PythStructs.sol";
  4. import "./IPythEvents.sol";
  5. /// @title Consume prices from the Pyth Network (https://pyth.network/).
  6. /// @dev Please refer to the guidance at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for how to consume prices safely.
  7. /// @author Pyth Data Association
  8. interface IPyth is IPythEvents {
  9. /// @notice Returns the period (in seconds) that a price feed is considered valid since its publish time
  10. function getValidTimePeriod() external view returns (uint validTimePeriod);
  11. /// @notice Returns the price and confidence interval.
  12. /// @dev Reverts if the price has not been updated within the last `getValidTimePeriod()` seconds.
  13. /// @param id The Pyth Price Feed ID of which to fetch the price and confidence interval.
  14. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  15. function getPrice(
  16. bytes32 id
  17. ) external view returns (PythStructs.Price memory price);
  18. /// @notice Returns the exponentially-weighted moving average price and confidence interval.
  19. /// @dev Reverts if the EMA price is not available.
  20. /// @param id The Pyth Price Feed ID of which to fetch the EMA price and confidence interval.
  21. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  22. function getEmaPrice(
  23. bytes32 id
  24. ) external view returns (PythStructs.Price memory price);
  25. /// @notice Returns the price of a price feed without any sanity checks.
  26. /// @dev This function returns the most recent price update in this contract without any recency checks.
  27. /// This function is unsafe as the returned price update may be arbitrarily far in the past.
  28. ///
  29. /// Users of this function should check the `publishTime` in the price to ensure that the returned price is
  30. /// sufficiently recent for their application. If you are considering using this function, it may be
  31. /// safer / easier to use either `getPrice` or `getPriceNoOlderThan`.
  32. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  33. function getPriceUnsafe(
  34. bytes32 id
  35. ) external view returns (PythStructs.Price memory price);
  36. /// @notice Returns the price that is no older than `age` seconds of the current time.
  37. /// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in
  38. /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
  39. /// recently.
  40. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  41. function getPriceNoOlderThan(
  42. bytes32 id,
  43. uint age
  44. ) external view returns (PythStructs.Price memory price);
  45. /// @notice Returns the exponentially-weighted moving average price of a price feed without any sanity checks.
  46. /// @dev This function returns the same price as `getEmaPrice` in the case where the price is available.
  47. /// However, if the price is not recent this function returns the latest available price.
  48. ///
  49. /// The returned price can be from arbitrarily far in the past; this function makes no guarantees that
  50. /// the returned price is recent or useful for any particular application.
  51. ///
  52. /// Users of this function should check the `publishTime` in the price to ensure that the returned price is
  53. /// sufficiently recent for their application. If you are considering using this function, it may be
  54. /// safer / easier to use either `getEmaPrice` or `getEmaPriceNoOlderThan`.
  55. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  56. function getEmaPriceUnsafe(
  57. bytes32 id
  58. ) external view returns (PythStructs.Price memory price);
  59. /// @notice Returns the exponentially-weighted moving average price that is no older than `age` seconds
  60. /// of the current time.
  61. /// @dev This function is a sanity-checked version of `getEmaPriceUnsafe` which is useful in
  62. /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
  63. /// recently.
  64. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  65. function getEmaPriceNoOlderThan(
  66. bytes32 id,
  67. uint age
  68. ) external view returns (PythStructs.Price memory price);
  69. /// @notice Update price feeds with given update messages.
  70. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  71. /// `getUpdateFee` with the length of the `updateData` array.
  72. /// Prices will be updated if they are more recent than the current stored prices.
  73. /// The call will succeed even if the update is not the most recent.
  74. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
  75. /// @param updateData Array of price update data.
  76. function updatePriceFeeds(bytes[] calldata updateData) external payable;
  77. /// @notice Wrapper around updatePriceFeeds that rejects fast if a price update is not necessary. A price update is
  78. /// necessary if the current on-chain publishTime is older than the given publishTime. It relies solely on the
  79. /// given `publishTimes` for the price feeds and does not read the actual price update publish time within `updateData`.
  80. ///
  81. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  82. /// `getUpdateFee` with the length of the `updateData` array.
  83. ///
  84. /// `priceIds` and `publishTimes` are two arrays with the same size that correspond to senders known publishTime
  85. /// of each priceId when calling this method. If all of price feeds within `priceIds` have updated and have
  86. /// a newer or equal publish time than the given publish time, it will reject the transaction to save gas.
  87. /// Otherwise, it calls updatePriceFeeds method to update the prices.
  88. ///
  89. /// @dev Reverts if update is not needed or the transferred fee is not sufficient or the updateData is invalid.
  90. /// @param updateData Array of price update data.
  91. /// @param priceIds Array of price ids.
  92. /// @param publishTimes Array of publishTimes. `publishTimes[i]` corresponds to known `publishTime` of `priceIds[i]`
  93. function updatePriceFeedsIfNecessary(
  94. bytes[] calldata updateData,
  95. bytes32[] calldata priceIds,
  96. uint64[] calldata publishTimes
  97. ) external payable;
  98. /// @notice Returns the required fee to update an array of price updates.
  99. /// @param updateData Array of price update data.
  100. /// @return feeAmount The required fee in Wei.
  101. function getUpdateFee(
  102. bytes[] calldata updateData
  103. ) external view returns (uint feeAmount);
  104. /// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
  105. /// within `minPublishTime` and `maxPublishTime`.
  106. ///
  107. /// You can use this method if you want to use a Pyth price at a fixed time and not the most recent price;
  108. /// otherwise, please consider using `updatePriceFeeds`. This method may store the price updates on-chain, if they
  109. /// are more recent than the current stored prices.
  110. ///
  111. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  112. /// `getUpdateFee` with the length of the `updateData` array.
  113. ///
  114. ///
  115. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  116. /// no update for any of the given `priceIds` within the given time range.
  117. /// @param updateData Array of price update data.
  118. /// @param priceIds Array of price ids.
  119. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  120. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  121. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  122. function parsePriceFeedUpdates(
  123. bytes[] calldata updateData,
  124. bytes32[] calldata priceIds,
  125. uint64 minPublishTime,
  126. uint64 maxPublishTime
  127. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  128. /// @notice Similar to `parsePriceFeedUpdates` but ensures the updates returned are
  129. /// the first updates published in minPublishTime. That is, if there are multiple updates for a given timestamp,
  130. /// this method will return the first update. This method may store the price updates on-chain, if they
  131. /// are more recent than the current stored prices.
  132. ///
  133. ///
  134. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  135. /// no update for any of the given `priceIds` within the given time range and uniqueness condition.
  136. /// @param updateData Array of price update data.
  137. /// @param priceIds Array of price ids.
  138. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  139. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  140. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  141. function parsePriceFeedUpdatesUnique(
  142. bytes[] calldata updateData,
  143. bytes32[] calldata priceIds,
  144. uint64 minPublishTime,
  145. uint64 maxPublishTime
  146. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  147. }