IPyth.sol 12 KB

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