PulseSchedulerTestUtils.t.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "forge-std/Test.sol";
  4. import "@openzeppelin/contracts/utils/math/SafeCast.sol";
  5. import "../../contracts/pulse/SchedulerUpgradeable.sol";
  6. import "../../contracts/pulse/SchedulerState.sol";
  7. import "@pythnetwork/pulse-sdk-solidity/SchedulerStructs.sol";
  8. import "./MockPriceFeedTestUtils.sol";
  9. abstract contract PulseSchedulerTestUtils is Test, MockPriceFeedTestUtils {
  10. /// Helper function to add a test subscription with 2 price IDs
  11. function addTestSubscription(
  12. SchedulerUpgradeable scheduler,
  13. address whitelistedReader
  14. ) internal returns (uint256) {
  15. SchedulerStructs.SubscriptionParams
  16. memory params = createDefaultSubscriptionParams(
  17. 2,
  18. whitelistedReader
  19. );
  20. uint256 minimumBalance = scheduler.getMinimumBalance(
  21. uint8(params.priceIds.length)
  22. );
  23. return scheduler.createSubscription{value: minimumBalance}(params);
  24. }
  25. /// Helper function to add a test subscription with variable number of feeds
  26. function addTestSubscriptionWithFeeds(
  27. SchedulerUpgradeable scheduler,
  28. uint8 numFeeds,
  29. address whitelistedReader
  30. ) internal returns (uint256) {
  31. SchedulerStructs.SubscriptionParams
  32. memory params = createDefaultSubscriptionParams(
  33. numFeeds,
  34. whitelistedReader
  35. );
  36. uint256 minimumBalance = scheduler.getMinimumBalance(
  37. uint8(params.priceIds.length)
  38. );
  39. return scheduler.createSubscription{value: minimumBalance}(params);
  40. }
  41. /// Helper function to add a test subscription with specific update criteria
  42. function addTestSubscriptionWithUpdateCriteria(
  43. SchedulerUpgradeable scheduler,
  44. SchedulerStructs.UpdateCriteria memory updateCriteria,
  45. address whitelistedReader
  46. ) internal returns (uint256) {
  47. bytes32[] memory priceIds = createPriceIds();
  48. address[] memory readerWhitelist = new address[](1);
  49. readerWhitelist[0] = whitelistedReader;
  50. SchedulerStructs.SubscriptionParams memory params = SchedulerStructs
  51. .SubscriptionParams({
  52. priceIds: priceIds,
  53. readerWhitelist: readerWhitelist,
  54. whitelistEnabled: true,
  55. isActive: true,
  56. isPermanent: false,
  57. updateCriteria: updateCriteria
  58. });
  59. uint256 minimumBalance = scheduler.getMinimumBalance(
  60. uint8(params.priceIds.length)
  61. );
  62. return scheduler.createSubscription{value: minimumBalance}(params);
  63. }
  64. // Helper function to create default subscription parameters
  65. function createDefaultSubscriptionParams(
  66. uint8 numFeeds,
  67. address whitelistedReader
  68. ) internal pure returns (SchedulerStructs.SubscriptionParams memory) {
  69. bytes32[] memory priceIds = createPriceIds(numFeeds);
  70. address[] memory readerWhitelist = new address[](1);
  71. readerWhitelist[0] = whitelistedReader;
  72. SchedulerStructs.UpdateCriteria memory updateCriteria = SchedulerStructs
  73. .UpdateCriteria({
  74. updateOnHeartbeat: true,
  75. heartbeatSeconds: 60,
  76. updateOnDeviation: true,
  77. deviationThresholdBps: 100
  78. });
  79. return
  80. SchedulerStructs.SubscriptionParams({
  81. priceIds: priceIds,
  82. readerWhitelist: readerWhitelist,
  83. whitelistEnabled: true,
  84. isActive: true,
  85. isPermanent: false,
  86. updateCriteria: updateCriteria
  87. });
  88. }
  89. }