IPyth.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Returns the required fee to update a TWAP price.
  89. /// @return feeAmount The required fee in Wei.
  90. function getTwapUpdateFee() external view returns (uint feeAmount);
  91. /// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
  92. /// within `minPublishTime` and `maxPublishTime`.
  93. ///
  94. /// You can use this method if you want to use a Pyth price at a fixed time and not the most recent price;
  95. /// otherwise, please consider using `updatePriceFeeds`. This method may store the price updates on-chain, if they
  96. /// are more recent than the current stored prices.
  97. ///
  98. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  99. /// `getUpdateFee` with the length of the `updateData` array.
  100. ///
  101. ///
  102. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  103. /// no update for any of the given `priceIds` within the given time range.
  104. /// @param updateData Array of price update data.
  105. /// @param priceIds Array of price ids.
  106. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  107. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  108. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  109. function parsePriceFeedUpdates(
  110. bytes[] calldata updateData,
  111. bytes32[] calldata priceIds,
  112. uint64 minPublishTime,
  113. uint64 maxPublishTime
  114. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  115. /// @notice Parse time-weighted average price (TWAP) from two consecutive price updates for the given `priceIds`.
  116. ///
  117. /// This method calculates TWAP between two data points by processing the difference in cumulative price values
  118. /// divided by the time period. It requires exactly two updates that contain valid price information
  119. /// for all the requested price IDs.
  120. ///
  121. /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
  122. /// `getUpdateFee` with the updateData array.
  123. ///
  124. /// @dev Reverts if:
  125. /// - The transferred fee is not sufficient
  126. /// - The updateData is invalid or malformed
  127. /// - The updateData array does not contain exactly 2 updates
  128. /// - There is no update for any of the given `priceIds`
  129. /// - The time ordering between data points is invalid (start time must be before end time)
  130. /// @param updateData Array containing exactly two price updates (start and end points for TWAP calculation)
  131. /// @param priceIds Array of price ids to calculate TWAP for
  132. /// @return twapPriceFeeds Array of TWAP price feeds corresponding to the given `priceIds` (with the same order)
  133. function parseTwapPriceFeedUpdates(
  134. bytes[] calldata updateData,
  135. bytes32[] calldata priceIds
  136. )
  137. external
  138. payable
  139. returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds);
  140. /// @notice Similar to `parsePriceFeedUpdates` but ensures the updates returned are
  141. /// the first updates published in minPublishTime. That is, if there are multiple updates for a given timestamp,
  142. /// this method will return the first update. This method may store the price updates on-chain, if they
  143. /// are more recent than the current stored prices.
  144. ///
  145. ///
  146. /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
  147. /// no update for any of the given `priceIds` within the given time range and uniqueness condition.
  148. /// @param updateData Array of price update data.
  149. /// @param priceIds Array of price ids.
  150. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  151. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  152. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  153. function parsePriceFeedUpdatesUnique(
  154. bytes[] calldata updateData,
  155. bytes32[] calldata priceIds,
  156. uint64 minPublishTime,
  157. uint64 maxPublishTime
  158. ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
  159. /// @dev Same as `parsePriceFeedUpdates`, but also returns the Pythnet slot
  160. /// associated with each price update.
  161. /// @param updateData Array of price update data.
  162. /// @param priceIds Array of price ids.
  163. /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
  164. /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
  165. /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
  166. /// @return slots Array of the Pythnet slot corresponding to the given `priceIds` (with the same order).
  167. function parsePriceFeedUpdatesWithSlots(
  168. bytes[] calldata updateData,
  169. bytes32[] calldata priceIds,
  170. uint64 minPublishTime,
  171. uint64 maxPublishTime
  172. )
  173. external
  174. payable
  175. returns (
  176. PythStructs.PriceFeed[] memory priceFeeds,
  177. uint64[] memory slots
  178. );
  179. }