MockPyth.sol 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import "./PythUtils.sol";
  7. contract MockPyth is AbstractPyth {
  8. mapping(bytes32 => PythStructs.PriceFeed) priceFeeds;
  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 createPriceFeedUpdateData or
  29. // by using web3.js or ethers abi utilities.
  30. // @note: The updateData expected here is different from the one used in the main contract.
  31. // In particular, the expected format is:
  32. // [
  33. // abi.encode(
  34. // PythStructs.PriceFeed(
  35. // bytes32 id,
  36. // PythStructs.Price price,
  37. // PythStructs.Price emaPrice
  38. // ),
  39. // uint64 prevPublishTime
  40. // )
  41. // ]
  42. function updatePriceFeeds(
  43. bytes[] calldata updateData
  44. ) public payable override {
  45. uint requiredFee = getUpdateFee(updateData);
  46. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  47. for (uint i = 0; i < updateData.length; i++) {
  48. PythStructs.PriceFeed memory priceFeed = abi.decode(
  49. updateData[i],
  50. (PythStructs.PriceFeed)
  51. );
  52. uint lastPublishTime = priceFeeds[priceFeed.id].price.publishTime;
  53. if (lastPublishTime < priceFeed.price.publishTime) {
  54. // Price information is more recent than the existing price information.
  55. priceFeeds[priceFeed.id] = priceFeed;
  56. emit PriceFeedUpdate(
  57. priceFeed.id,
  58. uint64(priceFeed.price.publishTime),
  59. priceFeed.price.price,
  60. priceFeed.price.conf
  61. );
  62. }
  63. }
  64. }
  65. function getUpdateFee(
  66. bytes[] calldata updateData
  67. ) public view override returns (uint feeAmount) {
  68. return singleUpdateFeeInWei * updateData.length;
  69. }
  70. function parsePriceFeedUpdatesInternal(
  71. bytes[] calldata updateData,
  72. bytes32[] calldata priceIds,
  73. uint64 minPublishTime,
  74. uint64 maxPublishTime,
  75. bool unique
  76. ) internal returns (PythStructs.PriceFeed[] memory feeds) {
  77. uint requiredFee = getUpdateFee(updateData);
  78. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  79. feeds = new PythStructs.PriceFeed[](priceIds.length);
  80. for (uint i = 0; i < priceIds.length; i++) {
  81. for (uint j = 0; j < updateData.length; j++) {
  82. uint64 prevPublishTime;
  83. (feeds[i], prevPublishTime) = abi.decode(
  84. updateData[j],
  85. (PythStructs.PriceFeed, uint64)
  86. );
  87. uint publishTime = feeds[i].price.publishTime;
  88. if (priceFeeds[feeds[i].id].price.publishTime < publishTime) {
  89. priceFeeds[feeds[i].id] = feeds[i];
  90. emit PriceFeedUpdate(
  91. feeds[i].id,
  92. uint64(publishTime),
  93. feeds[i].price.price,
  94. feeds[i].price.conf
  95. );
  96. }
  97. if (feeds[i].id == priceIds[i]) {
  98. if (
  99. minPublishTime <= publishTime &&
  100. publishTime <= maxPublishTime &&
  101. (!unique || prevPublishTime < minPublishTime)
  102. ) {
  103. break;
  104. } else {
  105. feeds[i].id = 0;
  106. }
  107. }
  108. }
  109. if (feeds[i].id != priceIds[i])
  110. revert PythErrors.PriceFeedNotFoundWithinRange();
  111. }
  112. }
  113. function parsePriceFeedUpdates(
  114. bytes[] calldata updateData,
  115. bytes32[] calldata priceIds,
  116. uint64 minPublishTime,
  117. uint64 maxPublishTime
  118. ) external payable override returns (PythStructs.PriceFeed[] memory feeds) {
  119. return
  120. parsePriceFeedUpdatesInternal(
  121. updateData,
  122. priceIds,
  123. minPublishTime,
  124. maxPublishTime,
  125. false
  126. );
  127. }
  128. function parsePriceFeedUpdatesUnique(
  129. bytes[] calldata updateData,
  130. bytes32[] calldata priceIds,
  131. uint64 minPublishTime,
  132. uint64 maxPublishTime
  133. ) external payable override returns (PythStructs.PriceFeed[] memory feeds) {
  134. return
  135. parsePriceFeedUpdatesInternal(
  136. updateData,
  137. priceIds,
  138. minPublishTime,
  139. maxPublishTime,
  140. true
  141. );
  142. }
  143. function parseTwapPriceFeedUpdates(
  144. bytes[] calldata updateData,
  145. bytes32[] calldata priceIds
  146. )
  147. external
  148. payable
  149. override
  150. returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds)
  151. {
  152. uint requiredFee = getUpdateFee(updateData);
  153. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  154. twapPriceFeeds = new PythStructs.TwapPriceFeed[](priceIds.length);
  155. // Process each price ID
  156. for (uint i = 0; i < priceIds.length; i++) {
  157. processTwapPriceFeed(updateData, priceIds[i], i, twapPriceFeeds);
  158. }
  159. return twapPriceFeeds;
  160. }
  161. // You can create this data either by calling createTwapPriceFeedUpdateData.
  162. // @note: The updateData expected here is different from the one used in the main contract.
  163. // In particular, the expected format is:
  164. // [
  165. // abi.encode(
  166. // bytes32 id,
  167. // PythStructs.TwapPriceInfo startInfo,
  168. // PythStructs.TwapPriceInfo endInfo
  169. // )
  170. // ]
  171. function processTwapPriceFeed(
  172. bytes[] calldata updateData,
  173. bytes32 priceId,
  174. uint index,
  175. PythStructs.TwapPriceFeed[] memory twapPriceFeeds
  176. ) private {
  177. // Decode TWAP feed directly
  178. PythStructs.TwapPriceFeed memory twapFeed = abi.decode(
  179. updateData[0],
  180. (PythStructs.TwapPriceFeed)
  181. );
  182. // Validate ID matches
  183. if (twapFeed.id != priceId)
  184. revert PythErrors.InvalidTwapUpdateDataSet();
  185. // Store the TWAP feed
  186. twapPriceFeeds[index] = twapFeed;
  187. // Emit event
  188. emit TwapPriceFeedUpdate(
  189. priceId,
  190. twapFeed.startTime,
  191. twapFeed.endTime,
  192. twapFeed.twap.price,
  193. twapFeed.twap.conf,
  194. twapFeed.downSlotsRatio
  195. );
  196. }
  197. /**
  198. * @notice Creates TWAP price feed update data with simplified parameters for testing
  199. * @param id The price feed ID
  200. * @param startTime Start time of the TWAP
  201. * @param endTime End time of the TWAP
  202. * @param price The price value
  203. * @param conf The confidence interval
  204. * @param expo Price exponent
  205. * @param downSlotsRatio Down slots ratio
  206. * @return twapData Encoded TWAP price feed data ready for parseTwapPriceFeedUpdates
  207. */
  208. function createTwapPriceFeedUpdateData(
  209. bytes32 id,
  210. uint64 startTime,
  211. uint64 endTime,
  212. int64 price,
  213. uint64 conf,
  214. int32 expo,
  215. uint32 downSlotsRatio
  216. ) public pure returns (bytes memory twapData) {
  217. PythStructs.Price memory twapPrice = PythStructs.Price({
  218. price: price,
  219. conf: conf,
  220. expo: expo,
  221. publishTime: endTime
  222. });
  223. PythStructs.TwapPriceFeed memory twapFeed = PythStructs.TwapPriceFeed({
  224. id: id,
  225. startTime: startTime,
  226. endTime: endTime,
  227. twap: twapPrice,
  228. downSlotsRatio: downSlotsRatio
  229. });
  230. twapData = abi.encode(twapFeed);
  231. }
  232. function createPriceFeedUpdateData(
  233. bytes32 id,
  234. int64 price,
  235. uint64 conf,
  236. int32 expo,
  237. int64 emaPrice,
  238. uint64 emaConf,
  239. uint64 publishTime,
  240. uint64 prevPublishTime
  241. ) public pure returns (bytes memory priceFeedData) {
  242. PythStructs.PriceFeed memory priceFeed;
  243. priceFeed.id = id;
  244. priceFeed.price.price = price;
  245. priceFeed.price.conf = conf;
  246. priceFeed.price.expo = expo;
  247. priceFeed.price.publishTime = publishTime;
  248. priceFeed.emaPrice.price = emaPrice;
  249. priceFeed.emaPrice.conf = emaConf;
  250. priceFeed.emaPrice.expo = expo;
  251. priceFeed.emaPrice.publishTime = publishTime;
  252. priceFeedData = abi.encode(priceFeed, prevPublishTime);
  253. }
  254. }