MockPyth.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: Apache-2.0
  2. pragma solidity ^0.8.0;
  3. import "./AbstractPyth.sol";
  4. import "./PythStructs.sol";
  5. import "./PythErrors.sol";
  6. contract MockPyth is AbstractPyth {
  7. mapping(bytes32 => PythStructs.PriceFeed) priceFeeds;
  8. uint64 sequenceNumber;
  9. uint singleUpdateFeeInWei;
  10. uint validTimePeriod;
  11. constructor(uint _validTimePeriod, uint _singleUpdateFeeInWei) {
  12. singleUpdateFeeInWei = _singleUpdateFeeInWei;
  13. validTimePeriod = _validTimePeriod;
  14. }
  15. function queryPriceFeed(
  16. bytes32 id
  17. ) public view override returns (PythStructs.PriceFeed memory priceFeed) {
  18. if (priceFeeds[id].id == 0) revert PythErrors.PriceFeedNotFound();
  19. return priceFeeds[id];
  20. }
  21. function priceFeedExists(bytes32 id) public view override returns (bool) {
  22. return (priceFeeds[id].id != 0);
  23. }
  24. function getValidTimePeriod() public view override returns (uint) {
  25. return validTimePeriod;
  26. }
  27. // Takes an array of encoded price feeds and stores them.
  28. // You can create this data either by calling createPriceFeedData or
  29. // by using web3.js or ethers abi utilities.
  30. function updatePriceFeeds(
  31. bytes[] calldata updateData
  32. ) public payable override {
  33. uint requiredFee = getUpdateFee(updateData);
  34. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  35. // Chain ID is id of the source chain that the price update comes from. Since it is just a mock contract
  36. // We set it to 1.
  37. uint16 chainId = 1;
  38. for (uint i = 0; i < updateData.length; i++) {
  39. PythStructs.PriceFeed memory priceFeed = abi.decode(
  40. updateData[i],
  41. (PythStructs.PriceFeed)
  42. );
  43. uint lastPublishTime = priceFeeds[priceFeed.id].price.publishTime;
  44. if (lastPublishTime < priceFeed.price.publishTime) {
  45. // Price information is more recent than the existing price information.
  46. priceFeeds[priceFeed.id] = priceFeed;
  47. emit PriceFeedUpdate(
  48. priceFeed.id,
  49. uint64(lastPublishTime),
  50. priceFeed.price.price,
  51. priceFeed.price.conf
  52. );
  53. }
  54. }
  55. // In the real contract, the input of this function contains multiple batches that each contain multiple prices.
  56. // This event is emitted when a batch is processed. In this mock contract we consider there is only one batch of prices.
  57. // Each batch has (chainId, sequenceNumber) as it's unique identifier. Here chainId is set to 1 and an increasing sequence number is used.
  58. emit BatchPriceFeedUpdate(chainId, sequenceNumber);
  59. sequenceNumber += 1;
  60. }
  61. function getUpdateFee(
  62. bytes[] calldata updateData
  63. ) public view override returns (uint feeAmount) {
  64. return singleUpdateFeeInWei * updateData.length;
  65. }
  66. function parsePriceFeedUpdates(
  67. bytes[] calldata updateData,
  68. bytes32[] calldata priceIds,
  69. uint64 minPublishTime,
  70. uint64 maxPublishTime
  71. ) external payable override returns (PythStructs.PriceFeed[] memory feeds) {
  72. uint requiredFee = getUpdateFee(updateData);
  73. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  74. feeds = new PythStructs.PriceFeed[](priceIds.length);
  75. for (uint i = 0; i < priceIds.length; i++) {
  76. for (uint j = 0; j < updateData.length; j++) {
  77. feeds[i] = abi.decode(updateData[j], (PythStructs.PriceFeed));
  78. if (feeds[i].id == priceIds[i]) {
  79. uint publishTime = feeds[i].price.publishTime;
  80. if (
  81. minPublishTime <= publishTime &&
  82. publishTime <= maxPublishTime
  83. ) {
  84. break;
  85. } else {
  86. feeds[i].id = 0;
  87. }
  88. }
  89. }
  90. if (feeds[i].id != priceIds[i])
  91. revert PythErrors.PriceFeedNotFoundWithinRange();
  92. }
  93. }
  94. function createPriceFeedUpdateData(
  95. bytes32 id,
  96. int64 price,
  97. uint64 conf,
  98. int32 expo,
  99. int64 emaPrice,
  100. uint64 emaConf,
  101. uint64 publishTime
  102. ) public pure returns (bytes memory priceFeedData) {
  103. PythStructs.PriceFeed memory priceFeed;
  104. priceFeed.id = id;
  105. priceFeed.price.price = price;
  106. priceFeed.price.conf = conf;
  107. priceFeed.price.expo = expo;
  108. priceFeed.price.publishTime = publishTime;
  109. priceFeed.emaPrice.price = emaPrice;
  110. priceFeed.emaPrice.conf = emaConf;
  111. priceFeed.emaPrice.expo = expo;
  112. priceFeed.emaPrice.publishTime = publishTime;
  113. priceFeedData = abi.encode(priceFeed);
  114. }
  115. }