AbstractPulse.sol 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: Apache-2.0
  2. pragma solidity ^0.8.0;
  3. import "./SchedulerStructs.sol";
  4. import "./IScheduler.sol";
  5. import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
  6. import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
  7. import "@pythnetwork/pyth-sdk-solidity/PythErrors.sol";
  8. abstract contract AbstractPulse is IScheduler {
  9. /**
  10. * @notice Get a price that is no older than the specified age
  11. * @param subscriptionId The ID of the subscription
  12. * @param priceId The price ID to get the price for
  13. * @param maxAge The maximum acceptable age of the price in seconds
  14. * @return price The price
  15. */
  16. function getPriceNoOlderThan(
  17. uint256 subscriptionId,
  18. bytes32 priceId,
  19. uint256 maxAge
  20. ) public view virtual returns (PythStructs.Price memory price) {
  21. bytes32[] memory priceIds = new bytes32[](1);
  22. priceIds[0] = priceId;
  23. PythStructs.Price[] memory prices = getPricesNoOlderThan(subscriptionId, priceIds, maxAge);
  24. return prices[0];
  25. }
  26. /**
  27. * @notice Get prices that are no older than the specified age
  28. * @param subscriptionId The ID of the subscription
  29. * @param priceIds Array of price IDs to get prices for
  30. * @param maxAge The maximum acceptable age of the prices in seconds
  31. * @return prices Array of prices
  32. */
  33. function getPricesNoOlderThan(
  34. uint256 subscriptionId,
  35. bytes32[] memory priceIds,
  36. uint256 maxAge
  37. ) public view virtual returns (PythStructs.Price[] memory prices) {
  38. prices = getPricesUnsafe(subscriptionId, priceIds);
  39. for (uint i = 0; i < prices.length; i++) {
  40. if (block.timestamp - prices[i].publishTime > maxAge) {
  41. revert PythErrors.StalePrice();
  42. }
  43. }
  44. return prices;
  45. }
  46. /**
  47. * @notice Get an EMA price that is no older than the specified age
  48. * @param subscriptionId The ID of the subscription
  49. * @param priceId The price ID to get the EMA price for
  50. * @param maxAge The maximum acceptable age of the price in seconds
  51. * @return price The EMA price
  52. */
  53. function getEmaPriceNoOlderThan(
  54. uint256 subscriptionId,
  55. bytes32 priceId,
  56. uint256 maxAge
  57. ) public view virtual returns (PythStructs.Price memory price) {
  58. bytes32[] memory priceIds = new bytes32[](1);
  59. priceIds[0] = priceId;
  60. PythStructs.Price[] memory prices = getEmaPricesNoOlderThan(subscriptionId, priceIds, maxAge);
  61. return prices[0];
  62. }
  63. /**
  64. * @notice Get EMA prices that are no older than the specified age
  65. * @param subscriptionId The ID of the subscription
  66. * @param priceIds Array of price IDs to get EMA prices for
  67. * @param maxAge The maximum acceptable age of the prices in seconds
  68. * @return prices Array of EMA prices
  69. */
  70. function getEmaPricesNoOlderThan(
  71. uint256 subscriptionId,
  72. bytes32[] memory priceIds,
  73. uint256 maxAge
  74. ) public view virtual returns (PythStructs.Price[] memory prices) {
  75. prices = getEmaPriceUnsafe(subscriptionId, priceIds);
  76. for (uint i = 0; i < prices.length; i++) {
  77. if (block.timestamp - prices[i].publishTime > maxAge) {
  78. revert PythErrors.StalePrice();
  79. }
  80. }
  81. return prices;
  82. }
  83. }