MockPyth.sol 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 getTwapUpdateFee(
  71. bytes[] calldata updateData
  72. ) public view override returns (uint feeAmount) {
  73. return singleUpdateFeeInWei * updateData.length;
  74. }
  75. function parsePriceFeedUpdatesWithConfig(
  76. bytes[] calldata updateData,
  77. bytes32[] calldata priceIds,
  78. uint64 minAllowedPublishTime,
  79. uint64 maxAllowedPublishTime,
  80. bool checkUniqueness,
  81. bool checkUpdateDataIsMinimal,
  82. bool storeUpdatesIfFresh
  83. )
  84. public
  85. payable
  86. returns (PythStructs.PriceFeed[] memory feeds, uint64[] memory slots)
  87. {
  88. uint requiredFee = getUpdateFee(updateData);
  89. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  90. feeds = new PythStructs.PriceFeed[](priceIds.length);
  91. slots = new uint64[](priceIds.length);
  92. for (uint i = 0; i < priceIds.length; i++) {
  93. for (uint j = 0; j < updateData.length; j++) {
  94. uint64 prevPublishTime;
  95. (feeds[i], prevPublishTime) = abi.decode(
  96. updateData[j],
  97. (PythStructs.PriceFeed, uint64)
  98. );
  99. uint publishTime = feeds[i].price.publishTime;
  100. slots[i] = uint64(publishTime); // use PublishTime as mock slot
  101. if (priceFeeds[feeds[i].id].price.publishTime < publishTime) {
  102. priceFeeds[feeds[i].id] = feeds[i];
  103. emit PriceFeedUpdate(
  104. feeds[i].id,
  105. uint64(publishTime),
  106. feeds[i].price.price,
  107. feeds[i].price.conf
  108. );
  109. }
  110. if (feeds[i].id == priceIds[i]) {
  111. if (
  112. minAllowedPublishTime <= publishTime &&
  113. publishTime <= maxAllowedPublishTime &&
  114. (!checkUniqueness ||
  115. prevPublishTime < minAllowedPublishTime)
  116. ) {
  117. break;
  118. } else {
  119. feeds[i].id = 0;
  120. }
  121. }
  122. }
  123. if (feeds[i].id != priceIds[i])
  124. revert PythErrors.PriceFeedNotFoundWithinRange();
  125. }
  126. }
  127. function parsePriceFeedUpdates(
  128. bytes[] calldata updateData,
  129. bytes32[] calldata priceIds,
  130. uint64 minPublishTime,
  131. uint64 maxPublishTime
  132. ) external payable override returns (PythStructs.PriceFeed[] memory feeds) {
  133. (feeds, ) = parsePriceFeedUpdatesWithConfig(
  134. updateData,
  135. priceIds,
  136. minPublishTime,
  137. maxPublishTime,
  138. false,
  139. true,
  140. false
  141. );
  142. }
  143. function parsePriceFeedUpdatesUnique(
  144. bytes[] calldata updateData,
  145. bytes32[] calldata priceIds,
  146. uint64 minPublishTime,
  147. uint64 maxPublishTime
  148. ) external payable override returns (PythStructs.PriceFeed[] memory feeds) {
  149. (feeds, ) = parsePriceFeedUpdatesWithConfig(
  150. updateData,
  151. priceIds,
  152. minPublishTime,
  153. maxPublishTime,
  154. false,
  155. true,
  156. false
  157. );
  158. }
  159. function parseTwapPriceFeedUpdates(
  160. bytes[] calldata updateData,
  161. bytes32[] calldata priceIds
  162. )
  163. external
  164. payable
  165. override
  166. returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds)
  167. {
  168. uint requiredFee = getUpdateFee(updateData);
  169. if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
  170. twapPriceFeeds = new PythStructs.TwapPriceFeed[](priceIds.length);
  171. // Process each price ID
  172. for (uint i = 0; i < priceIds.length; i++) {
  173. processTwapPriceFeed(updateData, priceIds[i], i, twapPriceFeeds);
  174. }
  175. return twapPriceFeeds;
  176. }
  177. // You can create this data either by calling createTwapPriceFeedUpdateData.
  178. // @note: The updateData expected here is different from the one used in the main contract.
  179. // In particular, the expected format is:
  180. // [
  181. // abi.encode(
  182. // bytes32 id,
  183. // PythStructs.TwapPriceInfo startInfo,
  184. // PythStructs.TwapPriceInfo endInfo
  185. // )
  186. // ]
  187. function processTwapPriceFeed(
  188. bytes[] calldata updateData,
  189. bytes32 priceId,
  190. uint index,
  191. PythStructs.TwapPriceFeed[] memory twapPriceFeeds
  192. ) private {
  193. // Decode TWAP feed directly
  194. PythStructs.TwapPriceFeed memory twapFeed = abi.decode(
  195. updateData[0],
  196. (PythStructs.TwapPriceFeed)
  197. );
  198. // Validate ID matches
  199. if (twapFeed.id != priceId)
  200. revert PythErrors.InvalidTwapUpdateDataSet();
  201. // Store the TWAP feed
  202. twapPriceFeeds[index] = twapFeed;
  203. // Emit event
  204. emit TwapPriceFeedUpdate(
  205. priceId,
  206. twapFeed.startTime,
  207. twapFeed.endTime,
  208. twapFeed.twap.price,
  209. twapFeed.twap.conf,
  210. twapFeed.downSlotsRatio
  211. );
  212. }
  213. /**
  214. * @notice Creates TWAP price feed update data with simplified parameters for testing
  215. * @param id The price feed ID
  216. * @param startTime Start time of the TWAP
  217. * @param endTime End time of the TWAP
  218. * @param price The price value
  219. * @param conf The confidence interval
  220. * @param expo Price exponent
  221. * @param downSlotsRatio Down slots ratio
  222. * @return twapData Encoded TWAP price feed data ready for parseTwapPriceFeedUpdates
  223. */
  224. function createTwapPriceFeedUpdateData(
  225. bytes32 id,
  226. uint64 startTime,
  227. uint64 endTime,
  228. int64 price,
  229. uint64 conf,
  230. int32 expo,
  231. uint32 downSlotsRatio
  232. ) public pure returns (bytes memory twapData) {
  233. PythStructs.Price memory twapPrice = PythStructs.Price({
  234. price: price,
  235. conf: conf,
  236. expo: expo,
  237. publishTime: endTime
  238. });
  239. PythStructs.TwapPriceFeed memory twapFeed = PythStructs.TwapPriceFeed({
  240. id: id,
  241. startTime: startTime,
  242. endTime: endTime,
  243. twap: twapPrice,
  244. downSlotsRatio: downSlotsRatio
  245. });
  246. twapData = abi.encode(twapFeed);
  247. }
  248. function createPriceFeedUpdateData(
  249. bytes32 id,
  250. int64 price,
  251. uint64 conf,
  252. int32 expo,
  253. int64 emaPrice,
  254. uint64 emaConf,
  255. uint64 publishTime,
  256. uint64 prevPublishTime
  257. ) public pure returns (bytes memory priceFeedData) {
  258. PythStructs.PriceFeed memory priceFeed;
  259. priceFeed.id = id;
  260. priceFeed.price.price = price;
  261. priceFeed.price.conf = conf;
  262. priceFeed.price.expo = expo;
  263. priceFeed.price.publishTime = publishTime;
  264. priceFeed.emaPrice.price = emaPrice;
  265. priceFeed.emaPrice.conf = emaConf;
  266. priceFeed.emaPrice.expo = expo;
  267. priceFeed.emaPrice.publishTime = publishTime;
  268. priceFeedData = abi.encode(priceFeed, prevPublishTime);
  269. }
  270. }