IPyth.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 price of a price feed without any sanity checks.
  10. /// @dev This function returns the most recent price update in this contract without any recency checks.
  11. /// This function is unsafe as the returned price update may be arbitrarily far in the past.
  12. ///
  13. /// Users of this function should check the `publishTime` in the price to ensure that the returned price is
  14. /// sufficiently recent for their application. If you are considering using this function, it may be
  15. /// safer / easier to use `getPriceNoOlderThan`.
  16. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  17. function getPriceUnsafe(
  18. bytes32 id
  19. ) external view returns (PythStructs.Price memory price);
  20. /// @notice Returns the price that is no older than `age` seconds of the current time.
  21. /// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in
  22. /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
  23. /// recently.
  24. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  25. function getPriceNoOlderThan(
  26. bytes32 id,
  27. uint age
  28. ) external view returns (PythStructs.Price memory price);
  29. /// @notice Returns the exponentially-weighted moving average price of a price feed without any sanity checks.
  30. /// @dev This function returns the same price as `getEmaPrice` in the case where the price is available.
  31. /// However, if the price is not recent this function returns the latest available price.
  32. ///
  33. /// The returned price can be from arbitrarily far in the past; this function makes no guarantees that
  34. /// the returned price is recent or useful for any particular application.
  35. ///
  36. /// Users of this function should check the `publishTime` in the price to ensure that the returned price is
  37. /// sufficiently recent for their application. If you are considering using this function, it may be
  38. /// safer / easier to use either `getEmaPrice` or `getEmaPriceNoOlderThan`.
  39. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  40. function getEmaPriceUnsafe(
  41. bytes32 id
  42. ) external view returns (PythStructs.Price memory price);
  43. /// @notice Returns the exponentially-weighted moving average price that is no older than `age` seconds
  44. /// of the current time.
  45. /// @dev This function is a sanity-checked version of `getEmaPriceUnsafe` which is useful in
  46. /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
  47. /// recently.
  48. /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
  49. function getEmaPriceNoOlderThan(
  50. bytes32 id,
  51. uint age
  52. ) external view returns (PythStructs.Price memory price);
  53. /// @notice Update price feeds with given update messages.
  54. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  55. /// `getUpdateFee` with the length of the `updateData` array.
  56. /// Prices will be updated if they are more recent than the current stored prices.
  57. /// The call will succeed even if the update is not the most recent.
  58. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
  59. /// @param updateData Array of price update data.
  60. function updatePriceFeeds(bytes[] calldata updateData) external payable;
  61. /// @notice Wrapper around updatePriceFeeds that rejects fast if a price update is not necessary. A price update is
  62. /// necessary if the current on-chain publishTime is older than the given publishTime. It relies solely on the
  63. /// given `publishTimes` for the price feeds and does not read the actual price update publish time within `updateData`.
  64. ///
  65. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  66. /// `getUpdateFee` with the length of the `updateData` array.
  67. ///
  68. /// `priceIds` and `publishTimes` are two arrays with the same size that correspond to senders known publishTime
  69. /// of each priceId when calling this method. If all of price feeds within `priceIds` have updated and have
  70. /// a newer or equal publish time than the given publish time, it will reject the transaction to save gas.
  71. /// Otherwise, it calls updatePriceFeeds method to update the prices.
  72. ///
  73. /// @dev Reverts if update is not needed or the transferred fee is not sufficient or the updateData is invalid.
  74. /// @param updateData Array of price update data.
  75. /// @param priceIds Array of price ids.
  76. /// @param publishTimes Array of publishTimes. `publishTimes[i]` corresponds to known `publishTime` of `priceIds[i]`
  77. function updatePriceFeedsIfNecessary(
  78. bytes[] calldata updateData,
  79. bytes32[] calldata priceIds,
  80. uint64[] calldata publishTimes
  81. ) external payable;
  82. /// @notice Returns the required fee to update an array of price updates.
  83. /// @param updateData Array of price update data.
  84. /// @return feeAmount The required fee in Wei.
  85. function getUpdateFee(
  86. bytes[] calldata updateData
  87. ) external view returns (uint feeAmount);
  88. /// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
  89. /// within `minPublishTime` and `maxPublishTime`.
  90. ///
  91. /// You can use this method if you want to use a Pyth price at a fixed time and not the most recent price;
  92. /// otherwise, please consider using `updatePriceFeeds`. This method may store the price updates on-chain, if they
  93. /// are more recent than the current stored prices.
  94. ///
  95. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  96. /// `getUpdateFee` with the length of the `updateData` array.
  97. ///
  98. ///
  99. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  100. /// no update for any of the given `priceIds` within the given time range.
  101. /// @param updateData Array of price update data.
  102. /// @param priceIds Array of price ids.
  103. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  104. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  105. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  106. function parsePriceFeedUpdates(
  107. bytes[] calldata updateData,
  108. bytes32[] calldata priceIds,
  109. uint64 minPublishTime,
  110. uint64 maxPublishTime
  111. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  112. /// @notice Parse time-weighted average price (TWAP) from two consecutive price updates for the given `priceIds`.
  113. ///
  114. /// This method calculates TWAP between two data points by processing the difference in cumulative price values
  115. /// divided by the time period. It requires exactly two updates that contain valid price information
  116. /// for all the requested price IDs.
  117. ///
  118. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  119. /// `getUpdateFee` with the updateData array.
  120. ///
  121. /// @dev Reverts if:
  122. /// - The transferred fee is not sufficient
  123. /// - The updateData is invalid or malformed
  124. /// - The updateData array does not contain exactly 2 updates
  125. /// - There is no update for any of the given `priceIds`
  126. /// - The time ordering between data points is invalid (start time must be before end time)
  127. /// @param updateData Array containing exactly two price updates (start and end points for TWAP calculation)
  128. /// @param priceIds Array of price ids to calculate TWAP for
  129. /// @return twapPriceFeeds Array of TWAP price feeds corresponding to the given `priceIds` (with the same order)
  130. function parseTwapPriceFeedUpdates(
  131. bytes[] calldata updateData,
  132. bytes32[] calldata priceIds
  133. )
  134. external
  135. payable
  136. returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds);
  137. /// @notice Similar to `parsePriceFeedUpdates` but ensures the updates returned are
  138. /// the first updates published in minPublishTime. That is, if there are multiple updates for a given timestamp,
  139. /// this method will return the first update. This method may store the price updates on-chain, if they
  140. /// are more recent than the current stored prices.
  141. ///
  142. ///
  143. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  144. /// no update for any of the given `priceIds` within the given time range and uniqueness condition.
  145. /// @param updateData Array of price update data.
  146. /// @param priceIds Array of price ids.
  147. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  148. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  149. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  150. function parsePriceFeedUpdatesUnique(
  151. bytes[] calldata updateData,
  152. bytes32[] calldata priceIds,
  153. uint64 minPublishTime,
  154. uint64 maxPublishTime
  155. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  156. /// @dev Same as `parsePriceFeedUpdates`, but also returns the Pythnet slot
  157. /// associated with each price update.
  158. /// @param updateData Array of price update data.
  159. /// @param priceIds Array of price ids.
  160. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  161. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  162. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  163. /// @return slots Array of the Pythnet slot corresponding to the given `priceIds` (with the same order).
  164. function parsePriceFeedUpdatesWithSlots(
  165. bytes[] calldata updateData,
  166. bytes32[] calldata priceIds,
  167. uint64 minPublishTime,
  168. uint64 maxPublishTime
  169. )
  170. external
  171. payable
  172. returns (
  173. PythStructs.PriceFeed[] memory priceFeeds,
  174. uint64[] memory slots
  175. );
  176. }